React-bootstrap-typeahead: The `id` prop will be required in future versions to make the component accessible for users of assistive technologies such as screen readers.

Created on 18 Jun 2019  路  13Comments  路  Source: ericgio/react-bootstrap-typeahead

users = [
{ 
"id": "5d08f94da9d6d25011d55e87",
"name": "Leo"
},
...
]

                  <Typeahead
                    labelKey="name"
                    options={users}
                    placeholder="Search user..."
                    onChange={this.handleChangeTy('userSelect')}
                  />

Most helpful comment

Hi @Angelk90, sure:

<Typeahead
  id="my-typeahead-id"
  labelKey="name"
  options={users}
  placeholder="Search user..."
  onChange={this.handleChangeTy('userSelect')}
/>

It works like the id on any normal element.

All 13 comments

You need to add an id to the component, not the options.

@ericgio , can I ask you more clarifications? You can give me an example.

Hi @Angelk90, sure:

<Typeahead
  id="my-typeahead-id"
  labelKey="name"
  options={users}
  placeholder="Search user..."
  onChange={this.handleChangeTy('userSelect')}
/>

It works like the id on any normal element.

The id prop doesn't actually get passed through to the input. It isn't in the list of props that are passed through:

https://github.com/ericgio/react-bootstrap-typeahead/blob/74fccbec27338a2c8046c1edd59792a59462217c/src/Typeahead.react.js#L29-L52

The id prop doesn't actually get passed through to the input

That's correct. The warning doesn't refer to the input's id; it refers to an id for the typeahead component that is ultimately applied to the menu and used for accessibility. If you need to pass an id to the input itself, you would do that via inputProps.

Why do we need to pass this ID through? The only reason I can think of is for linking the input with the dropdown, but a class name is already automatically generated for this.

What am I missing?

@cameron-martin: It's used for accessibility.

I got that, but what're you meant to link to that ID externally (from outside the component)?

@cameron-martin I'm sorry, but I don't understand your question. You don't need to "link" anything, you just need to provide the id and the accessibility stuff happens internally.

But a random id is generated automatically to use internally, so why do I need to provide one?

The randomly generated id is going away in favor of an explicit, user-defined one. That change is based on what I've seen to be common practice in other libraries, with react-bootstrap being one particularly relevant case.

Ah makes sense. There seems to be some interest in introducing a method for unique ID generation into React, particularly because the current methods do not work with both SSR & suspense. This would likely change the common practice to being generating these IDs internally. See the open RFC and PR.

FYI, the above PR has now landed so should be available in the next version of React :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DaveyEdwards picture DaveyEdwards  路  5Comments

alex84G picture alex84G  路  5Comments

gilm123 picture gilm123  路  10Comments

mflauer picture mflauer  路  4Comments

abdulrahman-khankan picture abdulrahman-khankan  路  6Comments