-
Notifications
You must be signed in to change notification settings - Fork 843
Forms: use inner blocks for dropdown field #44348
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
Draft
simison
wants to merge
18
commits into
trunk
Choose a base branch
from
update/forms-select-inner-blocks
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
073241e
Forms: explore using inner blocks for dropdown
simison 8f1c2c5
Use useInnerBlocksProps
simison 296c97e
Use dropdown option sub block
simison 2f1e82c
changelog
simison a4d5ebc
Create new option on Enter
simison 9aa497d
Copy paste and splitting
simison 1e30253
Allow merging options
simison 7141a36
Disallow formatting
simison adfcd84
Support copy-pasting over existing items
simison 63e3e23
Fix ensuring list isnt empty on removal
simison 504b1ec
Simplify output
simison 536c282
Disable dropdown container styling for now
simison 739ae45
remove old stuff
simison 541b86f
Show option value in List view sidebar
simison e6f8e7c
Cleanup/linter
simison aaa003d
For now no styling
simison ee3e114
Hide dropdown when block is not selected
simison f247b1b
Polish
simison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/packages/forms/changelog/update-forms-select-inner-blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: changed | ||
|
|
||
| Forms: improve adding, editing and removing options for dropdown field |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
projects/packages/forms/src/blocks/dropdown-option/edit.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { RichText, useBlockProps, store as blockEditorStore } from '@wordpress/block-editor'; | ||
| import { createBlock } from '@wordpress/blocks'; | ||
| import { Button, Flex, FlexItem } from '@wordpress/components'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { close } from '@wordpress/icons'; | ||
|
|
||
| export default function DropdownOptionEdit( props ) { | ||
| const { attributes, clientId, mergeBlocks, setAttributes } = props; | ||
| const { option } = attributes; | ||
| const className = 'jetpack-field-dropdown__option'; | ||
| const blockProps = useBlockProps( { | ||
| className, | ||
| } ); | ||
| const { removeBlocks, replaceBlocks, insertBlocks } = useDispatch( blockEditorStore ); | ||
| const { | ||
| getBlockIndex, | ||
| getBlockRootClientId, | ||
| getMultiSelectedBlockClientIds, | ||
| getNextBlockClientId, | ||
| getPreviousBlockClientId, | ||
| } = useSelect( blockEditorStore ); | ||
|
|
||
| const onPaste = event => { | ||
| const pastedText = event.clipboardData.getData( 'text/plain' ); | ||
|
|
||
| // While pasting, multiple blocks were selected | ||
| const multiSelectedBlockClientIds = getMultiSelectedBlockClientIds(); | ||
| if ( multiSelectedBlockClientIds.length ) { | ||
| const lines = pastedText.split( '\n' ); | ||
| const newOptions = lines.map( line => | ||
| createBlock( 'jetpack/dropdown-option', { option: line } ) | ||
| ); | ||
| replaceBlocks( multiSelectedBlockClientIds, newOptions ); | ||
| return; | ||
| } | ||
|
|
||
| // Otherwise pasting in a single block… | ||
|
|
||
| // Check if the pasted text contains multiple lines | ||
| if ( pastedText.includes( '\n' ) ) { | ||
| event.preventDefault(); | ||
|
|
||
| const lines = pastedText.split( '\n' ); | ||
|
|
||
| // Grab first element of the pasted list as a value for current option block | ||
| const firstLine = lines.shift(); | ||
| setAttributes( { option: `${ option || '' }${ firstLine }` } ); | ||
|
|
||
| // Append rest of the lines as new option blocks | ||
| const newOptions = lines.map( line => | ||
| createBlock( 'jetpack/dropdown-option', { option: line } ) | ||
| ); | ||
| const rootClientId = getBlockRootClientId( clientId ); | ||
| const index = getBlockIndex( clientId ); | ||
|
|
||
| insertBlocks( newOptions, index + 1, rootClientId ); | ||
| } | ||
| }; | ||
|
|
||
| const onRemove = () => { | ||
| const nextBlockClientId = getNextBlockClientId( clientId ); | ||
| const previousBlockClientId = getPreviousBlockClientId( clientId ); | ||
|
|
||
| if ( ! nextBlockClientId && ! previousBlockClientId ) { | ||
| // If this is the the only option, remove by emptying value | ||
| setAttributes( { option: '' } ); | ||
| return; | ||
| } | ||
|
|
||
| return removeBlocks( clientId ); | ||
| }; | ||
|
|
||
| return ( | ||
| <div { ...blockProps }> | ||
| <Flex> | ||
| <FlexItem isBlock> | ||
| <RichText | ||
| __unstablePastePlainText | ||
| allowedFormats={ [] } | ||
| aria-label={ __( 'Dropdown option value', 'jetpack-forms' ) } | ||
| identifier="option" | ||
| onChange={ value => setAttributes( { option: value } ) } | ||
| onMerge={ mergeBlocks } | ||
| onPaste={ onPaste } | ||
| onRemove={ onRemove } | ||
| placeholder={ __( 'Add option…', 'jetpack-forms' ) } | ||
| tagName="div" | ||
| value={ option || '' } | ||
| withoutInteractiveFormatting | ||
| /> | ||
| </FlexItem> | ||
| <FlexItem> | ||
| <Button | ||
| className="jetpack-field-dropdown__option-remove" | ||
| label={ __( 'Remove', 'jetpack-forms' ) } | ||
| variant="tertiary" | ||
| onClick={ onRemove } | ||
| icon={ close } | ||
| ></Button> | ||
| </FlexItem> | ||
| </Flex> | ||
| </div> | ||
| ); | ||
| } |
38 changes: 38 additions & 0 deletions
38
projects/packages/forms/src/blocks/dropdown-option/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { lineSolid } from '@wordpress/icons'; | ||
| import { getIconColor } from '../shared/util/block-icons'; | ||
| import edit from './edit'; | ||
| import save from './save'; | ||
|
|
||
| const name = 'dropdown-option'; | ||
| const settings = { | ||
| apiVersion: 3, | ||
| title: __( 'Option', 'jetpack-forms' ), | ||
| description: __( 'Option for dropdown fields.', 'jetpack-forms' ), | ||
| parent: [ 'jetpack/dropdown' ], | ||
| category: 'contact-form', | ||
| icon: { | ||
| foreground: getIconColor(), | ||
| src: lineSolid, | ||
| }, | ||
| attributes: { option: { type: 'string', default: '' } }, | ||
| supports: { | ||
| anchor: false, | ||
| customClassName: false, | ||
| html: false, | ||
| reusable: false, | ||
| splitting: true, | ||
| }, | ||
| merge: ( attributes, { option = '' } ) => ( { | ||
| ...attributes, | ||
| option: ( attributes.option || '' ) + option, | ||
| } ), | ||
| edit, | ||
| save, | ||
| __experimentalLabel: ( { option } ) => option, | ||
| }; | ||
|
|
||
| export default { | ||
| name, | ||
| settings, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export default () => null; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { InnerBlocks, useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; | ||
|
|
||
| import './editor.scss'; | ||
|
|
||
| const DEFAULT_BLOCK = { | ||
| name: 'jetpack/dropdown-option', | ||
| }; | ||
|
|
||
| const TEMPLATE = [ [ DEFAULT_BLOCK.name ] ]; | ||
|
|
||
| export default function DropdownEdit() { | ||
| const blockProps = useBlockProps( { className: 'jetpack-field-dropdown__popover' } ); | ||
| const innerBlocksProps = useInnerBlocksProps( blockProps ); | ||
|
|
||
| return ( | ||
| <div { ...innerBlocksProps }> | ||
| <InnerBlocks | ||
| __experimentalCaptureToolbars={ true } | ||
| defaultBlock={ DEFAULT_BLOCK } | ||
| directInsert={ true } | ||
| template={ TEMPLATE } | ||
| templateLock={ false } | ||
| templateInsertUpdatesSelection={ true } | ||
| /> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| .wp-block-jetpack-dropdown { | ||
| display: none; | ||
|
|
||
| p { | ||
| margin-bottom: 10px; | ||
| } | ||
| } | ||
|
|
||
| .is-selected, | ||
| .has-child-selected { | ||
|
|
||
| .wp-block-jetpack-dropdown { | ||
| display: block; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { menu } from '@wordpress/icons'; | ||
| import { getIconColor } from '../shared/util/block-icons'; | ||
| import edit from './edit'; | ||
| import save from './save'; | ||
|
|
||
| const name = 'dropdown'; | ||
| const settings = { | ||
| apiVersion: 3, | ||
| title: __( 'Dropdown', 'jetpack-forms' ), | ||
| description: __( 'Options for dropdown fields.', 'jetpack-forms' ), | ||
| parent: [ 'jetpack/field-select' ], | ||
| allowedBlocks: [ 'jetpack/dropdown-option' ], | ||
| category: 'contact-form', | ||
| icon: { | ||
| foreground: getIconColor(), | ||
| src: menu, | ||
| }, | ||
| supports: { | ||
| anchor: false, | ||
| reusable: false, | ||
| html: false, | ||
| }, | ||
| edit, | ||
| save, | ||
| }; | ||
|
|
||
| export default { | ||
| name, | ||
| settings, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; | ||
|
|
||
| export default () => { | ||
| const blockProps = useBlockProps.save(); | ||
| const innerBlocksProps = useInnerBlocksProps.save( blockProps ); | ||
| return <div { ...innerBlocksProps } />; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
TODO: use block gap variable?