I have a React TextField that is taking user input and it is representing a date. When the user clicks on the field I want the number keyboard to open instead of the full alphabet. I was looking at the React docs here and trying to mimic their example.
My TextField looks like the following:
<TextField
{ ...materialConfiguration }
floatingLabelFixed={value.length > 0}
floatingLabelText={label}
errorText={errorText}
onChange={this.onChange}
onKeyUp={this.debouncedOnKeyUp}
onBlur={this.onBlur}
type="number"
label="Number"
id="standard-number"
>
<Cleave value={value} options={{date: true, datePattern: ['m', 'd', 'Y']}} />
</TextField>
I added the type
label
and id
fields from the React example thinking that was what made the keyboard change but it does not work. How can I get this input to open the number pad?
The React example is this:
<TextField
id="standard-number"
label="Number"
value={this.state.age}
onChange={this.handleChange('age')}
type="number"
className={classes.textField}
InputLabelProps={{
shrink: true,
}}
margin="normal"
/>
Thanks
To only allow numbers to be entered in an input in React, we can set the pattern attribute of the input to only allow digits to be inputted. Then in the onChange handler, we only set the value of the input when the inputted value is valid. We create the val state with the useState hook.
You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.
UPDATED in 2021
<TextField id="quantity" label="Quantity" inputProps={{ inputMode: 'numeric' }}/>
using inputProps
does solve the issue. inputProps
object is attributes applied to the input element.
https://material-ui.com/api/text-field/#textfield-api
Yo have to add this attributes to your input tag:
<input type='number' inputMode='numeric' pattern="[0-9]*" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With