Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input Field Block: Use useblockProps hook in save function #56507

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions packages/block-library/src/form-input/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* External dependencies
*/
import classNames from 'classnames';
import removeAccents from 'remove-accents';

/**
* WordPress dependencies
*/
import {
RichText,
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
} from '@wordpress/block-editor';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';

const getNameFromLabelV1 = ( content ) => {
return (
removeAccents( stripHTML( content ) )
// Convert anything that's not a letter or number to a hyphen.
.replace( /[^\p{L}\p{N}]+/gu, '-' )
// Convert to lowercase
.toLowerCase()
// Remove any remaining leading or trailing hyphens.
.replace( /(^-+)|(-+$)/g, '' )
);
};

// Version without wrapper div in saved markup
// See: https://github.com/WordPress/gutenberg/pull/56507
const v1 = {
attributes: {
type: {
type: 'string',
default: 'text',
},
name: {
type: 'string',
},
label: {
type: 'string',
default: 'Label',
selector: '.wp-block-form-input__label-content',
source: 'html',
__experimentalRole: 'content',
},
inlineLabel: {
type: 'boolean',
default: false,
},
required: {
type: 'boolean',
default: false,
selector: '.wp-block-form-input__input',
source: 'attribute',
attribute: 'required',
},
placeholder: {
type: 'string',
selector: '.wp-block-form-input__input',
source: 'attribute',
attribute: 'placeholder',
__experimentalRole: 'content',
},
value: {
type: 'string',
default: '',
selector: 'input',
source: 'attribute',
attribute: 'value',
},
visibilityPermissions: {
type: 'string',
default: 'all',
},
},
supports: {
className: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

className support wasn't present in the v1 block, but deprecation didn't work properly unless I explicitly added it. This is probably because the v1 block did not use a hook in the save() function, meaning the block's classname itself was not output.

anchor: true,
reusable: false,
spacing: {
margin: [ 'top', 'bottom' ],
},
__experimentalBorder: {
radius: true,
__experimentalSkipSerialization: true,
__experimentalDefaultControls: {
radius: true,
},
},
},
save( { attributes } ) {
const { type, name, label, inlineLabel, required, placeholder, value } =
attributes;

const borderProps = getBorderClassesAndStyles( attributes );
const colorProps = getColorClassesAndStyles( attributes );

const inputStyle = {
...borderProps.style,
...colorProps.style,
};

const inputClasses = classNames(
'wp-block-form-input__input',
colorProps.className,
borderProps.className
);
const TagName = type === 'textarea' ? 'textarea' : 'input';

if ( 'hidden' === type ) {
return <input type={ type } name={ name } value={ value } />;
}

/* eslint-disable jsx-a11y/label-has-associated-control */
return (
<label
className={ classNames( 'wp-block-form-input__label', {
'is-label-inline': inlineLabel,
} ) }
>
<span className="wp-block-form-input__label-content">
<RichText.Content value={ label } />
</span>
<TagName
className={ inputClasses }
type={ 'textarea' === type ? undefined : type }
name={ name || getNameFromLabelV1( label ) }
required={ required }
aria-required={ required }
placeholder={ placeholder || undefined }
style={ inputStyle }
/>
</label>
);
/* eslint-enable jsx-a11y/label-has-associated-control */
},
};

const deprecated = [ v1 ];

export default deprecated;
2 changes: 1 addition & 1 deletion packages/block-library/src/form-input/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function InputFieldBlock( { attributes, setAttributes, className } ) {
</PanelBody>
</InspectorControls>
) }
<InspectorControls __experimentalGroup="advanced">
<InspectorControls group="advanced">
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a small fix that updates a deprecated prop.

<TextControl
autoComplete="off"
label={ __( 'Name' ) }
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/form-input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Internal dependencies
*/
import initBlock from '../utils/init-block';
import deprecated from './deprecated';
import edit from './edit';
import metadata from './block.json';
import save from './save';
Expand All @@ -12,6 +13,7 @@ const { name } = metadata;
export { metadata, name };

