Skip to content

Dropdown menu doesn't close when tabbing from an input #100

@madoci

Description

@madoci

Describe the bug

When an input is the last focusable element of a Dropdown menu, tabbing from this element change the focus to the next focusable element outside the menu, but does not close the menu.

To Reproduce

Steps to reproduce the behavior:

  1. Create a Dropdown with a toggle and a menu containing a text input, and another input after and oustide the Dropdown.
  2. Open the menu by clicking the toggle.
  3. Focus the text input inside the menu.
  4. Type the 'Tab' key to focus the input outside the Dropdown.
  5. The menu stays open.

Reproducible Example

Minimal example by using CodeSandbox.

Expected behavior

The menu should close when tabbing out from the menu.

Screenshots

dropdown-tab-bug

Environment (please complete the following information)

  • Operating System: debian
  • Chrome, Version 123.0.6312.58
  • react-restart/ui Version 1.6.8

Additional context

I believe it can be solved by adding a key !== 'Tab' condition to Dropdown.tsx:279 like so :

if (
  isInput &&
  (key === ' ' ||
    (key !== 'Escape' && key !== 'Tab' && fromMenu) ||
    (key === 'Escape' && (target as HTMLInputElement).type === 'search'))
) {
  return;
}

If the 'Tab' key does not change focus and is instead used as a tabulation inside the text input, the next keyup event would have that input as a target and the menu will not be toggled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions