-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9252df7
Input Field Block: Use blockProps hook in save function
t-hamano 16a69c0
Merge branch 'trunk' into fix/form-input-save-hook
t-hamano 61d62be
Merge branch 'trunk' into fix/form-input-save-hook
t-hamano 36bf894
Update fixture files
t-hamano b0d97b6
Update packages/block-library/src/form-input/deprecated.js
t-hamano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/integration/fixtures/blocks/core__form-input.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
6 changes: 6 additions & 0 deletions
6
test/integration/fixtures/blocks/core__form-input__deprecated-v1.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
15 changes: 15 additions & 0 deletions
15
test/integration/fixtures/blocks/core__form-input__deprecated-v1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
] |
11 changes: 11 additions & 0 deletions
11
test/integration/fixtures/blocks/core__form-input__deprecated-v1.parsed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
test/integration/fixtures/blocks/core__form-input__deprecated-v1.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.