Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default value in material-UI select box in react?

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/


Update

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 enter image description here

like image 749
user944513 Avatar asked Sep 05 '18 10:09

user944513


People also ask

How do I select the default value in material UI select?

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" .

How set selected value of select in React?

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.


1 Answers

You need to provide correct MenuItem value in state to be matched on render.

Here is the working codesandbox: Default Select Value Material-UI

like image 193
Sakhi Mansoor Avatar answered Sep 25 '22 00:09

Sakhi Mansoor