diff --git a/projects/packages/forms/changelog/add-more-form-inner-block-php-unit-tests b/projects/packages/forms/changelog/add-more-form-inner-block-php-unit-tests new file mode 100644 index 000000000000..fdae01cd5ccf --- /dev/null +++ b/projects/packages/forms/changelog/add-more-form-inner-block-php-unit-tests @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Adding block supports checks for new inner blocks, and radio field unit test coverage diff --git a/projects/packages/forms/changelog/add-new-form-field-blocks b/projects/packages/forms/changelog/add-new-form-field-blocks new file mode 100644 index 000000000000..4051d354e1ea --- /dev/null +++ b/projects/packages/forms/changelog/add-new-form-field-blocks @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add label, input, option, and options blocks for improving form fields diff --git a/projects/packages/forms/changelog/add-php-unit-tests-migrate-form-fields-to-inner-blocks b/projects/packages/forms/changelog/add-php-unit-tests-migrate-form-fields-to-inner-blocks new file mode 100644 index 000000000000..dd4096c8e566 --- /dev/null +++ b/projects/packages/forms/changelog/add-php-unit-tests-migrate-form-fields-to-inner-blocks @@ -0,0 +1,4 @@ +Significance: patch +Type: added + +Added tests for migration of form fields to inner blocks diff --git a/projects/packages/forms/changelog/migrate-form-fields-to-inner-blocks b/projects/packages/forms/changelog/migrate-form-fields-to-inner-blocks new file mode 100644 index 000000000000..be215d2cdc7a --- /dev/null +++ b/projects/packages/forms/changelog/migrate-form-fields-to-inner-blocks @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Migrate form field blocks to use new inner label, input, option, and options blocks diff --git a/projects/packages/forms/changelog/update-cover-annotation-migrate-form-fields-to-inner-blocks b/projects/packages/forms/changelog/update-cover-annotation-migrate-form-fields-to-inner-blocks new file mode 100644 index 000000000000..3fb3a7a06367 --- /dev/null +++ b/projects/packages/forms/changelog/update-cover-annotation-migrate-form-fields-to-inner-blocks @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Hoisting `@cover` annotation in PHP unit tests to class level. diff --git a/projects/packages/forms/src/blocks/contact-form/child-blocks.js b/projects/packages/forms/src/blocks/contact-form/child-blocks.js index afa443a45a7b..0e0ae8fa7425 100644 --- a/projects/packages/forms/src/blocks/contact-form/child-blocks.js +++ b/projects/packages/forms/src/blocks/contact-form/child-blocks.js @@ -1,784 +1,48 @@ -import { InnerBlocks, useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; -import { createBlock } from '@wordpress/blocks'; -import { Path, Icon } from '@wordpress/components'; -import { __, _x } from '@wordpress/i18n'; -import { globe, envelope, mobile, upload } from '@wordpress/icons'; -import { filter, isEmpty, map, startsWith, trim } from 'lodash'; -import JetpackField from './components/jetpack-field'; -import JetpackFieldCheckbox from './components/jetpack-field-checkbox'; -import JetpackFieldConsent from './components/jetpack-field-consent'; -import JetpackDatePicker from './components/jetpack-field-datepicker'; -import JetpackDropdown from './components/jetpack-field-dropdown'; -import JetpackFieldFile from './components/jetpack-field-file'; -import JetpackFieldMultipleChoice from './components/jetpack-field-multiple-choice'; -import JetpackFieldMultipleChoiceItem from './components/jetpack-field-multiple-choice/item'; -import JetpackFieldNumber from './components/jetpack-field-number'; -import JetpackFieldSingleChoice from './components/jetpack-field-single-choice'; -import JetpackFieldSingleChoiceItem from './components/jetpack-field-single-choice/item'; -import JetpackFieldTextarea from './components/jetpack-field-textarea'; -import { getIconColor } from './util/block-icons'; -import { useFormWrapper } from './util/form'; -import mergeSettings from './util/merge-settings'; -import renderMaterialIcon from './util/render-material-icon'; - -const FieldDefaults = { - apiVersion: 3, - category: 'contact-form', - supports: { - reusable: false, - html: false, - }, - attributes: { - label: { - type: 'string', - default: null, - role: 'content', - }, - required: { - type: 'boolean', - default: false, - }, - requiredText: { - type: 'string', - role: 'content', - }, - options: { - type: 'array', - default: [], - role: 'content', - }, - defaultValue: { - type: 'string', - default: '', - role: 'content', - }, - placeholder: { - type: 'string', - default: '', - role: 'content', - }, - id: { - type: 'string', - default: '', - }, - width: { - type: 'number', - default: 100, - }, - borderRadius: { - type: 'number', - default: '', - }, - borderWidth: { - type: 'number', - default: '', - }, - labelFontSize: { - type: 'string', - }, - fieldFontSize: { - type: 'string', - }, - lineHeight: { - type: 'number', - }, - labelLineHeight: { - type: 'number', - }, - inputColor: { - type: 'string', - }, - labelColor: { - type: 'string', - }, - fieldBackgroundColor: { - type: 'string', - }, - buttonBackgroundColor: { - type: 'string', - }, - buttonBorderRadius: { - type: 'number', - default: '', - }, - buttonBorderWidth: { - type: 'number', - default: '', - }, - borderColor: { - type: 'string', - }, - shareFieldAttributes: { - type: 'boolean', - default: true, - }, - }, - transforms: { - to: [ - { - type: 'block', - blocks: [ 'jetpack/field-number' ], - transform: attributes => createBlock( 'jetpack/field-number', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-text' ], - transform: attributes => createBlock( 'jetpack/field-text', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-name' ], - transform: attributes => createBlock( 'jetpack/field-name', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-email' ], - transform: attributes => createBlock( 'jetpack/field-email', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-url' ], - transform: attributes => createBlock( 'jetpack/field-url', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-date' ], - transform: attributes => createBlock( 'jetpack/field-date', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-telephone' ], - transform: attributes => createBlock( 'jetpack/field-telephone', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-textarea' ], - transform: attributes => createBlock( 'jetpack/field-textarea', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-checkbox-multiple' ], - transform: ( attributes, innerBlocks ) => { - let newInnerBlocks = []; - - if ( ! isEmpty( innerBlocks ) ) { - const optionBlocks = filter( innerBlocks, ( { name } ) => - startsWith( name, 'jetpack/field-option' ) - ); - - newInnerBlocks = map( optionBlocks, block => - createBlock( 'jetpack/field-option-checkbox', { - label: block.attributes.label, - fieldType: 'checkbox', - } ) - ); - } else if ( attributes.options?.length ) { - newInnerBlocks = map( attributes.options, option => - createBlock( 'jetpack/field-option-checkbox', { - label: option, - fieldType: 'checkbox', - } ) - ); - } - - return createBlock( 'jetpack/field-checkbox-multiple', attributes, newInnerBlocks ); - }, - }, - { - type: 'block', - blocks: [ 'jetpack/field-radio' ], - transform: ( attributes, innerBlocks ) => { - let newInnerBlocks = []; - - if ( ! isEmpty( innerBlocks ) ) { - const optionBlocks = filter( innerBlocks, ( { name } ) => - startsWith( name, 'jetpack/field-option' ) - ); - - newInnerBlocks = map( optionBlocks, block => - createBlock( 'jetpack/field-option-radio', { - label: block.attributes.label, - fieldType: 'radio', - } ) - ); - } else if ( attributes.options?.length ) { - newInnerBlocks = map( attributes.options, option => - createBlock( 'jetpack/field-option-radio', { - label: option, - fieldType: 'radio', - } ) - ); - } - - return createBlock( 'jetpack/field-radio', attributes, newInnerBlocks ); - }, - }, - { - type: 'block', - blocks: [ 'jetpack/field-select' ], - transform: ( attributes, innerBlocks ) => { - if ( ! isEmpty( innerBlocks ) ) { - const optionBlocks = filter( innerBlocks, ( { name } ) => - startsWith( name, 'jetpack/field-option' ) - ); - attributes.options = map( optionBlocks, b => b.attributes.label ); - } - - attributes.options = attributes.options?.length ? attributes.options : [ '' ]; - return createBlock( 'jetpack/field-select', attributes ); - }, - }, - { - type: 'block', - blocks: [ 'jetpack/field-consent' ], - transform: attributes => createBlock( 'jetpack/field-consent', attributes ), - }, - { - type: 'block', - blocks: [ 'jetpack/field-checkbox' ], - transform: attributes => createBlock( 'jetpack/field-checkbox', attributes ), - }, - ], - }, - save: () => null, - example: {}, -}; - -// Storing in variables to avoid JS mangling breaking translation calls -const severalOptionsDefault = __( 'Choose several options', 'jetpack-forms' ); -const oneOptionDefault = __( 'Choose one option', 'jetpack-forms' ); - -const multiFieldV1 = fieldType => ( { - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: fieldType === 'checkbox' ? severalOptionsDefault : oneOptionDefault, - }, - }, - migrate: attributes => { - const blockName = `jetpack/field-option-${ fieldType }`; - const nonEmptyOptions = filter( attributes.options, o => ! isEmpty( trim( o ) ) ); - const newInnerBlocks = map( nonEmptyOptions, option => - createBlock( blockName, { - label: option, - } ) - ); - - attributes.options = []; - - return [ attributes, newInnerBlocks ]; - }, - isEligible: attr => attr.options && filter( attr.options, o => ! isEmpty( trim( o ) ) ).length, - save: () => null, -} ); - -const editField = type => props => { - useFormWrapper( props ); - - return ( - - ); -}; - -const EditTextarea = props => { - useFormWrapper( props ); - - return ( - - ); -}; - -const EditCheckbox = props => { - useFormWrapper( props ); - - return ( - - ); -}; - -const EditConsent = ( { - attributes, - clientId, - isSelected, - name, - setAttributes, - insertBlocksAfter, -} ) => { - useFormWrapper( { attributes, clientId, name } ); - - const { id, width, consentType, implicitConsentMessage, explicitConsentMessage } = attributes; - return ( - - ); -}; - -const EditNumber = props => { - useFormWrapper( props ); - - return ( - - ); -}; +import DeprecatedOptionCheckbox from '../deprecated/field-option-checkbox'; +import DeprecatedOptionRadio from '../deprecated/field-option-radio'; +import JetpackDropzone from '../dropzone'; +import JetpackCheckboxField from '../field-checkbox'; +import JetpackConsentField from '../field-consent/'; +import JetpackDateField from '../field-date'; +import JetpackEmailField from '../field-email'; +import JetpackFieldFile from '../field-file'; +import JetpackMultipleChoiceField from '../field-multiple-choice'; +import JetpackNameField from '../field-name'; +import JetpackNumberField from '../field-number'; +import JetpackDropdownField from '../field-select'; +import JetpackSingleChoiceField from '../field-single-choice'; +import JetpackTelephoneField from '../field-telephone'; +import JetpackTextField from '../field-text'; +import JetpackTextareaField from '../field-textarea'; +import JetpackUrlField from '../field-url'; +import JetpackInput from '../input'; +import JetpackLabel from '../label'; +import JetpackOption from '../option'; +import JetpackOptions from '../options'; export const childBlocks = [ - { - name: 'field-text', - settings: { - ...FieldDefaults, - title: __( 'Text Input Field', 'jetpack-forms' ), - description: __( 'Collect short text responses from site visitors.', 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: renderMaterialIcon( - - ), - }, - edit: editField( 'text' ), - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Text', 'jetpack-forms' ), - role: 'content', - }, - }, - }, - }, - { - name: 'field-number', - settings: { - ...FieldDefaults, - title: __( 'Number Input Field', 'jetpack-forms' ), - description: __( 'Collect numbers from site visitors.', 'jetpack-forms' ), - icon: renderMaterialIcon( - - ), - edit: EditNumber, - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Number', 'jetpack-forms' ), - }, - min: { - type: 'number', - default: '', - }, - max: { - type: 'number', - default: '', - }, - }, - }, - }, - { - name: 'field-name', - settings: { - ...FieldDefaults, - title: __( 'Name Field', 'jetpack-forms' ), - description: __( "Collect the site visitor's name.", 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: renderMaterialIcon( - - ), - }, - edit: editField( 'text' ), - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Name', 'jetpack-forms' ), - role: 'content', - }, - }, - }, - }, - { - name: 'field-email', - settings: { - ...FieldDefaults, - title: __( 'Email Field', 'jetpack-forms' ), - keywords: [ __( 'e-mail', 'jetpack-forms' ), __( 'mail', 'jetpack-forms' ), 'email' ], - description: __( 'Collect email addresses from your visitors.', 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: , - }, - edit: editField( 'email' ), - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Email', 'jetpack-forms' ), - role: 'content', - }, - }, - }, - }, - { - name: 'field-url', - settings: { - ...FieldDefaults, - title: __( 'Website Field', 'jetpack-forms' ), - keywords: [ - __( 'url', 'jetpack-forms' ), - __( 'internet page', 'jetpack-forms' ), - __( 'link', 'jetpack-forms' ), - __( 'website', 'jetpack-forms' ), - ], - description: __( 'Collect a website address from your site visitors.', 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: , - }, - edit: editField( 'url' ), - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Website', 'jetpack-forms' ), - role: 'content', - }, - }, - }, - }, - { - name: 'field-date', - settings: { - ...FieldDefaults, - title: __( 'Date Picker', 'jetpack-forms' ), - keywords: [ - __( 'Calendar', 'jetpack-forms' ), - _x( 'day month year', 'block search term', 'jetpack-forms' ), - ], - description: __( 'Capture date information with a date picker.', 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: renderMaterialIcon( - - ), - }, - edit: JetpackDatePicker, - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Date', 'jetpack-forms' ), - role: 'content', - }, - dateFormat: { - type: 'string', - default: 'yy-mm-dd', - }, - }, - }, - }, - { - name: 'field-telephone', - settings: { - ...FieldDefaults, - title: __( 'Phone Number Field', 'jetpack-forms' ), - keywords: [ - __( 'Phone', 'jetpack-forms' ), - __( 'Cellular phone', 'jetpack-forms' ), - __( 'Mobile', 'jetpack-forms' ), - ], - description: __( 'Collect phone numbers from site visitors.', 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: , - }, - edit: editField( 'tel' ), - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: 'Phone', - role: 'content', - }, - }, - }, - }, - { - name: 'field-file', - settings: { - ...FieldDefaults, - title: __( 'File Upload Field', 'jetpack-forms' ), - keywords: [ - __( 'File', 'jetpack-forms' ), - __( 'Upload', 'jetpack-forms' ), - __( 'Attachment', 'jetpack-forms' ), - ], - description: __( 'Allow visitors to upload files through your form.', 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: , - }, - edit: JetpackFieldFile, - save: () => { - const blockProps = useBlockProps.save(); - const innerBlocksProps = useInnerBlocksProps.save( { - className: 'jetpack-form-file-field__content-wrap', - } ); - return ( -
-
-
- ); - }, - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Upload a file', 'jetpack-forms' ), - role: 'content', - }, - filetype: { - type: 'string', - default: '', - }, - }, - isBeta: true, - }, - }, - { - name: 'field-textarea', - settings: { - ...FieldDefaults, - title: __( 'Multi-line Text Field', 'jetpack-forms' ), - keywords: [ - __( 'Textarea', 'jetpack-forms' ), - 'textarea', - __( 'Multiline text', 'jetpack-forms' ), - ], - description: __( 'Capture longform text responses from site visitors.', 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: renderMaterialIcon( - - ), - }, - edit: EditTextarea, - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Message', 'jetpack-forms' ), - role: 'content', - }, - }, - }, - }, - { - name: 'field-checkbox', - settings: { - ...FieldDefaults, - title: __( 'Checkbox', 'jetpack-forms' ), - keywords: [ __( 'Confirm', 'jetpack-forms' ), __( 'Accept', 'jetpack-forms' ) ], - description: __( 'Confirm or select information with a single checkbox.', 'jetpack-forms' ), - icon: { - foreground: getIconColor(), - src: renderMaterialIcon( - - ), - }, - edit: EditCheckbox, - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: '', - role: 'content', - }, - }, - }, - }, - { - name: 'field-consent', - settings: { - ...FieldDefaults, - title: __( 'Terms Consent', 'jetpack-forms' ), - keywords: [ __( 'Consent', 'jetpack-forms' ) ], - description: __( - 'Communicate site terms and offer visitors consent to those terms.', - 'jetpack-forms' - ), - icon: { - foreground: getIconColor(), - src: renderMaterialIcon( - <> - - - - ), - }, - attributes: { - ...FieldDefaults.attributes, - label: { - type: 'string', - default: __( 'Consent', 'jetpack-forms' ), - }, - consentType: { - type: 'string', - default: 'implicit', - }, - implicitConsentMessage: { - type: 'string', - default: __( - "By submitting your information, you're giving us permission to email you. You may unsubscribe at any time.", - 'jetpack-forms' - ), - }, - explicitConsentMessage: { - type: 'string', - default: __( 'Can we send you an email from time to time?', 'jetpack-forms' ), - }, - }, - edit: EditConsent, - }, - }, - { - name: JetpackFieldSingleChoice.name, - settings: mergeSettings( FieldDefaults, { - ...JetpackFieldSingleChoice.settings, - deprecated: [ - { - save() { - return ; - }, - }, - multiFieldV1( 'radio' ), - ], - } ), - }, - JetpackFieldSingleChoiceItem, - { - name: JetpackFieldMultipleChoice.name, - settings: mergeSettings( FieldDefaults, { - ...JetpackFieldMultipleChoice.settings, - deprecated: [ - { - save() { - return ; - }, - }, - multiFieldV1( 'checkbox' ), - ], - } ), - }, - JetpackFieldMultipleChoiceItem, - { - name: 'field-select', - settings: { - ...FieldDefaults, - title: __( 'Dropdown Field', 'jetpack-forms' ), - keywords: [ - __( 'Choose', 'jetpack-forms' ), - __( 'Dropdown', 'jetpack-forms' ), - __( 'Option', 'jetpack-forms' ), - ], - description: __( - 'Add a compact select box, that when expanded, allows visitors to choose one value from the list.', - 'jetpack-forms' - ), - icon: { - foreground: getIconColor(), - src: renderMaterialIcon( - - ), - }, - edit: JetpackDropdown, - attributes: { - ...FieldDefaults.attributes, - toggleLabel: { - type: 'string', - default: null, - role: 'content', - }, - options: { - type: 'array', - default: [ '' ], - role: 'content', - }, - }, - }, - }, + JetpackLabel, + JetpackDropzone, + JetpackInput, + JetpackOption, + JetpackOptions, + JetpackCheckboxField, + JetpackConsentField, + JetpackDateField, + JetpackDropdownField, + JetpackEmailField, + JetpackMultipleChoiceField, + JetpackNameField, + JetpackNumberField, + JetpackSingleChoiceField, + JetpackTextField, + JetpackUrlField, + JetpackTelephoneField, + JetpackTextareaField, + JetpackFieldFile, + // The following are required for these blocks to be parsed correctly in block + // deprecations. They have been flagged with `supports.inserter: false` to + // prevent further use. + DeprecatedOptionCheckbox, + DeprecatedOptionRadio, ]; diff --git a/projects/packages/forms/src/blocks/contact-form/class-contact-form-block.php b/projects/packages/forms/src/blocks/contact-form/class-contact-form-block.php index f9dbd4076142..e29e9e500933 100644 --- a/projects/packages/forms/src/blocks/contact-form/class-contact-form-block.php +++ b/projects/packages/forms/src/blocks/contact-form/class-contact-form-block.php @@ -92,53 +92,181 @@ public static function register_child_blocks() { return; } + // Field inner block types. + Blocks::jetpack_register_block( + 'jetpack/input', + array( + 'supports' => array( + '__experimentalBorder' => array( + 'color' => true, + 'radius' => true, + 'style' => true, + 'width' => true, + ), + 'color' => array( + 'text' => true, + 'background' => true, + 'gradients' => false, + ), + 'typography' => array( + 'fontSize' => true, + 'lineHeight' => true, + '__experimentalFontFamily' => true, + '__experimentalFontWeight' => true, + '__experimentalFontStyle' => true, + '__experimentalTextTransform' => true, + '__experimentalTextDecoration' => true, + '__experimentalLetterSpacing' => true, + ), + ), + 'selectors' => array( + 'border' => '.wp-block-jetpack-input, .is-style-outlined .notched-label:has(+ .wp-block-jetpack-input) > *,.is-style-outlined .wp-block-jetpack-input + .notched-label > *, .is-style-outlined .wp-block-jetpack-field-select .notched-label > *', + 'color' => '.wp-block-jetpack-input, .is-style-outlined .notched-label:has(+ .wp-block-jetpack-input) > *,.is-style-outlined .wp-block-jetpack-input + .notched-label > *, .is-style-outlined .wp-block-jetpack-field-select .notched-label > *', + ), + 'uses_context' => array( 'jetpack/field-defaultValue' ), + ) + ); + Blocks::jetpack_register_block( + 'jetpack/label', + array( + 'supports' => array( + 'color' => array( + 'text' => true, + 'background' => false, + 'gradients' => false, + ), + 'typography' => array( + 'fontSize' => true, + 'lineHeight' => true, + '__experimentalFontFamily' => true, + '__experimentalFontWeight' => true, + '__experimentalFontStyle' => true, + '__experimentalTextTransform' => true, + '__experimentalTextDecoration' => true, + '__experimentalLetterSpacing' => true, + ), + ), + 'uses_context' => array( + 'jetpack/field-required', + 'jetpack/field-dateFormat', + ), + ) + ); + Blocks::jetpack_register_block( + 'jetpack/options', + array( + 'supports' => array( + '__experimentalBorder' => array( + 'color' => true, + 'radius' => true, + 'style' => true, + 'width' => true, + ), + 'color' => array( + 'text' => false, + 'background' => true, + ), + 'spacing' => array( + 'blockGap' => false, + ), + ), + 'provides_context' => array( + 'jetpack/field-options-type' => 'type', + ), + 'selectors' => array( + 'border' => '.wp-block-jetpack-options, .is-style-outlined .notched-label:has(+ .wp-block-jetpack-options) > *', + 'color' => '.wp-block-jetpack-options, .is-style-outlined .notched-label:has(+ .wp-block-jetpack-options) > *', + ), + ) + ); + Blocks::jetpack_register_block( + 'jetpack/option', + array( + 'supports' => array( + 'color' => array( + 'text' => true, + 'background' => false, + 'gradients' => false, + ), + 'typography' => array( + 'fontSize' => true, + 'lineHeight' => true, + '__experimentalFontFamily' => true, + '__experimentalFontWeight' => true, + '__experimentalFontStyle' => true, + '__experimentalTextTransform' => true, + '__experimentalTextDecoration' => true, + '__experimentalLetterSpacing' => true, + ), + ), + 'uses_context' => array( + 'jetpack/field-defaultValue', + 'jetpack/field-options-type', + 'jetpack/field-required', + ), + ) + ); // Field render methods. Blocks::jetpack_register_block( 'jetpack/field-text', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_text' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_text' ), + 'provides_context' => array( 'jetpack/field-required' => 'required' ), ) ); Blocks::jetpack_register_block( 'jetpack/field-name', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_name' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_name' ), + 'provides_context' => array( 'jetpack/field-required' => 'required' ), ) ); Blocks::jetpack_register_block( 'jetpack/field-email', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_email' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_email' ), + 'provides_context' => array( 'jetpack/field-required' => 'required' ), ) ); Blocks::jetpack_register_block( 'jetpack/field-url', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_url' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_url' ), + 'provides_context' => array( 'jetpack/field-required' => 'required' ), ) ); Blocks::jetpack_register_block( 'jetpack/field-date', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_date' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_date' ), + 'provides_context' => array( + 'jetpack/field-required' => 'required', + 'jetpack/field-dateFormat' => 'dateFormat', + ), ) ); Blocks::jetpack_register_block( 'jetpack/field-telephone', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_telephone' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_telephone' ), + 'provides_context' => array( 'jetpack/field-required' => 'required' ), ) ); Blocks::jetpack_register_block( 'jetpack/field-textarea', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_textarea' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_textarea' ), + 'provides_context' => array( 'jetpack/field-required' => 'required' ), ) ); Blocks::jetpack_register_block( 'jetpack/field-checkbox', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_checkbox' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_checkbox' ), + 'provides_context' => array( + 'jetpack/field-required' => 'required', + 'jetpack/field-defaultValue' => 'defaultValue', + ), ) ); Blocks::jetpack_register_block( @@ -147,18 +275,21 @@ public static function register_child_blocks() { 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_checkbox_multiple' ), ) ); + Blocks::jetpack_register_block( 'jetpack/field-option-checkbox', array( 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_option' ), ) ); + Blocks::jetpack_register_block( 'jetpack/field-radio', array( 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_radio' ), ) ); + Blocks::jetpack_register_block( 'jetpack/field-option-radio', array( @@ -168,7 +299,8 @@ public static function register_child_blocks() { Blocks::jetpack_register_block( 'jetpack/field-select', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_select' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_select' ), + 'provides_context' => array( 'jetpack/field-required' => 'required' ), ) ); Blocks::jetpack_register_block( @@ -181,7 +313,32 @@ public static function register_child_blocks() { Blocks::jetpack_register_block( 'jetpack/field-number', array( - 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_number' ), + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_field_number' ), + 'provides_context' => array( 'jetpack/field-required' => 'required' ), + ) + ); + + /** + * The blocks 'jetpack/field-checkbox-multiple' and 'jetpack/field-radio' are wrapper blocks. + * Styles must be registered so that they are available to be overridden by the theme or global styles. + * Form field blocks define the block style via the settings in their index.js files. + * A follow up issue is to update them to use block.json files, which can be reused + * in both JS and PHP block registration. + */ + register_block_style( + array( 'jetpack/field-checkbox-multiple', 'jetpack/field-radio' ), + array( + 'name' => 'list', + 'label' => __( 'List', 'jetpack-forms' ), + 'is_default' => true, + ) + ); + + register_block_style( + array( 'jetpack/field-checkbox-multiple', 'jetpack/field-radio' ), + array( + 'name' => 'button', + 'label' => __( 'Button', 'jetpack-forms' ), ) ); @@ -203,6 +360,13 @@ private static function register_beta_blocks() { ) ); + Blocks::jetpack_register_block( + 'jetpack/dropzone', + array( + 'render_callback' => array( Contact_Form_Plugin::class, 'gutenblock_render_dropzone' ), + ) + ); + add_action( 'jetpack_register_gutenberg_extensions', array( __CLASS__, 'set_file_field_extension_available' ) diff --git a/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-datepicker.js b/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-datepicker.js deleted file mode 100644 index 4c114b12accf..000000000000 --- a/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-datepicker.js +++ /dev/null @@ -1,132 +0,0 @@ -import { useBlockProps } from '@wordpress/block-editor'; -import { createBlock, getDefaultBlockName } from '@wordpress/blocks'; -import { SelectControl } from '@wordpress/components'; -import { compose } from '@wordpress/compose'; -import { __ } from '@wordpress/i18n'; -import clsx from 'clsx'; -import { useFormStyle, useFormWrapper } from '../util/form'; -import { withSharedFieldAttributes } from '../util/with-shared-field-attributes'; -import JetpackFieldControls from './jetpack-field-controls'; -import JetpackFieldLabel from './jetpack-field-label'; -import { useJetpackFieldStyles } from './use-jetpack-field-styles'; - -const currentYear = new Date().getFullYear(); - -// WARNING: sync data with Contact_Form_Field::render_date_field in class-contact-form-field.php -const DATE_FORMATS = [ - { - value: 'mm/dd/yy', - /* translators: date format. DD is the day of the month, MM the month, and YYYY the year (e.g., 12/31/2023). */ - label: __( 'MM/DD/YYYY', 'jetpack-forms' ), - example: `12/31/${ currentYear }`, - }, - { - value: 'dd/mm/yy', - /* translators: date format. DD is the day of the month, MM the month, and YYYY the year (e.g., 31/12/2023). */ - label: __( 'DD/MM/YYYY', 'jetpack-forms' ), - example: `21/12/${ currentYear }`, - }, - { - value: 'yy-mm-dd', - /* translators: date format. DD is the day of the month, MM the month, and YYYY the year (e.g., 2023-12-31). */ - label: __( 'YYYY-MM-DD', 'jetpack-forms' ), - example: `${ currentYear }-12-31`, - }, -]; - -const JetpackDatePicker = props => { - const { attributes, clientId, isSelected, name, setAttributes, insertBlocksAfter } = props; - const { id, label, required, requiredText, width, placeholder, dateFormat } = attributes; - - useFormWrapper( { attributes, clientId, name } ); - - const { blockStyle, fieldStyle } = useJetpackFieldStyles( attributes ); - const formStyle = useFormStyle( clientId ); - const blockProps = useBlockProps( { - className: clsx( 'jetpack-field', { - 'is-selected': isSelected, - 'has-placeholder': !! placeholder, - } ), - style: blockStyle, - } ); - - return ( - <> -
- f.value === dateFormat )?.label })` } - required={ required } - requiredText={ requiredText } - setAttributes={ setAttributes } - style={ formStyle } - /> - setAttributes( { placeholder: e.target.value } ) } - style={ fieldStyle } - type="text" - value={ placeholder } - onKeyDown={ event => { - if ( event.defaultPrevented || event.key !== 'Enter' ) { - return; - } - insertBlocksAfter( createBlock( getDefaultBlockName() ) ); - } } - /> -
- - ( { - value, - label: `${ optionLabel } (${ example })`, - } ) ) } - onChange={ value => - setAttributes( { - dateFormat: value, - } ) - } - value={ attributes.dateFormat } - help={ __( - 'Select the format in which the date will be displayed.', - 'jetpack-forms' - ) } - __nextHasNoMarginBottom={ true } - __next40pxDefaultSize={ true } - /> - ), - }, - ] } - /> - - ); -}; - -export default compose( - withSharedFieldAttributes( [ - 'borderRadius', - 'borderWidth', - 'labelFontSize', - 'fieldFontSize', - 'lineHeight', - 'labelLineHeight', - 'inputColor', - 'labelColor', - 'fieldBackgroundColor', - 'borderColor', - ] ) -)( JetpackDatePicker ); diff --git a/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-file/index.js b/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-file/index.js deleted file mode 100644 index 3e3f505f0b52..000000000000 --- a/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-file/index.js +++ /dev/null @@ -1,146 +0,0 @@ -import { getJetpackExtensionAvailability } from '@automattic/jetpack-shared-extension-utils'; -import { useBlockProps, InnerBlocks } from '@wordpress/block-editor'; -import { compose } from '@wordpress/compose'; -import { useSelect } from '@wordpress/data'; -import { useMemo } from '@wordpress/element'; -import { __ } from '@wordpress/i18n'; -import { useFormWrapper } from '../../util/form'; -import { withSharedFieldAttributes } from '../../util/with-shared-field-attributes'; -import JetpackFieldControls from '../jetpack-field-controls'; -import JetpackFieldLabel from '../jetpack-field-label'; -import { UpsellNudge } from '../upsell-nudge'; -import { useJetpackFieldStyles } from '../use-jetpack-field-styles'; -import './editor.css'; - -const ALLOWED_BLOCKS = [ - 'core/columns', - 'core/group', - 'core/heading', - 'core/html', - 'core/image', - 'core/list', - 'core/paragraph', - 'core/row', - 'core/separator', - 'core/spacer', - 'core/stack', - 'core/subhead', -]; - -const BLOCKS_TEMPLATE = [ - [ - 'core/group', - { - style: { - spacing: { - padding: { - top: '48px', - bottom: '48px', - left: '48px', - right: '48px', - }, - margin: { - top: '0', - bottom: '0', - }, - }, - border: { - style: 'dashed', - width: '1px', - color: 'rgba(125,125,125,0.3)', - }, - }, - allowedBlocks: ALLOWED_BLOCKS, - }, - [ - [ - 'core/paragraph', - { - align: 'center', - content: __( - 'Select a file or drag and drop your file here.', - 'jetpack-forms' - ), - style: { - spacing: { - padding: { - top: '8px', - bottom: '8px', - }, - }, - typography: { - fontSize: '16px', - }, - }, - }, - ], - ], - ], -]; - -const JetpackFieldFile = props => { - const { attributes, clientId, isSelected, setAttributes, name } = props; - const { id, label, required, requiredText, width } = attributes; - const fieldFileAvailability = getJetpackExtensionAvailability( 'field-file' ); - - useFormWrapper( { attributes, clientId, name } ); - const { blockStyle } = useJetpackFieldStyles( attributes ); - - const blockProps = useBlockProps( { - className: `jetpack-field${ isSelected ? ' is-selected' : '' }`, - style: blockStyle, - } ); - - const isInnerBlockSelected = useSelect( select => { - const selectedBlockClientId = select( 'core/block-editor' ).getSelectedBlockClientId(); - const blockParents = select( 'core/block-editor' ).getBlockParents( selectedBlockClientId ); - return blockParents.includes( clientId ); - } ); - - const requiresCustomUpgradeNudge = useMemo( () => { - return ( - ( ! fieldFileAvailability || ! fieldFileAvailability.available ) && - fieldFileAvailability?.unavailableReason?.includes( 'nudge_disabled' ) - ); - }, [ fieldFileAvailability ] ); - - return ( - <> -
- { requiresCustomUpgradeNudge && ( isSelected || isInnerBlockSelected ) && ( - - ) } - -
-
- - -
-
-
- - - - ); -}; - -export default compose( - withSharedFieldAttributes( [ 'labelFontSize', 'labelLineHeight', 'labelColor' ] ) -)( JetpackFieldFile ); diff --git a/projects/packages/forms/src/blocks/contact-form/edit.js b/projects/packages/forms/src/blocks/contact-form/edit.js index 595bf1726b2e..d25f895ae62a 100644 --- a/projects/packages/forms/src/blocks/contact-form/edit.js +++ b/projects/packages/forms/src/blocks/contact-form/edit.js @@ -23,6 +23,7 @@ import { useRef } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import clsx from 'clsx'; import { filter, isArray, map } from 'lodash'; +import { SyncedAttributeProvider } from '../shared/hooks/use-synced-attributes'; import { childBlocks } from './child-blocks'; import InspectorHint from './components/inspector-hint'; import { ContactFormPlaceholder } from './components/jetpack-contact-form-placeholder'; @@ -32,7 +33,6 @@ import IntegrationControls from './components/jetpack-integration-controls'; import JetpackManageResponsesSettings from './components/jetpack-manage-responses-settings'; import VariationPicker from './variation-picker'; import './util/form-styles.js'; - const validFields = filter( childBlocks, ( { settings } ) => { return ( ! settings.parent || @@ -239,9 +239,11 @@ function JetpackContactFormEdit( { name, attributes, setAttributes, clientId, cl } return ( - -
{ elt }
-
+ + +
{ elt }
+
+
); } diff --git a/projects/packages/forms/src/blocks/contact-form/editor.scss b/projects/packages/forms/src/blocks/contact-form/editor.scss index 2d0038220023..eca426117f31 100644 --- a/projects/packages/forms/src/blocks/contact-form/editor.scss +++ b/projects/packages/forms/src/blocks/contact-form/editor.scss @@ -53,6 +53,10 @@ > div { flex: 1 1 100%; } + // Resets the flex property for the dropzone innerblock. + .wp-block-jetpack-dropzone .wp-block { + flex: initial; + } .wp-block { flex: 0 0 100%; @@ -278,6 +282,7 @@ .jetpack-field-label { display: flex; flex-direction: row; + flex-wrap: wrap; justify-content: flex-start; align-items: baseline; @@ -289,16 +294,26 @@ // Duplicated classname to preserve specificity level. .jetpack-field-label__input.jetpack-field-label__input { cursor: text; - font-weight: 700; margin-bottom: 0.25em; } .jetpack-field-label__suffix { margin-inline-start: 0.25em; - font-weight: 700; } + .components-toggle-control .components-base-control__field { + margin-bottom: 0; + } + + &[style*="--jetpack--contact-form--label--font-size"] { + font-size: var(--jetpack--contact-form--label--font-size); + } +} + +.jetpack-field-label, +.jetpack-field-option { + .required { word-break: normal; color: unset; @@ -307,19 +322,11 @@ margin-inline-start: 0.25em; font-weight: 400; } - - .components-toggle-control .components-base-control__field { - margin-bottom: 0; - } - - &[style*="--jetpack--contact-form--label--font-size"] { - font-size: var(--jetpack--contact-form--label--font-size); - } } .jetpack-field-label__input { min-height: unset; - + pointer-events: auto; padding: 0; } @@ -332,20 +339,29 @@ .jetpack-field__input, .jetpack-field__textarea { width: 100%; - font: inherit; box-sizing: border-box; box-shadow: unset; - border: 1px solid #8c8f94; - border-radius: 0; padding: 16px; margin: 0; } - .jetpack-field__textarea { + // Reduced specificity to 0,1,0 so that global styles can override it. + :where( .jetpack-field__input, .jetpack-field__textarea, .jetpack-field-dropdown__toggle ) { + font: inherit; + border: 1px solid #8c8f94; + border-radius: 0; + } + + :where( .jetpack-field-dropdown__toggle ) { + background-color: var(--jetpack--contact-form--input-background); + } + + // Reduced specificity to 0,1,0 so that global styles can override it. + :where( .jetpack-field__textarea ) { border: var(--jetpack--contact-form--border, 1px solid #8c8f94); border-color: var(--jetpack--contact-form--border-color, #8c8f94); - border-style: var(--jetpack--contact-form--border-style, solid ); - border-width: var(--jetpack--contact-form--border-size, 1px ); + border-style: var(--jetpack--contact-form--border-style, solid); + border-width: var(--jetpack--contact-form--border-left-size, 1px); } .components-base-control__field { @@ -361,21 +377,33 @@ .jetpack-option__type.jetpack-option__type { align-items: center; - all: initial; - appearance: none; - color: var(--jetpack--contact-form--text-color); display: flex; - border: none; - font-size: var(--jetpack--contact-form--font-size, 16px ); - font-family: var(--jetpack--contact-form--font-family); - height: var(--jetpack--contact-form--font-size, 16px ); justify-content: center; margin: 0 10px 0 0; - outline: none; position: relative; - width: var(--jetpack--contact-form--font-size, 16px); pointer-events: none; + /* Force opinionated styles for the input. */ + width: 1em !important; + height: 1em !important; + box-shadow: none !important; + border: none !important; + appearance: none !important; + outline: none !important; + + /* Some classic themes set the min-widths, which gives an elliptical shape to the input. */ + min-width: auto; + } + + :where(.jetpack-option__type) { + all: initial; + color: currentColor; + // Inherit the font size from the parent element, this needs + // to override the `all: initial` above. + // Set the height and width to 1em, so that the input scales + // with the font size. + font-size: inherit; + &::after, &::before { all: initial; color: inherit; @@ -393,76 +421,13 @@ content: ' '; display: flex; font-weight: 700; - height: var(--jetpack--contact-form--font-size, 16px); + height: 1em; justify-content: center; - width: var(--jetpack--contact-form--font-size, 16px); + width: 1em; } &[type="radio"]::before { border-radius: 50%; - transform: translateY(15%); - } - } - - &.jetpack-field-multiple { - - &.is-style-button { - - .jetpack-field-option, - .wp-block-jetpack-field-option-checkbox, - .wp-block-jetpack-field-option-radio { - background: var(--jetpack--contact-form--button-outline--background-color); - color: var(--jetpack--contact-form--button-outline--text-color); - border: var(--jetpack--contact-form--button-outline--border); - border-width: var(--jetpack--contact-form--button-outline--border-size); - border-color: currentColor; - border-radius: var(--jetpack--contact-form--button-outline--border-radius); - padding: var(--jetpack--contact-form--button-outline--padding); - line-height: var(--jetpack--contact-form--button-outline--line-height); - align-items: center; - - &.field-option-radio { - position: relative; - flex-direction: row-reverse; - gap: 16px; - - .jetpack-option__type { - display: none; - } - } - - .jetpack-option__type { - color: var(--jetpack--contact-form--button-outline--color); - } - } - - .wp-block-jetpack-field-option-radio { - - .jetpack-option__type { - display: none; - } - } - } - - .jetpack-field-multiple__add-option { - margin: 0; - padding: 0; - - .dashicon { - width: 1rem; - height: 1rem; - display: flex; - margin-left: 0; - margin-right: 0.75rem; - - &::before { - font-size: 1rem; - } - } - - svg { - margin-right: 12px; - } } } @@ -490,23 +455,75 @@ .jetpack-option__type::before { display: block; /* display: flex causes baselines to not align */ + transform: translateY(0.15em); } } +} + +.jetpack-field-multiple:where(.is-style-button) { + // Reduced specificity to 0,1,0 so that global styles can override it. + :where(.jetpack-field-option) { + background: var(--jetpack--contact-form--button-outline--background-color); + color: var(--jetpack--contact-form--button-outline--text-color); + border: var(--jetpack--contact-form--button-outline--border); + border-width: var(--jetpack--contact-form--button-outline--border-size); + border-color: currentColor; + border-radius: var(--jetpack--contact-form--button-outline--border-radius); + font-size: var(--jetpack--contact-form--font-size, 16px ); + font-family: var(--jetpack--contact-form--font-family); + padding: var(--jetpack--contact-form--button-outline--padding); + line-height: var(--jetpack--contact-form--button-outline--line-height); + } + + // Increase specificity to override the `align-items: baseline` from the `jetpack-field-option` class. + .jetpack-field-option.jetpack-field-option { + align-items: center; + } + + .jetpack-field-option.field-option-radio { + // TODO - Investigate if this is needed. + position: relative; + flex-direction: row-reverse; + gap: 16px; + + // Hide the radio input when in the button style. + // TODO - Check why this isn't done for the checkboxes. + .jetpack-option__type { + display: none; + } + } + + .jetpack-field-option .jetpack-option__type { + color: inherit; + } - .jetpack-field-option.field-option-radio, .wp-block-jetpack-field-option-radio { .jetpack-option__type { - transform: translateY(5%); /* Small offset to compensate the slightly odd perceived alignment that's due to the circular shape */ + display: none; } } } -:where(:not(.contact-form)) > .jetpack-field { +// TODO - See if we can find usage of this class. It's possibly a dead style. +.jetpack-field-multiple .jetpack-field-multiple__add-option { + margin: 0; + padding: 0; - .jetpack-field__input, .jetpack-field__textarea { - padding-left: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius)); - padding-right: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius)); + .dashicon { + width: 1rem; + height: 1rem; + display: flex; + margin-left: 0; + margin-right: 0.75rem; + + &::before { + font-size: 1rem; + } + } + + svg { + margin-right: 12px; } } @@ -521,10 +538,20 @@ gap: 12px; flex: 1; + &.has-background { + padding: var(--jetpack--contact-form--input-padding-left, 16px) 16px; + } + + &.jetpack-field-multiple__list--has-border { + padding-left: var(--jetpack--contact-form--input-padding-left, 16px); + padding-right: var(--jetpack--contact-form--input-padding-left, 16px); + } + &:empty { display: none; } + // TODO - This looks like dead code, it looks like field select can't contain a multiple list. // TODO: make this a class, @enej [data-type='jetpack/field-select'] & { border: 1px solid rgba( 0, 0, 0, 0.4 ); @@ -533,16 +560,18 @@ } } +// Ensure global styles can ovewrite the default border color. +:where(.jetpack-field-multiple__list.jetpack-field-multiple__list) { + border-color: var(--jetpack--contact-form--border-color); +} + .jetpack-option { display: flex; align-items: center; margin: 0; } -.jetpack-option__type.jetpack-option__type { - margin-top: 0; -} - +// TODO - This looks like dead code, along with the `JetpackOption` component. // Duplicated to elevate specificity in order to overwrite core styles .jetpack-option__input.jetpack-option__input.jetpack-option__input { color: inherit; @@ -567,6 +596,7 @@ font-size: inherit; } +// TODO - This is possibly dead CSS. // Duplicated to elevate specificity in order to overwrite calypso styles .jetpack-option__remove.jetpack-option__remove { padding: 6px; @@ -580,10 +610,6 @@ .jetpack-field-checkbox, .jetpack-field-consent { display: flex; - - .jetpack-field-label .jetpack-field-label__input { - font-weight: 400; - } } .jetpack-field-checkbox { @@ -595,15 +621,11 @@ } .jetpack-field-consent { - align-items: center; + font-weight: 400; - .jetpack-field-label { - flex-grow: 1; - - .jetpack-field-label__input { - font-size: 13px; - text-transform: uppercase; - } + :where(.wp-block-jetpack-option) { + font-size: 13px; + text-transform: uppercase; } } @@ -658,16 +680,10 @@ } &__toggle { - border: var(--jetpack--contact-form--border); - border-color: var(--jetpack--contact-form--border-color); - border-style: var(--jetpack--contact-form--border-style); - border-width: var(--jetpack--contact-form--border-size); - border-radius: var(--jetpack--contact-form--border-radius); - background-color: var(--jetpack--contact-form--input-background); color: var(--jetpack--contact-form--text-color); font-size: var(--jetpack--contact-form--font-size, 16px); - line-height: var(--jetpack--contact-form--line-height); - padding: var(--jetpack--contact-form--input-padding); + padding: var(--jetpack--contact-form--input-padding, 16px); + border-color: var(--jetpack--contact-form--border-color); display: flex; align-items: center; justify-content: space-between; @@ -684,38 +700,41 @@ border: 2px solid transparent; overflow: visible; margin-right: 4px; + // Match the frontend. + color: inherit; + font-size: inherit; } &__icon::after { + inset-inline-end: 20px; content: ""; display: block; - box-sizing: border-box; - width: 100%; - height: 100%; - border-bottom: 2px solid; - border-right: 2px solid; - transform: rotate(45deg); - margin-top: -2px; + width: calc(1em / 2.5); + height: calc(1em / 2.5); + border-bottom: calc(1em / 10) solid; + border-right: calc(1em / 10) solid; + transform: translateY(-12%) rotate(45deg); + transform-origin: center center; + pointer-events: none; + z-index: 1; } &__popover { - border: var(--jetpack--contact-form--border); - border-color: var(--jetpack--contact-form--border-color); - border-style: var(--jetpack--contact-form--border-style); - border-width: var(--jetpack--contact-form--border-size); - border-radius: var(--jetpack--contact-form--border-radius); - background-color: var(--jetpack--contact-form--input-background); - color: var(--jetpack--contact-form--text-color); + // The editor should not render the border styles for the dropdown toggle. + // The frontend renders a default select box. font-size: var(--jetpack--contact-form--font-size, 16px); max-height: 165px; overflow: auto; - padding: 0; line-height: normal; margin-top: 8px; - box-shadow: 0 2px 6px rgb(0 0 0 / 5%); + background-color: #fff; + color: #000; + border: 1px solid #000; + padding: 10px; + border-radius: unset; .rich-text { - padding: var(--jetpack--contact-form--input-padding); + margin: 10px; } } } @@ -745,8 +764,8 @@ right: -10px; } - .jetpack-field { - --notch-width: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius)); + .jetpack-field, + .wp-block-jetpack-field-checkbox-multiple { margin-top: 8px; box-sizing: border-box; @@ -760,62 +779,65 @@ display: flex; box-sizing: border-box; text-align: left; - z-index: 1; } .notched-label__leading { - width: var(--notch-width); - border: var(--jetpack--contact-form--border); - border-color: var(--jetpack--contact-form--border-color); - border-style: var(--jetpack--contact-form--border-style); - border-width: var(--jetpack--contact-form--border-size); - border-right: none; - border-radius: var(--jetpack--contact-form--border-radius); - border-top-right-radius: unset; - border-bottom-right-radius: unset; + width: var(--jetpack--contact-form--notch-width); + border-right: none !important; + border-top-right-radius: unset !important; + border-bottom-right-radius: unset !important; + box-sizing: content-box; + max-width: 100px; } .notched-label__notch { - border-radius: unset; - border: var(--jetpack--contact-form--border); - border-color: var(--jetpack--contact-form--border-color); - border-style: var(--jetpack--contact-form--border-style); - border-width: var(--jetpack--contact-form--border-size); + border-radius: unset !important; padding: 0 4px; transition: border 150ms linear; - } - - // For some reason, when keeping the rule below together with the above selector, - // on production builds, the attributes are being reordered, causing side-effects - // stylelint-disable-next-line no-duplicate-selectors - .notched-label__notch { - border-left: none; - border-right: none; + // For some reason, when keeping the rule below together with the above selector, + // on production builds, the attributes are being reordered, causing side-effects + // stylelint-disable-next-line no-duplicate-selectors + border-left: none !important; + border-right: none !important; + box-sizing: content-box; + + // This is required to ensure the color is not overridden by the global styles. + // See the selector in class-contact-form-block.php for 'jetpack/input' registration. + color: unset !important; } .notched-label__label { position: relative; top: 50%; transform: translateY(-50%); - will-change: transform; transition: all 150ms cubic-bezier(0.4, 0, 0.2, 1); margin: 0; - label { + .jetpack-field-label__suffix { font-weight: 300; } } - .notched-label__trailing { - flex-grow: 1; - border: var(--jetpack--contact-form--border); - border-color: var(--jetpack--contact-form--border-color); - border-style: var(--jetpack--contact-form--border-style); - border-width: var(--jetpack--contact-form--border-size); - border-left: none; - border-radius: var(--jetpack--contact-form--border-radius); - border-top-left-radius: unset; - border-bottom-left-radius: unset; + :where(.notched-label__filler) { + border-left: none !important; + border-right: none !important; + border-radius: unset !important; + box-sizing: content-box; + } + + :where(.notched-label__trailing) { + border-left: none !important; + border-top-left-radius: unset !important; + border-bottom-left-radius: unset !important; + box-sizing: content-box; + max-width: 100px; + } + + .jetpack-field-label .jetpack-field-label__input, + .jetpack-field-label .required { + will-change: font-size; + transition: font-size 150ms cubic-bezier(0.4, 0, 0.2, 1); + margin-bottom: 0; } &.jetpack-field-textarea .notched-label__label { @@ -823,46 +845,38 @@ transform: unset; } + &.is-non-animated-label .notched-label__label { + position: static; + transform: unset; + } + &.is-selected, &.has-placeholder { .notched-label__notch { - border-top-color: transparent; + border-top-color: transparent !important; } .notched-label__label { - top: calc(var(--jetpack--contact-form--border-size) * -1); + top: calc(var(--jetpack--contact-form--border-top-size, 1px) * -1); transform: translateY(-50%); - font-size: 0.8em; } - &.jetpack-field-dropdown { - - .jetpack-field-dropdown__toggle { - border-color: transparent; - padding-left: calc(var(--notch-width) + 4px); - padding-right: calc(var(--notch-width) + 4px); - } + .jetpack-field-label .required { + // The required text is always 85% of the label font size. + // The label font size is reduced to 0.8em in a separate rule, so that needs to be accounted for. + // 0.85 * 0.8 = 0.68em. + font-size: 0.68em; } - } - &.jetpack-field-multiple { - background-color: var(--jetpack--contact-form--input-background); - border-radius: var(--jetpack--contact-form--border-radius); - border: var(--jetpack--contact-form--border); - border-color: var(--jetpack--contact-form--border-color); - border-style: var(--jetpack--contact-form--border-style); - border-width: var(--jetpack--contact-form--border-size); - padding: var(--jetpack--contact-form--input-padding, 16px); - padding-top: calc(var(--jetpack--contact-form--input-padding, 16px) + 4px); - border-top: none; + .jetpack-field-label__input { + font-size: 0.8em; + } - .notched-label { - height: unset; + &.jetpack-field-dropdown { - &__leading, &__notch, &__trailing { - border-bottom: none; - border-left: none; - border-right: none; + .jetpack-field-dropdown__toggle .jetpack-field-dropdown__icon, + .jetpack-field-dropdown__toggle .rich-text { + z-index: 1; } } } @@ -873,22 +887,27 @@ .jetpack-field-dropdown { - &__popover .rich-text { - padding-left: calc(var(--notch-width) + 4px); - padding-right: calc(var(--notch-width) + 4px); - } - &__icon { margin-right: 0; } } - .jetpack-field__input, .jetpack-field__textarea { + .jetpack-field-multiple__list, .jetpack-field__input, .jetpack-field__textarea, .jetpack-field-dropdown__toggle { display: flex; - border-top-color: transparent !important; //Need to override styles coming from the style attribute + + /* + * For the Outlined block style, labels render the input background and + * border styles. + * Unfortunately, the !important rule is required to overcome the inline + * styles that block supports apply for block instance styles. + */ + background: none !important; + border-color: transparent !important; + border-radius: unset !important; outline: none; - padding-left: calc(var(--notch-width) + 4px); - padding-right: calc(var(--notch-width) + 4px); + padding: var(--jetpack--contact-form--input-padding, 16px); + padding-top: calc(var(--jetpack--contact-form--input-padding-top, 16px) + 4px); + padding-left: min(100px, var(--jetpack--contact-form--notch-width)); } } } @@ -896,9 +915,6 @@ .is-style-animated & { .jetpack-field { - --left-offset: calc(var(--jetpack--contact-form--input-padding-left, 16px) + var(--jetpack--contact-form--border-size)); - --label-left: max(var(--left-offset), var(--jetpack--contact-form--border-radius)); - --field-padding: calc(var(--label-left) - var(--jetpack--contact-form--border-size)); .jetpack-field__input { outline: none; @@ -914,63 +930,80 @@ .jetpack-field-dropdown__toggle { padding-top: 1.4em; - padding-left: var(--field-padding); - padding-right: var(--field-padding); + padding-left: var(--jetpack--contact-form--animated-left-offset); } - &.jetpack-field-dropdown { + .animated-label__label { + position: absolute; + z-index: 1; + top: calc(50% + (var(--jetpack--contact-form--border-top-size, 0px) * 0.5) - (var(--jetpack--contact-form--border-bottom-size, 0px) * 0.5)); + left: calc(var(--jetpack--contact-form--border-left-size, 1px) + var(--jetpack--contact-form--animated-left-offset)); + width: auto; + max-width: 100%; + box-sizing: border-box; + margin: 0; + transform: translateY(-50%); + transition: all 150ms cubic-bezier(0.4, 0, 0.2, 1); + will-change: transform, top; - .jetpack-field-dropdown__popover { + .jetpack-field-label__input { + margin: 0; + transition: font-size 150ms cubic-bezier(0.4, 0, 0.2, 1); + will-change: font-size; + } - .rich-text { - padding-left: var(--field-padding); - padding-right: var(--field-padding); - } + .required { + transition: font-size 150ms cubic-bezier(0.4, 0, 0.2, 1); + will-change: font-size; } } - &.jetpack-field-multiple { - padding-bottom: var(--jetpack--contact-form--input-padding, 16px); - padding-top: 1.8em; + .jetpack-field-multiple__list.jetpack-field-multiple__list--has-border { + padding-left: var(--jetpack--contact-form--animated-left-offset); + } - &.has-placeholder { + &.is-selected.jetpack-field-multiple .animated-label__label, + &.has-placeholder.jetpack-field-multiple .animated-label__label { + position: static; + margin-bottom: 0.25em; - .animated-label__label { - font-size: 1em; - top: 0; - left: 0; - } + .jetpack-field-label__input { + font-size: inherit; } } - .animated-label__label { - position: absolute; - z-index: 1; - top: 50%; - left: var(--label-left); - width: 100%; - max-width: 100%; - box-sizing: border-box; - margin: 0; - transform: translateY(-50%); - transition: all 150ms cubic-bezier(0.4, 0, 0.2, 1); + &.is-non-animated-label .animated-label__label { + position: static; + transform: translateY(0); + } + + &.is-selected.is-non-animated-label.jetpack-field .animated-label__label { .jetpack-field-label__input { - margin: 0; + font-size: inherit; } } &.jetpack-field-textarea .animated-label__label { transform: unset; - top: var(--jetpack--contact-form--input-padding-top, 16px); + top: calc(2px + var(--jetpack--contact-form--border-top-size, 1px)); } - &.is-selected, &.has-placeholder { + &.is-selected .animated-label__label:not(.is-selected), + &.has-placeholder .animated-label__label { + transform: translateY(0); + top: calc(2px + var(--jetpack--contact-form--border-top-size, 1px)); - .animated-label__label { + .jetpack-field-label__input, + .jetpack-field-label__suffix { font-size: 0.75em; - transform: translateY(0); - top: calc(2px + var(--jetpack--contact-form--border-size)); + } + + .required { + // The required text is usually 85% of the label font size. + // The label is being reduced to 0.75em in a separate rule which doesn't apply to the required text font size. + // Thus we take into account here - 0.85 * 0.75 = 0.6375em. + font-size: 0.6375em; } } } @@ -985,6 +1018,56 @@ } } +:where(.is-style-outlined .jetpack-contact-form .jetpack-field .notched-label__leading), +:where(.is-style-outlined .jetpack-contact-form .jetpack-field .notched-label__notch), +:where(.is-style-outlined .jetpack-contact-form .jetpack-field .notched-label__filler), +:where(.is-style-outlined .jetpack-contact-form .jetpack-field .notched-label__trailing) { + border: var(--jetpack--contact-form--border); + background-color: var(--jetpack--contact-form--input-background); +} + +:where( .wp-block-jetpack-contact-form.is-style-outlined ) { + + .notched-label__leading { + border-color: var(--jetpack--contact-form--border-color); + width: var(--jetpack--contact-form--notch-width); + border-right: none !important; + border-top-right-radius: unset !important; + border-bottom-right-radius: unset !important; + } + + .notched-label__notch { + border-color: var(--jetpack--contact-form--border-color); + border-style: var(--jetpack--contact-form--border-style); + border-radius: unset !important; + padding: 0 4px; + transition: border 150ms linear; + // For some reason, when keeping the rule below together with the above selector, + // on production builds, the attributes are being reordered, causing side-effects + border-left: none !important; + border-right: none !important; + } + + .notched-label__filler { + flex-grow: 1; + border-color: var(--jetpack--contact-form--border-color); + border-style: var(--jetpack--contact-form--border-style); + border-radius: unset !important; + border-left: none !important; + border-right: none !important; + } + + .notched-label__trailing { + flex-grow: 1; + border-color: var(--jetpack--contact-form--border-color); + border-style: var(--jetpack--contact-form--border-style); + border-radius: var(--jetpack--contact-form--border-radius); + border-left: none !important; + border-top-left-radius: unset !important; + border-bottom-left-radius: unset !important; + } +} + .block-editor-block-inspector .components-base-control .components-base-control:last-child { margin-bottom: 0; } @@ -1008,3 +1091,38 @@ justify-content: center; } } + +:where(.wp-block-jetpack-input) { + width: 100%; + box-sizing: border-box; +} + +.wp-block-jetpack-checkbox { + align-items: center; +} + +.jetpack-field-option__label-wrapper { + display: grid; + grid-template-columns: 1fr auto; + align-items: baseline; +} + +.notched-label, +.notched-label__notch, +.notched-label__leading, +.notched-label__trailing { + pointer-events: none; +} + +:where(.wp-block-jetpack-label) { + font-weight: 700; +} + +.is-style-outlined :where(.wp-block-jetpack-label) { + font-weight: 300; +} + +.jetpack-field-dropdown__toggle .rich-text { + flex-grow: 1; +} + diff --git a/projects/packages/forms/src/blocks/contact-form/index.js b/projects/packages/forms/src/blocks/contact-form/index.js index 3fc61c0b5e0f..366ea754a4ff 100644 --- a/projects/packages/forms/src/blocks/contact-form/index.js +++ b/projects/packages/forms/src/blocks/contact-form/index.js @@ -60,6 +60,9 @@ export const settings = { align: [ 'wide', 'full' ], }, attributes: defaultAttributes, + providesContext: { + 'jetpack/form-className': 'className', + }, edit, save: () => { const blockProps = useBlockProps.save(); diff --git a/projects/packages/forms/src/blocks/contact-form/util/form.js b/projects/packages/forms/src/blocks/contact-form/util/form.js index ea928c194e9c..013787258c7d 100644 --- a/projects/packages/forms/src/blocks/contact-form/util/form.js +++ b/projects/packages/forms/src/blocks/contact-form/util/form.js @@ -21,7 +21,7 @@ export const useFormWrapper = ( { attributes, clientId, name } ) => { lock: { remove: true }, }; - const { replaceBlock } = useDispatch( blockEditorStore ); + const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore ); const parents = useSelect( select => { return select( blockEditorStore ).getBlockParentsByBlockName( clientId, FORM_BLOCK_NAME ); @@ -29,6 +29,9 @@ export const useFormWrapper = ( { attributes, clientId, name } ) => { useEffect( () => { if ( ! parents?.length ) { + // As this is an automated update, ensure it doesn't end up in the undo stack + // by calling `__unstableMarkNextChangeAsNotPersistent`. + __unstableMarkNextChangeAsNotPersistent(); replaceBlock( clientId, createBlock( FORM_BLOCK_NAME, {}, [ diff --git a/projects/packages/forms/src/blocks/contact-form/variation-picker.js b/projects/packages/forms/src/blocks/contact-form/variation-picker.js index ba780cb2ba1f..41a5e5bbeff8 100644 --- a/projects/packages/forms/src/blocks/contact-form/variation-picker.js +++ b/projects/packages/forms/src/blocks/contact-form/variation-picker.js @@ -5,7 +5,7 @@ import { } from '@wordpress/block-editor'; import { createBlock, store as blocksStore } from '@wordpress/blocks'; import { Button, Modal } from '@wordpress/components'; -import { useDispatch, useSelect } from '@wordpress/data'; +import { useDispatch, useRegistry, useSelect } from '@wordpress/data'; import { useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import clsx from 'clsx'; @@ -21,6 +21,7 @@ const createBlocksFromInnerBlocksTemplate = innerBlocksTemplate => { }; export default function VariationPicker( { blockName, setAttributes, clientId, classNames } ) { + const registry = useRegistry(); const [ isPatternsModalOpen, setIsPatternsModalOpen ] = useState( false ); const { replaceInnerBlocks, selectBlock } = useDispatch( blockEditorStore ); const { blockType, defaultVariation, variations } = useSelect( @@ -57,18 +58,20 @@ export default function VariationPicker( { blockName, setAttributes, clientId, c ) } variations={ filter( variations, v => ! v.hiddenFromPicker ) } onSelect={ ( nextVariation = defaultVariation ) => { - if ( nextVariation.attributes ) { - setAttributes( nextVariation.attributes ); - } + registry.batch( () => { + if ( nextVariation.attributes ) { + setAttributes( nextVariation.attributes ); + } - if ( nextVariation.innerBlocks ) { - replaceInnerBlocks( - clientId, - createBlocksFromInnerBlocksTemplate( nextVariation.innerBlocks ) - ); - } + if ( nextVariation.innerBlocks ) { + replaceInnerBlocks( + clientId, + createBlocksFromInnerBlocksTemplate( nextVariation.innerBlocks ) + ); + } - selectBlock( clientId ); + selectBlock( clientId ); + } ); } } />
diff --git a/projects/packages/forms/src/blocks/contact-form/variations.js b/projects/packages/forms/src/blocks/contact-form/variations.js index e415b0da07be..10d623f76e57 100644 --- a/projects/packages/forms/src/blocks/contact-form/variations.js +++ b/projects/packages/forms/src/blocks/contact-form/variations.js @@ -29,9 +29,24 @@ const variations = compact( [ ), }, innerBlocks: [ - [ 'jetpack/field-name', { required: true, label: __( 'Name', 'jetpack-forms' ) } ], - [ 'jetpack/field-email', { required: true, label: __( 'Email', 'jetpack-forms' ) } ], - [ 'jetpack/field-textarea', { label: __( 'Message', 'jetpack-forms' ) } ], + [ + 'jetpack/field-name', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Name', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-email', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Email', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-textarea', + {}, + [ + [ 'jetpack/label', { label: __( 'Message', 'jetpack-forms' ) } ], + [ 'jetpack/input', { type: 'textarea' } ], + ], + ], [ 'jetpack/button', { @@ -65,17 +80,51 @@ const variations = compact( [ ), }, innerBlocks: [ - [ 'jetpack/field-name', { required: true, label: __( 'Name', 'jetpack-forms' ) } ], - [ 'jetpack/field-email', { required: true, label: __( 'Email', 'jetpack-forms' ) } ], + [ + 'jetpack/field-name', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Name', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-email', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Email', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], [ 'jetpack/field-radio', - { - label: __( 'Attending?', 'jetpack-forms' ), - required: true, - options: [ __( 'Yes', 'jetpack-forms' ), __( 'No', 'jetpack-forms' ) ], - }, + { required: true }, + [ + [ 'jetpack/label', { label: __( 'Attending?', 'jetpack-forms' ) } ], + [ + 'jetpack/options', + { type: 'radio' }, + [ + [ + 'jetpack/option', + { + label: __( 'Yes', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + [ + 'jetpack/option', + { + label: __( 'No', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + ], + ], + ], + ], + [ + 'jetpack/field-textarea', + {}, + [ + [ 'jetpack/label', { label: __( 'Other Details', 'jetpack-forms' ) } ], + [ 'jetpack/input', { type: 'textarea' } ], + ], ], - [ 'jetpack/field-textarea', { label: __( 'Other Details', 'jetpack-forms' ) } ], [ 'jetpack/button', { @@ -92,23 +141,56 @@ const variations = compact( [ innerBlocks: [ { name: 'jetpack/field-name', - attributes: { required: true, label: __( 'Name', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Name', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-email', - attributes: { required: true, label: __( 'Email', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Email', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-radio', - attributes: { - label: __( 'Attending?', 'jetpack-forms' ), - required: true, - options: [ __( 'Yes', 'jetpack-forms' ), __( 'No', 'jetpack-forms' ) ], - }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Attending?', 'jetpack-forms' ) } }, + { + name: 'jetpack/options', + attributes: { type: 'radio' }, + innerBlocks: [ + { + name: 'jetpack/option', + attributes: { + label: __( 'Yes', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( 'No', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + ], + }, + ], }, { name: 'jetpack/field-textarea', - attributes: { label: __( 'Other Details', 'jetpack-forms' ) }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { label: __( 'Other Details', 'jetpack-forms' ) }, + }, + { name: 'jetpack/input', attributes: { type: 'textarea' } }, + ], }, { name: 'jetpack/button', @@ -144,13 +226,24 @@ const variations = compact( [ ), }, innerBlocks: [ - [ 'jetpack/field-name', { required: true, label: __( 'Name', 'jetpack-forms' ) } ], - [ 'jetpack/field-email', { required: true, label: __( 'Email', 'jetpack-forms' ) } ], - [ 'jetpack/field-telephone', { label: __( 'Phone', 'jetpack-forms' ) } ], + [ + 'jetpack/field-name', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Name', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-email', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Email', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-telephone', + {}, + [ [ 'jetpack/label', { label: __( 'Phone', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], [ 'jetpack/field-select', { - label: __( 'How did you hear about us?', 'jetpack-forms' ), options: [ __( 'Search Engine', 'jetpack-forms' ), __( 'Social Media', 'jetpack-forms' ), @@ -159,8 +252,31 @@ const variations = compact( [ __( 'Friend or Family', 'jetpack-forms' ), ], }, + [ + [ 'jetpack/label', { label: __( 'How did you hear about us?', 'jetpack-forms' ) } ], + [ + 'jetpack/input', + { + type: 'dropdown', + placeholder: __( 'Select one option', 'jetpack-forms' ), + }, + ], + ], + ], + [ + 'jetpack/field-textarea', + {}, + [ + [ + 'jetpack/label', + { + label: __( 'Other Details', 'jetpack-forms' ), + defaultLabel: __( 'Message', 'jetpack-forms' ), + }, + ], + [ 'jetpack/input', { type: 'textarea' } ], + ], ], - [ 'jetpack/field-textarea', { label: __( 'Other Details', 'jetpack-forms' ) } ], [ 'jetpack/button', { @@ -177,20 +293,45 @@ const variations = compact( [ innerBlocks: [ { name: 'jetpack/field-name', - attributes: { required: true, label: __( 'Name', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { label: __( 'Name', 'jetpack-forms' ) }, + }, + { + name: 'jetpack/input', + }, + ], }, { name: 'jetpack/field-email', - attributes: { required: true, label: __( 'Email', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { label: __( 'Email', 'jetpack-forms' ) }, + }, + { + name: 'jetpack/input', + }, + ], }, { name: 'jetpack/field-telephone', - attributes: { required: true, label: __( 'Phone', 'jetpack-forms' ) }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { label: __( 'Phone', 'jetpack-forms' ) }, + }, + { + name: 'jetpack/input', + }, + ], }, { name: 'jetpack/field-select', attributes: { - label: __( 'How did you hear about us?', 'jetpack-forms' ), options: [ __( 'Search Engine', 'jetpack-forms' ), __( 'Social Media', 'jetpack-forms' ), @@ -199,10 +340,39 @@ const variations = compact( [ __( 'Friend or Family', 'jetpack-forms' ), ], }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'How did you hear about us?', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'dropdown', + placeholder: __( 'Select one option', 'jetpack-forms' ), + }, + }, + ], }, { name: 'jetpack/field-textarea', - attributes: { label: __( 'Other Details', 'jetpack-forms' ) }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Other Details', 'jetpack-forms' ), + defaultLabel: __( 'Message', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'textarea', + }, + }, + ], }, { name: 'jetpack/button', @@ -240,19 +410,61 @@ const variations = compact( [ ), }, innerBlocks: [ - [ 'jetpack/field-name', { required: true, label: __( 'Name', 'jetpack-forms' ) } ], - [ 'jetpack/field-email', { required: true, label: __( 'Email', 'jetpack-forms' ) } ], - [ 'jetpack/field-telephone', { required: true, label: __( 'Phone', 'jetpack-forms' ) } ], - [ 'jetpack/field-date', { label: __( 'Date', 'jetpack-forms' ), required: true } ], + [ + 'jetpack/field-name', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Name', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-email', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Email', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-telephone', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Phone', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-date', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Date', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], [ 'jetpack/field-radio', - { - label: __( 'Time', 'jetpack-forms' ), - required: true, - options: [ __( 'Morning', 'jetpack-forms' ), __( 'Afternoon', 'jetpack-forms' ) ], - }, + { required: true }, + [ + [ 'jetpack/label', { label: __( 'Time', 'jetpack-forms' ) } ], + [ + 'jetpack/options', + { type: 'radio' }, + [ + [ + 'jetpack/option', + { + label: __( 'Morning', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + [ + 'jetpack/option', + { + label: __( 'Afternoon', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + ], + ], + ], + ], + [ + 'jetpack/field-textarea', + {}, + [ + [ 'jetpack/label', { label: __( 'Notes', 'jetpack-forms' ) } ], + [ 'jetpack/input', { type: 'textarea' } ], + ], ], - [ 'jetpack/field-textarea', { label: __( 'Notes', 'jetpack-forms' ) } ], [ 'jetpack/button', { @@ -269,31 +481,69 @@ const variations = compact( [ innerBlocks: [ { name: 'jetpack/field-name', - attributes: { required: true, label: __( 'Name', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Name', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-email', - attributes: { required: true, label: __( 'Email', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Email', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-telephone', - attributes: { required: true, label: __( 'Phone', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Phone', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-date', - attributes: { required: true, label: __( 'Date', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Date', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-radio', - attributes: { - label: __( 'Time', 'jetpack-forms' ), - required: true, - options: [ __( 'Morning', 'jetpack-forms' ), __( 'Afternoon', 'jetpack-forms' ) ], - }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Time', 'jetpack-forms' ) } }, + { + name: 'jetpack/options', + attributes: { type: 'radio' }, + innerBlocks: [ + { + name: 'jetpack/option', + attributes: { + label: __( 'Morning', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( 'Afternoon', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + ], + }, + ], }, { name: 'jetpack/field-textarea', - attributes: { label: __( 'Notes', 'jetpack-forms' ) }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Notes', 'jetpack-forms' ) } }, + { name: 'jetpack/input', attributes: { type: 'textarea' } }, + ], }, { name: 'jetpack/button', @@ -339,23 +589,72 @@ const variations = compact( [ ), }, innerBlocks: [ - [ 'jetpack/field-name', { required: true, label: __( 'Name', 'jetpack-forms' ) } ], - [ 'jetpack/field-email', { required: true, label: __( 'Email', 'jetpack-forms' ) } ], + [ + 'jetpack/field-name', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Name', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-email', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Email', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], [ 'jetpack/field-radio', - { - label: __( 'Please rate our website', 'jetpack-forms' ), - required: true, - options: [ - __( '1 - Very Bad', 'jetpack-forms' ), - __( '2 - Poor', 'jetpack-forms' ), - __( '3 - Average', 'jetpack-forms' ), - __( '4 - Good', 'jetpack-forms' ), - __( '5 - Excellent', 'jetpack-forms' ), + { required: true }, + [ + [ 'jetpack/label', { label: __( 'Please rate our website', 'jetpack-forms' ) } ], + [ + 'jetpack/options', + { type: 'radio' }, + [ + [ + 'jetpack/option', + { + label: __( '1 - Very Bad', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + [ + 'jetpack/option', + { + label: __( '2 - Poor', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + [ + 'jetpack/option', + { + label: __( '3 - Average', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + [ + 'jetpack/option', + { + label: __( '4 - Good', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + [ + 'jetpack/option', + { + label: __( '5 - Excellent', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + ], + ], ], - }, + ], + ], + [ + 'jetpack/field-textarea', + {}, + [ + [ 'jetpack/label', { label: __( 'How could we improve?', 'jetpack-forms' ) } ], + [ 'jetpack/input', { type: 'textarea' } ], + ], ], - [ 'jetpack/field-textarea', { label: __( 'How could we improve?', 'jetpack-forms' ) } ], [ 'jetpack/button', { @@ -372,29 +671,80 @@ const variations = compact( [ innerBlocks: [ { name: 'jetpack/field-name', - attributes: { required: true, label: __( 'Name', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Name', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-email', - attributes: { required: true, label: __( 'Email', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Email', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-radio', - attributes: { - label: __( 'Please rate our website', 'jetpack-forms' ), - required: true, - options: [ - __( '1 - Very Bad', 'jetpack-forms' ), - __( '2 - Poor', 'jetpack-forms' ), - __( '3 - Average', 'jetpack-forms' ), - __( '4 - Good', 'jetpack-forms' ), - __( '5 - Excellent', 'jetpack-forms' ), - ], - }, + attributes: { required: true }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { label: __( 'Please rate our website', 'jetpack-forms' ) }, + }, + { + name: 'jetpack/options', + attributes: { type: 'radio' }, + innerBlocks: [ + { + name: 'jetpack/option', + attributes: { + label: __( '1 - Very Bad', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( '2 - Poor', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( '3 - Average', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( '4 - Good', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( '5 - Excellent', 'jetpack-forms' ), + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + }, + }, + ], + }, + ], }, { name: 'jetpack/field-textarea', - attributes: { label: __( 'How could we improve?', 'jetpack-forms' ) }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { label: __( 'How could we improve?', 'jetpack-forms' ) }, + }, + { name: 'jetpack/input', attributes: { type: 'textarea' } }, + ], }, { name: 'jetpack/button', @@ -421,9 +771,21 @@ const variations = compact( [ src: people, }, innerBlocks: [ - [ 'jetpack/field-name', { required: true, label: __( 'Name', 'jetpack-forms' ) } ], - [ 'jetpack/field-email', { required: true, label: __( 'Email', 'jetpack-forms' ) } ], - [ 'jetpack/field-consent', {} ], + [ + 'jetpack/field-name', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Name', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-email', + { required: true }, + [ [ 'jetpack/label', { label: __( 'Email', 'jetpack-forms' ) } ], [ 'jetpack/input' ] ], + ], + [ + 'jetpack/field-consent', + {}, + [ [ 'jetpack/label' ], [ 'jetpack/input', { type: 'checkbox' } ] ], + ], [ 'jetpack/button', { @@ -438,15 +800,27 @@ const variations = compact( [ innerBlocks: [ { name: 'jetpack/field-name', - attributes: { required: true, label: __( 'Name', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Name', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-email', - attributes: { required: true, label: __( 'Email', 'jetpack-forms' ) }, + attributes: { required: true }, + innerBlocks: [ + { name: 'jetpack/label', attributes: { label: __( 'Email', 'jetpack-forms' ) } }, + { name: 'jetpack/input' }, + ], }, { name: 'jetpack/field-consent', attributes: {}, + innerBlocks: [ + { name: 'jetpack/label' }, + { name: 'jetpack/input', attributes: { type: 'checkbox' } }, + ], }, { name: 'jetpack/button', diff --git a/projects/packages/forms/src/blocks/deprecated/field-option-checkbox/index.js b/projects/packages/forms/src/blocks/deprecated/field-option-checkbox/index.js new file mode 100644 index 000000000000..28e6a864db59 --- /dev/null +++ b/projects/packages/forms/src/blocks/deprecated/field-option-checkbox/index.js @@ -0,0 +1,31 @@ +import { __ } from '@wordpress/i18n'; + +const name = 'field-option-checkbox'; +const settings = { + apiVersion: 3, + category: 'contact-form', + attributes: { + label: { + type: 'string', + role: 'content', + }, + fieldType: { + enum: [ 'checkbox', 'radio' ], + default: 'checkbox', + }, + }, + supports: { + html: false, + inserter: false, + reusable: false, + splitting: true, + }, + title: __( 'Multiple Choice Option', 'jetpack-forms' ), + edit: () => null, + save: () => null, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/deprecated/field-option-radio/index.js b/projects/packages/forms/src/blocks/deprecated/field-option-radio/index.js new file mode 100644 index 000000000000..f82c51a2acec --- /dev/null +++ b/projects/packages/forms/src/blocks/deprecated/field-option-radio/index.js @@ -0,0 +1,31 @@ +import { __ } from '@wordpress/i18n'; + +const name = 'field-option-radio'; +const settings = { + apiVersion: 3, + category: 'contact-form', + attributes: { + label: { + type: 'string', + role: 'content', + }, + fieldType: { + enum: [ 'checkbox', 'radio' ], + default: 'checkbox', + }, + }, + supports: { + html: false, + inserter: false, + reusable: false, + splitting: true, + }, + title: __( 'Single Choice Option', 'jetpack-forms' ), + edit: () => null, + save: () => null, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/dropzone/edit.js b/projects/packages/forms/src/blocks/dropzone/edit.js new file mode 100644 index 000000000000..a09cc63aaf5b --- /dev/null +++ b/projects/packages/forms/src/blocks/dropzone/edit.js @@ -0,0 +1,37 @@ +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; + +import './editor.scss'; + +const BLOCKS_TEMPLATE = [ + [ + 'core/paragraph', + { + content: __( 'Drag and drop or click to select a file.', 'jetpack-forms' ), + style: { + spacing: { + padding: { + top: '8px', + bottom: '8px', + }, + margin: { + top: '0', + bottom: '0', + }, + }, + typography: { + fontSize: '16px', + }, + }, + }, + ], +]; + +export default function DropzoneEdit() { + const blockProps = useBlockProps( { className: 'jetpack-form-file-field__dropzone' } ); + const innerBlocksProps = useInnerBlocksProps( blockProps, { + template: BLOCKS_TEMPLATE, + } ); + + return
; +} diff --git a/projects/packages/forms/src/blocks/dropzone/editor.scss b/projects/packages/forms/src/blocks/dropzone/editor.scss new file mode 100644 index 000000000000..237aea3c8405 --- /dev/null +++ b/projects/packages/forms/src/blocks/dropzone/editor.scss @@ -0,0 +1,6 @@ +:where( .jetpack-form-file-field__dropzone ) { + padding: 48px; + margin-top: 8px; + margin-bottom: 8px; + border: 1px dashed var(--jp-form-color-border, rgba( 125, 125, 125, 0.3 ) ); +} diff --git a/projects/packages/forms/src/blocks/dropzone/index.js b/projects/packages/forms/src/blocks/dropzone/index.js new file mode 100644 index 000000000000..cf1586ed0dba --- /dev/null +++ b/projects/packages/forms/src/blocks/dropzone/index.js @@ -0,0 +1,95 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import edit from './edit'; +import save from './save'; + +const name = 'dropzone'; +const settings = { + apiVersion: 3, + title: __( 'File upload dropzone', 'jetpack-forms' ), + description: __( 'A dropzone for file uploads.', 'jetpack-forms' ), + parent: [ 'jetpack/field-file' ], + allowedBlocks: [ + 'core/buttons', + 'core/heading', + 'core/image', + 'core/list', + 'core/paragraph', + 'core/separator', + 'core/spacer', + ], + category: 'contact-form', + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + attributes: { + style: { + type: 'object', + default: { + layout: { + type: 'flex', + justifyContent: 'center', + orientation: 'vertical', + }, + }, + }, + }, + supports: { + reusable: false, + html: false, + // Mimic the layout settings of the core Group block. + layout: { + type: 'flex', + allowSwitching: false, + allowInheriting: false, + allowJustification: true, + allowVerticalAlignment: false, + allowOrientation: false, + default: { + type: 'flex', + }, + }, + color: { + gradients: true, + heading: true, + button: true, + link: true, + __experimentalDefaultControls: { + background: true, + text: true, + }, + }, + spacing: { + margin: [ 'top', 'bottom' ], + padding: true, + }, + dimensions: { + minHeight: true, + }, + __experimentalBorder: { + color: true, + radius: true, + style: true, + width: true, + __experimentalDefaultControls: { + color: true, + radius: true, + style: true, + width: true, + }, + }, + align: [ 'wide', 'full' ], + }, + edit, + save, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/dropzone/save.js b/projects/packages/forms/src/blocks/dropzone/save.js new file mode 100644 index 000000000000..d1f7875a1452 --- /dev/null +++ b/projects/packages/forms/src/blocks/dropzone/save.js @@ -0,0 +1,7 @@ +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const blockProps = useBlockProps.save(); + const innerBlocksProps = useInnerBlocksProps.save( blockProps ); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-checkbox/deprecated.js b/projects/packages/forms/src/blocks/field-checkbox/deprecated.js new file mode 100644 index 000000000000..ff4c9f91e021 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-checkbox/deprecated.js @@ -0,0 +1,31 @@ +import { createBlock } from '@wordpress/blocks'; +import { __ } from '@wordpress/i18n'; +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; +import deprecateFieldStyles from '../shared/util/deprecate-field-styles'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { + type: 'string', + default: '', + role: 'content', + }, + }, + migrate( attributes ) { + const { restAttributes, labelStyles } = deprecateFieldStyles( attributes ); + const newInnerBlocks = [ + createBlock( 'jetpack/option', { + label: attributes.label, + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + isStandalone: true, + style: labelStyles, + } ), + ]; + + return [ restAttributes, newInnerBlocks ]; + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-checkbox/edit.js b/projects/packages/forms/src/blocks/field-checkbox/edit.js new file mode 100644 index 000000000000..4560bcf1d67b --- /dev/null +++ b/projects/packages/forms/src/blocks/field-checkbox/edit.js @@ -0,0 +1,101 @@ +import { + InspectorControls, + BlockControls, + useBlockProps, + useInnerBlocksProps, +} from '@wordpress/block-editor'; +import { PanelBody, ToggleControl } from '@wordpress/components'; +import { useCallback } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import ToolbarRequiredGroup from '../contact-form/components/block-controls/toolbar-required-group'; +import JetpackFieldWidth from '../contact-form/components/jetpack-field-width'; +import JetpackManageResponsesSettings from '../contact-form/components/jetpack-manage-responses-settings'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; +import useJetpackFieldStyles from '../shared/hooks/use-jetpack-field-styles'; +import { ALLOWED_INNER_BLOCKS } from '../shared/util/constants'; + +export default function CheckboxFieldEdit( props ) { + const { setAttributes, attributes } = props; + const { defaultValue, required, width } = attributes; + + useFormWrapper( props ); + + // TODO: Is this block style needed? + const { blockStyle } = useJetpackFieldStyles( attributes ); + const blockProps = useBlockProps( { + className: 'jetpack-field jetpack-field-checkbox', + style: { + ...blockStyle, + // alignItems: 'center', // TODO: Is this needed on the frontend too? + }, + } ); + + const template = [ [ 'jetpack/option', { isStandalone: true } ] ]; + const innerBlocksProps = useInnerBlocksProps( blockProps, { + allowedBlocks: ALLOWED_INNER_BLOCKS, + template, + templateLock: 'all', + } ); + + const onDefaultValueChange = useCallback( + value => setAttributes( { defaultValue: value ? 'true' : '' } ), + [ setAttributes ] + ); + + const onRequiredToggle = useCallback( + () => setAttributes( { required: ! required } ), + [ setAttributes, required ] + ); + + const onRequiredChange = useCallback( + value => setAttributes( { required: value } ), + [ setAttributes ] + ); + + const onShareFieldAttributesChange = useCallback( + value => setAttributes( { shareFieldAttributes: value } ), + [ setAttributes ] + ); + + return ( + <> +
+ + + + + + + + + + + + + + + + + + + + + ); +} diff --git a/projects/packages/forms/src/blocks/field-checkbox/index.js b/projects/packages/forms/src/blocks/field-checkbox/index.js new file mode 100644 index 000000000000..0a5458d97b5b --- /dev/null +++ b/projects/packages/forms/src/blocks/field-checkbox/index.js @@ -0,0 +1,56 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-checkbox'; +const settings = { + ...defaultSettings, + title: __( 'Checkbox', 'jetpack-forms' ), + keywords: [ __( 'Confirm', 'jetpack-forms' ), __( 'Accept', 'jetpack-forms' ) ], + description: __( 'Confirm or select information with a single checkbox.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + edit, + attributes: { + ...defaultSettings.attributes, + defaultValue: { + type: 'string', + default: '', + }, + }, + providesContext: { + ...defaultSettings.providesContext, + 'jetpack/field-defaultValue': 'defaultValue', + }, + deprecated, + save, + example: { + attributes: { defaulValue: '' }, + innerBlocks: [ + { + name: 'jetpack/option', + attributes: { + label: __( 'Option', 'jetpack-forms' ), + isStandalone: true, + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-checkbox/save.js b/projects/packages/forms/src/blocks/field-checkbox/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-checkbox/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-consent/deprecated.js b/projects/packages/forms/src/blocks/field-consent/deprecated.js new file mode 100644 index 000000000000..e30816b3423e --- /dev/null +++ b/projects/packages/forms/src/blocks/field-consent/deprecated.js @@ -0,0 +1,47 @@ +import { createBlock } from '@wordpress/blocks'; +import { __ } from '@wordpress/i18n'; +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; +import deprecateFieldStyles from '../shared/util/deprecate-field-styles'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { + type: 'string', + default: __( 'Consent', 'jetpack-forms' ), + }, + consentType: { + type: 'string', + default: 'implicit', + }, + implicitConsentMessage: { + type: 'string', + default: __( + "By submitting your information, you're giving us permission to email you. You may unsubscribe at any time.", + 'jetpack-forms' + ), + }, + explicitConsentMessage: { + type: 'string', + default: __( 'Can we send you an email from time to time?', 'jetpack-forms' ), + }, + }, + migrate( attributes ) { + const { restAttributes, labelStyles } = deprecateFieldStyles( attributes ); + const newInnerBlocks = [ + createBlock( 'jetpack/option', { + defaultLabel: __( 'Add option…', 'jetpack-forms' ), + hideInput: attributes.consentType === 'implicit', + isStandalone: true, + label: attributes.label, + requiredText: attributes.requiredText, + style: labelStyles, + } ), + ]; + + return [ restAttributes, newInnerBlocks ]; + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-consent/edit.js b/projects/packages/forms/src/blocks/field-consent/edit.js new file mode 100644 index 000000000000..5863ea48e069 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-consent/edit.js @@ -0,0 +1,189 @@ +import { + InspectorControls, + store as blockEditorStore, + useBlockProps, + useInnerBlocksProps, +} from '@wordpress/block-editor'; +import { getBlockType } from '@wordpress/blocks'; +import { BaseControl, PanelBody, SelectControl, ToggleControl } from '@wordpress/components'; +import { usePrevious } from '@wordpress/compose'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { useCallback, useEffect, useMemo } from '@wordpress/element'; +import { __, sprintf } from '@wordpress/i18n'; +import JetpackFieldWidth from '../contact-form/components/jetpack-field-width'; +import JetpackManageResponsesSettings from '../contact-form/components/jetpack-manage-responses-settings'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function ConsentFieldEdit( props ) { + const { attributes, clientId, setAttributes } = props; + const { consentType, width, implicitConsentMessage, explicitConsentMessage } = attributes; + + useFormWrapper( props ); + + const blockProps = useBlockProps( { + className: 'jetpack-field jetpack-field-consent', + } ); + + // Memoized default label values from the block type definition. + const defaultLabels = useMemo( () => { + const consentBlockType = getBlockType( 'jetpack/consent' ); + const defaultAttributes = consentBlockType?.attributes || {}; + + return { + implicit: defaultAttributes.implicitConsentMessage?.default || '', + explicit: defaultAttributes.explicitConsentMessage?.default || '', + }; + }, [] ); + + // Template is only used on initial block insertion. + const template = useMemo( + () => [ + [ + 'jetpack/option', + { + label: implicitConsentMessage, + defaultLabel: sprintf( + /* translators: placeholder is a type of consent: implicit or explicit */ + __( 'Add %s consent message…', 'jetpack-forms' ), + 'implicit' + ), + isStandalone: true, + hideInput: true, + }, + ], + ], + [ implicitConsentMessage ] + ); + + const innerBlocksProps = useInnerBlocksProps( blockProps, { + allowedBlocks: [ 'jetpack/option' ], + template, + templateLock: 'all', + } ); + + const optionBlock = useSelect( + select => select( blockEditorStore ).getBlocks( clientId )[ 0 ], + [ clientId ] + ); + const { clientId: optionBlockId } = optionBlock ?? {}; + + const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } = + useDispatch( blockEditorStore ); + + const prevConsentType = usePrevious( consentType ); + const prevLabel = usePrevious( optionBlock?.attributes?.label ); + + // Update the inner option block when the consentType changes. + useEffect( () => { + if ( optionBlockId && consentType !== prevConsentType ) { + const label = consentType === 'explicit' ? explicitConsentMessage : implicitConsentMessage; + + // As this is an automated update, ensure it doesn't end up in the undo stack + // by calling `__unstableMarkNextChangeAsNotPersistent`. + __unstableMarkNextChangeAsNotPersistent(); + updateBlockAttributes( optionBlockId, { + label, + defaultLabel: sprintf( + /* translators: placeholder is a type of consent: implicit or explicit */ + __( 'Add %s consent message…', 'jetpack-forms' ), + consentType + ), + hideInput: consentType !== 'explicit', + } ); + } + }, [ + optionBlockId, + consentType, + prevConsentType, + explicitConsentMessage, + implicitConsentMessage, + updateBlockAttributes, + __unstableMarkNextChangeAsNotPersistent, + ] ); + + // Persist user-edited labels to the correct parent attribute. + useEffect( () => { + if ( ! optionBlock?.attributes?.label || consentType !== prevConsentType ) { + return; + } + + const currentLabel = optionBlock.attributes.label; + const defaultLabel = defaultLabels[ consentType ]; + + const isNewlyTyped = prevLabel && currentLabel !== prevLabel && currentLabel !== defaultLabel; + + if ( isNewlyTyped ) { + // As this is an automated update, ensure it doesn't end up in the undo stack + // by calling `__unstableMarkNextChangeAsNotPersistent`. + __unstableMarkNextChangeAsNotPersistent(); + setAttributes( { + [ consentType === 'explicit' ? 'explicitConsentMessage' : 'implicitConsentMessage' ]: + currentLabel, + } ); + } + }, [ + optionBlock?.attributes?.label, + prevLabel, + consentType, + prevConsentType, + defaultLabels, + setAttributes, + __unstableMarkNextChangeAsNotPersistent, + ] ); + + const onShareFieldAttributesChange = useCallback( + value => { + setAttributes( { shareFieldAttributes: value } ); + }, + [ setAttributes ] + ); + + const onConsentTypeChange = useCallback( + value => { + setAttributes( { consentType: value } ); + }, + [ setAttributes ] + ); + + return ( + <> +
+ + + + + + + + + + + + + + + + ); +} diff --git a/projects/packages/forms/src/blocks/field-consent/index.js b/projects/packages/forms/src/blocks/field-consent/index.js new file mode 100644 index 000000000000..d21caf3c5a21 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-consent/index.js @@ -0,0 +1,77 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-consent'; +const settings = { + ...defaultSettings, + title: __( 'Terms Consent', 'jetpack-forms' ), + keywords: [ __( 'Consent', 'jetpack-forms' ) ], + description: __( + 'Communicate site terms and offer visitors consent to those terms.', + 'jetpack-forms' + ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + <> + + + + ), + }, + edit, + attributes: { + ...defaultSettings.attributes, + consentType: { + type: 'string', + default: 'implicit', + }, + defaultValue: { + type: 'string', + default: '', + }, + implicitConsentMessage: { + type: 'string', + default: __( + "By submitting your information, you're giving us permission to email you. You may unsubscribe at any time.", + 'jetpack-forms' + ), + }, + explicitConsentMessage: { + type: 'string', + default: __( 'Can we send you an email from time to time?', 'jetpack-forms' ), + }, + }, + providesContext: { + ...defaultSettings.providesContext, + 'jetpack/field-defaultValue': 'defaultValue', + }, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/option', + attributes: { + label: __( + "By submitting your information, you're giving us permission to email you. You may unsubscribe at any time.", + 'jetpack-forms' + ), + isStandalone: true, + hideInput: true, + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-consent/save.js b/projects/packages/forms/src/blocks/field-consent/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-consent/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-date/deprecated.js b/projects/packages/forms/src/blocks/field-date/deprecated.js new file mode 100644 index 000000000000..178f0e4bf534 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-date/deprecated.js @@ -0,0 +1,12 @@ +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { type: 'string', default: 'Date' }, + dateFormat: { type: 'string', default: 'yy-mm-dd' }, + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-date/edit.js b/projects/packages/forms/src/blocks/field-date/edit.js new file mode 100644 index 000000000000..e33562da868a --- /dev/null +++ b/projects/packages/forms/src/blocks/field-date/edit.js @@ -0,0 +1,82 @@ +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; +import { getBlockType } from '@wordpress/blocks'; +import { SelectControl } from '@wordpress/components'; +import { useCallback, useMemo } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import clsx from 'clsx'; +import JetpackFieldControls from '../shared/components/jetpack-field-controls'; +import useFieldSelected from '../shared/hooks/use-field-selected'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; +import useJetpackFieldStyles from '../shared/hooks/use-jetpack-field-styles'; +import { ALLOWED_INNER_BLOCKS, DATE_FORMAT_OPTIONS } from '../shared/util/constants'; + +export default function DateFieldEdit( props ) { + const { attributes, clientId, isSelected, name, setAttributes } = props; + const { id, required, width, dateFormat } = attributes; + + useFormWrapper( { attributes, clientId, name } ); + const { blockStyle } = useJetpackFieldStyles( attributes ); + const { isInnerBlockSelected, hasPlaceholder } = useFieldSelected( clientId ); + const blockProps = useBlockProps( { + className: clsx( 'jetpack-field', { + 'is-selected': isSelected || isInnerBlockSelected, + 'has-placeholder': hasPlaceholder, + } ), + style: blockStyle, + } ); + + const labelBlockType = getBlockType( 'jetpack/label' ); + const defaultLabel = labelBlockType.attributes.label.default; + const template = useMemo( () => { + return [ + [ 'jetpack/label', { label: __( 'Date', 'jetpack-forms' ), required, defaultLabel } ], + [ 'jetpack/input' ], + ]; + }, [ defaultLabel, required ] ); + + const innerBlocksProps = useInnerBlocksProps( blockProps, { + allowedBlocks: ALLOWED_INNER_BLOCKS, + template, + templateLock: 'all', + } ); + const onChange = useCallback( + value => { + setAttributes( { dateFormat: value } ); + }, + [ setAttributes ] + ); + + return ( + <> +
+ + ), + }, + ] } + /> + + ); +} diff --git a/projects/packages/forms/src/blocks/field-date/index.js b/projects/packages/forms/src/blocks/field-date/index.js new file mode 100644 index 000000000000..39c29b69e660 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-date/index.js @@ -0,0 +1,63 @@ +import { Path } from '@wordpress/components'; +import { __, _x } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-date'; +const settings = { + ...defaultSettings, + title: __( 'Date Picker', 'jetpack-forms' ), + keywords: [ + __( 'Calendar', 'jetpack-forms' ), + _x( 'day month year', 'block search term', 'jetpack-forms' ), + ], + description: __( 'Capture date information with a date picker.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + edit, + providesContext: { + ...defaultSettings.providesContext, + 'jetpack/field-dateFormat': 'dateFormat', + }, + attributes: { + ...defaultSettings.attributes, + dateFormat: { + type: 'string', + default: 'yy-mm-dd', + }, + }, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Date', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'text', + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-date/save.js b/projects/packages/forms/src/blocks/field-date/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-date/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-email/deprecated.js b/projects/packages/forms/src/blocks/field-email/deprecated.js new file mode 100644 index 000000000000..058c76753a1b --- /dev/null +++ b/projects/packages/forms/src/blocks/field-email/deprecated.js @@ -0,0 +1,11 @@ +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { type: 'string', default: 'Email' }, + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-email/edit.js b/projects/packages/forms/src/blocks/field-email/edit.js new file mode 100644 index 000000000000..e1530bdc1d25 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-email/edit.js @@ -0,0 +1,25 @@ +import { __ } from '@wordpress/i18n'; +import JetpackField from '../shared/components/jetpack-field'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function EmailFieldEdit( props ) { + useFormWrapper( props ); + + return ( + + ); +} diff --git a/projects/packages/forms/src/blocks/field-email/index.js b/projects/packages/forms/src/blocks/field-email/index.js new file mode 100644 index 000000000000..f30c5187d811 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-email/index.js @@ -0,0 +1,44 @@ +import { Icon } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { envelope } from '@wordpress/icons'; +import { getIconColor } from '../contact-form/util/block-icons'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-email'; +const settings = { + ...defaultSettings, + title: __( 'Email Field', 'jetpack-forms' ), + keywords: [ __( 'e-mail', 'jetpack-forms' ), __( 'mail', 'jetpack-forms' ), 'email' ], + description: __( 'Collect email addresses from your visitors.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: , + }, + edit, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Email', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'email', + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-email/save.js b/projects/packages/forms/src/blocks/field-email/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-email/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-file/edit.js b/projects/packages/forms/src/blocks/field-file/edit.js new file mode 100644 index 000000000000..e618a3aeab80 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-file/edit.js @@ -0,0 +1,62 @@ +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; +import clsx from 'clsx'; +import JetpackFieldControls from '../shared/components/jetpack-field-controls'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; +import useJetpackFieldStyles from '../shared/hooks/use-jetpack-field-styles'; +import './editor.scss'; + +export default function FileFieldEdit( props ) { + const { attributes, clientId, isSelected, setAttributes, name, className } = props; + const { id, required, width } = attributes; + + useFormWrapper( { attributes, clientId, name } ); + const { blockStyle } = useJetpackFieldStyles( attributes ); + + const classes = clsx( className, 'jetpack-field is-non-animated-label', { + 'is-selected': isSelected, + [ `jetpack-field__width-${ width }` ]: width, + } ); + + const blockProps = useBlockProps( { + className: classes, + style: blockStyle, + } ); + + const uploadLabel = __( 'Upload a file', 'jetpack-forms' ); + const innerBlocksProps = useInnerBlocksProps( blockProps, { + template: [ + [ + 'jetpack/label', + { + label: uploadLabel, + defaultLabel: uploadLabel, + lock: { move: true, remove: true }, + }, + ], + [ + 'jetpack/dropzone', + { + lock: { move: true, remove: true }, + layout: { type: 'flex', justifyContent: 'center', orientation: 'vertical' }, + }, + ], + ], + allowedBlocks: [ 'jetpack/dropzone', 'jetpack/label' ], + renderAppender: false, + } ); + + return ( + <> +
+ + + ); +} diff --git a/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-file/editor.css b/projects/packages/forms/src/blocks/field-file/editor.scss similarity index 100% rename from projects/packages/forms/src/blocks/contact-form/components/jetpack-field-file/editor.css rename to projects/packages/forms/src/blocks/field-file/editor.scss diff --git a/projects/packages/forms/src/blocks/field-file/index.js b/projects/packages/forms/src/blocks/field-file/index.js new file mode 100644 index 000000000000..848f7c04f21d --- /dev/null +++ b/projects/packages/forms/src/blocks/field-file/index.js @@ -0,0 +1,45 @@ +import { Icon } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { upload } from '@wordpress/icons'; +import { getIconColor } from '../contact-form/util/block-icons'; +import defaultSettings from '../shared/settings'; +import edit from './edit'; +import save from './save'; + +const name = 'field-file'; +const settings = { + ...defaultSettings, + title: __( 'File Upload Field', 'jetpack-forms' ), + keywords: [ + __( 'File', 'jetpack-forms' ), + __( 'Upload', 'jetpack-forms' ), + __( 'Attachment', 'jetpack-forms' ), + __( 'Dropzone', 'jetpack-forms' ), + ], + description: __( 'Allow visitors to upload files through your form.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: , + }, + edit, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'File Upload', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/dropzone', + }, + ], + }, + isBeta: true, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-file/save.js b/projects/packages/forms/src/blocks/field-file/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-file/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-multiple-choice/deprecated.js b/projects/packages/forms/src/blocks/field-multiple-choice/deprecated.js new file mode 100644 index 000000000000..cecfd79bdae6 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-multiple-choice/deprecated.js @@ -0,0 +1,72 @@ +import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; +import migrateInnerOptionBlocks from '../shared/deprecations/migrate-inner-option-blocks'; +import migrateOptionsToInnerBlocks from '../shared/deprecations/migrate-options-to-inner-blocks'; +import multiFieldV1 from '../shared/deprecations/multiple-choice-field-deprecation'; + +const v1 = multiFieldV1( 'checkbox' ); +const v2 = { + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { + type: 'string', + default: __( 'Choose several options', 'jetpack-forms' ), + }, + }, + supports: INNER_BLOCKS_DEPRECATION.supports, + migrate( attributes, innerBlocks ) { + /** + * When there are no inner blocks, the InnerBlocks.content in this version's + * save function is effectively the same as the original (v1) version's + * save function i.e. () => null. + * + * As isEligible is only used when the block's save function is valid, this + * deprecation will actually match before v1. The deprecation's will need + * to remain separate for the occasions where a v2 version of the block + * actually has inner blocks. + * + * Given this can match on both the v1 and v2 saved markup, it will need to + * handle migrating both scenarios. + */ + if ( ! innerBlocks?.length && attributes.options?.length ) { + return migrateOptionsToInnerBlocks( attributes, 'checkbox' ); + } + + return migrateInnerOptionBlocks( attributes, innerBlocks, 'checkbox' ); + }, + save() { + return ; + }, +}; +const v3 = { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { + type: 'string', + default: 'Choose several options', + }, + }, + isEligible( _attributes, innerBlocks ) { + if ( innerBlocks.length !== 2 ) { + return true; + } + + return innerBlocks.some( + block => block.name !== `jetpack/label` && block.name !== `jetpack/options` + ); + }, + migrate( attributes, innerBlocks ) { + return migrateInnerOptionBlocks( attributes, innerBlocks, 'checkbox' ); + }, + save() { + return ( +
+ +
+ ); + }, +}; + +export default [ v3, v2, v1 ]; diff --git a/projects/packages/forms/src/blocks/field-multiple-choice/edit.js b/projects/packages/forms/src/blocks/field-multiple-choice/edit.js new file mode 100644 index 000000000000..f6fcdc601535 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-multiple-choice/edit.js @@ -0,0 +1,59 @@ +import { + store as blockEditorStore, + useBlockProps, + useInnerBlocksProps, +} from '@wordpress/block-editor'; +import { useSelect } from '@wordpress/data'; +import { __ } from '@wordpress/i18n'; +import clsx from 'clsx'; +import JetpackFieldControls from '../shared/components/jetpack-field-controls'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function MultipleChoiceFieldEdit( props ) { + const { className, clientId, setAttributes, isSelected, attributes } = props; + const { required, id, width } = attributes; + + useFormWrapper( props ); + + const innerBlocks = useSelect( + select => select( blockEditorStore ).getBlock( clientId ).innerBlocks, + [ clientId ] + ); + const options = innerBlocks?.[ 1 ]?.innerBlocks; + const classes = clsx( className, 'jetpack-field jetpack-field-multiple', { + 'is-selected': isSelected, + 'has-placeholder': !! options?.length, + } ); + + const blockProps = useBlockProps( { className: classes } ); + + const innerBlockProps = useInnerBlocksProps( blockProps, { + template: [ + [ + 'jetpack/label', + { + label: __( 'Choose several options', 'jetpack-forms' ), + defaultLabel: __( 'Add label…', 'jetpack-forms' ), + }, + ], + [ 'jetpack/options', { type: 'checkbox' } ], + ], + templateLock: 'all', + } ); + + return ( + <> +
+ + + ); +} diff --git a/projects/packages/forms/src/blocks/field-multiple-choice/index.js b/projects/packages/forms/src/blocks/field-multiple-choice/index.js new file mode 100644 index 000000000000..3a17227f1814 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-multiple-choice/index.js @@ -0,0 +1,74 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-checkbox-multiple'; +const settings = { + ...defaultSettings, + title: __( 'Multiple Choice (Checkbox)', 'jetpack-forms' ), + keywords: [ __( 'Choose Multiple', 'jetpack-forms' ), __( 'Option', 'jetpack-forms' ) ], + description: __( + 'Offer users a list of choices, and allow them to select multiple options.', + 'jetpack-forms' + ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + edit, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Choose several options', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/options', + attributes: { + type: 'checkbox', + }, + innerBlocks: [ + { + name: 'jetpack/option', + attributes: { + label: __( 'First option', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( 'Second option', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( 'Third option', 'jetpack-forms' ), + }, + }, + ], + }, + ], + }, + allowedBlocks: [ 'jetpack/label', 'jetpack/field-options' ], + deprecated, + save, + styles: [ + { name: 'list', label: __( 'List', 'jetpack-forms' ), isDefault: true }, + { name: 'button', label: __( 'Button', 'jetpack-forms' ) }, + ], +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-multiple-choice/save.js b/projects/packages/forms/src/blocks/field-multiple-choice/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-multiple-choice/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-name/deprecated.js b/projects/packages/forms/src/blocks/field-name/deprecated.js new file mode 100644 index 000000000000..ada9beb02ced --- /dev/null +++ b/projects/packages/forms/src/blocks/field-name/deprecated.js @@ -0,0 +1,11 @@ +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { type: 'string', default: 'Name' }, + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-name/edit.js b/projects/packages/forms/src/blocks/field-name/edit.js new file mode 100644 index 000000000000..b50a01d7bded --- /dev/null +++ b/projects/packages/forms/src/blocks/field-name/edit.js @@ -0,0 +1,25 @@ +import { __ } from '@wordpress/i18n'; +import JetpackField from '../shared/components/jetpack-field'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function NameFieldEdit( props ) { + useFormWrapper( props ); + + return ( + + ); +} diff --git a/projects/packages/forms/src/blocks/field-name/index.js b/projects/packages/forms/src/blocks/field-name/index.js new file mode 100644 index 000000000000..5e281a4fc11b --- /dev/null +++ b/projects/packages/forms/src/blocks/field-name/index.js @@ -0,0 +1,45 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-name'; +const settings = { + ...defaultSettings, + title: __( 'Name Field', 'jetpack-forms' ), + description: __( 'Collect the site visitor’s name.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + edit, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Name', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'text', + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-name/save.js b/projects/packages/forms/src/blocks/field-name/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-name/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-number/deprecated.js b/projects/packages/forms/src/blocks/field-number/deprecated.js new file mode 100644 index 000000000000..7ef8886e5aae --- /dev/null +++ b/projects/packages/forms/src/blocks/field-number/deprecated.js @@ -0,0 +1,13 @@ +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { type: 'string', default: 'Number' }, + max: { type: 'string' }, + min: { type: 'string' }, + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-number/edit.js b/projects/packages/forms/src/blocks/field-number/edit.js new file mode 100644 index 000000000000..7f61778dd43b --- /dev/null +++ b/projects/packages/forms/src/blocks/field-number/edit.js @@ -0,0 +1,25 @@ +import { __ } from '@wordpress/i18n'; +import JetpackField from '../shared/components/jetpack-field'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function NumberFieldEdit( props ) { + useFormWrapper( props ); + + return ( + + ); +} diff --git a/projects/packages/forms/src/blocks/field-number/index.js b/projects/packages/forms/src/blocks/field-number/index.js new file mode 100644 index 000000000000..d17590da1256 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-number/index.js @@ -0,0 +1,45 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-number'; +const settings = { + ...defaultSettings, + title: __( 'Number Input Field', 'jetpack-forms' ), + description: __( 'Collect numbers from site visitors.', 'jetpack-forms' ), + icon: renderMaterialIcon( + + ), + edit, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Number', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'number', + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-number/save.js b/projects/packages/forms/src/blocks/field-number/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-number/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-select/deprecated.js b/projects/packages/forms/src/blocks/field-select/deprecated.js new file mode 100644 index 000000000000..7bf14eb96ae0 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-select/deprecated.js @@ -0,0 +1,45 @@ +import { createBlock } from '@wordpress/blocks'; +import { __ } from '@wordpress/i18n'; +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; +import deprecateFieldStyles from '../shared/util/deprecate-field-styles'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + toggleLabel: { + type: 'string', + default: null, + role: 'content', + }, + options: { + type: 'array', + default: [ '' ], + role: 'content', + }, + }, + migrate( attributes ) { + const { restAttributes, labelStyles, inputStyles } = deprecateFieldStyles( attributes ); + const { toggleLabel, ...newAttributes } = restAttributes; + + const labelBlock = createBlock( 'jetpack/label', { + label: attributes.label, + requiredText: attributes.requiredText, + style: labelStyles, + } ); + + const inputBlock = createBlock( 'jetpack/input', { + placeholder: toggleLabel ?? __( 'Select one option', 'jetpack-forms' ), + style: inputStyles, + type: 'dropdown', + } ); + + return [ newAttributes, [ labelBlock, inputBlock ] ]; + }, + isEligible( attributes, innerBlocks ) { + const hasToggleLabel = attributes.toggleLabel && attributes.toggleLabel !== ''; + return hasToggleLabel || ! innerBlocks.length; + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-dropdown.js b/projects/packages/forms/src/blocks/field-select/edit.js similarity index 54% rename from projects/packages/forms/src/blocks/contact-form/components/jetpack-field-dropdown.js rename to projects/packages/forms/src/blocks/field-select/edit.js index 74678750b3ee..ba5023140c5a 100644 --- a/projects/packages/forms/src/blocks/contact-form/components/jetpack-field-dropdown.js +++ b/projects/packages/forms/src/blocks/field-select/edit.js @@ -1,32 +1,71 @@ -import { RichText, useBlockProps } from '@wordpress/block-editor'; -import { compose } from '@wordpress/compose'; -import { useEffect, useRef } from '@wordpress/element'; +import { + RichText, + store as blockEditorStore, + useBlockProps, + useInnerBlocksProps, +} from '@wordpress/block-editor'; +import { useSelect } from '@wordpress/data'; +import { useMemo, useRef } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import clsx from 'clsx'; -import { isEmpty, isNil, noop, split, trim } from 'lodash'; -import { getCaretPosition } from '../util/caret'; -import { setFocus } from '../util/focus'; -import { useFormStyle, useFormWrapper } from '../util/form'; -import { withSharedFieldAttributes } from '../util/with-shared-field-attributes'; -import JetpackFieldControls from './jetpack-field-controls'; -import JetpackFieldLabel from './jetpack-field-label'; -import { useJetpackFieldStyles } from './use-jetpack-field-styles'; - -const JetpackDropdown = ( { attributes, clientId, isSelected, name, setAttributes } ) => { - const { id, label, options, required, requiredText, toggleLabel, width } = attributes; - const optionsWrapper = useRef( undefined ); - const formStyle = useFormStyle( clientId ); +import JetpackFieldControls from '../shared/components/jetpack-field-controls'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; +import useJetpackFieldStyles from '../shared/hooks/use-jetpack-field-styles'; +import { getCaretPosition } from '../shared/util/caret'; +import { ALLOWED_INNER_BLOCKS } from '../shared/util/constants'; +import setFocus from '../shared/util/set-focus'; + +const noop = () => undefined; + +export default function DropdownFieldEdit( props ) { + const { attributes, clientId, isSelected, name, setAttributes } = props; + const { id, options, required, width } = attributes; const { blockStyle } = useJetpackFieldStyles( attributes ); + const { isInnerBlockSelected, inputBlockAttributes } = useSelect( + select => { + const { getBlock, hasSelectedInnerBlock } = select( blockEditorStore ); + return { + isInnerBlockSelected: hasSelectedInnerBlock( clientId, true ), + inputBlockAttributes: getBlock( clientId ).innerBlocks[ 1 ]?.attributes, + }; + }, + [ clientId ] + ); + const blockProps = useBlockProps( { className: clsx( 'jetpack-field jetpack-field-dropdown', { - 'is-selected': isSelected, - 'has-placeholder': ! isEmpty( toggleLabel ), + 'is-selected': isSelected || isInnerBlockSelected, + 'has-placeholder': !! inputBlockAttributes?.placeholder, } ), style: blockStyle, } ); + const optionsWrapper = useRef( undefined ); useFormWrapper( { attributes, clientId, name } ); + const template = useMemo( () => { + return [ + [ 'jetpack/label', { required } ], + [ + 'jetpack/input', + { type: 'dropdown', placeholder: __( 'Select one option', 'jetpack-forms' ) }, + ], + ]; + }, [ required ] ); + + const innerBlocksProps = useInnerBlocksProps( + { className: 'jetpack-field-dropdown__wrapper' }, + { + allowedBlocks: ALLOWED_INNER_BLOCKS, + template, + templateLock: 'all', + } + ); + + const optionWrapperStyles = { + className: 'jetpack-field-dropdown__popover', + }; + const changeFocus = ( index, cursorToEnd ) => setFocus( optionsWrapper.current, '[role=textbox]', index, cursorToEnd ); @@ -55,7 +94,7 @@ const JetpackDropdown = ( { attributes, clientId, isSelected, name, setAttribute }; const handleChangeOption = index => value => { - const values = split( value, '\n' ).filter( op => op && trim( op ) !== '' ); + const values = ( value || '' ).split( '\n' ).filter( op => op && op.trim() !== '' ); if ( ! values.length ) { return; @@ -104,44 +143,11 @@ const JetpackDropdown = ( { attributes, clientId, isSelected, name, setAttribute changeFocus( Math.max( index - 1, 0 ), true ); }; - useEffect( () => { - if ( isNil( label ) ) { - setAttributes( { label: '' } ); - } - - if ( isNil( toggleLabel ) ) { - setAttributes( { toggleLabel: __( 'Select one option', 'jetpack-forms' ) } ); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [] ); - return (
-
- -
- { - setAttributes( { toggleLabel: value } ); - } } - allowedFormats={ [ 'core/bold', 'core/italic' ] } - withoutInteractiveFormatting - /> - -
-
- - { isSelected && ( -
+
+ { ( isSelected || isInnerBlockSelected ) && ( +
{ options.map( ( option, index ) => (
); -}; - -export default compose( - withSharedFieldAttributes( [ - 'borderRadius', - 'borderWidth', - 'labelFontSize', - 'fieldFontSize', - 'lineHeight', - 'labelLineHeight', - 'inputColor', - 'labelColor', - 'fieldBackgroundColor', - 'borderColor', - ] ) -)( JetpackDropdown ); +} diff --git a/projects/packages/forms/src/blocks/field-select/index.js b/projects/packages/forms/src/blocks/field-select/index.js new file mode 100644 index 000000000000..a0c9673a681b --- /dev/null +++ b/projects/packages/forms/src/blocks/field-select/index.js @@ -0,0 +1,69 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-select'; +const settings = { + ...defaultSettings, + title: __( 'Dropdown Field', 'jetpack-forms' ), + keywords: [ + __( 'Choose', 'jetpack-forms' ), + __( 'Dropdown', 'jetpack-forms' ), + __( 'Option', 'jetpack-forms' ), + ], + description: __( + 'Add a compact select box, that when expanded, allows visitors to choose one value from the list.', + 'jetpack-forms' + ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + edit, + attributes: { + ...defaultSettings.attributes, + options: { + type: 'array', + default: [ '' ], + role: 'content', + }, + }, + deprecated, + save, + example: { + attributes: { + options: [ + __( 'First option', 'jetpack-forms' ), + __( 'Second option', 'jetpack-forms' ), + __( 'Third option', 'jetpack-forms' ), + ], + }, + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Dropdown', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { type: 'dropdown', placeholder: __( 'Select one option', 'jetpack-forms' ) }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-select/save.js b/projects/packages/forms/src/blocks/field-select/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-select/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-single-choice/deprecated.js b/projects/packages/forms/src/blocks/field-single-choice/deprecated.js new file mode 100644 index 000000000000..98d9ad75e5f1 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-single-choice/deprecated.js @@ -0,0 +1,72 @@ +import { InnerBlocks, useBlockProps } from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; +import migrateInnerOptionBlocks from '../shared/deprecations/migrate-inner-option-blocks'; +import migrateOptionsToInnerBlocks from '../shared/deprecations/migrate-options-to-inner-blocks'; +import multiFieldV1 from '../shared/deprecations/multiple-choice-field-deprecation'; + +const v1 = multiFieldV1( 'radio' ); +const v2 = { + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { + type: 'string', + default: __( 'Choose one option', 'jetpack-forms' ), + }, + }, + supports: INNER_BLOCKS_DEPRECATION.supports, + migrate( attributes, innerBlocks ) { + /** + * When there are no inner blocks, the InnerBlocks.content in this version's + * save function is effectively the same as the original (v1) version's + * save function i.e. () => null. + * + * As isEligible is only used when the block's save function is valid, this + * deprecation will actually match before v1. The deprecation's will need + * to remain separate for the occasions where a v2 version of the block + * actually has inner blocks. + * + * Given this can match on both the v1 and v2 saved markup, it will need to + * handle migrating both scenarios. + */ + if ( ! innerBlocks?.length && attributes.options?.length ) { + return migrateOptionsToInnerBlocks( attributes, 'checkbox' ); + } + + return migrateInnerOptionBlocks( attributes, innerBlocks, 'radio' ); + }, + save() { + return ; + }, +}; +const v3 = { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { + type: 'string', + default: 'Choose one option', + }, + }, + isEligible( _attributes, innerBlocks ) { + if ( innerBlocks.length !== 2 ) { + return true; + } + + return innerBlocks.some( + block => block.name !== `jetpack/label` && block.name !== `jetpack/options` + ); + }, + migrate( attributes, innerBlocks ) { + return migrateInnerOptionBlocks( attributes, innerBlocks, 'radio' ); + }, + save() { + return ( +
+ +
+ ); + }, +}; + +export default [ v3, v2, v1 ]; diff --git a/projects/packages/forms/src/blocks/field-single-choice/edit.js b/projects/packages/forms/src/blocks/field-single-choice/edit.js new file mode 100644 index 000000000000..4817f99b630c --- /dev/null +++ b/projects/packages/forms/src/blocks/field-single-choice/edit.js @@ -0,0 +1,59 @@ +import { + store as blockEditorStore, + useBlockProps, + useInnerBlocksProps, +} from '@wordpress/block-editor'; +import { useSelect } from '@wordpress/data'; +import { __ } from '@wordpress/i18n'; +import clsx from 'clsx'; +import JetpackFieldControls from '../shared/components/jetpack-field-controls'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function SingleChoiceFieldEdit( props ) { + const { className, clientId, setAttributes, isSelected, attributes } = props; + const { required, id, width } = attributes; + + useFormWrapper( props ); + + const innerBlocks = useSelect( + select => select( blockEditorStore ).getBlock( clientId ).innerBlocks, + [ clientId ] + ); + const options = innerBlocks?.[ 1 ]?.innerBlocks; + const classes = clsx( className, 'jetpack-field jetpack-field-multiple', { + 'is-selected': isSelected, + 'has-placeholder': !! options?.length, + } ); + + const blockProps = useBlockProps( { className: classes } ); + + const innerBlockProps = useInnerBlocksProps( blockProps, { + template: [ + [ + 'jetpack/label', + { + label: __( 'Choose one option', 'jetpack-forms' ), + defaultLabel: __( 'Add label…', 'jetpack-forms' ), + }, + ], + [ 'jetpack/options', { type: 'radio' } ], + ], + templateLock: 'all', + } ); + + return ( + <> +
+ + + ); +} diff --git a/projects/packages/forms/src/blocks/field-single-choice/index.js b/projects/packages/forms/src/blocks/field-single-choice/index.js new file mode 100644 index 000000000000..69869fd53214 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-single-choice/index.js @@ -0,0 +1,72 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-radio'; +const settings = { + ...defaultSettings, + title: __( 'Single Choice (Radio)', 'jetpack-forms' ), + keywords: [ + __( 'Choose', 'jetpack-forms' ), + __( 'Select', 'jetpack-forms' ), + __( 'Option', 'jetpack-forms' ), + ], + description: __( + 'Offer users a list of choices, and allow them to select a single option.', + 'jetpack-forms' + ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + edit, + allowedBlocks: [ 'jetpack/label', 'jetpack/field-options' ], + deprecated, + save, + styles: [ + { name: 'list', label: __( 'List', 'jetpack-forms' ), isDefault: true }, + { name: 'button', label: __( 'Button', 'jetpack-forms' ) }, + ], + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Choose one option', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/options', + attributes: { + type: 'radio', + }, + innerBlocks: [ + { + name: 'jetpack/option', + attributes: { + label: __( 'First option', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/option', + attributes: { + label: __( 'Second option', 'jetpack-forms' ), + }, + }, + ], + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-single-choice/save.js b/projects/packages/forms/src/blocks/field-single-choice/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-single-choice/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-telephone/deprecated.js b/projects/packages/forms/src/blocks/field-telephone/deprecated.js new file mode 100644 index 000000000000..acec2be1ce7a --- /dev/null +++ b/projects/packages/forms/src/blocks/field-telephone/deprecated.js @@ -0,0 +1,11 @@ +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { type: 'string', default: 'Phone' }, + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-telephone/edit.js b/projects/packages/forms/src/blocks/field-telephone/edit.js new file mode 100644 index 000000000000..c5ca899d2284 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-telephone/edit.js @@ -0,0 +1,25 @@ +import { __ } from '@wordpress/i18n'; +import JetpackField from '../shared/components/jetpack-field'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function TelephoneFieldEdit( props ) { + useFormWrapper( props ); + + return ( + + ); +} diff --git a/projects/packages/forms/src/blocks/field-telephone/index.js b/projects/packages/forms/src/blocks/field-telephone/index.js new file mode 100644 index 000000000000..c670483aeb49 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-telephone/index.js @@ -0,0 +1,48 @@ +import { Icon } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { mobile } from '@wordpress/icons'; +import { getIconColor } from '../contact-form/util/block-icons'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-telephone'; +const settings = { + ...defaultSettings, + title: __( 'Phone Number Field', 'jetpack-forms' ), + keywords: [ + __( 'Phone', 'jetpack-forms' ), + __( 'Cellular phone', 'jetpack-forms' ), + __( 'Mobile', 'jetpack-forms' ), + ], + description: __( 'Collect phone numbers from site visitors.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: , + }, + edit, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Phone', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'tel', + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-telephone/save.js b/projects/packages/forms/src/blocks/field-telephone/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-telephone/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-text/deprecated.js b/projects/packages/forms/src/blocks/field-text/deprecated.js new file mode 100644 index 000000000000..ca52be41e48e --- /dev/null +++ b/projects/packages/forms/src/blocks/field-text/deprecated.js @@ -0,0 +1,11 @@ +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { type: 'string', default: 'Text' }, + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-text/edit.js b/projects/packages/forms/src/blocks/field-text/edit.js new file mode 100644 index 000000000000..569c9bbd36f9 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-text/edit.js @@ -0,0 +1,25 @@ +import { __ } from '@wordpress/i18n'; +import JetpackField from '../shared/components/jetpack-field'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function TextFieldEdit( props ) { + useFormWrapper( props ); + + return ( + + ); +} diff --git a/projects/packages/forms/src/blocks/field-text/index.js b/projects/packages/forms/src/blocks/field-text/index.js new file mode 100644 index 000000000000..106925f0e190 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-text/index.js @@ -0,0 +1,45 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-text'; +const settings = { + ...defaultSettings, + title: __( 'Text Input Field', 'jetpack-forms' ), + description: __( 'Collect short text responses from site visitors.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + edit, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Text', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'text', + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-text/save.js b/projects/packages/forms/src/blocks/field-text/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-text/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-textarea/deprecated.js b/projects/packages/forms/src/blocks/field-textarea/deprecated.js new file mode 100644 index 000000000000..368e0965ce54 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-textarea/deprecated.js @@ -0,0 +1,28 @@ +import { createBlock } from '@wordpress/blocks'; +import { __ } from '@wordpress/i18n'; +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; +import deprecateFieldStyles from '../shared/util/deprecate-field-styles'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + migrate( attributes ) { + const { restAttributes, labelStyles, inputStyles } = deprecateFieldStyles( attributes ); + const newInnerBlocks = [ + createBlock( 'jetpack/label', { + label: attributes.label, + defaultLabel: __( 'Message', 'jetpack-forms' ), + requiredText: attributes.requiredText, + style: labelStyles, + } ), + createBlock( 'jetpack/input', { + placeholder: attributes.placeholder, + style: inputStyles, + type: 'textarea', + } ), + ]; + + return [ restAttributes, newInnerBlocks ]; + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-textarea/edit.js b/projects/packages/forms/src/blocks/field-textarea/edit.js new file mode 100644 index 000000000000..6d79d865f8ad --- /dev/null +++ b/projects/packages/forms/src/blocks/field-textarea/edit.js @@ -0,0 +1,54 @@ +import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor'; +import { useMemo } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import clsx from 'clsx'; +import JetpackFieldControls from '../shared/components/jetpack-field-controls'; +import useFieldSelected from '../shared/hooks/use-field-selected'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; +import useJetpackFieldStyles from '../shared/hooks/use-jetpack-field-styles'; +import { ALLOWED_INNER_BLOCKS } from '../shared/util/constants'; + +export default function TextareaFieldEdit( props ) { + const { attributes, clientId, id, isSelected, label, requiredText, setAttributes } = props; + const { required, width } = attributes; + + useFormWrapper( props ); + const { blockStyle } = useJetpackFieldStyles( attributes ); + const { isInnerBlockSelected, hasPlaceholder } = useFieldSelected( clientId ); + const blockProps = useBlockProps( { + className: clsx( 'jetpack-field jetpack-field-textarea', { + 'is-selected': isSelected || isInnerBlockSelected, + 'has-placeholder': hasPlaceholder, + } ), + style: blockStyle, + } ); + const defaultLabel = __( 'Message', 'jetpack-forms' ); + + const templateLabel = label ?? ''; + const template = useMemo( () => { + return [ + [ 'jetpack/label', { label: templateLabel, defaultLabel, requiredText } ], + [ 'jetpack/input', { type: 'textarea' } ], + ]; + }, [ templateLabel, defaultLabel, requiredText ] ); + + const innerBlocksProps = useInnerBlocksProps( blockProps, { + allowedBlocks: ALLOWED_INNER_BLOCKS, + template, + templateLock: 'all', + } ); + + return ( + <> +
+ + + ); +} diff --git a/projects/packages/forms/src/blocks/field-textarea/index.js b/projects/packages/forms/src/blocks/field-textarea/index.js new file mode 100644 index 000000000000..bfdab03c0f19 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-textarea/index.js @@ -0,0 +1,50 @@ +import { Path } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { getIconColor } from '../contact-form/util/block-icons'; +import renderMaterialIcon from '../contact-form/util/render-material-icon'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-textarea'; +const settings = { + ...defaultSettings, + title: __( 'Multi-line Text Field', 'jetpack-forms' ), + keywords: [ + __( 'Textarea', 'jetpack-forms' ), + 'textarea', + __( 'Multiline text', 'jetpack-forms' ), + ], + description: __( 'Capture longform text responses from site visitors.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: renderMaterialIcon( + + ), + }, + edit, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Message', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'textarea', + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-textarea/save.js b/projects/packages/forms/src/blocks/field-textarea/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-textarea/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/field-url/deprecated.js b/projects/packages/forms/src/blocks/field-url/deprecated.js new file mode 100644 index 000000000000..de7a26a85c52 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-url/deprecated.js @@ -0,0 +1,11 @@ +import INNER_BLOCKS_DEPRECATION from '../shared/deprecations/inner-blocks-deprecation'; + +export default [ + { + ...INNER_BLOCKS_DEPRECATION, + attributes: { + ...INNER_BLOCKS_DEPRECATION.attributes, + label: { type: 'string', default: 'Website' }, + }, + }, +]; diff --git a/projects/packages/forms/src/blocks/field-url/edit.js b/projects/packages/forms/src/blocks/field-url/edit.js new file mode 100644 index 000000000000..0539d90b0bb8 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-url/edit.js @@ -0,0 +1,25 @@ +import { __ } from '@wordpress/i18n'; +import JetpackField from '../shared/components/jetpack-field'; +import useFormWrapper from '../shared/hooks/use-form-wrapper'; + +export default function UrlFieldEdit( props ) { + useFormWrapper( props ); + + return ( + + ); +} diff --git a/projects/packages/forms/src/blocks/field-url/index.js b/projects/packages/forms/src/blocks/field-url/index.js new file mode 100644 index 000000000000..31323764117b --- /dev/null +++ b/projects/packages/forms/src/blocks/field-url/index.js @@ -0,0 +1,49 @@ +import { Icon } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { globe } from '@wordpress/icons'; +import { getIconColor } from '../contact-form/util/block-icons'; +import defaultSettings from '../shared/settings'; +import deprecated from './deprecated'; +import edit from './edit'; +import save from './save'; + +const name = 'field-url'; +const settings = { + ...defaultSettings, + title: __( 'Website Field', 'jetpack-forms' ), + keywords: [ + __( 'url', 'jetpack-forms' ), + __( 'internet page', 'jetpack-forms' ), + __( 'link', 'jetpack-forms' ), + __( 'website', 'jetpack-forms' ), + ], + description: __( 'Collect a website address from your site visitors.', 'jetpack-forms' ), + icon: { + foreground: getIconColor(), + src: , + }, + edit, + deprecated, + save, + example: { + innerBlocks: [ + { + name: 'jetpack/label', + attributes: { + label: __( 'Website', 'jetpack-forms' ), + }, + }, + { + name: 'jetpack/input', + attributes: { + type: 'url', + }, + }, + ], + }, +}; + +export default { + name, + settings, +}; diff --git a/projects/packages/forms/src/blocks/field-url/save.js b/projects/packages/forms/src/blocks/field-url/save.js new file mode 100644 index 000000000000..5cd085a8c7b2 --- /dev/null +++ b/projects/packages/forms/src/blocks/field-url/save.js @@ -0,0 +1,6 @@ +import { useInnerBlocksProps } from '@wordpress/block-editor'; + +export default () => { + const innerBlocksProps = useInnerBlocksProps.save(); + return
; +}; diff --git a/projects/packages/forms/src/blocks/input/edit.js b/projects/packages/forms/src/blocks/input/edit.js new file mode 100644 index 000000000000..c15df9901b4e --- /dev/null +++ b/projects/packages/forms/src/blocks/input/edit.js @@ -0,0 +1,119 @@ +import { InspectorControls, RichText, useBlockProps } from '@wordpress/block-editor'; +import { PanelBody, __experimentalNumberControl as NumberControl } from '@wordpress/components'; // eslint-disable-line @wordpress/no-unsafe-wp-apis +import { useCallback } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { clsx } from 'clsx'; +import useInsertAfterOnEnterKeyDown from '../shared/hooks/use-insert-after-on-enter-key-down'; +import { useSyncedAttributes } from '../shared/hooks/use-synced-attributes'; +import useVariationStyleProperties from '../shared/hooks/use-variation-style-properties.js'; +import { ALLOWED_FORMATS } from '../shared/util/constants.js'; + +const SYNCED_ATTRIBUTE_KEYS = [ + 'backgroundColor', + 'borderColor', + 'fontFamily', + 'fontSize', + 'style', + 'textColor', +]; + +const TEXT_FIELDS = [ 'number', 'text', 'email', 'url', 'tel' ]; + +const getInputClass = type => { + if ( type === 'dropdown' ) { + return 'jetpack-field-dropdown__toggle'; + } + if ( type && ! TEXT_FIELDS.includes( type ) ) { + return `jetpack-field__${ type }`; + } + return 'jetpack-field__input'; +}; + +const InputEdit = ( { attributes, clientId, isSelected, name, setAttributes, context } ) => { + const { 'jetpack/field-share-attributes': isSynced } = context; + useSyncedAttributes( name, isSynced, SYNCED_ATTRIBUTE_KEYS, attributes, setAttributes ); + const { max, min, placeholder, type } = attributes; + const variationProps = useVariationStyleProperties( { + clientId, + inputBlockName: name, + inputBlockAttributes: attributes, + } ); + const className = clsx( getInputClass( attributes.type ), { + inline: type === 'checkbox' || type === 'radio', + } ); + const blockProps = useBlockProps( { className, style: variationProps?.cssVars } ); + const onKeyDown = useInsertAfterOnEnterKeyDown( clientId ); + + const onChange = useCallback( + event => { + setAttributes( { placeholder: event.target.value } ); + }, + [ setAttributes ] + ); + + if ( type === 'dropdown' ) { + return ( +
+ setAttributes( { placeholder: value } ) } + value={ placeholder ? placeholder : __( 'Select one option', 'jetpack-forms' ) } + withoutInteractiveFormatting + /> + +
+ ); + } + + if ( type === 'textarea' ) { + return