Skip to content

[useSelect] aria-activedescendant does not have valid value with controlled selectedItem #921

@stefanprobst

Description

@stefanprobst
  • downshift version: 4.0.8
  • node version: 12.14.1
  • npm (or yarn) version: yarn 1.21.1

Relevant code or config

import React from 'react'
import { render } from 'react-dom'
import { useSelect } from 'downshift'

const items = ['Apples', 'Bananas', 'Oranges']

const Select = ({ items, selectedItem }) => {
  const {
    getItemProps,
    getLabelProps,
    getMenuProps,
    getToggleButtonProps,
    isOpen,
  } = useSelect({ items, selectedItem })

  return (
    <div>
      <label {...getLabelProps()}>Fruit: </label>
      <button {...getToggleButtonProps()}>{selectedItem}</button>
      {/* <ul {...getMenuProps(isOpen ? undefined : { "aria-activedescendant": null })}> */}
      <ul {...getMenuProps()}>
        {isOpen &&
          items.map((item, index) => (
            <li key={item} {...getItemProps({ item, index })}>
              {item}
            </li>
          ))}
      </ul>
    </div>
  )
}

const App = () => <Select items={items} selectedItem={items[0]} />

render(<App />, document.getElementById('root'))

What you did:
use useSelect with controlled props (items and selectedItem)

What happened:
when used with controlled props, useSelect's getMenuProps ouputs an aria-activedescendant attribute pointing to a non-existing id (because the <li> elements are only rendered when the select is open.
Lighthouse therefore complains with "[aria-*] attributes do not have valid values".

Reproduction repository:

Problem description:

Suggested solution:
I'm currently working aroung this with {...getMenuProps(isOpen ? undefined : { "aria-activedescendant": null })} (see above), but i guess there is a better solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions