reactjs - Select single option in SelectField redux-form-material-ui -


i have forms in redux app. use redux-form-material-ui well. i'd selectfields have 1 option set default option. way setting initialvalues while constructing form component?

surprisingly answer seems "no". @ least doesn't seem easy passing prop.

material-ui (the original, not redux-form variation) doesn't support defaultvalue option selectfield. found similar thread redux-form.

you still solve if have data selectfield options in array. maybe have function turns array array of menuitems.

// example option data const optiondataarray = [{ value: "first option", key: 0 }, { value: "second option", key: 1 }]; const tomenuitem = menudata =>      (<menuitem value={value} key={key} primarytext={ value } />)   ); 

then render them like

<selectfield value={      optiondataarray.length === 1 ?         optiondataarray[0].value :        null  } >   { optiondataarray.map(tomenuitem) }  </selectfield> 

i haven't tried this, makes one-item selectfields controlled components pre-selected input. , i'm hoping null value interpreted if didn't provide @ all.

but if second assumption/hope isn't true, perhaps make function this:

const getselectfieldvalueprop = alloptiondata => {   if(alloptiondata.length == 1) {     return { value: alloptiondata[0].value }   } else {     return {};   } } 

and use like

<selectfield {...getselectfieldvalueprop(optiondataarray) } >   { optiondataarray.map(tomenuitem) }  </selectfield> 

that way one-item selectfields should value prop set.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -