Skip to content

Commit

Permalink
Input Field Block: Use useblockProps hook in save function (WordPre…
Browse files Browse the repository at this point in the history
…ss#56507)

* Input Field Block: Use blockProps hook in save function

* Update fixture files

* Update packages/block-library/src/form-input/deprecated.js

Co-authored-by: Aaron Robertshaw <[email protected]>

---------

Co-authored-by: Aaron Robertshaw <[email protected]>
  • Loading branch information
t-hamano and aaronrobertshaw authored Dec 12, 2023
1 parent dc95863 commit 0456925
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 57 deletions.
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,
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">
<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 -->
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

0 comments on commit 0456925

Please sign in to comment.