Describe the bug
In WordPress 5.0.2 with Gutenberg in the core (not the plugin), when using input fields to capture text from the user and providing a value= from props.attributes, the input fields are not editable. The outputted html code looks fine (no disabled or readonly attributes exist). However, in the browser, I am not able to change/edit the value in the input field. (Update: it tries to inject an [object Object] into text field.) Other components, like ColorPalette and Button, are working just fine.
The only errors I can find are from Firefox (only a separate warning from Chrome)..
Firefox Error 1:
Error: Permission denied to access property "nodeType" /wp-includes/js/tinymce/wp-tinymce.php?ver=4800-20180716 line 2
Firefox Error 2:
TypeError: Argument 1 of Node.contains does not implement interface Node. /wp-includes/js/tinymce/wp-tinymce.php?ver=4800-20180716 line 2
Chrome Warning 1:
The specified value "[object Object]" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?
I'd really like to use basic input fields/components to accept data from users. Any help with my Gutenberg block code is greatly appreciated.
To Reproduce
Here is my code in question:
...
registerBlockType( 'wcn/button', {
...
attributes: {
borderWidth: {
type: 'number',
default: 2
}
},
edit: (props) => {
const onChangeBorderWidth = newBorderWidth => {
props.setAttributes( { borderWidth: newBorderWidth })
}
return (
<div className={ props.className }>
<input
name="borderWidth"
type="text"
value={ props.attributes.borderWidth }
onChange={ onChangeBorderWidth }
/>
</div>
)
}
...
Expected behavior
I was expecting to edit the input field to take a number value (like it has for decades). However, the number value assigned is not changeable. Very strange.
Desktop Development Environment:
- OS: Windows 10
- Browser Chrome and Firefox
- Version 71.0.3578.98 and 63.0.3
Describe the bug
In WordPress 5.0.2 with Gutenberg in the core (not the plugin), when using input fields to capture text from the user and providing a value= from props.attributes, the input fields are not editable. The outputted html code looks fine (no disabled or readonly attributes exist). However, in the browser, I am not able to change/edit the value in the input field. (Update: it tries to inject an [object Object] into text field.) Other components, like ColorPalette and Button, are working just fine.
The only errors I can find are from Firefox (only a separate warning from Chrome)..
Firefox Error 1:
Error: Permission denied to access property "nodeType" /wp-includes/js/tinymce/wp-tinymce.php?ver=4800-20180716 line 2
Firefox Error 2:
TypeError: Argument 1 of Node.contains does not implement interface Node. /wp-includes/js/tinymce/wp-tinymce.php?ver=4800-20180716 line 2
Chrome Warning 1:
The specified value "[object Object]" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?
I'd really like to use basic input fields/components to accept data from users. Any help with my Gutenberg block code is greatly appreciated.
To Reproduce
Here is my code in question:
...
registerBlockType( 'wcn/button', {
...
attributes: {
borderWidth: {
type: 'number',
default: 2
}
},
edit: (props) => {
const onChangeBorderWidth = newBorderWidth => {
props.setAttributes( { borderWidth: newBorderWidth })
}
return (
<div className={ props.className }>
<input
name="borderWidth"
type="text"
value={ props.attributes.borderWidth }
onChange={ onChangeBorderWidth }
/>
</div>
)
}
...
Expected behavior
I was expecting to edit the input field to take a number value (like it has for decades). However, the number value assigned is not changeable. Very strange.
Desktop Development Environment:
- OS: Windows 10
- Browser Chrome and Firefox
- Version 71.0.3578.98 and 63.0.3
1 Answer
Reset to default 4It turns out that in order to get to the value of an input field, you must point to it in your callback function, like so...
const onChangeBorderWidth = newBorderWidth => {
props.setAttributes( { borderWidth: newBorderWidth.target.value })
}
This grabs the event's input target value and assigns it to the correct attribute.
HINT: You don't have to do this with Gutenberg's TextControl component.. by default, it returns this event.target.value
number
make any difference? – Alvaro Commented Dec 31, 2018 at 3:17onChangeBorderWidth
function you are changing thecolor
rather than theborderWidth
attribute. Please let me know if this solves the problem. – Alvaro Commented Dec 31, 2018 at 14:44The specified value "[object Object]" is not a valid number. The value must match to the following regular expression..
It's like any time I edit the field, an [Object] is it's new value, not what I typed from keyboard. – Marty McGee Commented Dec 31, 2018 at 15:07