Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/block-editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- Convert the block attribute filter hooks to TypeScript ([#84723](https://github.com/WordPress/gutenberg/pull/84723)).
- Convert `InspectorControls` to TypeScript ([#84725](https://github.com/WordPress/gutenberg/pull/84725)).
- Convert the anchor and custom class name hooks to TypeScript ([#84726](https://github.com/WordPress/gutenberg/pull/84726)).
- Convert the auto-generated inspector controls to TypeScript ([#84727](https://github.com/WordPress/gutenberg/pull/84727)).

## 18.1.0 (2026-10-07)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import { store as blockEditorStore } from '../store';
import { unlock } from '../lock-unlock';
import BlockContext from '../components/block-context';
import { generateFieldsFromAttributes } from './generate-fields-from-attributes';
import type { AutoGenerateAttribute, AutoRegisterControlsProps } from './types';

/**
* Checks if a block has any attributes marked for auto-generated inspector controls.
*
* @param {Object} blockTypeAttributes - The block type's attributes object.
* @return {boolean} True if any attribute has autoGenerateControl marker.
* @param blockTypeAttributes - The block type's attributes object.
* @return True if any attribute has autoGenerateControl marker.
*/
function hasAutoGenerateControl( blockTypeAttributes ) {
function hasAutoGenerateControl(
blockTypeAttributes: Record< string, AutoGenerateAttribute > | undefined
): boolean {
if ( ! blockTypeAttributes ) {
return false;
}
Expand All @@ -32,12 +35,16 @@ function hasAutoGenerateControl( blockTypeAttributes ) {
* Fields are generated on-the-fly from attributes marked with `autoGenerateControl`
* during PHP registration.
*
* @param {Object} props Component props.
* @param {string} props.name Block name.
* @param {string} props.clientId Block client ID.
* @param {Function} props.setAttributes Function to update block attributes.
* @param props Component props.
* @param props.name Block name.
* @param props.clientId Block client ID.
* @param props.setAttributes Function to update block attributes.
*/
function AutoRegisterControls( { name, clientId, setAttributes } ) {
function AutoRegisterControls( {
name,
clientId,
setAttributes,
}: AutoRegisterControlsProps ) {
const blockEditingMode = useBlockEditingMode();

const blockContext = useContext( BlockContext );
Expand Down Expand Up @@ -95,7 +102,8 @@ function AutoRegisterControls( { name, clientId, setAttributes } ) {
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<DataForm
data={ attributes }
// The controls only render for a block in the store.
data={ attributes! }
fields={ fields }
form={ form }
onChange={ setAttributes }
Expand All @@ -108,7 +116,7 @@ function AutoRegisterControls( { name, clientId, setAttributes } ) {
export default {
edit: AutoRegisterControls,
attributeKeys: [],
hasSupport( name ) {
hasSupport( name: string ) {
const blockType = getBlockType( name );
return hasAutoGenerateControl( blockType?.attributes );
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Field, FieldTypeName, Form } from '@wordpress/dataviews';
import type { AutoGenerateAttribute } from './types';

/**
* Generates DataForm field definitions from block attributes.
*
Expand All @@ -9,12 +12,14 @@
* function processes only those pre-validated attributes.
* See https://github.com/WordPress/wordpress-develop/blob/8f6d31266deecd7eea258bd3d45597355cce13d0/src/wp-includes/block-supports/auto-register.php#L30
*
* @param {Object} attributes - Block type attributes from block registration
* @return {{ fields: Array, form: Object }} DataForm fields and form definitions
* @param attributes - Block type attributes from block registration
* @return DataForm fields and form definitions
*/
export function generateFieldsFromAttributes( attributes ) {
const fields = [];
const fieldIds = [];
export function generateFieldsFromAttributes(
attributes: Record< string, AutoGenerateAttribute >
): { fields: Field< Record< string, unknown > >[]; form: Form } {
const fields: Field< Record< string, unknown > >[] = [];
const fieldIds: string[] = [];

Object.entries( attributes ).forEach( ( [ name, def ] ) => {
if ( ! def.autoGenerateControl ) {
Expand All @@ -37,18 +42,22 @@ export function generateFieldsFromAttributes( attributes ) {
/**
* Creates a DataForm field definition from a block attribute definition.
*
* @param {string} name - The attribute name
* @param {Object} def - The attribute definition from block.json
* @return {Object|null} DataForm field definition or null if type not supported
* @param name - The attribute name
* @param def - The attribute definition from block.json
* @return DataForm field definition
*/
function createFieldFromAttribute( name, def ) {
function createFieldFromAttribute(
name: string,
def: AutoGenerateAttribute
): Field< Record< string, unknown > > {
const type = def.type;
const field = {
const field: Field< Record< string, unknown > > = {
id: name,
label: def.label || name,
// Only 'string' needs mapping to 'text'; others are 1:1 with DataForm types.
// This mapping will be unnecessary once #74105 lands.
type: type === 'string' ? 'text' : type,
// PHP only marks attributes whose type DataForm supports.
type: ( type === 'string' ? 'text' : type ) as FieldTypeName,
};

// Add elements for enums (DataForm idiom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getBlockType,
} from '@wordpress/blocks';
import AutoRegisterControls from '../auto-inspector-controls';
import type { AutoGenerateAttribute } from '../types';

describe( 'auto-inspector-controls', () => {
const blockName = 'test/auto-inspector-controls-block';
Expand Down Expand Up @@ -36,20 +37,21 @@ describe( 'auto-inspector-controls', () => {
} );

it( 'should return true for blocks with autoGenerateControl markers', () => {
const attributes: Record< string, AutoGenerateAttribute > = {
title: {
type: 'string',
autoGenerateControl: true,
},
count: {
type: 'integer',
autoGenerateControl: true,
},
};
registerBlockType( blockName, {
apiVersion: 3,
title: 'Test Block',
category: 'text',
attributes: {
title: {
type: 'string',
autoGenerateControl: true,
},
count: {
type: 'integer',
autoGenerateControl: true,
},
},
attributes,
edit: () => null,
save: () => null,
} );
Expand All @@ -68,6 +70,7 @@ describe( 'auto-inspector-controls', () => {
apiVersion: 3,
title: 'Test Block',
category: 'text',
attributes: {},
edit: () => null,
save: () => null,
} );
Expand All @@ -78,21 +81,22 @@ describe( 'auto-inspector-controls', () => {
} );

it( 'should return true when at least one attribute has autoGenerateControl', () => {
const attributes: Record< string, AutoGenerateAttribute > = {
// This one has the marker
title: {
type: 'string',
autoGenerateControl: true,
},
// This one doesn't (e.g., added by block supports)
className: {
type: 'string',
},
};
registerBlockType( blockName, {
apiVersion: 3,
title: 'Test Block',
category: 'text',
attributes: {
// This one has the marker
title: {
type: 'string',
autoGenerateControl: true,
},
// This one doesn't (e.g., added by block supports)
className: {
type: 'string',
},
},
attributes,
edit: () => null,
save: () => null,
} );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { describe, expect, it } from 'vitest';
import { generateFieldsFromAttributes } from '../generate-fields-from-attributes';
import type { AutoGenerateAttribute } from '../types';

/**
* Helper to mark attributes for auto-generated inspector controls.
* In production, this marker is added by PHP during block registration.
*
* @param {Object} attrs - Attributes object
* @return {Object} Attributes with autoGenerateControl marker
* @param attrs - Attributes object
* @return Attributes with autoGenerateControl marker
*/
function markForAutoInspectorControl( attrs ) {
const result = {};
function markForAutoInspectorControl(
attrs: Record< string, AutoGenerateAttribute >
): Record< string, AutoGenerateAttribute > {
const result: Record< string, AutoGenerateAttribute > = {};
for ( const [ name, def ] of Object.entries( attrs ) ) {
result[ name ] = { ...def, autoGenerateControl: true };
}
Expand Down
25 changes: 24 additions & 1 deletion packages/block-editor/src/hooks/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { ComponentType } from 'react';
import type { Block, BlockType, ColorProps } from '@wordpress/blocks';
import type {
Block,
BlockAttribute,
BlockType,
ColorProps,
} from '@wordpress/blocks';
import type { BlockVisibility } from '../components/block-visibility/use-block-visibility';

/**
Expand Down Expand Up @@ -214,3 +219,21 @@ export interface CustomClassNameEditProps {
* A block whose `className` attribute is read.
*/
export type BlockWithClassName = Block< { className?: string } >;

/**
* A block attribute that PHP marks for an auto-generated inspector control.
*/
export interface AutoGenerateAttribute extends BlockAttribute {
autoGenerateControl?: boolean;
label?: string;
items?: Pick< BlockAttribute, 'type' | 'enum' >;
}

/**
* Props of the auto-generated inspector controls.
*/
export interface AutoRegisterControlsProps {
name: string;
clientId: string;
setAttributes: BlockSupportEditProps[ 'setAttributes' ];
}
3 changes: 2 additions & 1 deletion packages/block-editor/src/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { ThunkArgs, select as globalSelect } from '@wordpress/data';
import type { BlockEditingMode } from '../components/block-editing-mode';
import type { BlockVisibility } from '../components/block-visibility/use-block-visibility';
import type { WPDropOperation } from '../components/use-on-block-drop/types';
import type { Bindings } from '../utils/block-bindings';
import type { store } from '.';
import type * as privateSelectors from './private-selectors';
import type * as privateActions from './private-actions';
Expand All @@ -23,7 +24,7 @@ export interface BlockAttributes {
metadata?: Record< string, unknown > & {
name?: string;
patternName?: string;
bindings?: Record< string, unknown >;
bindings?: Bindings;
Comment thread
manzoorwanijk marked this conversation as resolved.
Resolved
blockVisibility?: BlockVisibility;
};
lock?: { move?: boolean; remove?: boolean; edit?: boolean };
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/utils/block-bindings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type Binding = { source?: string };
type Bindings = Record< string, Binding >;
type Binding = { source?: string; args?: Record< string, unknown > };
export type Bindings = Record< string, Binding >;
type BlockBindingsSource = { usesContext?: readonly string[] };

const DEFAULT_ATTRIBUTE = '__default';
Expand Down
5 changes: 0 additions & 5 deletions tools/eslint/suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2103,11 +2103,6 @@
"count": 2
}
},
"packages/block-editor/src/hooks/auto-inspector-controls.jsx": {
"react/jsx-filename-extension": {
"count": 1
}
},
"packages/block-editor/src/hooks/background.jsx": {
"react/jsx-filename-extension": {
"count": 1
Expand Down
Loading