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 1 commit
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
144 changes: 144 additions & 0 deletions packages/block-library/src/form-input/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* 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';

const getNameFromLabelV1 = ( content ) => {
const dummyElement = document.createElement( 'div' );
dummyElement.innerHTML = content;
// Get the slug.
return (
removeAccents( dummyElement.innerText )
// 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, '' )
);
};
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 copied from the getNameFromLabel() function present in save.js and used for version 1 deprecation. This is because I think deprecations may not be handled correctly if the original function is updated and the output markup changes.


// Version without wrapper div in saved markup
// See: https://github.com/WordPress/gutenberg/pull/XXXXX
t-hamano marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -54,30 +55,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 */
}
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,12 @@
[
{
"name": "core/missing",
"isValid": true,
"attributes": {
"originalName": "core/form-input",
"originalUndelimitedContent": "<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>",
"originalContent": "<!-- wp:form-input -->\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<!-- /wp:form-input -->"
},
"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,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 -->
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I expected new markup to be generated here, but strangely the markup hasn't changed. This may be related to the fact that the hook was not used in the save function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Your expectation was right, this would appear to indicate that the deprecation migration isn't running. At least for the fixture tests.

Loading