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

Forms: use a placeholder attribute in the editor instead of value #41712

Merged
merged 3 commits into from
Feb 27, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Forms: use placeholder attribute in editor instead of value
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { createHigherOrderComponent, compose } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import clsx from 'clsx';
import { isEmpty } from 'lodash';
import { useFormStyle } from '../util/form';
import { withSharedFieldAttributes } from '../util/with-shared-field-attributes';
import JetpackFieldControls from './jetpack-field-controls';
Expand All @@ -20,7 +19,7 @@ const JetpackField = props => {
requiredText,
label,
setAttributes,
placeholder,
placeholder = '',
width,
insertBlocksAfter,
type,
Expand All @@ -31,7 +30,7 @@ const JetpackField = props => {
const blockProps = useBlockProps( {
className: clsx( 'jetpack-field', {
'is-selected': isSelected,
'has-placeholder': ! isEmpty( placeholder ),
'has-placeholder': placeholder !== '',
} ),
style: blockStyle,
} );
Expand All @@ -51,8 +50,9 @@ const JetpackField = props => {
className="jetpack-field__input"
onChange={ e => setAttributes( { placeholder: e.target.value } ) }
style={ fieldStyle }
type={ type }
value={ placeholder }
type={ isSelected ? 'text' : type }
Copy link
Member Author

Choose a reason for hiding this comment

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

This is so that you can enter text placeholders for number input. Otherwise, you could only enter numbers.

The downside is that you might lose input[type="number"] specific styling:

Screenshot 2025-02-27 at 17 02 30

value={ isSelected ? placeholder : '' }
placeholder={ placeholder }
onClick={ event => type === 'file' && event.preventDefault() }
onKeyDown={ event => {
if ( event.defaultPrevented || event.key !== 'Enter' ) {
Expand All @@ -70,7 +70,6 @@ const JetpackField = props => {
setAttributes={ setAttributes }
placeholder={ placeholder }
attributes={ attributes }
hidePlaceholder={ type === 'number' }
/>
</>
);
Expand Down
Loading