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

Improve listbox support in selectOptions #1231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

GeorgeTaveras1231
Copy link

@GeorgeTaveras1231 GeorgeTaveras1231 commented Aug 9, 2024

Allow role="option" elements to contain more complex html

What:

Allow role="option" elements to contain complex HTML when using selectOption. selectOption will perform a look up of the listbox option elements, only comparing the trimmed visible text against the given option value.

Example

<ul role="listbox">
  <li role="option">
     Some text
    <span>Nested HTML text</span>
  </li>
</ul>

Why:

The current implementation is too strict and in some cases does not make sense. If the role="option" element contains any HTML structure, it compares the given option against the raw innerHTML.

Using the example above, the call that would work would me

userEvent.selectOption(listbox, '     Some text\n    <span>Nested HTML text</span>') // 💀 

With these fixes, this becomes

userEvent.selectOption(listbox, 'Some text Nested HTML text') // 👍🏽  

How:

Implement a misc util named getVisibleText which clones the node, removes nodes that are not visible, extracts the textContent, then removes duplicate spaces.

The visibility heuristic follows the implementation in ./utils/isVisible (using getComputedStyle) and also looks at aria-hidden (since the role="listbox"/role="option" is rooted on aria, this seemed like a reasonable approach).

Checklist:

  • Documentation
  • Tests
  • Ready to be merged

George Taveras added 2 commits August 9, 2024 17:06
Allow `role="option"` elements to contain more complex html
Copy link

codesandbox-ci bot commented Aug 9, 2024

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.

@alexanderop
Copy link

I hope this gets merged soon would be really useful

@adarshotto
Copy link

It's a pain to use selectOption without these improvements.

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.

3 participants