export const settings = {
deprecated,
edit,
save,
variations,
Expand Down
45 changes: 25 additions & 20 deletions packages/block-library/src/form-input/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import removeAccents from 'remove-accents';
*/
import {
RichText,
useBlockProps,
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
} from '@wordpress/block-editor';
Expand Down Expand Up @@ -52,30 +53,34 @@ export default function save( { attributes } ) {
);
const TagName = type === 'textarea' ? 'textarea' : 'input';

const blockProps = useBlockProps.save();

if ( 'hidden' === type ) {
return <input type={ type } name={ name } value={ value } />;
}

/* eslint-disable jsx-a11y/label-has-associated-control */
return (
<label
className={ classNames( 'wp-block-form-input__label', {
'is-label-inline': inlineLabel,
} ) }
>
<span className="wp-block-form-input__label-content">
<RichText.Content value={ label } />
</span>
<TagName
className={ inputClasses }
type={ 'textarea' === type ? undefined : type }
name={ name || getNameFromLabel( label ) }
required={ required }
aria-required={ required }
placeholder={ placeholder || undefined }
style={ inputStyle }
/>
</label>
<div { ...blockProps }>
{ /* eslint-disable jsx-a11y/label-has-associated-control */ }
<label
className={ classNames( 'wp-block-form-input__label', {
'is-label-inline': inlineLabel,
} ) }
>
<span className="wp-block-form-input__label-content">
<RichText.Content value={ label } />
</span>
<TagName
className={ inputClasses }
type={ 'textarea' === type ? undefined : type }
name={ name || getNameFromLabel( label ) }
required={ required }
aria-required={ required }
placeholder={ placeholder || undefined }
style={ inputStyle }
/>
</label>
{ /* eslint-enable jsx-a11y/label-has-associated-control */ }
</div>
);
/* eslint-enable jsx-a11y/label-has-associated-control */
}
2 changes: 1 addition & 1 deletion test/integration/fixtures/blocks/core__form-input.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:form-input -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Label</span><input class="wp-block-form-input__input" type="text" name="label" aria-required="false"/></label>
<div class="wp-block-form-input"><label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Label</span><input class="wp-block-form-input__input" type="text" name="label" aria-required="false"/></label></div>
<!-- /wp:form-input -->
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions test/integration/fixtures/blocks/core__form-input.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"blockName": "core/form-input",
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Label</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"label\" aria-required=\"false\"/></label>\n",
"innerHTML": "\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Label</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"label\" aria-required=\"false\"/></label></div>\n",
"innerContent": [
"\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Label</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"label\" aria-required=\"false\"/></label>\n"
"\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Label</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"label\" aria-required=\"false\"/></label></div>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:form-input -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Label</span><input class="wp-block-form-input__input" type="text" name="label" aria-required="false"/></label>
<div class="wp-block-form-input"><label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Label</span><input class="wp-block-form-input__input" type="text" name="label" aria-required="false"/></label></div>
<!-- /wp:form-input -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- wp:form-input -->
<label class="wp-block-form-input__label">
<span class="wp-block-form-input__label-content">Label</span>
<input class="wp-block-form-input__input" type="text" name="label" aria-required="false"/>
</label>
<!-- /wp:form-input -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"name": "core/form-input",
"isValid": true,
"attributes": {
"type": "text",
"label": "Label",
"inlineLabel": false,
"required": false,
"value": "",
"visibilityPermissions": "all"
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"blockName": "core/form-input",
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n<label class=\"wp-block-form-input__label\">\n\t<span class=\"wp-block-form-input__label-content\">Label</span>\n\t<input class=\"wp-block-form-input__input\" type=\"text\" name=\"label\" aria-required=\"false\"/>\n</label>\n",
"innerContent": [
"\n<label class=\"wp-block-form-input__label\">\n\t<span class=\"wp-block-form-input__label-content\">Label</span>\n\t<input class=\"wp-block-form-input__input\" type=\"text\" name=\"label\" aria-required=\"false\"/>\n</label>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:form-input -->
<div class="wp-block-form-input"><label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Label</span><input class="wp-block-form-input__input" type="text" name="label" aria-required="false"/></label></div>
<!-- /wp:form-input -->
16 changes: 8 additions & 8 deletions test/integration/fixtures/blocks/core__form.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<!-- wp:form -->
<form class="wp-block-form" enctype="text/plain">
<!-- wp:form-input {"label":"Name","required":true} -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Name</span><input class="wp-block-form-input__input" type="text" name="name" required aria-required="true"/></label>
<!-- wp:form-input -->
<div class="wp-block-form-input"><label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Name</span><input class="wp-block-form-input__input" type="text" name="name" required aria-required="true"/></label></div>
<!-- /wp:form-input -->

<!-- wp:form-input {"type":"email","label":"Email","required":true} -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Email</span><input class="wp-block-form-input__input" type="email" name="email" required aria-required="true"/></label>
<!-- wp:form-input {"type":"email"} -->
<div class="wp-block-form-input"><label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Email</span><input class="wp-block-form-input__input" type="email" name="email" required aria-required="true"/></label></div>
<!-- /wp:form-input -->

<!-- wp:form-input {"type":"url","label":"Website"} -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Website</span><input class="wp-block-form-input__input" type="url" name="website" aria-required="false"/></label>
<!-- wp:form-input {"type":"url"} -->
<div class="wp-block-form-input"><label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Website</span><input class="wp-block-form-input__input" type="url" name="website" aria-required="false"/></label></div>
<!-- /wp:form-input -->

<!-- wp:form-input {"type":"textarea","label":"Comment","required":true} -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Comment</span><textarea class="wp-block-form-input__input" name="comment" required aria-required="true"></textarea></label>
<!-- wp:form-input {"type":"textarea"} -->
<div class="wp-block-form-input"><label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Comment</span><textarea class="wp-block-form-input__input" name="comment" required aria-required="true"></textarea></label></div>
<!-- /wp:form-input -->

<!-- wp:form-submit-button -->
Expand Down
32 changes: 12 additions & 20 deletions test/integration/fixtures/blocks/core__form.parsed.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,44 @@
"innerBlocks": [
{
"blockName": "core/form-input",
"attrs": {
"label": "Name",
"required": true
},
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Name</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"name\" required aria-required=\"true\"/></label>\n",
"innerHTML": "\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Name</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"name\" required aria-required=\"true\"/></label></div>\n",
"innerContent": [
"\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Name</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"name\" required aria-required=\"true\"/></label>\n"
"\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Name</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"name\" required aria-required=\"true\"/></label></div>\n"
]
},
{
"blockName": "core/form-input",
"attrs": {
"type": "email",
"label": "Email",
"required": true
"type": "email"
},
"innerBlocks": [],
"innerHTML": "\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Email</span><input class=\"wp-block-form-input__input\" type=\"email\" name=\"email\" required aria-required=\"true\"/></label>\n",
"innerHTML": "\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Email</span><input class=\"wp-block-form-input__input\" type=\"email\" name=\"email\" required aria-required=\"true\"/></label></div>\n",
"innerContent": [
"\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Email</span><input class=\"wp-block-form-input__input\" type=\"email\" name=\"email\" required aria-required=\"true\"/></label>\n"
"\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Email</span><input class=\"wp-block-form-input__input\" type=\"email\" name=\"email\" required aria-required=\"true\"/></label></div>\n"
]
},
{
"blockName": "core/form-input",
"attrs": {
"type": "url",
"label": "Website"
"type": "url"
},
"innerBlocks": [],
"innerHTML": "\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Website</span><input class=\"wp-block-form-input__input\" type=\"url\" name=\"website\" aria-required=\"false\"/></label>\n",
"innerHTML": "\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Website</span><input class=\"wp-block-form-input__input\" type=\"url\" name=\"website\" aria-required=\"false\"/></label></div>\n",
"innerContent": [
"\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Website</span><input class=\"wp-block-form-input__input\" type=\"url\" name=\"website\" aria-required=\"false\"/></label>\n"
"\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Website</span><input class=\"wp-block-form-input__input\" type=\"url\" name=\"website\" aria-required=\"false\"/></label></div>\n"
]
},
{
"blockName": "core/form-input",
"attrs": {
"type": "textarea",
"label": "Comment",
"required": true
"type": "textarea"
},
"innerBlocks": [],
"innerHTML": "\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Comment</span><textarea class=\"wp-block-form-input__input\" name=\"comment\" required aria-required=\"true\"></textarea></label>\n",
"innerHTML": "\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Comment</span><textarea class=\"wp-block-form-input__input\" name=\"comment\" required aria-required=\"true\"></textarea></label></div>\n",
"innerContent": [
"\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Comment</span><textarea class=\"wp-block-form-input__input\" name=\"comment\" required aria-required=\"true\"></textarea></label>\n"
"\n<div class=\"wp-block-form-input\"><label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Comment</span><textarea class=\"wp-block-form-input__input\" name=\"comment\" required aria-required=\"true\"></textarea></label></div>\n"
]
},
{
Expand Down
Loading
Loading