简体   繁体   中英

react-select cannot set id

Have a react-select component and the id field does not get set? Is it possible to set an id?

<Select
    id={field}
    className="reqform-project-dropdown"
    {...f.props(field)}
    disabled={
        readOnly ||
        onEdit ||
        f.props(field).read_only
    }
    clearable={false}
    options={this.props.common.tests}
    onChange={this.handleProjChange.bind(this, field,
        f.props(field).multiple,
        'test_req_unique_prefix')}
    labelKey="test_name"
    valueKey="test_req_unique_prefix"
/>

I have resorted to setting the id of the parent div, but it seems silly I can't do it directly for the select.

In version >= 2 of react-select, the prop to set the id is called inputId . Use it like this:

<Select inputId="your-custom-id" />

You can use the inputProps prop as in the docs.

If what you want is focus when clicking the corresponding label, passing id inside inputProps should work.

<label htmlFor={'fieldId'} />
<Select inputProps={{ id: 'fieldId' }} /> // should be an object

I didn't see anything about setting an id attribute in their docs but you can use ref to get a refference to the DOM element and add it through this object.

class App extends React.Component {
  componentDidMount() {
    this.select.wrapper.id = "myId";
  }

  render() {
    return (
      <div>
        <Select
          ref={ref => { this.select = ref; }}
        />
      </div>
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM