简体   繁体   English

react-select无法设置id

[英]react-select cannot set id

Have a react-select component and the id field does not get set? 有一个react-select组件并且id字段没有设置? Is it possible to set an id? 是否可以设置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. 我已经设置了父div的id,但看起来很傻我不能直接为select做。

In version >= 2 of react-select, the prop to set the id is called inputId . inputId = 2的react-select中,设置id的prop称为inputId Use it like this: 像这样使用它:

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

You can use the inputProps prop as in the docs. 您可以像在文档中一样使用inputProps prop。

If what you want is focus when clicking the corresponding label, passing id inside inputProps should work. 如果你想要的是在点击相应的标签时焦点,在inputProps传递id应该有效。

<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. 我没有看到在他们的文档中设置id属性的任何内容,但您可以使用ref来获取DOM元素的引用并通过此对象添加它。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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