Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
d2b7a2c
Add `onKeyDown` prop to ListBoxItem for custom keyboard handling
hasegawa-101 Nov 13, 2025
b404954
Update type annotation in ListBox example and fix style formatting in…
hasegawa-101 Nov 13, 2025
dfd80e1
fix: remove custom keyboard handling example from ListBox docs and story
hasegawa-101 Nov 14, 2025
49e754d
test: add unit tests for `onKeyDown` prop in ListBox component
hasegawa-101 Nov 14, 2025
330d9a3
Merge branch 'main' into issue-8732
hasegawa-101 Nov 14, 2025
00f7be5
test: remove redundant assertions for `onKeyDown` in ListBox tests
hasegawa-101 Nov 14, 2025
e7356f7
test: update `onKeyDown` tests in ListBox to use user-event for bette…
hasegawa-101 Nov 15, 2025
2fbcf50
Add `onKeyDown` prop to ListBoxItem for custom keyboard handling
hasegawa-101 Nov 13, 2025
139f601
Update type annotation in ListBox example and fix style formatting in…
hasegawa-101 Nov 13, 2025
f1d2493
fix: remove custom keyboard handling example from ListBox docs and story
hasegawa-101 Nov 14, 2025
299d78c
test: add unit tests for `onKeyDown` prop in ListBox component
hasegawa-101 Nov 14, 2025
0c8979b
test: remove redundant assertions for `onKeyDown` in ListBox tests
hasegawa-101 Nov 14, 2025
81ccc92
test: update `onKeyDown` tests in ListBox to use user-event for bette…
hasegawa-101 Nov 15, 2025
416ceee
Merge remote-tracking branch 'origin/issue-8732' into issue-8732
hasegawa-101 Nov 15, 2025
cded5a0
chore: remove unnecessary blank lines in ListBox story
hasegawa-101 Nov 15, 2025
175fc31
chore: remove trailing blank line in ListBox story
hasegawa-101 Nov 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/react-aria-components/src/ListBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ export interface ListBoxItemProps<T = object> extends RenderProps<ListBoxItemRen
* Handler that is called when a user performs an action on the item. The exact user event depends on
* the collection's `selectionBehavior` prop and the interaction modality.
*/
onAction?: () => void
onAction?: () => void,
/** Handler that is called when a key is pressed on the item. */
onKeyDown?: (e: React.KeyboardEvent) => void
}

/**
Expand All @@ -379,6 +381,8 @@ export const ListBoxItem = /*#__PURE__*/ createLeafComponent(ItemNode, function
onHoverEnd: item.props.onHoverEnd
});

let keyDownProps = props.onKeyDown ? {onKeyDown: props.onKeyDown} : undefined;

let draggableItem: DraggableItemResult | null = null;
if (dragState && dragAndDropHooks) {
draggableItem = dragAndDropHooks.useDraggableItem!({key: item.key, hasAction: states.hasAction}, dragState);
Expand Down Expand Up @@ -422,7 +426,7 @@ export const ListBoxItem = /*#__PURE__*/ createLeafComponent(ItemNode, function

return (
<ElementType
{...mergeProps(DOMProps, renderProps, optionProps, hoverProps, draggableItem?.dragProps, droppableItem?.dropProps)}
{...mergeProps(DOMProps, renderProps, optionProps, hoverProps, keyDownProps, draggableItem?.dragProps, droppableItem?.dropProps)}
ref={ref}
data-allows-dragging={!!dragState || undefined}
data-selected={states.isSelected || undefined}
Expand Down
14 changes: 14 additions & 0 deletions packages/react-aria-components/test/ListBox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1861,4 +1861,18 @@ describe('ListBox', () => {
expect(onClick).toHaveBeenCalledTimes(1);
});
});

describe('onKeyDown', () => {
it('should call onKeyDown handler when key is pressed on item', async () => {
let onKeyDown = jest.fn();
renderListbox({}, {onKeyDown});

await user.tab();
await user.keyboard('{Delete}');
expect(onKeyDown).toHaveBeenCalledTimes(1);

await user.keyboard('{Backspace}');
expect(onKeyDown).toHaveBeenCalledTimes(2);
});
});
});