fix(listbox): pass missing press events to usePress (#4812)

* fix(listbox): pass missing press events to usePress

* feat(listbox): add test case for press event

* chore(changeset): add changeset
This commit is contained in:
աӄա 2025-02-11 21:05:23 +08:00 committed by GitHub
parent 8dc5aaa3ca
commit b2e7e04e89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"@heroui/listbox": patch
---
pass missing press events to usePress (#4798)

View File

@ -299,4 +299,48 @@ describe("Listbox", () => {
expect(menuItem).not.toHaveProperty("className", expect.stringContaining("truncate"));
});
it("should trigger press events", async () => {
const onPress = jest.fn();
const onPressStart = jest.fn();
const onPressEnd = jest.fn();
const onPressUp = jest.fn();
const onPressChange = jest.fn();
const {getAllByRole} = render(
<Listbox aria-label="Actions">
<ListboxItem
key="new"
onPress={onPress}
onPressChange={onPressChange}
onPressEnd={onPressEnd}
onPressStart={onPressStart}
onPressUp={onPressUp}
>
New file
</ListboxItem>
<ListboxItem key="copy">Copy link</ListboxItem>
<ListboxItem key="edit">Edit file</ListboxItem>
<ListboxItem key="delete" color="danger">
Delete file
</ListboxItem>
</Listbox>,
);
let listboxItems = getAllByRole("option");
expect(listboxItems.length).toBe(4);
await user.click(listboxItems[0]);
expect(onPress).toHaveBeenCalled();
expect(onPressStart).toHaveBeenCalled();
expect(onPressEnd).toHaveBeenCalled();
expect(onPressUp).toHaveBeenCalled();
expect(onPressChange).toHaveBeenCalled();
});
});

View File

@ -41,6 +41,10 @@ export function useListboxItem<T extends object>(originalProps: UseListboxItemPr
classNames,
autoFocus,
onPress,
onPressUp,
onPressStart,
onPressEnd,
onPressChange,
onClick: deprecatedOnClick,
shouldHighlightOnFocus,
hideSelectedIcon = false,
@ -74,6 +78,10 @@ export function useListboxItem<T extends object>(originalProps: UseListboxItemPr
ref: domRef,
isDisabled: isDisabled,
onPress,
onPressUp,
onPressStart,
onPressEnd,
onPressChange,
});
const {isHovered, hoverProps} = useHover({