Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add appropriate roles to option and listbox elements #5625

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

MssKnd
Copy link

@MssKnd MssKnd commented Apr 30, 2023

This pull request aims to improve the accessibility of react-select by adding appropriate roles to the option and listbox elements.

Changes:

  • Added the option role and aria-selected to the option-equivalent elements
  • Added the listbox role to the listbox-equivalent elements

I noticed that while using react-select in my own project, I couldn't use getByRole with react-testing-library to target the option-equivalent elements. This led me to create this PR to address the issue.

By merging this PR, we can make react-select more accessible and provide a better experience for users with assistive technologies. Please let me know if any further changes or tests are needed. I look forward to contributing to this project and making it more inclusive for all users.

@changeset-bot
Copy link

changeset-bot bot commented Apr 30, 2023

⚠️ No Changeset found

Latest commit: 50930cd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@MssKnd MssKnd marked this pull request as ready for review April 30, 2023 05:06
@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 30, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't use getByRole with react-testing-library to target the option-equivalent elements.

I ran into this same exact problem.

As work-around, these roles can be added using components prop. It would be great to have these set by default as this PR does.

import ReactSelect, { type OptionProps, type MenuListProps, components } from 'react-select';

export default function Select() {
  return (
    <ReactSelect components={{Option, MenuList}} />
  )
}

function Option({innerProps, isSelected, ...props}: OptionProps) {
    return (
        <components.Option
            {...props}
            isSelected={isSelected}
            innerProps={{...innerProps, role: 'option', 'aria-selected': isSelected}}
        />
    );
}

function MenuList({innerProps, ...props}: MenuListProps) {
    return (
        <components.MenuList {...props} innerProps={{...innerProps, role: 'listbox'}} />
    );
}

@MssKnd
Copy link
Author

MssKnd commented Aug 24, 2023

@AriPerkkio
Thank you for your nice information!
I am dealing with the same work-around right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants