-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from 1 commit
9252df7
16a69c0
61d62be
36bf894
b0d97b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, '' ) | ||
); | ||
}; | ||
|
||
// 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ function InputFieldBlock( { attributes, setAttributes, className } ) { | |
</PanelBody> | ||
</InspectorControls> | ||
) } | ||
<InspectorControls __experimentalGroup="advanced"> | ||
<InspectorControls group="advanced"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' ) } | ||
|
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 --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
There was a problem hiding this comment.
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 insave.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.