-
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
Fix spacer NaN warning #14785
Fix spacer NaN warning #14785
Conversation
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.
Hi again @Jackie6. This didn't seem to solve the issue when I tested it. My steps were:
- Add a spacer to post
- Delete the height value of the spacer in the sidebar
- Save and refresh the post
The issue seems to be that this code still causes NaN
to be set as the attribute because parseInt
is called with an empty string:
setAttributes( {
height: parseInt( event.target.value, 10 ),
} );
@@ -58,7 +58,7 @@ const SpacerEdit = ( { attributes, isSelected, setAttributes, toggleSelection, i | |||
height: parseInt( event.target.value, 10 ), | |||
} ); | |||
} } | |||
value={ height } | |||
value={ isNaN( height ) ? '' : height } |
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.
Instead of passing an empty string, you can pass 20
if height is NaN, since the min value of the input field is set to 20. ( check line 62 )
Hi @talldan Thanks for comments. What if we enter a value that is less than the min height? |
Hey @Jackie6. I think that current message is just provided by the browser, and not every browser shows it (I couldn't see it in Chrome). I feel like the best behaviour might be to add some checks here: gutenberg/packages/block-library/src/spacer/edit.js Lines 57 to 59 in ed31b3a
One thing to be careful about here is backward compatibility. It might be necessary to add a block deprecation: |
Hi, @talldan, thanks for your suggestions. I modify the code as is discussed in your comment. I find it is not very user-friendly. For example, if I would like to enter "40" as the height of the spacer, As is showed in the code, gutenberg/packages/block-library/src/spacer/edit.js Lines 62 to 63 in ed31b3a
The valid height should be n*10, n is an integer at least 2. Why not just make the height uneditable? The height can only be adjusted by using the range control rather than enter a value directly. In addition, I do not understand why a deprecation should be added. Because I do not include any new feature that may affect backward compatibility. |
Hey again @Jackie6. A solution could be to store the raw value of the input so that the user's changes to the input field aren't overwritten. Could use react's Something like: const [ inputHeightValue, setInputHeightValue ] = useState( height );
// ...
<input
onChange={ ( event ) => {
let customHeight = parseInt( event.target.value, 10 );
setInputHeightValue( customHeight );
if ( isNaN( customHeight ) ) {
// Set to the default size
customHeight = 100;
} else if ( customHeight < 20 ) {
// Set to the minimum size
customHeight = 20;
}
setAttributes( {
height: customHeight,
} );
} }
value={ inputHeightValue } This way the value of the input field can be different to the displayed height of the spacer. Would also have to make sure to update the
You're right, sorry about that, there would need to be changes to the |
@talldan Thanks for comments. Now the code works better. |
This is looking good, @Jackie6. One final thing I think needs to be done is to also update the input field's value when the drag handle is used on the spacer:
|
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.
Thanks for making those changes! 😄
@@ -15,6 +15,7 @@ import { withInstanceId } from '@wordpress/compose'; | |||
const SpacerEdit = ( { attributes, isSelected, setAttributes, toggleSelection, instanceId } ) => { | |||
const { height } = attributes; | |||
const id = `block-spacer-height-input-${ instanceId }`; | |||
const [ inputHeightValue, setInputHeightValue ] = useState( height ); |
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.
I'm not sure I understand why we need a sperate state value here. Why height
is not enough?
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.
When the user deletes the value in the input entirely, we wanted height
to revert to the default of 100
. That felt like the right user experience to me at the time.
It creates a situation where the input has one value ''
and the spacer has another 100
.
I was also thinking about an uncontrolled input, but then there's still a requirement to set the initial value.
Are there other ways to achieve this?
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.
makes sense, I didn't see this subtle difference at first. Thanks for clarifying.
* Fix spacer NaN warning * Set the min height of spacer block * Reset the customHeight if it is invalid * Add local state in spacer * Update input field's value when using drag handle
* Fix spacer NaN warning * Set the min height of spacer block * Reset the customHeight if it is invalid * Add local state in spacer * Update input field's value when using drag handle
Description
Fix #14426
How has this been tested?
Types of changes
Change the value in BaseControl of the spacer block
Checklist: