javascript - Get the event.target in formik Select onChange - ReactJS - Stack Overflow

Following is the Select in my withFormik Form. Which is working fine.<Selectid="userList"n

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
Add a ment  | 

1 Answer 1

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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信