Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set min/max on TextField type="number"?

I'm using Material-UI v1.0.0-beta23 along with redux-form and redux-form-material-ui. I have a Field that is type number and I want to set the min and max values on the tag. I've tried both inputProps and min/max and neither seem to do what I want. I looked at the source code and don't see an obvious way to do this. Is it possible, and if so, how?

Here is my JSX showing things I've tried.

<Field   name="maxNodeSelectedCount"   component={TextField}   label="Max Node Selected Count"   type="number"   inputProps={{ min: 0, max: 10 }}   min={11}   max={20}   format={formatOption}   normalize={normalizeOption}   {...propertyFieldDefaults} /> 

Here is what the DOM looks like:

<input type="number" class="MuiInput-input-346 MuiInput-inputSingleline-349 MuiInput-inputDense-347" value="-3" name="options.maxNodeSelectedCount"> 
like image 418
Mike Suiter Avatar asked Dec 13 '17 16:12

Mike Suiter


People also ask

How do I set the height of a TextField in material UI?

Another method for setting Material-UI TextField width and height is a combination of sx at the TextField root and passing sx styling values to the composing Input component via InputProps . The width is set with TextField prop sx={{width: 300}} . The height is set by passing sx: { height: 80 } to the InputProps prop.

How do I add maxLength in TextField in material UI?

You can set the maxLength property for limiting the text in text box. Instead of onChange method you can pass maxLength to the inputProps (lowercase i, not InputProps) props of material-ui TextField . Basically we can edit all input element's native attrs via inputProps object. This was the easiest solution.

What is TextField in material UI?

Text fields let users enter and edit text. Text fields allow users to enter text into a UI. They typically appear in forms and dialogs.


2 Answers

The redux-form Field will pass props through to the component:

All other props will be passed along to the element generated by the component prop.

The TextField has a property named InputProps which can be used to pass props to the Input component it renders. This is also true if you're using redux-form-material-ui. Its TextField is simply a wrapper for the material-ui component.

The material-ui Input component has a property named inputProps which can be used to pass props to the input element it renders.

Ok, so what do I do?

You need to pass props all the way through, from Field, to TextField, to Input, to the input element.

So if we set InputProps on Field, it will be passed to TextField, which will pass it to the Input component. If the object we pass contains an inner inputProps (intentional lowercase 'i'), the Input component will pass it to the input element.

A game of hot-proptato:

Basically, define an inputProps object within InputProps and apply it to Field:

<Field     name="maxNodeSelectedCount"     component={TextField}     label="Max Node Selected Count"     type="number"     InputProps={{ inputProps: { min: 0, max: 10 } }}     format={formatOption}     normalize={normalizeOption}     {...propertyFieldDefaults}   /> 
  • Field passes InputProps to TextField
  • TextField passes the contents of InputProps to the Input component
  • Input passed the contents of inputProps to the input element

There is a caveat with this:

The current implementation of TextField sets its own value for inputProps so that the inputClassName is applied to the input element.

By handing your own value of inputProps to TextField, you will overwrite the version applied by TextField, leaving inputClassName unset. If are using inputClassName you will need to include it in the inner inputProps object.

EDIT: I have submitted an issue and pull request to address this caveat in a future release.

like image 125
Ken Gregory Avatar answered Sep 23 '22 01:09

Ken Gregory


Simply use your inputprops well

<TextField      type="number"     InputProps={{         inputProps: {              max: 100, min: 10          }     }}     label="what ever" /> 

notice the upper and lower case in the inputprops

credit to Ken Gregory

like image 30
Chukwuemeka Maduekwe Avatar answered Sep 22 '22 01:09

Chukwuemeka Maduekwe