-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Block Fields: show all form fields by default #74486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
21f68ee
a81afb8
883ec6f
ef462a9
f198125
0b1129e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { | |
| import { createHigherOrderComponent } from '@wordpress/compose'; | ||
| import { DataForm } from '@wordpress/dataviews'; | ||
| import { useContext, useState, useMemo } from '@wordpress/element'; | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
|
|
@@ -169,7 +170,25 @@ function denormalizeLinkValue( value, fieldDef ) { | |
| return result; | ||
| } | ||
|
|
||
| function BlockFields( { clientId, blockType, attributes, setAttributes } ) { | ||
| /** | ||
| * Component that renders a DataForm for a single block's attributes | ||
| * @param {Object} props | ||
| * @param {string} props.clientId The clientId of the block. | ||
| * @param {Object} props.blockType The blockType definition. | ||
| * @param {Object} props.attributes The block's attribute values. | ||
| * @param {Function} props.setAttributes Action to set the block's attributes. | ||
| * @param {boolean} props.isCollapsed Whether the DataForm is rendered as 'collapsed' with only the first field | ||
| * displayed by default. When collapsed a dropdown is displayed to allow | ||
| * displaying additional fields. The block's title is displayed as the title. | ||
| * The collapsed mode is often used when multiple BlockForms are shown together. | ||
| */ | ||
| function BlockFields( { | ||
| clientId, | ||
| blockType, | ||
| attributes, | ||
| setAttributes, | ||
| isCollapsed = false, | ||
| } ) { | ||
| const blockTitle = useBlockDisplayTitle( { | ||
| clientId, | ||
| context: 'list-view', | ||
|
|
@@ -178,9 +197,19 @@ function BlockFields( { clientId, blockType, attributes, setAttributes } ) { | |
|
|
||
| const blockTypeFields = blockType?.[ fieldsKey ]; | ||
|
|
||
| const [ form, setForm ] = useState( () => { | ||
| return blockType?.[ formKey ]; | ||
| } ); | ||
| const computedForm = useMemo( () => { | ||
| if ( ! isCollapsed ) { | ||
| return blockType?.[ formKey ]; | ||
| } | ||
|
|
||
| // For a collapsed form only show the first field by default. | ||
| return { | ||
| ...blockType?.[ formKey ], | ||
| fields: [ blockType?.[ formKey ]?.fields?.[ 0 ] ], | ||
| }; | ||
| }, [ blockType, isCollapsed ] ); | ||
|
|
||
| const [ form, setForm ] = useState( computedForm ); | ||
|
|
||
| // Build DataForm fields with proper structure | ||
| const dataFormFields = useMemo( () => { | ||
|
|
@@ -310,21 +339,29 @@ function BlockFields( { clientId, blockType, attributes, setAttributes } ) { | |
| <div className="block-editor-block-fields__container"> | ||
| <div className="block-editor-block-fields__header"> | ||
| <HStack spacing={ 1 }> | ||
| <BlockIcon | ||
| className="block-editor-block-fields__header-icon" | ||
| icon={ blockInformation?.icon } | ||
| /> | ||
| <Truncate | ||
| className="block-editor-block-fields__header-title" | ||
| numberOfLines={ 1 } | ||
| > | ||
| { blockTitle } | ||
| </Truncate> | ||
| <FieldsDropdownMenu | ||
| fields={ dataFormFields } | ||
| visibleFields={ form.fields } | ||
| onToggleField={ handleToggleField } | ||
| /> | ||
| { isCollapsed && ( | ||
| <> | ||
| <BlockIcon | ||
| className="block-editor-block-fields__header-icon" | ||
| icon={ blockInformation?.icon } | ||
| /> | ||
| <h2 className="block-editor-block-fields__header-title"> | ||
| <Truncate numberOfLines={ 1 }> | ||
| { blockTitle } | ||
| </Truncate> | ||
| </h2> | ||
| <FieldsDropdownMenu | ||
| fields={ dataFormFields } | ||
| visibleFields={ form.fields } | ||
| onToggleField={ handleToggleField } | ||
| /> | ||
| </> | ||
| ) } | ||
| { ! isCollapsed && ( | ||
| <h2 className="block-editor-block-fields__header-title"> | ||
| { __( 'Content' ) } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit left field, but I was just curious: do you think block fields will ever expand to support other attribute types beyond content?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, there's an issue (Block Fields: Support attribute groups) for adding this support. I figure we'll cross the bridge of fixing the hard coded label when the support is added 😄 |
||
| </h2> | ||
| ) } | ||
| </HStack> | ||
| </div> | ||
| <DataForm | ||
|
|
@@ -371,6 +408,7 @@ const withBlockFields = createHigherOrderComponent( | |
| <BlockFields | ||
| { ...props } | ||
| blockType={ blockType } | ||
| isCollapsed | ||
| /> | ||
| </PrivateInspectorControlsFill> | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,7 +61,7 @@ if ( window.__experimentalContentOnlyInspectorFields ) { | |
| }, | ||
| ]; | ||
| settings[ formKey ] = { | ||
| fields: [ 'video' ], | ||
| fields: [ 'video', 'caption' ], | ||
| }; | ||
|
Comment on lines
63
to
65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker at all, but with this new logic where we're always showing all fields in single selection mode, or otherwise defaulting to only showing the first field by default, is there still a need for (I don't have a strong opinion on this one, it was just an observation!)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'll keep it for now. There's a chance it might be extended in future, but not sure yet. We'll see how it goes. |
||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies if this is obvious, is it worth adding a comment explaining why isCollapsed logic differs between single blocks and patterns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a doc block 👍