Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/real-apes-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/autocomplete": patch
---

support onClear in Autocomplete (#5297)
6 changes: 6 additions & 0 deletions apps/docs/content/docs/components/autocomplete.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,12 @@ properties to customize the popover, listbox and input components.
type: "() => void",
description: "Handler that is called when the Autocomplete's Popover is closed.",
default: "-"
},
{
attribute: "onClear",
type: "() => void",
description: "Handler that is called when the clear button is clicked.",
default: "-"
}
]}
/>
Expand Down
14 changes: 12 additions & 2 deletions packages/components/autocomplete/__tests__/autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,16 @@ describe("Autocomplete", () => {
expect(autocomplete).toHaveFocus();
});

it("should clear value after clicking clear button", async () => {
it("should clear the value and onClear is triggered", async () => {
const onClear = jest.fn();

const wrapper = render(
<Autocomplete aria-label="Favorite Animal" data-testid="autocomplete" label="Favorite Animal">
<Autocomplete
aria-label="Favorite Animal"
data-testid="autocomplete"
label="Favorite Animal"
onClear={onClear}
>
<AutocompleteItem key="penguin">Penguin</AutocompleteItem>
<AutocompleteItem key="zebra">Zebra</AutocompleteItem>
<AutocompleteItem key="shark">Shark</AutocompleteItem>
Expand Down Expand Up @@ -204,6 +211,9 @@ describe("Autocomplete", () => {
// click the clear button
await user.click(clearButton);

// onClear is triggered
expect(onClear).toHaveBeenCalledTimes(1);

// assert that the input has empty value
expect(autocomplete).toHaveValue("");

Expand Down
7 changes: 7 additions & 0 deletions packages/components/autocomplete/src/use-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ interface Props<T> extends Omit<HTMLHeroUIProps<"input">, keyof ComboBoxProps<T>
* Callback fired when the select menu is closed.
*/
onClose?: () => void;
/**
* Callback fired when the value is cleared.
* if you pass this prop, the clear button will be shown.
*/
onClear?: () => void;
/**
* Whether to enable virtualization of the listbox items.
* By default, virtualization is automatically enabled when the number of items is greater than 50.
Expand Down Expand Up @@ -186,6 +191,7 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
errorMessage,
onOpenChange,
onClose,
onClear,
isReadOnly = false,
...otherProps
} = props;
Expand Down Expand Up @@ -453,6 +459,7 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
}
state.setInputValue("");
state.open();
onClear?.();
},
"data-visible": !!state.selectedItem || state.inputValue?.length > 0,
className: slots.clearButton({
Expand Down