Skip to content

Conversation

@hasegawa-101
Copy link

Closes #8732

Added onKeyDown prop to ListBoxItem to support custom keyboard event handling for individual items,
enabling features like deleting items with Delete/Backspace keys.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub
    Issue
    .
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria
    Practices

📝 Test Instructions:

  1. Run Storybook: yarn start
  2. Navigate to "ListBox With Keyboard Delete" story
  3. Focus on an item and press Delete or Backspace
  4. Verify the item is removed and keyboard navigation still works

🧢 Your Project:

Open source contribution

@hasegawa-101 hasegawa-101 changed the title Add onKeyDown prop to ListBoxItem for custom keyboard handling feat: add onKeyDown prop to ListBoxItem for custom keyboard handling Nov 13, 2025
@hasegawa-101 hasegawa-101 marked this pull request as ready for review November 13, 2025 17:36
Copy link
Member

@snowystinger snowystinger left a comment

Choose a reason for hiding this comment

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

Thanks for starting this, would you mind removing the docs and storybook in favour of a unit test? it can be added just after this describe block

describe('press events', () => {


The `<ListBoxItem>` component works with frameworks and client side routers like [Next.js](https://nextjs.org/) and [React Router](https://reactrouter.com/en/main). As with other React Aria components that support links, this works via the <TypeLink links={docs.links} type={docs.exports.RouterProvider} /> component at the root of your app. See the [client side routing guide](routing.html) to learn how to set this up.

## Custom keyboard handling
Copy link
Member

Choose a reason for hiding this comment

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

No need to document this, it'll be in the props table and is a pretty common pattern

@hasegawa-101
Copy link
Author

@snowystinger

Thanks for the review! I've addressed your feedback. Please take another look.

@nwidynski
Copy link
Contributor

nwidynski commented Nov 14, 2025

This should be using useKeyboard instead of native event listeners, otherwise we leak the event upwards.

let {getAllByRole} = renderListbox({}, {onKeyDown});
let options = getAllByRole('option');

fireEvent.keyDown(options[0], {key: 'Delete'});
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fireEvent.keyDown(options[0], {key: 'Delete'});
await user.tab();
await user.keyboard('ArrowDown');

@hasegawa-101
Copy link
Author

Fixed! I've updated the test to use userEvent.keyboard() instead of fireEvent.keyDown() to prevent event leaking 🙏

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.

ListBox: Backspace/Delete

3 participants