react-select无法设置ID

Dan*_*ohn 2 reactjs react-redux

有一个react-select组件,并且没有设置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"
/>
Run Code Online (Sandbox Code Playgroud)

我采取了设置父div的ID的方法,但是我似乎无法直接为选择操作做傻事。

Hon*_*fus 9

在react-select的版本> = 2中,设置ID的道具称为inputId。像这样使用它:

<Select inputId="your-custom-id" />
Run Code Online (Sandbox Code Playgroud)


cfr*_*ser 6

您可以使用inputProps文档中的道具。

如果您想要的是单击相应标签时的焦点,则传入id内部inputProps应该可以工作。

<label htmlFor={'fieldId'} />
<Select inputProps={{ id: 'fieldId' }} /> // should be an object
Run Code Online (Sandbox Code Playgroud)