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
;
+ }
+
+ return (
+ <>
+
+ { type === 'number' && (
+
+
+ setAttributes( { min: value } ) }
+ max={ max }
+ __nextHasNoMarginBottom={ true }
+ __next40pxDefaultSize={ true }
+ help={ __(
+ 'The minimum value to accept in the input. Leaving empty allows any negative and positive values.',
+ 'jetpack-forms'
+ ) }
+ />
+
+ setAttributes( {
+ max: value,
+ } )
+ }
+ min={ min }
+ __nextHasNoMarginBottom={ true }
+ __next40pxDefaultSize={ true }
+ help={ __( 'The maximum value to accept in the input.', 'jetpack-forms' ) }
+ />
+
+
+ ) }
+ >
+ );
+};
+
+export default InputEdit;
diff --git a/projects/packages/forms/src/blocks/input/index.js b/projects/packages/forms/src/blocks/input/index.js
new file mode 100644
index 000000000000..f01dc9fffbe4
--- /dev/null
+++ b/projects/packages/forms/src/blocks/input/index.js
@@ -0,0 +1,79 @@
+import { Path } from '@wordpress/components';
+import { __ } from '@wordpress/i18n';
+import renderMaterialIcon from '../contact-form/util/render-material-icon';
+import edit from './edit';
+import save from './save';
+
+const name = 'input';
+const settings = {
+ apiVersion: 3,
+ title: __( 'Input', 'jetpack-forms' ),
+ description: __( 'An input for a form field', 'jetpack-forms' ),
+ category: 'contact-form',
+ icon: {
+ src: renderMaterialIcon(
+
+ ),
+ },
+ parent: [
+ 'jetpack/field-date',
+ 'jetpack/field-email',
+ 'jetpack/field-name',
+ 'jetpack/field-number',
+ 'jetpack/field-select',
+ 'jetpack/field-telephone',
+ 'jetpack/field-text',
+ 'jetpack/field-textarea',
+ ],
+ usesContext: [ 'jetpack/field-share-attributes' ],
+ supports: {
+ reusable: false,
+ html: false,
+ color: {
+ text: true,
+ background: true,
+ gradients: false,
+ },
+ __experimentalBorder: {
+ color: true,
+ radius: true,
+ style: true,
+ width: true,
+ __experimentalDefaultControls: {
+ color: true,
+ radius: true,
+ style: true,
+ width: true,
+ },
+ },
+ typography: {
+ fontSize: true,
+ lineHeight: true,
+ __experimentalFontFamily: true,
+ __experimentalFontWeight: true,
+ __experimentalFontStyle: true,
+ __experimentalTextTransform: true,
+ __experimentalTextDecoration: true,
+ __experimentalLetterSpacing: true,
+ __experimentalDefaultControls: {
+ fontSize: true,
+ },
+ },
+ },
+ attributes: {
+ placeholder: {
+ type: 'string',
+ default: '',
+ },
+ type: { type: 'string' },
+ min: { type: 'number' },
+ max: { type: 'number' },
+ },
+ edit,
+ save,
+};
+
+export default {
+ name,
+ settings,
+};
diff --git a/projects/packages/forms/src/blocks/input/save.js b/projects/packages/forms/src/blocks/input/save.js
new file mode 100644
index 000000000000..461f67a0a4bc
--- /dev/null
+++ b/projects/packages/forms/src/blocks/input/save.js
@@ -0,0 +1 @@
+export default () => null;
diff --git a/projects/packages/forms/src/blocks/label/edit.js b/projects/packages/forms/src/blocks/label/edit.js
new file mode 100644
index 000000000000..7bbf2a20ed58
--- /dev/null
+++ b/projects/packages/forms/src/blocks/label/edit.js
@@ -0,0 +1,150 @@
+import { RichText, store as blockEditorStore, useBlockProps } from '@wordpress/block-editor';
+import { useSelect } from '@wordpress/data';
+import { __ } from '@wordpress/i18n';
+import { clsx } from 'clsx';
+import { useSyncedAttributes } from '../shared/hooks/use-synced-attributes';
+import useVariationStyleProperties from '../shared/hooks/use-variation-style-properties.js';
+import { ALLOWED_FORMATS, DATE_FORMATS, FORM_STYLE } from '../shared/util/constants.js';
+import getBlockStyle from '../shared/util/get-block-style.js';
+
+const SYNCED_ATTRIBUTE_KEYS = [ 'textColor', 'fontFamily', 'fontSize', 'style' ];
+
+const emptyToNull = str => ( str === '' ? null : str );
+const getLabelOrFallback = ( label, placeholder ) => {
+ if ( label === '' ) {
+ return placeholder;
+ }
+
+ return label ?? placeholder;
+};
+
+const OPTIONS_FIELDS = [ 'jetpack/field-radio', 'jetpack/field-checkbox-multiple' ];
+
+function useSiblingBlock( clientId ) {
+ const inputBlock = useSelect(
+ select => {
+ const { getBlock, getBlockRootClientId } = select( blockEditorStore );
+
+ // Get the parent block's clientId.
+ const parentClientId = getBlockRootClientId( clientId );
+ if ( ! parentClientId ) {
+ return {};
+ }
+ // Get the parent block
+ const parentBlock = getBlock( parentClientId );
+ if ( ! parentBlock ) {
+ return {};
+ }
+
+ const siblingBlockType = OPTIONS_FIELDS.includes( parentBlock.name )
+ ? 'jetpack/options'
+ : 'jetpack/input';
+
+ return parentBlock.innerBlocks.find( block => block.name === siblingBlockType );
+ },
+ [ clientId ]
+ );
+
+ return inputBlock;
+}
+
+const WithNotchedWrapper = ( {
+ formStyle,
+ styles,
+ cssVars,
+ className,
+ children,
+ forcePlainStyle = false,
+} ) => {
+ if ( formStyle === FORM_STYLE.OUTLINED && ! forcePlainStyle ) {
+ return (
+
+
+
+ { children }
+
+
+
+
+ );
+ }
+
+ return <>{ children }>;
+};
+
+const LabelEdit = ( { clientId, attributes, name, setAttributes, context } ) => {
+ const {
+ 'jetpack/form-className': formClassName,
+ 'jetpack/field-required': required,
+ 'jetpack/field-dateFormat': dateFormat,
+ 'jetpack/field-share-attributes': isSynced,
+ } = context;
+ useSyncedAttributes( name, isSynced, SYNCED_ATTRIBUTE_KEYS, attributes, setAttributes );
+
+ const { label, defaultLabel, requiredText } = attributes;
+ const defaultPlaceholder = __( 'Add label…', 'jetpack-forms' );
+ const placeholder = emptyToNull( defaultLabel ) ?? emptyToNull( label ) ?? defaultPlaceholder;
+ const suffix = dateFormat
+ ? `(${ DATE_FORMATS.find( f => f.value === dateFormat )?.label })`
+ : undefined;
+ const formStyle = getBlockStyle( formClassName );
+ const className = clsx( 'jetpack-field-label', {
+ 'notched-label__label': formStyle === FORM_STYLE.OUTLINED,
+ 'animated-label__label': formStyle === FORM_STYLE.ANIMATED,
+ 'below-label__label': formStyle === FORM_STYLE.BELOW,
+ } );
+
+ const inputBlock = useSiblingBlock( clientId );
+
+ const variationProps = useVariationStyleProperties( {
+ clientId,
+ inputBlockName: inputBlock?.name,
+ inputBlockAttributes: inputBlock?.attributes,
+ } );
+ const blockProps = useBlockProps( {
+ className,
+ style: variationProps?.cssVars,
+ } );
+
+ // The label value to use for the RichText field must manually fall back to the
+ // placeholder to be rendered in previews.
+ const isPreviewMode = useSelect( select => {
+ return select( blockEditorStore ).getSettings().isPreviewMode;
+ }, [] );
+ const labelValue = isPreviewMode ? getLabelOrFallback( label, defaultPlaceholder ) : label;
+
+ return (
+
+
+ setAttributes( { label: value } ) }
+ placeholder={ placeholder }
+ tagName="label"
+ value={ labelValue }
+ withoutInteractiveFormatting
+ />
+ { suffix && { suffix } }
+ { required && (
+ setAttributes( { requiredText: value } ) }
+ tagName="span"
+ value={ requiredText || __( '(required)', 'jetpack-forms' ) }
+ withoutInteractiveFormatting
+ />
+ ) }
+
+
+ );
+};
+
+export default LabelEdit;
diff --git a/projects/packages/forms/src/blocks/label/index.js b/projects/packages/forms/src/blocks/label/index.js
new file mode 100644
index 000000000000..d897c8623dd8
--- /dev/null
+++ b/projects/packages/forms/src/blocks/label/index.js
@@ -0,0 +1,79 @@
+import { Path } from '@wordpress/components';
+import { __ } from '@wordpress/i18n';
+import renderMaterialIcon from '../contact-form/util/render-material-icon';
+import edit from './edit';
+import save from './save';
+
+const name = 'label';
+const settings = {
+ apiVersion: 3,
+ title: __( 'Label', 'jetpack-forms' ),
+ description: __( 'A label for a form field', 'jetpack-forms' ),
+ category: 'contact-form',
+ icon: {
+ src: renderMaterialIcon(
+
+ ),
+ },
+ parent: [
+ 'jetpack/field-date',
+ 'jetpack/field-email',
+ 'jetpack/field-multiple-choice',
+ 'jetpack/field-name',
+ 'jetpack/field-number',
+ 'jetpack/field-select',
+ 'jetpack/field-single-choice',
+ 'jetpack/field-telephone',
+ 'jetpack/field-text',
+ 'jetpack/field-textarea',
+ ],
+ supports: {
+ reusable: false,
+ html: false,
+ color: {
+ text: true,
+ background: false,
+ gradients: false,
+ },
+ typography: {
+ fontSize: true,
+ lineHeight: true,
+ __experimentalFontFamily: true,
+ __experimentalFontWeight: true,
+ __experimentalFontStyle: true,
+ __experimentalTextTransform: true,
+ __experimentalTextDecoration: true,
+ __experimentalLetterSpacing: true,
+ __experimentalDefaultControls: {
+ fontSize: true,
+ },
+ },
+ },
+ attributes: {
+ label: {
+ type: 'string',
+ default: '',
+ },
+ defaultLabel: {
+ type: 'string',
+ default: '',
+ },
+ requiredText: {
+ type: 'string',
+ default: '',
+ },
+ },
+ usesContext: [
+ 'jetpack/form-className',
+ 'jetpack/field-required',
+ 'jetpack/field-dateFormat',
+ 'jetpack/field-share-attributes',
+ ],
+ edit,
+ save,
+};
+
+export default {
+ name,
+ settings,
+};
diff --git a/projects/packages/forms/src/blocks/label/save.js b/projects/packages/forms/src/blocks/label/save.js
new file mode 100644
index 000000000000..461f67a0a4bc
--- /dev/null
+++ b/projects/packages/forms/src/blocks/label/save.js
@@ -0,0 +1 @@
+export default () => null;
diff --git a/projects/packages/forms/src/blocks/option/edit.js b/projects/packages/forms/src/blocks/option/edit.js
new file mode 100644
index 000000000000..4e29aca21341
--- /dev/null
+++ b/projects/packages/forms/src/blocks/option/edit.js
@@ -0,0 +1,121 @@
+import { RichText, store as blockEditorStore, useBlockProps } from '@wordpress/block-editor';
+import { useDispatch, useSelect } from '@wordpress/data';
+import { __ } from '@wordpress/i18n';
+import { useSyncedAttributes } from '../shared/hooks/use-synced-attributes';
+import { ALLOWED_FORMATS } from '../shared/util/constants';
+import useEnter from './use-enter';
+
+const SYNCED_ATTRIBUTE_KEYS = [ 'textColor', 'fontFamily', 'fontSize', 'style' ];
+
+const getLabelOrFallback = ( label, placeholder ) => {
+ if ( label === '' ) {
+ return placeholder;
+ }
+
+ return label ?? placeholder;
+};
+
+const OptionEdit = ( { attributes, clientId, context, name, setAttributes } ) => {
+ const {
+ 'jetpack/field-defaultValue': defaultValue,
+ 'jetpack/field-options-type': type = 'checkbox',
+ 'jetpack/field-required': required,
+ 'jetpack/field-share-attributes': isSynced,
+ } = context;
+ const { hideInput, label, isStandalone, requiredText } = attributes;
+
+ useSyncedAttributes( name, isSynced, SYNCED_ATTRIBUTE_KEYS, attributes, setAttributes );
+
+ const { removeBlock } = useDispatch( blockEditorStore );
+ const siblingsCount = useSelect(
+ select => {
+ const { getBlockCount, getBlockRootClientId } = select( blockEditorStore );
+ return getBlockCount( getBlockRootClientId( clientId ) );
+ },
+ [ clientId ]
+ );
+
+ const onRemove = () => {
+ if ( siblingsCount <= 1 ) {
+ return;
+ }
+
+ removeBlock( clientId );
+ };
+
+ const blockProps = useBlockProps( {
+ className: `jetpack-field-option field-option-${ type }`,
+ } );
+
+ const useEnterRef = useEnter( { content: label, clientId, isStandalone } );
+ const useEnterRequiredRef = useEnter( { content: label, clientId, isStandalone } );
+
+ const isPreviewMode = useSelect( select => {
+ return select( blockEditorStore ).getSettings().isPreviewMode;
+ }, [] );
+ const placeholder = __( 'Add label…', 'jetpack-forms' );
+ // The label value to use for the RichText field must manually fall back to the
+ // placeholder to be rendered in previews.
+ const labelValue = isPreviewMode ? getLabelOrFallback( label, placeholder ) : label;
+
+ // Some fields such as Checkbox or Consent, do not have a list of options.
+ // Additionally, a checkbox field may also be flagged as required so we need
+ // to allow for custom required text.
+ if ( isStandalone ) {
+ return (
+
+ { ! hideInput && (
+
+ ) }
+
+ setAttributes( { label: newLabel } ) }
+ onRemove={ onRemove }
+ />
+ { required && (
+ setAttributes( { requiredText: value } ) }
+ tagName="span"
+ value={ requiredText || __( '(required)', 'jetpack-forms' ) }
+ withoutInteractiveFormatting
+ />
+ ) }
+
+
+ );
+ }
+
+ return (
+
+
+ setAttributes( { label: newLabel } ) }
+ onRemove={ onRemove }
+ />
+
+ );
+};
+
+export default OptionEdit;
diff --git a/projects/packages/forms/src/blocks/option/index.js b/projects/packages/forms/src/blocks/option/index.js
new file mode 100644
index 000000000000..1dbfb95f8701
--- /dev/null
+++ b/projects/packages/forms/src/blocks/option/index.js
@@ -0,0 +1,75 @@
+import { Path } from '@wordpress/components';
+import { __ } from '@wordpress/i18n';
+import renderMaterialIcon from '../contact-form/util/render-material-icon';
+import edit from './edit';
+import save from './save';
+
+const name = 'option';
+const settings = {
+ apiVersion: 3,
+ title: __( 'Option', 'jetpack-forms' ),
+ description: __( 'An option for a form choice field', 'jetpack-forms' ),
+ category: 'contact-form',
+ icon: {
+ src: renderMaterialIcon(
),
+ },
+ parent: [ 'jetpack/field-checkbox', 'jetpack/field-consent', 'jetpack/options' ],
+ supports: {
+ reusable: false,
+ html: false,
+ splitting: true,
+ color: {
+ text: true,
+ background: false,
+ gradients: false,
+ },
+ typography: {
+ fontSize: true,
+ lineHeight: true,
+ __experimentalFontFamily: true,
+ __experimentalFontWeight: true,
+ __experimentalFontStyle: true,
+ __experimentalTextTransform: true,
+ __experimentalTextDecoration: true,
+ __experimentalLetterSpacing: true,
+ __experimentalDefaultControls: {
+ fontSize: true,
+ },
+ },
+ },
+ attributes: {
+ defaultLabel: {
+ type: 'string',
+ default: '',
+ },
+ label: {
+ type: 'string',
+ default: '',
+ },
+ requiredText: {
+ type: 'string',
+ default: '',
+ },
+ hideInput: {
+ type: 'boolean',
+ default: false,
+ },
+ isStandalone: {
+ type: 'boolean',
+ default: false,
+ },
+ },
+ usesContext: [
+ 'jetpack/field-defaultValue',
+ 'jetpack/field-options-type',
+ 'jetpack/field-required',
+ 'jetpack/field-share-attributes',
+ ],
+ edit,
+ save,
+};
+
+export default {
+ name,
+ settings,
+};
diff --git a/projects/packages/forms/src/blocks/option/save.js b/projects/packages/forms/src/blocks/option/save.js
new file mode 100644
index 000000000000..461f67a0a4bc
--- /dev/null
+++ b/projects/packages/forms/src/blocks/option/save.js
@@ -0,0 +1 @@
+export default () => null;
diff --git a/projects/packages/forms/src/blocks/option/use-enter.js b/projects/packages/forms/src/blocks/option/use-enter.js
new file mode 100644
index 000000000000..5f0dea4cd7ce
--- /dev/null
+++ b/projects/packages/forms/src/blocks/option/use-enter.js
@@ -0,0 +1,83 @@
+import { store as blockEditorStore } from '@wordpress/block-editor';
+import { createBlock, cloneBlock, getDefaultBlockName } from '@wordpress/blocks';
+import { useRefEffect } from '@wordpress/compose';
+import { useDispatch, useSelect } from '@wordpress/data';
+import { useRef } from '@wordpress/element';
+
+const useEnter = props => {
+ const { replaceBlocks, insertBlock, selectionChange } = useDispatch( blockEditorStore );
+ const { getBlock, getBlockRootClientId, getBlockIndex } = useSelect( blockEditorStore );
+
+ const propsRef = useRef( props );
+ propsRef.current = props;
+
+ return useRefEffect( element => {
+ function onKeyDown( event ) {
+ if ( event.defaultPrevented || event.key !== 'Enter' ) {
+ return;
+ }
+
+ const { clientId, content, isStandalone } = propsRef.current;
+
+ if ( content.length && ! isStandalone ) {
+ return;
+ }
+
+ event.preventDefault();
+
+ if ( isStandalone ) {
+ const fieldBlockId = getBlockRootClientId( clientId );
+ const formBlockId = getBlockRootClientId( fieldBlockId );
+ const insertIndex = getBlockIndex( fieldBlockId );
+ const newBlock = createBlock( getDefaultBlockName() );
+
+ insertBlock( newBlock, insertIndex + 1, formBlockId );
+ selectionChange( newBlock.clientId );
+ } else {
+ const optionsBlockId = getBlockRootClientId( clientId );
+ const optionsBlock = getBlock( optionsBlockId );
+ const fieldBlockId = getBlockRootClientId( optionsBlockId );
+ const fieldBlock = getBlock( fieldBlockId );
+
+ if ( ! optionsBlock || ! fieldBlock ) {
+ return;
+ }
+
+ const optionIndex = getBlockIndex( clientId );
+ const allOptions = optionsBlock.innerBlocks;
+ const beforeOptions = allOptions.slice( 0, optionIndex );
+ const afterOptions = allOptions.slice( optionIndex + 1 );
+ const labelBlock = fieldBlock.innerBlocks[ 0 ];
+ const fieldBlockIndex = getBlockIndex( fieldBlockId );
+
+ const cloneField = optionSlice =>
+ cloneBlock( {
+ ...fieldBlock,
+ innerBlocks: [
+ cloneBlock( labelBlock ),
+ cloneBlock( {
+ ...optionsBlock,
+ innerBlocks: optionSlice,
+ } ),
+ ],
+ } );
+
+ const headField = cloneField( beforeOptions );
+ const middleBlock = createBlock( getDefaultBlockName() );
+ const tailField = afterOptions.length ? [ cloneField( afterOptions ) ] : [];
+
+ replaceBlocks(
+ fieldBlock.clientId,
+ [ headField, middleBlock, ...tailField ],
+ fieldBlockIndex
+ );
+ selectionChange( middleBlock.clientId );
+ }
+ }
+
+ element.addEventListener( 'keydown', onKeyDown );
+ return () => element.removeEventListener( 'keydown', onKeyDown );
+ }, [] );
+};
+
+export default useEnter;
diff --git a/projects/packages/forms/src/blocks/options/edit.js b/projects/packages/forms/src/blocks/options/edit.js
new file mode 100644
index 000000000000..bb055a2ec58e
--- /dev/null
+++ b/projects/packages/forms/src/blocks/options/edit.js
@@ -0,0 +1,33 @@
+import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
+import clsx from 'clsx';
+import { useSyncedAttributes } from '../shared/hooks/use-synced-attributes';
+import useVariationStyleProperties from '../shared/hooks/use-variation-style-properties.js';
+
+const SYNCED_ATTRIBUTE_KEYS = [ 'backgroundColor', 'borderColor', 'style', 'textColor' ];
+
+const OptionsEdit = ( { clientId, name, context, attributes, setAttributes } ) => {
+ const { 'jetpack/field-share-attributes': isSynced } = context;
+ useSyncedAttributes( name, isSynced, SYNCED_ATTRIBUTE_KEYS, attributes, setAttributes );
+ const variationProps = useVariationStyleProperties( {
+ clientId,
+ inputBlockName: name,
+ inputBlockAttributes: attributes,
+ } );
+
+ const blockProps = useBlockProps( {
+ className: clsx( 'jetpack-field-multiple__list', variationProps?.className ),
+ style: variationProps?.cssVars,
+ } );
+
+ const innerBlocksProps = useInnerBlocksProps( blockProps, {
+ allowedBlocks: [ `jetpack/option` ],
+ defaultBlock: `jetpack/option`,
+ template: [ [ `jetpack/option` ] ],
+ templateInsertUpdatesSelection: true,
+ templateLock: false,
+ } );
+
+ return
;
+};
+
+export default OptionsEdit;
diff --git a/projects/packages/forms/src/blocks/options/index.js b/projects/packages/forms/src/blocks/options/index.js
new file mode 100644
index 000000000000..e7541b127e48
--- /dev/null
+++ b/projects/packages/forms/src/blocks/options/index.js
@@ -0,0 +1,55 @@
+import { Path } from '@wordpress/components';
+import { __ } from '@wordpress/i18n';
+import renderMaterialIcon from '../contact-form/util/render-material-icon';
+import edit from './edit';
+import save from './save';
+
+const name = 'options';
+const settings = {
+ apiVersion: 3,
+ title: __( 'Options', 'jetpack-forms' ),
+ description: __( 'A collection of option blocks', 'jetpack-forms' ),
+ category: 'contact-form',
+ icon: {
+ src: renderMaterialIcon(
+
+ ),
+ },
+ parent: [ 'jetpack/field-multiple-choice', 'jetpack/field-single-choice' ],
+ attributes: {
+ type: {
+ type: 'string',
+ default: 'checkbox',
+ },
+ },
+ allowedBlocks: [ 'jetpack/field-options' ],
+ providesContext: {
+ 'jetpack/field-options-type': 'type',
+ },
+ usesContext: [ 'jetpack/field-share-attributes' ],
+ supports: {
+ spacing: {
+ blockGap: false,
+ },
+ color: {
+ text: false,
+ background: true,
+ },
+ __experimentalBorder: {
+ color: true,
+ radius: true,
+ style: true,
+ width: true,
+ __experimentalDefaultControls: {
+ color: true,
+ radius: true,
+ style: true,
+ width: true,
+ },
+ },
+ },
+ edit,
+ save,
+};
+
+export default { name, settings };
diff --git a/projects/packages/forms/src/blocks/options/save.js b/projects/packages/forms/src/blocks/options/save.js
new file mode 100644
index 000000000000..7f611f30a67e
--- /dev/null
+++ b/projects/packages/forms/src/blocks/options/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/shared/components/jetpack-field-controls.js b/projects/packages/forms/src/blocks/shared/components/jetpack-field-controls.js
new file mode 100644
index 000000000000..1b734fb74a6d
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/components/jetpack-field-controls.js
@@ -0,0 +1,96 @@
+import {
+ InspectorAdvancedControls,
+ InspectorControls,
+ BlockControls,
+} from '@wordpress/block-editor';
+import { PanelBody, TextControl, ToggleControl } from '@wordpress/components';
+import { isValidElement } 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';
+
+const JetpackFieldControls = ( {
+ attributes,
+ id,
+ required,
+ setAttributes,
+ width,
+ extraFieldSettings = [],
+} ) => {
+ const setId = value => {
+ const newValue = value.replace( /[^a-zA-Z0-9_-]/g, '' );
+ setAttributes( { id: newValue } );
+ };
+
+ let fieldSettings = [
+
setAttributes( { required: value } ) }
+ help={ __( 'You can edit the "required" label in the editor', 'jetpack-forms' ) }
+ __nextHasNoMarginBottom={ true }
+ />,
+ ,
+ setAttributes( { shareFieldAttributes: value } ) }
+ help={ __( 'Deactivate for individual styling of this block', 'jetpack-forms' ) }
+ __nextHasNoMarginBottom={ true }
+ />,
+ ];
+
+ extraFieldSettings.forEach( ( { element, index } ) => {
+ if ( ! isValidElement( element ) ) {
+ return;
+ }
+
+ if ( index >= 0 && index < fieldSettings.length ) {
+ fieldSettings = [
+ ...fieldSettings.slice( 0, index ),
+ element,
+ ...fieldSettings.slice( index ),
+ ];
+ } else {
+ fieldSettings.push( element );
+ }
+ } );
+
+ return (
+ <>
+
+ setAttributes( { required: ! required } ) }
+ />
+
+
+
+
+
+
+
+ <>{ fieldSettings }>
+
+
+
+
+
+ >
+ );
+};
+
+export default JetpackFieldControls;
diff --git a/projects/packages/forms/src/blocks/shared/components/jetpack-field.js b/projects/packages/forms/src/blocks/shared/components/jetpack-field.js
new file mode 100644
index 000000000000..e8431b86ebc1
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/components/jetpack-field.js
@@ -0,0 +1,80 @@
+import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';
+import { getBlockType } from '@wordpress/blocks';
+import { createHigherOrderComponent } from '@wordpress/compose';
+import { useMemo } from '@wordpress/element';
+import { addFilter } from '@wordpress/hooks';
+import clsx from 'clsx';
+import useFieldSelected from '../hooks/use-field-selected';
+import useJetpackFieldStyles from '../hooks/use-jetpack-field-styles';
+import { ALLOWED_INNER_BLOCKS } from '../util/constants';
+import JetpackFieldControls from './jetpack-field-controls';
+
+const JetpackField = props => {
+ const {
+ attributes,
+ clientId,
+ id,
+ isSelected,
+ label,
+ required,
+ requiredText,
+ setAttributes,
+ type,
+ width,
+ } = props;
+ const { isInnerBlockSelected, hasPlaceholder } = useFieldSelected( clientId );
+ const { blockStyle } = useJetpackFieldStyles( attributes );
+ 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, required, defaultLabel, requiredText } ],
+ [ 'jetpack/input', { type } ],
+ ];
+ }, [ label, defaultLabel, required, requiredText, type ] );
+ const innerBlocksProps = useInnerBlocksProps( blockProps, {
+ allowedBlocks: ALLOWED_INNER_BLOCKS,
+ template,
+ templateLock: 'all',
+ } );
+
+ return (
+ <>
+
+
+ >
+ );
+};
+
+export default JetpackField;
+
+const withCustomClassName = createHigherOrderComponent( BlockListBlock => {
+ return props => {
+ if ( props.name.indexOf( 'jetpack/field' ) > -1 ) {
+ const customClassName = props.attributes.width
+ ? 'jetpack-field__width-' + props.attributes.width
+ : '';
+
+ return ;
+ }
+
+ return ;
+ };
+}, 'withCustomClassName' );
+
+addFilter( 'editor.BlockListBlock', 'jetpack/contact-form', withCustomClassName );
diff --git a/projects/packages/forms/src/blocks/shared/deprecations/inner-blocks-deprecation.js b/projects/packages/forms/src/blocks/shared/deprecations/inner-blocks-deprecation.js
new file mode 100644
index 000000000000..343a6fa2d1ff
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/deprecations/inner-blocks-deprecation.js
@@ -0,0 +1,109 @@
+import { createBlock } from '@wordpress/blocks';
+import deprecateFieldStyles from '../util/deprecate-field-styles';
+
+const INNER_BLOCKS_DEPRECATION = {
+ attributes: {
+ label: {
+ type: 'string',
+ default: null,
+ },
+ required: {
+ type: 'boolean',
+ default: false,
+ },
+ requiredText: {
+ type: 'string',
+ },
+ options: {
+ type: 'array',
+ default: [],
+ },
+ defaultValue: {
+ type: 'string',
+ default: '',
+ },
+ placeholder: {
+ type: 'string',
+ default: '',
+ },
+ 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,
+ },
+ },
+ supports: {
+ reusable: false,
+ html: false,
+ },
+ migrate: attributes => {
+ const { restAttributes, labelStyles, inputStyles } = deprecateFieldStyles( attributes );
+ const newInnerBlocks = [
+ createBlock( 'jetpack/label', {
+ label: attributes.label,
+ requiredText: attributes.requiredText,
+ style: labelStyles,
+ } ),
+ createBlock( 'jetpack/input', {
+ placeholder: attributes.placeholder,
+ style: inputStyles,
+ } ),
+ ];
+
+ return [ restAttributes, newInnerBlocks ];
+ },
+ isEligible: ( _attributes, innerBlocks ) => ! innerBlocks.length,
+ save: () => null,
+};
+
+export default INNER_BLOCKS_DEPRECATION;
diff --git a/projects/packages/forms/src/blocks/shared/deprecations/migrate-inner-option-blocks.js b/projects/packages/forms/src/blocks/shared/deprecations/migrate-inner-option-blocks.js
new file mode 100644
index 000000000000..12101ac04ece
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/deprecations/migrate-inner-option-blocks.js
@@ -0,0 +1,30 @@
+import { createBlock } from '@wordpress/blocks';
+import { __ } from '@wordpress/i18n';
+import deprecateFieldStyles from '../util/deprecate-field-styles';
+
+// Legacy choice fields used inner blocks for only individual options.
+// This function migrates that to use inner blocks for label + options,
+// moving the previous individual option blocks under the new options block.
+export default function migrateInnerOptionBlocks( attributes, innerBlocks, fieldType ) {
+ const { restAttributes, labelStyles, optionStyles } = deprecateFieldStyles( attributes );
+ const { options, ...updatedAttributes } = restAttributes;
+
+ const optionBlocks = innerBlocks.map( optionBlock =>
+ createBlock( 'jetpack/option', {
+ label: optionBlock.attributes.label,
+ defaultLabel: __( 'Add option…', 'jetpack-forms' ),
+ style: optionStyles,
+ } )
+ );
+
+ const newInnerBlocks = [
+ createBlock( 'jetpack/label', {
+ label: attributes.label,
+ requiredText: attributes.requiredText,
+ style: labelStyles,
+ } ),
+ createBlock( 'jetpack/options', { type: fieldType }, optionBlocks ),
+ ];
+
+ return [ updatedAttributes, newInnerBlocks ];
+}
diff --git a/projects/packages/forms/src/blocks/shared/deprecations/migrate-options-to-inner-blocks.js b/projects/packages/forms/src/blocks/shared/deprecations/migrate-options-to-inner-blocks.js
new file mode 100644
index 000000000000..a821a1106574
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/deprecations/migrate-options-to-inner-blocks.js
@@ -0,0 +1,30 @@
+import { createBlock } from '@wordpress/blocks';
+import { __ } from '@wordpress/i18n';
+import deprecateFieldStyles from '../util/deprecate-field-styles';
+
+const isValidOption = value => typeof value === 'string' && value.trim().length > 0;
+
+export default function migrateOptionsToInnerBlocks( attributes, fieldType ) {
+ const { restAttributes, labelStyles, optionStyles } = deprecateFieldStyles( attributes );
+ const { options, ...migratedAttributes } = restAttributes;
+
+ const nonEmptyOptions = options ? options.filter( option => isValidOption( option ) ) : [];
+ const optionBlocks = nonEmptyOptions.map( option =>
+ createBlock( 'jetpack/option', {
+ label: option,
+ defaultLabel: __( 'Add option…', 'jetpack-forms' ),
+ style: optionStyles,
+ } )
+ );
+
+ const newInnerBlocks = [
+ createBlock( 'jetpack/label', {
+ label: attributes.label,
+ requiredText: attributes.requiredText,
+ style: labelStyles,
+ } ),
+ createBlock( 'jetpack/options', { type: fieldType }, optionBlocks ),
+ ];
+
+ return [ migratedAttributes, newInnerBlocks ];
+}
diff --git a/projects/packages/forms/src/blocks/shared/deprecations/multiple-choice-field-deprecation.js b/projects/packages/forms/src/blocks/shared/deprecations/multiple-choice-field-deprecation.js
new file mode 100644
index 000000000000..5addffb02992
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/deprecations/multiple-choice-field-deprecation.js
@@ -0,0 +1,48 @@
+import { createBlock } from '@wordpress/blocks';
+import { __ } from '@wordpress/i18n';
+import deprecateFieldStyles from '../util/deprecate-field-styles';
+import INNER_BLOCKS_DEPRECATION from './inner-blocks-deprecation';
+
+// 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 isValidOption = value => typeof value === 'string' && value.trim().length > 0;
+
+export default function multiFieldV1( fieldType ) {
+ return {
+ attributes: {
+ ...INNER_BLOCKS_DEPRECATION.attributes,
+ label: {
+ type: 'string',
+ default: fieldType === 'checkbox' ? severalOptionsDefault : oneOptionDefault,
+ },
+ },
+ supports: INNER_BLOCKS_DEPRECATION.supports,
+ migrate: attributes => {
+ const { restAttributes, labelStyles, optionStyles } = deprecateFieldStyles( attributes );
+ const { options, ...migratedAttributes } = restAttributes;
+
+ const nonEmptyOptions = options ? options.filter( option => isValidOption( option ) ) : [];
+ const optionBlocks = nonEmptyOptions.map( option =>
+ createBlock( 'jetpack/option', {
+ label: option,
+ defaultLabel: __( 'Add option…', 'jetpack-forms' ),
+ style: optionStyles,
+ } )
+ );
+
+ const newInnerBlocks = [
+ createBlock( 'jetpack/label', {
+ label: attributes.label,
+ requiredText: attributes.requiredText,
+ style: labelStyles,
+ } ),
+ createBlock( 'jetpack/options', { type: fieldType }, optionBlocks ),
+ ];
+
+ return [ migratedAttributes, newInnerBlocks ];
+ },
+ save: () => null,
+ };
+}
diff --git a/projects/packages/forms/src/blocks/shared/hooks/use-field-selected.js b/projects/packages/forms/src/blocks/shared/hooks/use-field-selected.js
new file mode 100644
index 000000000000..49348ea3f272
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/hooks/use-field-selected.js
@@ -0,0 +1,15 @@
+import { store as blockEditorStore } from '@wordpress/block-editor';
+import { useSelect } from '@wordpress/data';
+
+export default function useFieldSelected( clientId ) {
+ return useSelect(
+ select => {
+ const { getBlock, hasSelectedInnerBlock } = select( blockEditorStore );
+ return {
+ isInnerBlockSelected: hasSelectedInnerBlock( clientId, true ),
+ hasPlaceholder: !! getBlock( clientId ).innerBlocks[ 1 ]?.attributes?.placeholder,
+ };
+ },
+ [ clientId ]
+ );
+}
diff --git a/projects/packages/forms/src/blocks/shared/hooks/use-form-style.js b/projects/packages/forms/src/blocks/shared/hooks/use-form-style.js
new file mode 100644
index 000000000000..715179fbd174
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/hooks/use-form-style.js
@@ -0,0 +1,19 @@
+import { store as blockEditorStore } from '@wordpress/block-editor';
+import { useSelect } from '@wordpress/data';
+import { FORM_BLOCK_NAME, FORM_STYLE } from '../util/constants';
+import getBlockStyle from '../util/get-block-style';
+
+const useFormStyle = clientId => {
+ const formBlockAttributes = useSelect( select => {
+ const [ formBlockClientId ] = select( blockEditorStore ).getBlockParentsByBlockName(
+ clientId,
+ FORM_BLOCK_NAME
+ );
+
+ return select( blockEditorStore ).getBlockAttributes( formBlockClientId );
+ } );
+
+ return getBlockStyle( formBlockAttributes?.className ) || FORM_STYLE.DEFAULT;
+};
+
+export default useFormStyle;
diff --git a/projects/packages/forms/src/blocks/shared/hooks/use-form-wrapper.js b/projects/packages/forms/src/blocks/shared/hooks/use-form-wrapper.js
new file mode 100644
index 000000000000..4c6ba85c8f01
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/hooks/use-form-wrapper.js
@@ -0,0 +1,38 @@
+import { store as blockEditorStore } from '@wordpress/block-editor';
+import { createBlock } from '@wordpress/blocks';
+import { useDispatch, useSelect } from '@wordpress/data';
+import { useEffect } from '@wordpress/element';
+import { __ } from '@wordpress/i18n';
+import { FORM_BLOCK_NAME } from '../util/constants';
+
+export default function useFormWrapper( { attributes, clientId, name } ) {
+ const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } = useDispatch( blockEditorStore );
+ const { getBlocks } = useSelect( blockEditorStore );
+
+ const parentForms = useSelect(
+ select => {
+ return select( blockEditorStore ).getBlockParentsByBlockName( clientId, FORM_BLOCK_NAME );
+ },
+ [ clientId ]
+ );
+
+ useEffect( () => {
+ if ( ! parentForms?.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, {}, [
+ createBlock( name, attributes, getBlocks( clientId ) ),
+ createBlock( 'jetpack/button', {
+ text: __( 'Submit', 'jetpack-forms' ),
+ element: 'button',
+ lock: { remove: true },
+ } ),
+ ] )
+ );
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [] );
+}
diff --git a/projects/packages/forms/src/blocks/shared/hooks/use-insert-after-on-enter-key-down.js b/projects/packages/forms/src/blocks/shared/hooks/use-insert-after-on-enter-key-down.js
new file mode 100644
index 000000000000..dc35c83b1d57
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/hooks/use-insert-after-on-enter-key-down.js
@@ -0,0 +1,42 @@
+import { store as blockEditorStore } from '@wordpress/block-editor';
+import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
+import { useSelect, useDispatch } from '@wordpress/data';
+import { useCallback } from '@wordpress/element';
+
+export default function ( clientId ) {
+ const { fieldParentId, parentIndex, formParentId } = useSelect(
+ select => {
+ const blockEditor = select( blockEditorStore );
+ const parentClientIds = blockEditor.getBlockParents( clientId );
+ const parentId = parentClientIds[ parentClientIds.length - 1 ];
+
+ return {
+ fieldParentId: parentId,
+ parentIndex: parentId ? blockEditor.getBlockIndex( parentId ) : undefined,
+ formParentId: parentClientIds[ parentClientIds.length - 2 ],
+ };
+ },
+ [ clientId ]
+ );
+
+ const { insertBlock } = useDispatch( blockEditorStore );
+
+ return useCallback(
+ event => {
+ if (
+ event.key === 'Enter' &&
+ ! event.shiftKey &&
+ fieldParentId &&
+ parentIndex !== undefined
+ ) {
+ event.preventDefault();
+ insertBlock(
+ createBlock( getDefaultBlockName() ),
+ parentIndex + 1,
+ formParentId // Insert in the same context as the field block.
+ );
+ }
+ },
+ [ insertBlock, fieldParentId, formParentId, parentIndex ]
+ );
+}
diff --git a/projects/packages/forms/src/blocks/shared/hooks/use-jetpack-field-styles.js b/projects/packages/forms/src/blocks/shared/hooks/use-jetpack-field-styles.js
new file mode 100644
index 000000000000..3a627f23ff27
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/hooks/use-jetpack-field-styles.js
@@ -0,0 +1,58 @@
+const isNumber = value => typeof value === 'number' && ! isNaN( value );
+
+const useJetpackFieldStyles = attributes => {
+ const blockStyle = {
+ '--jetpack--contact-form--border-color': attributes.borderColor,
+ '--jetpack--contact-form--border-radius': isNumber( attributes.borderRadius )
+ ? `${ attributes.borderRadius }px`
+ : null,
+ '--jetpack--contact-form--border-size': isNumber( attributes.borderWidth )
+ ? `${ attributes.borderWidth }px`
+ : null,
+ '--jetpack--contact-form--input-background': attributes.fieldBackgroundColor,
+ '--jetpack--contact-form--font-size': attributes.fieldFontSize,
+ '--jetpack--contact-form--line-height': attributes.lineHeight,
+ '--jetpack--contact-form--text-color': attributes.inputColor,
+ '--jetpack--contact-form--button-outline--text-color': attributes.inputColor,
+ '--jetpack--contact-form--button-outline--background-color': attributes.buttonBackgroundColor,
+ '--jetpack--contact-form--button-outline--border-radius': isNumber(
+ attributes.buttonBorderRadius
+ )
+ ? `${ attributes.buttonBorderRadius }px`
+ : null,
+ '--jetpack--contact-form--button-outline--border-size': isNumber( attributes.buttonBorderWidth )
+ ? `${ attributes.buttonBorderWidth }px`
+ : null,
+ };
+
+ const labelStyle = {
+ color: attributes.labelColor,
+ fontSize: attributes.labelFontSize,
+ lineHeight: attributes.labelLineHeight,
+ };
+
+ const fieldStyle = {
+ backgroundColor: attributes.fieldBackgroundColor,
+ borderColor: attributes.borderColor,
+ borderRadius: isNumber( attributes.borderRadius ) ? attributes.borderRadius : null,
+ borderWidth: isNumber( attributes.borderWidth ) ? attributes.borderWidth : null,
+ color: attributes.inputColor,
+ fontSize: attributes.fieldFontSize,
+ lineHeight: attributes.lineHeight,
+ };
+
+ const optionStyle = {
+ color: fieldStyle.color,
+ fontSize: fieldStyle.fontSize,
+ lineHeight: fieldStyle.lineHeight,
+ };
+
+ return {
+ blockStyle,
+ fieldStyle,
+ labelStyle,
+ optionStyle,
+ };
+};
+
+export default useJetpackFieldStyles;
diff --git a/projects/packages/forms/src/blocks/shared/hooks/use-synced-attributes.js b/projects/packages/forms/src/blocks/shared/hooks/use-synced-attributes.js
new file mode 100644
index 000000000000..08a503e4b9bd
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/hooks/use-synced-attributes.js
@@ -0,0 +1,192 @@
+import { usePrevious } from '@wordpress/compose';
+import { useDispatch } from '@wordpress/data';
+import { createContext, useContext, useEffect, useMemo, useReducer } from '@wordpress/element';
+import { isEqual } from 'lodash';
+/** @typedef {import('react')} React */
+
+/**
+ * Context for managing synced attributes across blocks.
+ *
+ * @type {React.Context<[syncedAttributes: object, setSyncedAttributes: Function]>}
+ */
+const SyncedAttributeContext = createContext( [ {}, () => {} ] );
+
+/**
+ * Provider component that manages synced attributes across blocks.
+ *
+ * @param {object} props - Component props.
+ * @param {object} props.children - Child components to render within the provider.
+ * @return {JSX.Element} The provider component.
+ */
+export function SyncedAttributeProvider( { children } ) {
+ const value = useReducer( ( state, action ) => {
+ const { name, attributes } = action;
+ return {
+ ...state,
+ [ name ]: {
+ ...( state[ name ] ?? {} ),
+ ...attributes,
+ },
+ };
+ }, {} );
+
+ return (
+ { children }
+ );
+}
+
+/**
+ * Uses the `SyncedAttributeContext`, but returns only the values for the given block type.
+ * The `setSyncedAttributes` function it returns is also adjusted so that it only updates
+ * the attributes for the given block type.
+ *
+ * @param {string} name - The name of the block.
+ * @param {[object, Function]} contextValue - The context value from SyncedAttributeContext.
+ * @return {Array} Array containing the synced attributes and the function to update them.
+ */
+function useSyncedAttributesForBlock( name, contextValue ) {
+ const [ syncedAttributes, setSyncedAttributes ] = contextValue;
+
+ return useMemo( () => {
+ return [ syncedAttributes[ name ], attributes => setSyncedAttributes( { name, attributes } ) ];
+ }, [ name, setSyncedAttributes, syncedAttributes ] );
+}
+
+/**
+ * Returns an object containing only the attributes that are in the syncedAttributeKeys array.
+ *
+ * @param {object} attributes - The attributes of the block.
+ * @param {string[]} syncedAttributeKeys - The keys of the attributes that are synced.
+ *
+ * @return {object} Object containing only the synced attributes.
+ */
+function pickSyncedAttributes( attributes, syncedAttributeKeys ) {
+ return syncedAttributeKeys.reduce( ( acc, key ) => {
+ acc[ key ] = attributes[ key ];
+ return acc;
+ }, {} );
+}
+
+/**
+ * Returns an object containing only the attributes that have different values compared to the attribute sets.
+ *
+ * When multiple attribute sets are provided, the function performs an AND check, so for a given key,
+ * the value in EVERY attribute set must be different for it to be considered modified.
+ *
+ * @param {object} sourceAttributes - The source attributes to compare against.
+ * @param {Object[]} attributeSets - Array of attribute sets to compare with the source.
+ *
+ * @return {object} Object containing only the modified attributes.
+ */
+function getModifiedAttributes( sourceAttributes, attributeSets ) {
+ if ( ! sourceAttributes ) {
+ return {};
+ }
+
+ return Object.keys( sourceAttributes ).reduce( ( acc, key ) => {
+ // Use lodash `isEqual` to perform a deep comparison of the attributes.
+ // With `!==` this would return a lot of false positives where different blocks
+ // have objects with different references, but the same inner attribute values.
+ // TODO - replace this with not lodash.
+ if ( attributeSets.every( set => ! isEqual( sourceAttributes[ key ], set?.[ key ] ) ) ) {
+ acc[ key ] = sourceAttributes[ key ];
+ }
+
+ return acc;
+ }, {} );
+}
+
+/**
+ * This hook is used to sync a subset of attributes of a block with the other blocks of the same type within a form.
+ * The syncing only happens if the block has the `shareFieldAttributes` attribute set to `true`.
+ *
+ * @param {string} name - The name of the block.
+ * @param {boolean} isSynced - Whether the block is synced.
+ * @param {string[]} syncedAttributeKeys - The keys of the attributes that are synced.
+ * @param {object} attributes - The attributes of the block.
+ * @param {Function} setOwnAttributes - The block's own `setAttributes` function, renamed for some clarity.
+ */
+export function useSyncedAttributes(
+ name,
+ isSynced,
+ syncedAttributeKeys,
+ attributes,
+ setOwnAttributes
+) {
+ const { __unstableMarkNextChangeAsNotPersistent } = useDispatch( 'core/block-editor' );
+ const contextValue = useContext( SyncedAttributeContext );
+
+ // The synced attributes are pulled from the parent form block via react context.
+ // They can be updated using the `setSyncedAttributes` function.
+ // These attributes are the source of truth for all blocks that are synced.
+ // If they change, the current block will be updated to match the synced attributes using its `setAttributes` function.
+ const [ syncedAttributes, setSyncedAttributes ] = useSyncedAttributesForBlock(
+ name,
+ contextValue
+ );
+
+ // The own attributes belong to the current block that this hook operates on.
+ // If these change and the block is synced, the synced attributes will be updated on the parent form using
+ // the `setSyncedAttributes` function.
+ const ownAttributes = useMemo(
+ () => pickSyncedAttributes( attributes, syncedAttributeKeys ),
+ [ attributes, syncedAttributeKeys ]
+ );
+ const previousOwnAttributes = usePrevious( ownAttributes ) ?? ownAttributes;
+
+ useEffect( () => {
+ // Skip syncing if not needed or possible.
+ // When a field is rendered in a preview i.e. without a parent form the useFormWrapper hook will
+ // wrap the field in a new block leading to a moment when the synced attribute context is not yet available.
+ if ( ! isSynced || ! contextValue ) {
+ return;
+ }
+
+ // If the synced attributes are unset, but the block does have attribute values, it indicates
+ // the synced attributes have not yet been primed. Set them to an initial value.
+ // The only drawback here it that this will cause a `setSyncedAttributes` call for every block on the
+ // first run through.
+ if ( ! syncedAttributes && ownAttributes ) {
+ setSyncedAttributes( ownAttributes );
+ return;
+ }
+
+ // Check whether the block's own attributes have changed compared to the block's previous own attributes
+ // and the synced attributes.
+ //
+ // The block's previous own attributes are checked to protect against a situation where a new field block is inserted
+ // into the form with different attribute values to the synced attributes.
+ // In this case we don't want every other block to sync to this new block, instead we want the new block
+ // to sync to the `syncedAttributes`.
+ const updatedOwnAttributes = getModifiedAttributes( ownAttributes, [
+ syncedAttributes,
+ previousOwnAttributes,
+ ] );
+ // If there are changes to the block's own attributes, update the synced attributes.
+ if ( Object.keys( updatedOwnAttributes ).length > 0 ) {
+ setSyncedAttributes( updatedOwnAttributes );
+ return;
+ }
+
+ // Check whether the synced attributes have changed when compared to the block's own attributes.
+ const updatedSyncedAttributes = getModifiedAttributes( syncedAttributes, [ ownAttributes ] );
+ // If there are changes to the synced attributes, update the block's own attributes.
+ if ( Object.keys( updatedSyncedAttributes ).length > 0 ) {
+ // Mark the change as not persistent to prevent the attribute modifications from being
+ // individual updates to the undo stack.
+ // It might be good to move towards batching these changes together, to do this the updates,
+ // would need to be made from the top level `SyncedAttributeProvider`.
+ __unstableMarkNextChangeAsNotPersistent();
+ setOwnAttributes( updatedSyncedAttributes );
+ }
+ }, [
+ isSynced,
+ setSyncedAttributes,
+ setOwnAttributes,
+ ownAttributes,
+ previousOwnAttributes,
+ syncedAttributes,
+ __unstableMarkNextChangeAsNotPersistent,
+ contextValue,
+ ] );
+}
diff --git a/projects/packages/forms/src/blocks/shared/hooks/use-variation-style-properties.js b/projects/packages/forms/src/blocks/shared/hooks/use-variation-style-properties.js
new file mode 100644
index 000000000000..379e9d131b1b
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/hooks/use-variation-style-properties.js
@@ -0,0 +1,225 @@
+import {
+ __experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles, // eslint-disable-line @wordpress/no-unsafe-wp-apis
+ __experimentalGetColorClassesAndStyles as getColorClassesAndStyles, // eslint-disable-line @wordpress/no-unsafe-wp-apis
+} from '@wordpress/block-editor';
+import { store as coreStore } from '@wordpress/core-data';
+import { useSelect } from '@wordpress/data';
+import { useMemo } from '@wordpress/element';
+import { isNumber, merge } from 'lodash';
+import { useFormStyle, FORM_STYLE } from '../../contact-form/util/form';
+
+/**
+ * Returns the value of the CSS var if it is a number, otherwise null.
+ * Match behaviour in projects/packages/forms/src/blocks/shared/hooks/use-jetpack-field-styles.js.
+ *
+ * @param {*} value - A value from the legacy form block attributes.
+ * @return {string|*} The value of the CSS var if it is a number, otherwise the value itself.
+ */
+function getIntAsPxValue( value ) {
+ if ( typeof value !== 'undefined' && isNumber( value ) ) {
+ return `${ value }px`;
+ }
+ return value;
+}
+
+/**
+ * Returns the value of the CSS var for the border radius for outlined forms, taking into account
+ * that the border radius might be a single or split value.
+ *
+ * @param {object} style - The style object.
+ *
+ * @return {string|undefined} The CSS var for the border radius.
+ */
+function getBorderRadiusCssVar( style ) {
+ // A single border radius value for all for corners, this is quicker to check, so it goes first.
+ if ( style?.borderRadius ) {
+ return getIntAsPxValue( style?.borderRadius );
+ }
+ // If corner radii are set on the top-left or bottom-left of the block, take the maximum of the two.
+ // We check the left side due to writing direction—this variable is used to offset text.
+ // TODO: this should factor in RTL languages.
+ if ( style?.borderTopLeftRadius || style?.borderBottomLeftRadius ) {
+ return `max( ${ style?.borderTopLeftRadius ?? 0 }, ${ style?.borderBottomLeftRadius ?? 0 } )`;
+ }
+}
+
+/**
+ * Returns the border widths for the input block.
+ *
+ * @param {object} blockBorderStyles - The attributes of the input block.
+ * @param {object} globalBorderStyles - The global styles.
+ * @return {object} The border widths.
+ */
+function getBorderWidths( blockBorderStyles, globalBorderStyles ) {
+ /*
+ * Use `getIntAsPxValue` on the borderWidth as legacy form blocks might not have a value unit.
+ * Sides (top, right, bottom, left) are available post-upgrade.
+ */
+ const borderWidth =
+ getIntAsPxValue( blockBorderStyles?.borderWidth ) || globalBorderStyles?.borderWidth;
+
+ if ( borderWidth ) {
+ return {
+ borderTopWidth: borderWidth,
+ borderRightWidth: borderWidth,
+ borderBottomWidth: borderWidth,
+ borderLeftWidth: borderWidth,
+ };
+ }
+
+ const borderTopWidth = blockBorderStyles?.borderTopWidth || globalBorderStyles?.borderTopWidth;
+ const borderRightWidth =
+ blockBorderStyles?.borderRightWidth || globalBorderStyles?.borderRightWidth;
+ const borderBottomWidth =
+ blockBorderStyles?.borderBottomWidth || globalBorderStyles?.borderBottomWidth;
+ const borderLeftWidth = blockBorderStyles?.borderLeftWidth || globalBorderStyles?.borderLeftWidth;
+
+ return {
+ borderTopWidth,
+ borderRightWidth,
+ borderBottomWidth,
+ borderLeftWidth,
+ };
+}
+
+/**
+ * Returns properties that help achieve the outlined and animated form styles.
+ *
+ * The outlined style in particular requires taking specific style properties (especially border and background)
+ * that the user can configure on the input or options block, and applying them to the label. The label displays
+ * the border rather than the input/options blocks. It requires some smoke and mirrors!
+ *
+ * The animated style requires the border size to calculate how much the label should be offset to give the
+ * appearance that it's within the input.
+ *
+ * This hook first resolves the global styles for the input or options block that the label is the sibling of
+ * and merges them with that block's own styles to get the final resolved style values.
+ *
+ * It uses WordPress core functions to get the generated classnames for those styles, and also calculates
+ * some CSS Vars that are used to achieve the style variations.
+ *
+ * @param {object} props - Properties to pass to the hook.
+ * @param {string} props.clientId - The client ID of the block.
+ * @param {string} props.inputBlockName - The name of the input or options block.
+ * @param {object} props.inputBlockAttributes - The attributes of the input or options block.
+ * @return {object} The calculated properties that help achieve the outlined and animated form styles.
+ */
+export default function useVariationStyleProperties( {
+ clientId,
+ inputBlockName,
+ inputBlockAttributes,
+} ) {
+ const formStyle = useFormStyle( clientId );
+ const { userConfig, baseConfig } = useSelect( select => {
+ const {
+ __experimentalGetCurrentGlobalStylesId,
+ getEditedEntityRecord,
+ __experimentalGetCurrentThemeBaseGlobalStyles,
+ } = select( coreStore );
+ const globalStylesId = __experimentalGetCurrentGlobalStylesId();
+ if ( ! globalStylesId ) {
+ return {
+ userConfig: null,
+ baseConfig: null,
+ };
+ }
+
+ return {
+ userConfig: getEditedEntityRecord( 'root', 'globalStyles', globalStylesId ),
+ baseConfig: __experimentalGetCurrentThemeBaseGlobalStyles(),
+ };
+ }, [] );
+
+ const inputBaseGlobalStyles = baseConfig?.styles?.blocks?.[ inputBlockName ];
+ const inputUserGlobalStyles = userConfig?.styles?.blocks?.[ inputBlockName ];
+ const mergedGlobalBlockStyles = useMemo(
+ () => merge( inputBaseGlobalStyles, inputUserGlobalStyles ),
+ [ inputBaseGlobalStyles, inputUserGlobalStyles ]
+ );
+
+ return useMemo( () => {
+ // Access the input block's attributes.
+ const blockBorderClassesAndStyles = getBorderClassesAndStyles( inputBlockAttributes ?? {} );
+ const globalBorderClassesAndStyles = getBorderClassesAndStyles( {
+ style: mergedGlobalBlockStyles,
+ } );
+ const borderWidths = getBorderWidths(
+ blockBorderClassesAndStyles?.style,
+ globalBorderClassesAndStyles?.style
+ );
+
+ // Add a class to apply padding to option groups that have a border.
+ const hasBorder =
+ inputBlockName === 'jetpack/options' &&
+ !! borderWidths?.borderLeftWidth &&
+ parseInt( borderWidths?.borderLeftWidth ) > 0;
+
+ const customBorderClasses = hasBorder ? 'jetpack-field-multiple__list--has-border' : '';
+
+ // Only return styles for outlined and animated forms.
+ if ( formStyle !== FORM_STYLE.OUTLINED && formStyle !== FORM_STYLE.ANIMATED ) {
+ return {
+ className: customBorderClasses,
+ };
+ }
+
+ // Notched HTML only needs the background color and associated classes.
+ const attributesWithBackgroundColor = inputBlockAttributes
+ ? {
+ backgroundColor: inputBlockAttributes?.backgroundColor,
+ style: {
+ color: {
+ background: inputBlockAttributes?.style?.color?.background,
+ },
+ },
+ }
+ : {};
+ const blockColorClassesAndStyles = getColorClassesAndStyles( attributesWithBackgroundColor );
+
+ /**
+ * Remove undefined classname values.
+ */
+ const filteredBlockColorClassesAndStyles = [
+ blockBorderClassesAndStyles?.className,
+ blockColorClassesAndStyles?.className,
+ customBorderClasses,
+ ]
+ .filter( Boolean )
+ .join( ' ' );
+
+ let styleSpecificCssVars = {};
+
+ if ( formStyle === FORM_STYLE.OUTLINED ) {
+ styleSpecificCssVars = {
+ '--jetpack--contact-form--notch-width':
+ 'max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius))',
+ };
+ }
+ if ( formStyle === FORM_STYLE.ANIMATED ) {
+ styleSpecificCssVars = {
+ // For the animated labels.
+ '--jetpack--contact-form--animated-left-offset': '16px', // Probably can be removed or we use `--jetpack--contact-form--input-padding`
+ };
+ }
+
+ return {
+ className: filteredBlockColorClassesAndStyles,
+ style: {
+ ...blockBorderClassesAndStyles?.style,
+ // Only background here.
+ backgroundColor: blockColorClassesAndStyles?.style?.backgroundColor,
+ },
+ cssVars: {
+ // Sets the value of --notch-width: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius)); for .notched-label.
+ '--jetpack--contact-form--border-radius':
+ getBorderRadiusCssVar( blockBorderClassesAndStyles.style ) ||
+ getBorderRadiusCssVar( globalBorderClassesAndStyles.style ),
+ '--jetpack--contact-form--border-left-size': borderWidths?.borderLeftWidth,
+ '--jetpack--contact-form--border-right-size': borderWidths?.borderRightWidth,
+ '--jetpack--contact-form--border-top-size': borderWidths?.borderTopWidth,
+ '--jetpack--contact-form--border-bottom-size': borderWidths?.borderBottomWidth,
+ ...styleSpecificCssVars,
+ },
+ };
+ }, [ inputBlockAttributes, mergedGlobalBlockStyles, formStyle, inputBlockName ] );
+}
diff --git a/projects/packages/forms/src/blocks/shared/settings/index.js b/projects/packages/forms/src/blocks/shared/settings/index.js
new file mode 100644
index 000000000000..5aea4e97e4af
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/settings/index.js
@@ -0,0 +1,32 @@
+import transforms from './transforms';
+
+export default {
+ apiVersion: 3,
+ attributes: {
+ id: { type: 'string' },
+ required: {
+ type: 'boolean',
+ default: false,
+ },
+ width: {
+ type: 'number',
+ default: 100,
+ },
+ shareFieldAttributes: {
+ type: 'boolean',
+ default: true,
+ },
+ },
+ category: 'contact-form',
+ providesContext: {
+ 'jetpack/field-required': 'required',
+ 'jetpack/field-share-attributes': 'shareFieldAttributes',
+ },
+ save: () => null,
+ supports: {
+ reusable: false,
+ html: false,
+ __experimentalExposeControlsToChildren: true,
+ },
+ transforms,
+};
diff --git a/projects/packages/forms/src/blocks/shared/settings/transforms.js b/projects/packages/forms/src/blocks/shared/settings/transforms.js
new file mode 100644
index 000000000000..646c8284dc28
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/settings/transforms.js
@@ -0,0 +1,299 @@
+import { createBlock } from '@wordpress/blocks';
+import { __, sprintf } from '@wordpress/i18n';
+
+const textFields = [
+ 'jetpack/field-text',
+ 'jetpack/field-email',
+ 'jetpack/field-name',
+ 'jetpack/field-url',
+ 'jetpack/field-telephone',
+ 'jetpack/field-textarea',
+ 'jetpack/field-number',
+ 'jetpack/field-date',
+];
+
+const choiceFields = [ 'jetpack/field-checkbox-multiple', 'jetpack/field-radio' ];
+const singleOptionFields = [ 'jetpack/field-checkbox', 'jetpack/field-consent' ];
+
+const fieldConfig = {
+ // Text-based fields.
+ 'jetpack/field-text': {
+ type: 'text',
+ label: __( 'Text', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-name': {
+ type: 'text',
+ label: __( 'Name', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-email': {
+ type: 'email',
+ label: __( 'Email', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-url': {
+ type: 'url',
+ label: __( 'Website', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-telephone': {
+ type: 'tel',
+ label: __( 'Phone', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-textarea': {
+ type: 'textarea',
+ label: __( 'Message', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-number': {
+ type: 'number',
+ label: __( 'Number', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-date': {
+ type: 'text',
+ label: __( 'Date', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-select': {
+ type: 'dropdown',
+ label: '',
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ // Choice-based fields.
+ 'jetpack/field-checkbox-multiple': {
+ type: 'checkbox',
+ label: __( 'Choose several options', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ 'jetpack/field-radio': {
+ type: 'radio',
+ label: __( 'Choose one option', 'jetpack-forms' ),
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ },
+ // Single option fields.
+ 'jetpack/field-checkbox': {
+ type: 'checkbox',
+ label: '',
+ defaultLabel: __( 'Add label…', 'jetpack-forms' ),
+ extraAttributes: {
+ isStandalone: true,
+ hideInput: false,
+ },
+ },
+ 'jetpack/field-consent': {
+ type: 'checkbox',
+ label: '',
+ defaultLabel: sprintf(
+ /* translators: placeholder is a type of consent: implicit or explicit */
+ __( 'Add %s consent message…', 'jetpack-forms' ),
+ 'implicit'
+ ),
+ extraAttributes: {
+ isStandalone: true,
+ hideInput: true,
+ },
+ },
+};
+
+const getFieldAttributes = attributes => ( {
+ required: attributes.required || false,
+ width: attributes.width || 100,
+ id: attributes.id || '',
+ shareFieldAttributes: attributes.shareFieldAttributes ?? true,
+} );
+
+const getOptionLabelsFromInnerBlocks = innerBlocks => {
+ const labels = [];
+
+ const optionsBlock = innerBlocks.find( block => block.name === 'jetpack/options' );
+ if ( optionsBlock?.innerBlocks?.length ) {
+ labels.push( ...optionsBlock.innerBlocks.map( block => block.attributes.label ?? '' ) );
+ } else {
+ const optionBlock = innerBlocks.find( block => block.name === 'jetpack/option' );
+ if ( optionBlock ) {
+ labels.push( optionBlock.attributes.label ?? '' );
+ }
+ }
+
+ return labels;
+};
+
+const createTextFieldInnerBlocks = ( blockName, existingInnerBlocks = [] ) => {
+ const config = fieldConfig[ blockName ];
+ if ( ! config ) {
+ return [];
+ }
+
+ const existingLabel = existingInnerBlocks.find( block => block.name === 'jetpack/label' );
+ const existingInput = existingInnerBlocks.find( block => block.name === 'jetpack/input' );
+
+ return [
+ createBlock( 'jetpack/label', {
+ ...( existingLabel?.attributes || {} ),
+ label: config.label,
+ defaultLabel: config.defaultLabel,
+ } ),
+ createBlock( 'jetpack/input', {
+ ...( existingInput?.attributes || {} ),
+ type: config.type,
+ } ),
+ ];
+};
+
+const createChoiceFieldInnerBlocks = ( blockName, existingInnerBlocks = [], attributes = {} ) => {
+ const config = fieldConfig[ blockName ];
+ if ( ! config ) {
+ return [];
+ }
+
+ const existingLabel = existingInnerBlocks.find( block => block.name === 'jetpack/label' );
+ const existingOptions = existingInnerBlocks.find( block => block.name === 'jetpack/options' );
+
+ const label = createBlock( 'jetpack/label', {
+ ...( existingLabel?.attributes || {} ),
+ label: config.label,
+ defaultLabel: config.defaultLabel,
+ } );
+
+ let optionBlocks = [];
+ if ( existingOptions?.innerBlocks ) {
+ optionBlocks = existingOptions.innerBlocks.map( block =>
+ createBlock( 'jetpack/option', {
+ ...block.attributes,
+ } )
+ );
+ } else if ( attributes.options?.length ) {
+ optionBlocks = attributes.options.map( optionLabel =>
+ createBlock( 'jetpack/option', {
+ label: optionLabel,
+ } )
+ );
+ } else {
+ optionBlocks = [ createBlock( 'jetpack/option' ) ];
+ }
+
+ const options = createBlock(
+ 'jetpack/options',
+ {
+ ...( existingOptions?.attributes || {} ),
+ type: config.type,
+ },
+ optionBlocks
+ );
+
+ return [ label, options ];
+};
+
+const createSingleOptionFieldInnerBlocks = ( blockName, existingInnerBlocks = [] ) => {
+ const config = fieldConfig[ blockName ];
+ if ( ! config ) {
+ return [];
+ }
+
+ const existingOption = existingInnerBlocks.find( block => block.name === 'jetpack/option' );
+
+ return [
+ createBlock( 'jetpack/option', {
+ ...( existingOption?.attributes || {} ),
+ label: config.label,
+ defaultLabel: config.defaultLabel,
+ ...config.extraAttributes,
+ } ),
+ ];
+};
+
+const textFieldTransforms = textFields.map( blockName => ( {
+ type: 'block',
+ blocks: [ blockName ],
+ transform: ( attributes, innerBlocks = [] ) => {
+ return createBlock(
+ blockName,
+ getFieldAttributes( attributes ),
+ createTextFieldInnerBlocks( blockName, innerBlocks )
+ );
+ },
+} ) );
+
+const choiceFieldTransforms = choiceFields.map( blockName => ( {
+ type: 'block',
+ blocks: [ blockName ],
+ transform: ( attributes, innerBlocks = [] ) => {
+ return createBlock(
+ blockName,
+ getFieldAttributes( attributes ),
+ createChoiceFieldInnerBlocks( blockName, innerBlocks, attributes )
+ );
+ },
+} ) );
+
+const singleOptionFieldTransforms = singleOptionFields.map( blockName => ( {
+ type: 'block',
+ blocks: [ blockName ],
+ transform: ( attributes, innerBlocks = [] ) => {
+ const fieldAttributes = getFieldAttributes( attributes );
+ return createBlock(
+ blockName,
+ {
+ ...fieldAttributes,
+ required: blockName === 'jetpack/field-consent' ? false : fieldAttributes.required,
+ },
+ createSingleOptionFieldInnerBlocks( blockName, innerBlocks )
+ );
+ },
+} ) );
+
+const transforms = {
+ from: [],
+ to: [
+ ...textFieldTransforms,
+ ...choiceFieldTransforms,
+ ...singleOptionFieldTransforms,
+ {
+ type: 'block',
+ blocks: [ 'jetpack/field-select' ],
+ transform: ( attributes, innerBlocks = [] ) => {
+ const optionLabels = getOptionLabelsFromInnerBlocks( innerBlocks );
+ return createBlock(
+ 'jetpack/field-select',
+ {
+ ...getFieldAttributes( attributes ),
+ options: optionLabels.length ? optionLabels : [ '' ],
+ },
+ createTextFieldInnerBlocks(
+ 'jetpack/field-select',
+ /**
+ * When transforming to a select field, add a default placeholder in a non mutating way
+ * to the input block if none exists.
+ *
+ * This is because the input block is created by the textFieldTransforms,
+ * and we want to avoid mutating the original block.
+ * The select block's edit.js checks for the placeholder attribute on the input block,
+ * and add the `has-placeholder` class to the field if it exists.
+ * However if the incoming transformed block doesn't have a placeholder, the edit.js will
+ * not add the class because it's trying to honor the placeholder attribute,
+ * BUT adds a default placeholder anyway.
+ */
+ innerBlocks.map( block => {
+ if ( block?.name === 'jetpack/input' ) {
+ return {
+ ...block,
+ attributes: {
+ ...( block?.attributes || {} ),
+ placeholder:
+ block?.attributes?.placeholder || __( 'Select one option', 'jetpack-forms' ),
+ },
+ };
+ }
+ return block;
+ } )
+ )
+ );
+ },
+ },
+ ],
+};
+
+export default transforms;
diff --git a/projects/packages/forms/src/blocks/shared/util/caret.js b/projects/packages/forms/src/blocks/shared/util/caret.js
new file mode 100644
index 000000000000..2a7617ace507
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/util/caret.js
@@ -0,0 +1,44 @@
+/**
+ * Get the caret position in an active contenteditable element
+ * From https://gist.github.com/loilo/f873a88631e660c59a1d5ab757ca9b1e
+ *
+ * @param {HTMLElement} target - Contenteditable element of which to get the caret position
+ * @return {number} The caret position
+ */
+export const getCaretPosition = target => {
+ const sel = target.ownerDocument.defaultView.getSelection();
+
+ if ( sel.rangeCount === 0 ) {
+ return 0;
+ }
+
+ const range = sel.getRangeAt( 0 );
+
+ const preCaretRange = range.cloneRange();
+ preCaretRange.selectNodeContents( target );
+ preCaretRange.setEnd( range.endContainer, range.endOffset );
+
+ return preCaretRange.toString().length;
+};
+
+/**
+ * Move the caret position in an active contenteditable element to the end
+ *
+ * @param {HTMLElement} target - Contenteditable element of which to move the caret
+ */
+export const moveCaretToEnd = target => {
+ const doc = target.ownerDocument;
+ if ( 'undefined' === typeof doc ) {
+ return;
+ }
+
+ // Add the contenteditable element to a new selection and collapse it to the end
+ const range = doc.createRange();
+ range.selectNodeContents( target );
+ range.collapse( false );
+
+ // Clear the window selection object and add the new selection
+ const selection = doc.getSelection();
+ selection.removeAllRanges();
+ selection.addRange( range );
+};
diff --git a/projects/packages/forms/src/blocks/shared/util/clean-empty-object.js b/projects/packages/forms/src/blocks/shared/util/clean-empty-object.js
new file mode 100644
index 000000000000..511e207b841a
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/util/clean-empty-object.js
@@ -0,0 +1,18 @@
+/**
+ * Removed falsy values from nested object.
+ *
+ * @param {*} object - Object to be cleaned.
+ * @return {*} Object cleaned from falsy values.
+ */
+const cleanEmptyObject = object => {
+ if ( object === null || typeof object !== 'object' || Array.isArray( object ) ) {
+ return object;
+ }
+
+ const cleanedNestedObjects = Object.entries( object )
+ .map( ( [ key, value ] ) => [ key, cleanEmptyObject( value ) ] )
+ .filter( ( [ , value ] ) => value !== undefined );
+ return ! cleanedNestedObjects.length ? undefined : Object.fromEntries( cleanedNestedObjects );
+};
+
+export default cleanEmptyObject;
diff --git a/projects/packages/forms/src/blocks/shared/util/constants.js b/projects/packages/forms/src/blocks/shared/util/constants.js
new file mode 100644
index 000000000000..abea7c28c74a
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/util/constants.js
@@ -0,0 +1,44 @@
+import { __ } from '@wordpress/i18n';
+
+export const ALLOWED_FORMATS = [ 'core/bold', 'core/italic' ];
+export const ALLOWED_INNER_BLOCKS = [ 'jetpack/label', 'jetpack/input' ];
+
+const currentYear = new Date().getFullYear();
+
+// WARNING: sync data with Contact_Form_Field::render_date_field in class-contact-form-field.php
+export 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`,
+ },
+];
+
+export const DATE_FORMAT_OPTIONS = DATE_FORMATS.map(
+ ( { value, label: optionLabel, example } ) => ( {
+ label: `${ optionLabel } (${ example })`,
+ value,
+ } )
+);
+
+export const FORM_BLOCK_NAME = 'jetpack/contact-form';
+
+export const FORM_STYLE = {
+ ANIMATED: 'animated',
+ BELOW: 'below',
+ DEFAULT: 'default',
+ OUTLINED: 'outlined',
+};
diff --git a/projects/packages/forms/src/blocks/shared/util/deprecate-field-styles.js b/projects/packages/forms/src/blocks/shared/util/deprecate-field-styles.js
new file mode 100644
index 000000000000..d18f78a171ea
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/util/deprecate-field-styles.js
@@ -0,0 +1,59 @@
+import cleanEmptyObject from './clean-empty-object';
+
+const deprecateFieldStyles = attributes => {
+ const {
+ borderColor,
+ borderRadius,
+ borderWidth,
+ fieldBackgroundColor,
+ fieldFontSize,
+ inputColor,
+ labelColor,
+ labelFontSize,
+ labelLineHeight,
+ lineHeight,
+ placeholder,
+ ...restAttributes
+ } = attributes;
+
+ const labelStyles = cleanEmptyObject( {
+ color: { text: labelColor },
+ typography: {
+ fontSize: labelFontSize,
+ lineHeight: labelLineHeight ? `${ labelLineHeight }` : undefined,
+ },
+ } );
+
+ const inputStyles = cleanEmptyObject( {
+ border: {
+ color: borderColor,
+ radius: !! borderRadius || borderRadius === 0 ? `${ borderRadius }px` : undefined,
+ style: 'solid',
+ width: !! borderWidth || borderWidth === 0 ? `${ borderWidth }px` : undefined,
+ },
+ color: {
+ text: inputColor,
+ background: fieldBackgroundColor,
+ },
+ typography: {
+ fontSize: fieldFontSize,
+ lineHeight: lineHeight ? `${ lineHeight }` : undefined,
+ },
+ } );
+
+ // Note: Legacy field blocks reused the input styles for options rather than
+ // label styles despite the underlying `label` element for options.
+ const optionStyles = cleanEmptyObject( {
+ color: {
+ text: inputColor,
+ },
+ typography: {
+ fontSize: fieldFontSize,
+ lineHeight: lineHeight ? `${ lineHeight }` : undefined,
+ },
+ } );
+
+ return { inputStyles, labelStyles, optionStyles, restAttributes };
+};
+
+export default deprecateFieldStyles;
diff --git a/projects/packages/forms/src/blocks/shared/util/get-block-style.js b/projects/packages/forms/src/blocks/shared/util/get-block-style.js
new file mode 100644
index 000000000000..d02050c5e57c
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/util/get-block-style.js
@@ -0,0 +1,6 @@
+const getBlockStyle = className => {
+ const styleClass = className && className.match( /is-style-([^\s]+)/i );
+ return styleClass ? styleClass[ 1 ] : '';
+};
+
+export default getBlockStyle;
diff --git a/projects/packages/forms/src/blocks/shared/util/set-focus.js b/projects/packages/forms/src/blocks/shared/util/set-focus.js
new file mode 100644
index 000000000000..584c12ce9964
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/util/set-focus.js
@@ -0,0 +1,22 @@
+export default function setFocus( wrapper, selector, index, cursorToEnd ) {
+ setTimeout( () => {
+ const input = wrapper.querySelectorAll( selector )[ index ];
+
+ if ( ! input ) {
+ return;
+ }
+
+ input.focus();
+
+ // Allows moving the cursor to the end of
+ // 'contenteditable' elements like
+ if ( document.createRange && cursorToEnd ) {
+ const range = document.createRange();
+ range.selectNodeContents( input );
+ range.collapse( false );
+ const selection = document.defaultView.getSelection();
+ selection.removeAllRanges();
+ selection.addRange( range );
+ }
+ }, 0 );
+}
diff --git a/projects/packages/forms/src/blocks/shared/util/test/deprecate-field-styles.test.js b/projects/packages/forms/src/blocks/shared/util/test/deprecate-field-styles.test.js
new file mode 100644
index 000000000000..74280e8af7d7
--- /dev/null
+++ b/projects/packages/forms/src/blocks/shared/util/test/deprecate-field-styles.test.js
@@ -0,0 +1,95 @@
+import deprecateFieldStyles from '../deprecate-field-styles';
+
+describe( 'deprecateFieldStyles', () => {
+ it( 'should deprecate input styles', () => {
+ const attributes = {
+ borderColor: '#000000',
+ borderRadius: 5,
+ borderWidth: 2,
+ fieldBackgroundColor: '#ffffff',
+ fieldFontSize: '16px',
+ inputColor: '#333333',
+ lineHeight: 1.5,
+ };
+
+ const result = deprecateFieldStyles( attributes );
+
+ expect( result.inputStyles ).toEqual( {
+ border: {
+ color: '#000000',
+ radius: '5px',
+ style: 'solid',
+ width: '2px',
+ },
+ color: {
+ text: '#333333',
+ background: '#ffffff',
+ },
+ typography: {
+ fontSize: '16px',
+ lineHeight: '1.5',
+ },
+ } );
+ } );
+
+ it( 'should deprecate label styles', () => {
+ const attributes = {
+ labelColor: '#666666',
+ labelFontSize: '14px',
+ labelLineHeight: 1.2,
+ };
+
+ const result = deprecateFieldStyles( attributes );
+
+ expect( result.labelStyles ).toEqual( {
+ color: {
+ text: '#666666',
+ },
+ typography: {
+ fontSize: '14px',
+ lineHeight: '1.2',
+ },
+ } );
+ } );
+
+ it( 'should deprecate option styles', () => {
+ const attributes = {
+ inputColor: '#333333',
+ fieldFontSize: '16px',
+ lineHeight: 1.5,
+ };
+
+ const result = deprecateFieldStyles( attributes );
+
+ expect( result.optionStyles ).toEqual( {
+ color: {
+ text: '#333333',
+ },
+ typography: {
+ fontSize: '16px',
+ lineHeight: '1.5',
+ },
+ } );
+ } );
+
+ it( 'should handle empty or undefined values', () => {
+ const attributes = {
+ borderRadius: 0,
+ borderWidth: 0,
+ placeholder: 'Enter text',
+ };
+
+ const result = deprecateFieldStyles( attributes );
+
+ expect( result.inputStyles ).toEqual( {
+ border: {
+ radius: '0px',
+ style: 'solid',
+ width: '0px',
+ },
+ } );
+ expect( result.labelStyles ).toBeUndefined();
+ expect( result.optionStyles ).toBeUndefined();
+ expect( result.restAttributes ).toEqual( {} );
+ } );
+} );
diff --git a/projects/packages/forms/src/class-jetpack-forms.php b/projects/packages/forms/src/class-jetpack-forms.php
index 902fd4ad4885..6d441336bde0 100644
--- a/projects/packages/forms/src/class-jetpack-forms.php
+++ b/projects/packages/forms/src/class-jetpack-forms.php
@@ -35,6 +35,9 @@ public static function load_contact_form() {
// Add hook to delete file attachments when a feedback post is deleted
add_action( 'before_delete_post', array( '\Automattic\Jetpack\Forms\ContactForm\Contact_Form', 'delete_feedback_files' ) );
+
+ // Enforces the availability of block support controls in the UI for classic themes.
+ add_filter( 'wp_theme_json_data_default', array( '\Automattic\Jetpack\Forms\ContactForm\Contact_Form', 'add_theme_json_data_for_classic_themes' ) );
}
/**
diff --git a/projects/packages/forms/src/contact-form/class-contact-form-field.php b/projects/packages/forms/src/contact-form/class-contact-form-field.php
index 8e58f2297fb1..a018a1d67925 100644
--- a/projects/packages/forms/src/contact-form/class-contact-form-field.php
+++ b/projects/packages/forms/src/contact-form/class-contact-form-field.php
@@ -8,6 +8,7 @@
namespace Automattic\Jetpack\Forms\ContactForm;
use Automattic\Jetpack\Assets;
+use Automattic\Jetpack\Forms\Jetpack_Forms;
/**
* Class for the contact-field shortcode.
@@ -51,6 +52,13 @@ class Contact_Form_Field extends Contact_Form_Shortcode {
*/
public $block_styles = '';
+ /**
+ * Classes to be applied to the field
+ *
+ * @var string
+ */
+ public $field_classes = '';
+
/**
* Styles to be applied to the field
*
@@ -58,6 +66,13 @@ class Contact_Form_Field extends Contact_Form_Shortcode {
*/
public $field_styles = '';
+ /**
+ * Classes to be applied to the field option
+ *
+ * @var string
+ */
+ public $option_classes = '';
+
/**
* Styles to be applied to the field option
*
@@ -65,6 +80,13 @@ class Contact_Form_Field extends Contact_Form_Shortcode {
*/
public $option_styles = '';
+ /**
+ * Classes to be applied to the field
+ *
+ * @var string
+ */
+ public $label_classes = '';
+
/**
* Styles to be applied to the field
*
@@ -82,40 +104,53 @@ class Contact_Form_Field extends Contact_Form_Shortcode {
public function __construct( $attributes, $content = null, $form = null ) {
$attributes = shortcode_atts(
array(
- 'label' => null,
- 'togglelabel' => null,
- 'type' => 'text',
- 'required' => false,
- 'requiredtext' => null,
- 'options' => array(),
- 'id' => null,
- 'style' => null,
- 'fieldbackgroundcolor' => null,
- 'buttonbackgroundcolor' => null,
- 'buttonborderradius' => null,
- 'buttonborderwidth' => null,
- 'textcolor' => null,
- 'default' => null,
- 'values' => null,
- 'placeholder' => null,
- 'class' => null,
- 'width' => null,
- 'consenttype' => null,
- 'dateformat' => null,
- 'implicitconsentmessage' => null,
- 'explicitconsentmessage' => null,
- 'borderradius' => null,
- 'borderwidth' => null,
- 'lineheight' => null,
- 'labellineheight' => null,
- 'bordercolor' => null,
- 'inputcolor' => null,
- 'labelcolor' => null,
- 'labelfontsize' => null,
- 'fieldfontsize' => null,
- 'min' => null,
- 'max' => null,
- 'maxfiles' => null,
+ 'label' => null,
+ 'togglelabel' => null,
+ 'type' => 'text',
+ 'required' => false,
+ 'requiredtext' => null,
+ 'options' => array(),
+ 'optionsdata' => array(),
+ 'id' => null,
+ 'style' => null,
+ 'fieldbackgroundcolor' => null,
+ 'buttonbackgroundcolor' => null,
+ 'buttonborderradius' => null,
+ 'buttonborderwidth' => null,
+ 'textcolor' => null,
+ 'default' => null,
+ 'values' => null,
+ 'placeholder' => null,
+ 'class' => null,
+ 'width' => null,
+ 'consenttype' => null,
+ 'dateformat' => null,
+ 'implicitconsentmessage' => null,
+ 'explicitconsentmessage' => null,
+ 'borderradius' => null,
+ 'borderwidth' => null,
+ 'lineheight' => null,
+ 'labellineheight' => null,
+ 'bordercolor' => null,
+ 'inputcolor' => null,
+ 'labelcolor' => null,
+ 'labelfontsize' => null,
+ 'fieldfontsize' => null,
+ 'labelclasses' => null,
+ 'labelstyles' => null,
+ 'inputclasses' => null,
+ 'inputstyles' => null,
+ 'optionclasses' => null,
+ 'optionstyles' => null,
+ 'min' => null,
+ 'max' => null,
+ 'maxfiles' => null,
+ 'fieldwrapperclasses' => null,
+ 'stylevariationattributes' => array(),
+ 'stylevariationclasses' => null,
+ 'stylevariationstyles' => null,
+ 'optionsclasses' => null,
+ 'optionsstyles' => null,
),
$attributes,
'contact-field'
@@ -146,6 +181,10 @@ public function __construct( $attributes, $content = null, $form = null ) {
}
}
+ if ( ! empty( $attributes['optionsdata'] ) ) {
+ $attributes['optionsdata'] = json_decode( html_entity_decode( $attributes['optionsdata'], ENT_COMPAT ), true );
+ }
+
if ( $form ) {
// make a unique field ID based on the label, with an incrementing number if needed to avoid clashes
$form_id = $form->get_attribute( 'id' );
@@ -307,33 +346,101 @@ public function render() {
$field_width = $this->get_attribute( 'width' );
$class = 'date' === $field_type ? 'jp-contact-form-date' : $this->get_attribute( 'class' );
- if ( is_numeric( $this->get_attribute( 'borderradius' ) ) ) {
- $this->block_styles .= '--jetpack--contact-form--border-radius: ' . esc_attr( $this->get_attribute( 'borderradius' ) ) . 'px;';
- $this->field_styles .= 'border-radius: ' . (int) $this->get_attribute( 'borderradius' ) . 'px;';
- }
- if ( is_numeric( $this->get_attribute( 'borderwidth' ) ) ) {
- $this->block_styles .= '--jetpack--contact-form--border-size: ' . esc_attr( $this->get_attribute( 'borderwidth' ) ) . 'px;';
- $this->field_styles .= 'border-width: ' . (int) $this->get_attribute( 'borderwidth' ) . 'px;';
- }
- if ( is_numeric( $this->get_attribute( 'lineheight' ) ) ) {
- $this->block_styles .= '--jetpack--contact-form--line-height: ' . esc_attr( $this->get_attribute( 'lineheight' ) ) . ';';
- $this->field_styles .= 'line-height: ' . (int) $this->get_attribute( 'lineheight' ) . ';';
- $this->option_styles .= 'line-height: ' . (int) $this->get_attribute( 'lineheight' ) . ';';
- }
- if ( ! empty( $this->get_attribute( 'bordercolor' ) ) ) {
- $this->block_styles .= '--jetpack--contact-form--border-color: ' . esc_attr( $this->get_attribute( 'bordercolor' ) ) . ';';
- $this->field_styles .= 'border-color: ' . esc_attr( $this->get_attribute( 'bordercolor' ) ) . ';';
- }
- if ( ! empty( $this->get_attribute( 'inputcolor' ) ) ) {
- $this->block_styles .= '--jetpack--contact-form--text-color: ' . esc_attr( $this->get_attribute( 'inputcolor' ) ) . ';';
- $this->block_styles .= '--jetpack--contact-form--button-outline--text-color: ' . esc_attr( $this->get_attribute( 'inputcolor' ) ) . ';';
- $this->field_styles .= 'color: ' . esc_attr( $this->get_attribute( 'inputcolor' ) ) . ';';
- $this->option_styles .= 'color: ' . esc_attr( $this->get_attribute( 'inputcolor' ) ) . ';';
- }
- if ( ! empty( $this->get_attribute( 'fieldbackgroundcolor' ) ) ) {
- $this->block_styles .= '--jetpack--contact-form--input-background: ' . esc_attr( $this->get_attribute( 'fieldbackgroundcolor' ) ) . ';';
- $this->field_styles .= 'background-color: ' . esc_attr( $this->get_attribute( 'fieldbackgroundcolor' ) ) . ';';
+ $label_classes = $this->get_attribute( 'labelclasses' );
+ $label_styles = $this->get_attribute( 'labelstyles' );
+ $input_classes = $this->get_attribute( 'inputclasses' );
+ $input_styles = $this->get_attribute( 'inputstyles' );
+ $option_classes = $this->get_attribute( 'optionclasses' );
+ $option_styles = $this->get_attribute( 'optionstyles' );
+
+ $has_block_support_styles = ! empty( $label_classes ) || ! empty( $label_styles ) || ! empty( $input_classes ) || ! empty( $input_styles ) || ! empty( $option_classes ) || ! empty( $option_styles );
+
+ if ( $has_block_support_styles ) {
+ // Do any of the block support classes need to be applied at the field wrapper level? Do we need to make the classes etc filterable as per the field classes?
+
+ // Classes.
+ if ( ! empty( $label_classes ) ) {
+ $this->label_classes .= esc_attr( $label_classes );
+ }
+ if ( ! empty( $input_classes ) ) {
+ $class .= $class ? ' ' . esc_attr( $input_classes ) : esc_attr( $input_classes );
+ $this->field_classes = $input_classes;
+ }
+ if ( ! empty( $option_classes ) ) {
+ $class .= $class ? ' ' . esc_attr( $option_classes ) : esc_attr( $option_classes );
+ $this->option_classes = $option_classes;
+ }
+
+ // Styles.
+ if ( ! empty( $label_styles ) ) {
+ $this->label_styles .= esc_attr( $label_styles );
+ }
+ if ( ! empty( $input_styles ) ) {
+ $this->field_styles .= esc_attr( $input_styles );
+ }
+ if ( ! empty( $option_styles ) ) {
+ $this->option_styles .= esc_attr( $option_styles );
+ }
+
+ // For Outline style support.
+ $form_style = $this->get_form_style();
+ if ( 'outlined' === $form_style || 'animated' === $form_style ) {
+ $output_data = $this->get_form_variation_style_properties( $form_style );
+ $this->block_styles .= esc_attr( $output_data['css_vars'] );
+ }
+ } else {
+ if ( is_numeric( $this->get_attribute( 'borderradius' ) ) ) {
+ $this->block_styles .= '--jetpack--contact-form--border-radius: ' . esc_attr( $this->get_attribute( 'borderradius' ) ) . 'px;';
+ $this->field_styles .= 'border-radius: ' . (int) $this->get_attribute( 'borderradius' ) . 'px;';
+ }
+
+ if ( is_numeric( $this->get_attribute( 'borderwidth' ) ) ) {
+ $this->block_styles .= '--jetpack--contact-form--border-size: ' . esc_attr( $this->get_attribute( 'borderwidth' ) ) . 'px;';
+ $this->field_styles .= 'border-width: ' . (int) $this->get_attribute( 'borderwidth' ) . 'px;';
+ }
+
+ if ( is_numeric( $this->get_attribute( 'lineheight' ) ) ) {
+ $this->block_styles .= '--jetpack--contact-form--line-height: ' . esc_attr( $this->get_attribute( 'lineheight' ) ) . ';';
+ $this->field_styles .= 'line-height: ' . (int) $this->get_attribute( 'lineheight' ) . ';';
+ $this->option_styles .= 'line-height: ' . (int) $this->get_attribute( 'lineheight' ) . ';';
+ }
+
+ if ( ! empty( $this->get_attribute( 'bordercolor' ) ) ) {
+ $this->block_styles .= '--jetpack--contact-form--border-color: ' . esc_attr( $this->get_attribute( 'bordercolor' ) ) . ';';
+ $this->field_styles .= 'border-color: ' . esc_attr( $this->get_attribute( 'bordercolor' ) ) . ';';
+ }
+
+ if ( ! empty( $this->get_attribute( 'inputcolor' ) ) ) {
+ $this->block_styles .= '--jetpack--contact-form--text-color: ' . esc_attr( $this->get_attribute( 'inputcolor' ) ) . ';';
+ $this->block_styles .= '--jetpack--contact-form--button-outline--text-color: ' . esc_attr( $this->get_attribute( 'inputcolor' ) ) . ';';
+ $this->field_styles .= 'color: ' . esc_attr( $this->get_attribute( 'inputcolor' ) ) . ';';
+ $this->option_styles .= 'color: ' . esc_attr( $this->get_attribute( 'inputcolor' ) ) . ';';
+ }
+
+ if ( ! empty( $this->get_attribute( 'fieldbackgroundcolor' ) ) ) {
+ $this->block_styles .= '--jetpack--contact-form--input-background: ' . esc_attr( $this->get_attribute( 'fieldbackgroundcolor' ) ) . ';';
+ $this->field_styles .= 'background-color: ' . esc_attr( $this->get_attribute( 'fieldbackgroundcolor' ) ) . ';';
+ }
+
+ if ( ! empty( $this->get_attribute( 'fieldfontsize' ) ) ) {
+ $this->block_styles .= '--jetpack--contact-form--font-size: ' . esc_attr( $this->get_attribute( 'fieldfontsize' ) ) . ';';
+ $this->field_styles .= 'font-size: ' . esc_attr( $this->get_attribute( 'fieldfontsize' ) ) . ';';
+ $this->option_styles .= 'font-size: ' . esc_attr( $this->get_attribute( 'fieldfontsize' ) ) . ';';
+ }
+
+ if ( ! empty( $this->get_attribute( 'labelcolor' ) ) ) {
+ $this->label_styles .= 'color: ' . esc_attr( $this->get_attribute( 'labelcolor' ) ) . ';';
+ }
+
+ if ( ! empty( $this->get_attribute( 'labelfontsize' ) ) ) {
+ $this->label_styles .= 'font-size: ' . esc_attr( $this->get_attribute( 'labelfontsize' ) ) . ';';
+ }
+
+ if ( is_numeric( $this->get_attribute( 'labellineheight' ) ) ) {
+ $this->label_styles .= 'line-height: ' . (int) $this->get_attribute( 'labellineheight' ) . ';';
+ }
}
+
if ( ! empty( $this->get_attribute( 'buttonbackgroundcolor' ) ) ) {
$this->block_styles .= '--jetpack--contact-form--button-outline--background-color: ' . esc_attr( $this->get_attribute( 'buttonbackgroundcolor' ) ) . ';';
}
@@ -344,21 +451,6 @@ public function render() {
$this->block_styles .= '--jetpack--contact-form--button-outline--border-size: ' . esc_attr( $this->get_attribute( 'buttonborderwidth' ) ) . 'px;';
}
- if ( ! empty( $this->get_attribute( 'fieldfontsize' ) ) ) {
- $this->block_styles .= '--jetpack--contact-form--font-size: ' . esc_attr( $this->get_attribute( 'fieldfontsize' ) ) . ';';
- $this->field_styles .= 'font-size: ' . esc_attr( $this->get_attribute( 'fieldfontsize' ) ) . ';';
- $this->option_styles .= 'font-size: ' . esc_attr( $this->get_attribute( 'fieldfontsize' ) ) . ';';
- }
-
- if ( ! empty( $this->get_attribute( 'labelcolor' ) ) ) {
- $this->label_styles .= 'color: ' . esc_attr( $this->get_attribute( 'labelcolor' ) ) . ';';
- }
- if ( ! empty( $this->get_attribute( 'labelfontsize' ) ) ) {
- $this->label_styles .= '--jetpack--contact-form--label--font-size:' . esc_attr( $this->get_attribute( 'labelfontsize' ) ) . ';';
- }
- if ( is_numeric( $this->get_attribute( 'labellineheight' ) ) ) {
- $this->label_styles .= 'line-height: ' . (int) $this->get_attribute( 'labellineheight' ) . ';';
- }
if ( ! empty( $field_width ) && ! $this->has_inset_label() ) {
$class .= ' grunion-field-width-' . $field_width;
@@ -482,25 +574,43 @@ public function get_computed_field_value( $field_type, $field_id ) {
public function render_label( $type, $id, $label, $required, $required_field_text, $extra_attrs = array(), $always_render = false ) {
$form_style = $this->get_form_style();
- if ( ! empty( $form_style ) && $form_style !== 'default' && ! $always_render ) {
- return '';
+ if ( ! empty( $form_style ) && $form_style !== 'default' ) {
+ if ( ! in_array( $type, array( 'checkbox', 'checkbox-multiple', 'radio', 'consent', 'file' ), true ) ) {
+ switch ( $form_style ) {
+ case 'outlined':
+ return $this->render_outline_label( $id, $label, $required, $required_field_text );
+ case 'animated':
+ return $this->render_animated_label( $id, $label, $required, $required_field_text );
+ case 'below':
+ return $this->render_below_label( $id, $label, $required, $required_field_text );
+ }
+ }
+
+ if ( ! $always_render ) {
+ return '';
+ }
}
if ( ! empty( $this->label_styles ) ) {
$extra_attrs['style'] = $this->label_styles;
}
+ $type_class = $type ? ' ' . $type : '';
+ $extra_attrs['class'] = "grunion-field-label{$type_class}" . ( $this->is_error() ? ' form-error' : '' );
+
+ if ( ! empty( $this->label_classes ) ) {
+ $extra_attrs['class'] .= ' ' . $this->label_classes;
+ }
+
$extra_attrs_string = '';
- if ( is_array( $extra_attrs ) && ! empty( $extra_attrs ) ) {
- foreach ( $extra_attrs as $attr => $val ) {
- $extra_attrs_string .= sprintf( '%s="%s" ', esc_attr( $attr ), esc_attr( $val ) );
- }
+
+ foreach ( $extra_attrs as $attr => $val ) {
+ $extra_attrs_string .= sprintf( '%s="%s" ', esc_attr( $attr ), esc_attr( $val ) );
}
$type_class = $type ? ' ' . $type : '';
return "