12

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

5 Answers 5

20

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

12

Yo have to add this attributes to your input tag:

<input type='number' inputMode='numeric' pattern="[0-9]*" />
4
  • But I don't have an input tag, this is a TextField
    – intA
    Commented Apr 25, 2019 at 17:58
  • Your TextField is a custom component or a library? if is custom pass the props to the children component, if is a library, try to add them to the TextField Commented Apr 25, 2019 at 18:00
  • 1
    it is the Material-UI TextField. I just tried adding the type inoputMode and pattern attributes to my TextField directly but that did not work either
    – intA
    Commented Apr 25, 2019 at 18:04
  • Searching for a solution using material-ui textfield as well. The solution provided doesn't work. When I tap into the text field, the keyboard shows up with the numbers already, however all other symbols are there too, brackets, etc. It's not specifically the numberpad keyboard. Commented Feb 26, 2022 at 3:03
4

Real world example - noting that inputProps and InputProps are not the same somehow.

<TextField
    inputProps={{
        inputMode: 'decimal',
    }}
    InputProps={{
        startAdornment: (
            <InputAdornment position="start">
                <Icon />
            </InputAdornment>
        ),
        endAdornment: (
            <InputAdornment position="end">
                <IconButton
                    disabled={disabled}
                    onClick={() => {
                        swapCurrencies()
                    }}
                >
                    <SwapVertIcon />
                </IconButton>
            </InputAdornment>
        ),
    }}
    name={'amount'}
    className={classes.root}
    fullWidth
    disabled={disabled}
    onChange={e => {
        doStuff()
    }} />

3

Useful to know as well, if you want a decimal point to appear. You will need to add the following to you input tag...

inputMode='decimal'

Example below

<input 
   type='number' 
   step='any' 
   inputMode='decimal' 
   pattern='\d*'
/>
0
import React, { Component } from 'react';
import { TextField } from '@mui/material';
     
 export default function NumericField(props) { 
    const [state,setState] = React.useState(""); 
    const handleChange=(event)=> {
    const regx = /^[0-9\b]+$/;
      if (event.target.value === '' || regx.test(event.target.value)) {
         setState( event.target.value);
  }

}
 return (
        <TextField 
            type="text"
            value={state}
            label={props.label} 
            fullWidth={props.fullWidth}
            onChange={handleChange} 
            id={props.id}
            required={props.required}
            disabled={props.disabled}
            error={props.error}
            helperText={props.helperText}
            name={props.name}/>
      
        );
    } 
    

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.