Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get my React TextField to open the number pad when inputting on a mobile device?

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

like image 519
intA Avatar asked Apr 25 '19 15:04

intA


People also ask

How do you input a number into React?

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.

Which attribute is required to open only a numeric keyboard?

You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.


2 Answers

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

like image 78
X.Creates Avatar answered Oct 28 '22 21:10

X.Creates


Yo have to add this attributes to your input tag:

<input type='number' inputMode='numeric' pattern="[0-9]*" />
like image 36
Ricardo Gonzalez Avatar answered Oct 28 '22 21:10

Ricardo Gonzalez