Skip to content

Commit

Permalink
Allow using groups and columns inside the experimental form block (#5…
Browse files Browse the repository at this point in the history
…5758)

* Allow using groups and columns inside forms

* Prevent nesting forms

* Move the hook inside our init call
  • Loading branch information
aristath authored Nov 2, 2023
1 parent f72677e commit 5d6c304
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
3 changes: 0 additions & 3 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ The basic building block for forms. ([Source](https://github.com/WordPress/guten
- **Name:** core/form-input
- **Experimental:** true
- **Category:** common
- **Parent:** core/form
- **Supports:** anchor, spacing (margin), ~~reusable~~
- **Attributes:** inlineLabel, label, name, placeholder, required, type, value, visibilityPermissions

Expand All @@ -305,7 +304,6 @@ Provide a notification message after the form has been submitted. ([Source](http
- **Name:** core/form-submission-notification
- **Experimental:** true
- **Category:** common
- **Parent:** core/form
- **Supports:**
- **Attributes:** type

Expand All @@ -316,7 +314,6 @@ A submission button for forms. ([Source](https://github.com/WordPress/gutenberg/
- **Name:** core/form-submit-button
- **Experimental:** true
- **Category:** common
- **Parent:** core/form
- **Supports:**
- **Attributes:**

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/form-input/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "core/form-input",
"title": "Input Field",
"category": "common",
"parent": [ "core/form" ],
"ancestor": [ "core/form" ],
"description": "The basic building block for forms.",
"keywords": [ "input", "form" ],
"textdomain": "default",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "core/form-submission-notification",
"title": "Form Submission Notification",
"category": "common",
"parent": [ "core/form" ],
"ancestor": [ "core/form" ],
"description": "Provide a notification message after the form has been submitted.",
"keywords": [ "form", "feedback", "notification", "message" ],
"textdomain": "default",
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/form-submit-button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"title": "Form Submit Button",
"category": "common",
"icon": "button",
"parent": [ "core/form" ],
"ancestor": [ "core/form" ],
"description": "A submission button for forms.",
"keywords": [ "submit", "button", "form" ],
"textdomain": "default",
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/form/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const ALLOWED_BLOCKS = [
'core/form-input',
'core/form-submit-button',
'core/form-submission-notification',
'core/group',
'core/columns',
];

const TEMPLATE = [
Expand Down
39 changes: 38 additions & 1 deletion packages/block-library/src/form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import metadata from './block.json';
import save from './save';
import variations from './variations';

/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';

const { name } = metadata;

export { metadata, name };
Expand All @@ -17,4 +22,36 @@ export const settings = {
variations,
};

export const init = () => initBlock( { name, metadata, settings } );
export const init = () => {
// Prevent adding forms inside forms.
const DISALLOWED_PARENTS = [ 'core/form' ];
addFilter(
'blockEditor.__unstableCanInsertBlockType',
'removeTemplatePartsFromPostTemplates',
(
canInsert,
blockType,
rootClientId,
{ getBlock, getBlockParentsByBlockName }
) => {
if ( blockType.name !== 'core/form' ) {
return canInsert;
}

for ( const disallowedParentType of DISALLOWED_PARENTS ) {
const hasDisallowedParent =
getBlock( rootClientId )?.name === disallowedParentType ||
getBlockParentsByBlockName(
rootClientId,
disallowedParentType
).length;
if ( hasDisallowedParent ) {
return false;
}
}
return true;
}
);

return initBlock( { name, metadata, settings } );
};

1 comment on commit 5d6c304

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 5d6c304.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6730414863
📝 Reported issues:

Please sign in to comment.