-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2eca3a
commit 9e64f38
Showing
16 changed files
with
236 additions
and
397 deletions.
There are no files selected for viewing
This file contains 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 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
76 changes: 76 additions & 0 deletions
76
projects/packages/forms/src/blocks/contact-form/components/jetpack-field-choice/item/edit.js
This file contains 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,76 @@ | ||
import { RichText, useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import clsx from 'clsx'; | ||
import { first } from 'lodash'; | ||
import { supportsParagraphSplitting } from '../../../util/block-support'; | ||
import { useParentAttributes } from '../../../util/use-parent-attributes'; | ||
import { useJetpackFieldStyles } from '../../use-jetpack-field-styles'; | ||
|
||
export default function JetpackFieldChoiceItemEdit( { | ||
attributes, | ||
clientId, | ||
name, | ||
onReplace, | ||
setAttributes, | ||
type, | ||
} ) { | ||
const { removeBlock } = useDispatch( 'core/block-editor' ); | ||
const parentAttributes = useParentAttributes( clientId ); | ||
const { optionStyle } = useJetpackFieldStyles( parentAttributes ); | ||
const siblingsCount = useSelect( | ||
select => { | ||
const blockEditor = select( 'core/block-editor' ); | ||
const parentBlockId = first( blockEditor.getBlockParents( clientId, true ) ); | ||
return blockEditor.getBlock( parentBlockId ).innerBlocks.length; | ||
}, | ||
[ clientId ] | ||
); | ||
const blockProps = useBlockProps(); | ||
|
||
const handleSplit = label => { | ||
return createBlock( name, { | ||
...attributes, | ||
clientId: label && attributes.label.indexOf( label ) === 0 ? attributes.clientId : undefined, | ||
label, | ||
} ); | ||
}; | ||
|
||
const handleDelete = () => { | ||
if ( siblingsCount <= 1 ) { | ||
return; | ||
} | ||
|
||
removeBlock( clientId ); | ||
}; | ||
|
||
const supportsSplitting = supportsParagraphSplitting(); | ||
const classes = clsx( 'jetpack-field-option', `field-option-${ type }`, blockProps.className ); | ||
const innerBlocksProps = useInnerBlocksProps( blockProps, { | ||
className: classes, | ||
style: optionStyle, | ||
} ); | ||
|
||
return ( | ||
<> | ||
<li { ...innerBlocksProps }> | ||
<input type={ type } className="jetpack-option__type" tabIndex="-1" /> | ||
<RichText | ||
identifier="label" | ||
tagName="div" | ||
className="wp-block" | ||
value={ attributes.label } | ||
placeholder={ __( 'Add option…', 'jetpack-forms' ) } | ||
allowedFormats={ [] } | ||
onChange={ newLabel => setAttributes( { label: newLabel } ) } | ||
preserveWhiteSpace={ false } | ||
withoutInteractiveFormatting | ||
onRemove={ handleDelete } | ||
onReplace={ onReplace } | ||
{ ...( supportsSplitting ? {} : { onSplit: handleSplit } ) } | ||
/> | ||
</li> | ||
</> | ||
); | ||
} |
18 changes: 18 additions & 0 deletions
18
...s/packages/forms/src/blocks/contact-form/components/jetpack-field-choice/item/settings.js
This file contains 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,18 @@ | ||
export default { | ||
apiVersion: 3, | ||
category: 'contact-form', | ||
attributes: { | ||
label: { | ||
type: 'string', | ||
}, | ||
fieldType: { | ||
enum: [ 'checkbox', 'radio' ], | ||
default: 'checkbox', | ||
}, | ||
}, | ||
supports: { | ||
reusable: false, | ||
html: false, | ||
splitting: true, | ||
}, | ||
}; |
6 changes: 6 additions & 0 deletions
6
projects/packages/forms/src/blocks/contact-form/components/jetpack-field-choice/settings.js
This file contains 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,6 @@ | ||
export default { | ||
styles: [ | ||
{ name: 'list', label: 'List', isDefault: true }, | ||
{ name: 'button', label: 'Button' }, | ||
], | ||
}; |
Oops, something went wrong.