You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Request: an alternative number control/input #9769
Is your feature request related to a problem? Please describe.
I am working on converting a widget to be a block. There are a handful of number fields in my widget, but some of them are optional, and some have very large ranges (small or no minimum, very large maximum). The two issues I have are:
the large range fields are very difficult to manage with the RangeControl slider. You can see the case a bit with the Text Settings font size slider on the paragraph block. A larger range of numbers makes the slider unusable.
on the RangeControl input, the number is required. If I do not want the number to be required and set the initial value as an empty string, the block won't load because of this error: Error loading block: Invalid parameter(s): attributes
Describe the solution you'd like
I would like to have a NumberControl control with no range slider, and ideally it could accept a blank value.
Describe alternatives you've considered
I've currently got a RangeControl input working with the type set to string instead of number, but the slider is still problematic, so I probably really have to convert it to a TextControl. Either way, I have to add in an extra validation step to cast the value, if given, to an integer. So as an alternative, I can probably work around this, but I still would like a number field that is just a plain number input.
If this is already an option that's available, I apologize--I looked through the handbook list and did not find it.
@robincornettTextControl has a type prop, so you can pass number and you'll get an <input type="number">. Then you should be able to pass min, max, and step as additional props, or any other HTML5 number input attributes. Let me know if this helps!
Thank you, I was able to get the TextControl set to a number input. Interestingly, the error about the attributes persists if the field is left empty unless I set it to be a string instead of a number for the attribute type, so it appears that setting the attribute type to number somehow makes it a required field.
Yes that's default behavior, an empty string is not considered to be a number, you can try handling 0 or -1 as empty or have a switch control before the input to enable or disable the input
Activity
chrisvanpatten commentedon Sep 10, 2018
@robincornett
TextControlhas atypeprop, so you can passnumberand you'll get an<input type="number">. Then you should be able to passmin,max, andstepas additional props, or any other HTML5 number input attributes. Let me know if this helps!youknowriad commentedon Sep 11, 2018
Also, you're free to use any React component or self-made React component. Gutenberg just happens to use some and offer them for plugin authors.
robincornett commentedon Sep 11, 2018
Thank you, I was able to get the TextControl set to a number input. Interestingly, the error about the attributes persists if the field is left empty unless I set it to be a string instead of a number for the attribute type, so it appears that setting the attribute type to number somehow makes it a required field.
Tofandel commentedon May 20, 2019
Yes that's default behavior, an empty string is not considered to be a number, you can try handling 0 or -1 as empty or have a switch control before the input to enable or disable the input