Following is the Select in my withFormik
Form. Which is working fine.
<Select
id="userList"
name="userList"
value={userList.names}
initialValue={values.userList}
className="select-box"
onChange={setFieldValue}
/>
But now on the basis of value selected I need to add/remove class from the select. so I tried e but it is returning the field name only
<Select
id="userList"
name="userList"
value={userList.names}
initialValue={values.userList}
className="select-box"
onChange={e => {
console.log(e) // => userList
}}
/>
I even tried this but no luck
<Select
id="userList"
name="userList"
value={userList.names}
initialValue={values.userList}
className="select-box"
onChange={(field, value) => {
console.log(field) // Response => userList
setFieldValue(field, value)
}}
/>
How can I access the event in onchange as on the basis of value I need to add / remove class from the select. Something like -
handleChange = e => {
// Here e is refering to the Select
if (e.target.value) {
e.target.classList.remove("gray");
e.target.classList.add("black");
} else {
e.target.classList.remove("black");
e.target.classList.add("gray");
}
};
Following is the Select in my withFormik
Form. Which is working fine.
<Select
id="userList"
name="userList"
value={userList.names}
initialValue={values.userList}
className="select-box"
onChange={setFieldValue}
/>
But now on the basis of value selected I need to add/remove class from the select. so I tried e but it is returning the field name only
<Select
id="userList"
name="userList"
value={userList.names}
initialValue={values.userList}
className="select-box"
onChange={e => {
console.log(e) // => userList
}}
/>
I even tried this but no luck
<Select
id="userList"
name="userList"
value={userList.names}
initialValue={values.userList}
className="select-box"
onChange={(field, value) => {
console.log(field) // Response => userList
setFieldValue(field, value)
}}
/>
How can I access the event in onchange as on the basis of value I need to add / remove class from the select. Something like -
handleChange = e => {
// Here e is refering to the Select
if (e.target.value) {
e.target.classList.remove("gray");
e.target.classList.add("black");
} else {
e.target.classList.remove("black");
e.target.classList.add("gray");
}
};
Share
Improve this question
asked Aug 29, 2019 at 6:30
NeshNesh
2,5719 gold badges40 silver badges56 bronze badges
1 Answer
Reset to default 3<Select />
as a controlled ponent would require the value
prop to use the state's selected
value.
value
prop is not meant for the <option>
values.
Thus, you can have something like:
state = {
selected: '',
}
handleChange = e => {
const selected = e.target.value; // selected name
switch(selected) {
// change class
}
this.setState({
selected
});
}
<Select value={this.state.selected} onChange={handleChange}>
{userList.names.map(user =>
<options value={user.name}>{user.name}</option>
)}
</Select>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744301151a4567507.html
评论列表(0条)