score:120
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.
score:-1
<TextField
label="Username"
id="outlined-start-adornment"
variant="outlined"
inputProps={{minlength:5, maxLength: 20}}
/>
score:0
This will definitely work
handleMobileNumber = (e) => {
if (e.target.value.split("").length <= 10) {
this.setState({
mobileNumber: e.target.value,
});
}
};
<TextField
label="Enter Mobile Number"
variant="outlined"
value={this.state.mobileNumber}
onChange={this.handleMobileNumber}
type="number"
/>
score:1
The other answers didn't work for me.
Material UI has a section for 3rd party integration here
It really does the job of writing only numbers and not allowing negatives.
import NumberFormat from 'react-number-format';
function NumberFormatCustom(props) {
const { inputRef, onChange, ...other } = props;
return (
<NumberFormat
{...other}
getInputRef={inputRef}
allowNegative={false}
onValueChange={(values) => {
onChange({
target: {
name: props.name,
value: values.value,
},
});
}}
isNumericString
/>
);
}
<TextField
label="react-number-format"
value={values.numberformat}
onChange={handleChange}
name="numberformat"
id="formatted-numberformat-input"
InputProps={{
inputComponent: NumberFormatCustom,
}}
/>
score:2
Put type
inside InputProps
:
<Field
name="number"
label="Number"
component={TextField}
InputProps={{
inputProps: {
type: 'number',
min: 0, max: 25,
},
}}
/>
score:12
You can use inputProps
to apply any attributes to the native input
element, including min
and max
.
<TextField type="number" inputProps={{ min: 4, max: 10 }} />
Please note that the min
/max
attributes do not prevent the user from typing invalid values in the TextField
. To restrict what the user can type, you can validate the value by adding onChange
handler like below:
const min = 0;
const max = 10;
export default function BasicTextFields() {
const [value, setValue] = useState<number>();
return (
<div>
<TextField
type="number"
inputProps={{ min, max }}
value={value}
onChange={(e) => {
var value = parseInt(e.target.value, 10);
if (value > max) value = max;
if (value < min) value = min;
setValue(value);
}}
/>
</div>
);
}
score:51
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
Source: stackoverflow.com
Related Query
- How can I center-align Material-ui TextField text and also set a min number value?
- React datepicker set min and max date
- React Typescript - Math.random gives wrong result even tho min value and max value is set
- Reactjs material ui textfield number max and min
- ReactJS set min and max value for number input using final-form-material-ui
- How to set max and min value for increment and decrement methods inside React
- How can I set min time on a MUI TextField of type='time'?
- how do I set the min and max date for date fields?
- Set min/max on TextField type="number"?
- how to get min or max dates from a list of dates using moment.js?
- Set TextField height material-ui
- ReactJS - How can I set a value for textfield from material-ui?
- Programmatically set value in material-ui Autocomplete TextField
- How to set max number of items that can be selected in react-select?
- Set max-content on `ListBox` from `@headlessui/react` to take max width of option while using Tailwind CSS?
- Can I set the Recharts axis domain max lower than dataMax?
- Material ui Textfield Hinttext overlap with with text when text is set with setState in react
- Min and Max Date in Material UI DatePicker in React
- How to get react noUiSlider min max values on update?
- How to set min date in TimePickerAndroid for react-native
- React.PropTypes.number float with min and max
- How can I set material-ui TextField to accept only Hexidecimal characters
- Material-UI Core TextField globally set InputLabelProps shrink
- how to set min height in semantic-ui react
- Material UI TextField label does not move up when the value for TextField is set porgramatically
- How to set ErrorMessage on TextField dynamically on Button onClick method
- How can I set the value of my MaterialUI TextField to uppercase?
- React MaterialUI - Set error on TextField Component after button is clicked
- set max based on other state value in input of range type (React)
- How to set TextField changes in to state using MaterialUI and Redux?
More Query from same tag
- React leaflet too much refresh map with context update value
- How can I merge ...object and return value of function call in setState(react-hook)?
- Redirect after submit in React
- Want to generate unique numeric id , can't use node-uuid since backend is expecting numeric id
- Automatically refresh the page after saving the edited form in React
- How to call the usehook within a function using react?
- Form validation react
- How to handle two menus in one react application
- how to hide empty label in react-bootstrap-typeahead
- React Routing not working on Heroku Deployment
- How to use IntersectionObserver with React?
- Toolbar mis-placed Navigation Item React
- Multiple components to result in one value with Formik
- Reactjs How to write the greater than 0 in jsx?
- unable to render lists of data while using class based component in both parent and child in reactjs
- React JS and TypeError
- How to use Route in React?
- Programatically going back to home page with React
- Make a div appear in front of another in Material UI when you hover
- Flex Box Items not aligning with the above row
- How to handle .active state in React-Bootstrap's Nav NavItem, with React-Router
- How to properly pass useReducer actions down to children without causing unnecessary renders
- JavaScript React setInterval WITHOUT fat-arrow function
- React Router How to Display straight Image for Twitter Cards
- React validate input field using errors array
- How to restrict Table Header to display once in map function?
- Get only CSS value of particular Tailwind CSS class
- Google App Engine/ Custom Domain - Preferring https over http
- How to disable/enable button when the iput field is empty or filled using state in react js?
- How to access this.props in child class in function that overrides parent's function