I am using Select box from material-ui
I want to show "select the value" option by default selected but after that user is not able to select this option.
<FormControl required className={classes.formControl}> <InputLabel htmlFor="circle">Circle</InputLabel> <Select value={circle} onChange={event => handleInput(event, "circle")} input={<Input name="circle" id="circle" />} > <MenuItem value="" disabled> <em>select the value</em> </MenuItem> <MenuItem value={10}>Ten</MenuItem> <MenuItem value={20}>Twenty</MenuItem> <MenuItem value={30}>Thirty</MenuItem> </Select> <FormHelperText>Some important helper text</FormHelperText> </FormControl>
Current code on sandbox: https://codesandbox.io/s/xoylmlj1qp
I want to make like this: https://jsfiddle.net/wc1mxdto/
I changed the state 20
to blank string in circle
form: { searchValue: "", circle: '', searchCriteria: "" }
now expected output should be dropdown should show "please select value" but currently it showing this
To set the default value in React Material-UI select box, we can set the initial value of the state that we use as the Select 's value prop's value. The initial value must match one of the value prop of values of MenuItem . to define the value state with useState . We set value 's initial value to "50k-100k" .
To set selected value of a select drop down to null with React, we can set the state we use as the value of the value prop of the select drop down to null . to set onChange to a function that calls setSelected to e. target. value which is the selected value if it's truthy.
You need to provide correct MenuItem
value in state to be matched on render.
Here is the working codesandbox: Default Select Value Material-UI
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