mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
fix(autocomplete): clear autocomplete value when pressing clear button (#4458)
This commit is contained in:
parent
16c57ece64
commit
7c2bc4a18e
5
.changeset/nice-books-yell.md
Normal file
5
.changeset/nice-books-yell.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@nextui-org/autocomplete": patch
|
||||
---
|
||||
|
||||
clear autocomplete value when pressing clear button (#4402)
|
||||
@ -235,6 +235,49 @@ describe("Autocomplete", () => {
|
||||
expect(autocomplete).toHaveFocus();
|
||||
});
|
||||
|
||||
it("should clear arbitrary value after clicking clear button", async () => {
|
||||
const wrapper = render(
|
||||
<Autocomplete aria-label="Favorite Animal" data-testid="autocomplete" label="Favorite Animal">
|
||||
<AutocompleteItem key="penguin" value="penguin">
|
||||
Penguin
|
||||
</AutocompleteItem>
|
||||
<AutocompleteItem key="zebra" value="zebra">
|
||||
Zebra
|
||||
</AutocompleteItem>
|
||||
<AutocompleteItem key="shark" value="shark">
|
||||
Shark
|
||||
</AutocompleteItem>
|
||||
</Autocomplete>,
|
||||
);
|
||||
|
||||
const autocomplete = wrapper.getByTestId("autocomplete");
|
||||
|
||||
// open the select listbox
|
||||
await user.click(autocomplete);
|
||||
|
||||
// assert that the autocomplete listbox is open
|
||||
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
|
||||
|
||||
await user.keyboard("pe");
|
||||
|
||||
const {container} = wrapper;
|
||||
|
||||
const clearButton = container.querySelector(
|
||||
"[data-slot='inner-wrapper'] button:nth-of-type(1)",
|
||||
)!;
|
||||
|
||||
expect(clearButton).not.toBeNull();
|
||||
|
||||
// click the clear button
|
||||
await user.click(clearButton);
|
||||
|
||||
// assert that the input has empty value
|
||||
expect(autocomplete).toHaveValue("");
|
||||
|
||||
// assert that input is focused
|
||||
expect(autocomplete).toHaveFocus();
|
||||
});
|
||||
|
||||
it("should keep the ListBox open after clicking clear button", async () => {
|
||||
const wrapper = render(
|
||||
<Autocomplete aria-label="Favorite Animal" data-testid="autocomplete" label="Favorite Animal">
|
||||
|
||||
@ -418,13 +418,9 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
|
||||
onPress: (e: PressEvent) => {
|
||||
slotsProps.clearButtonProps?.onPress?.(e);
|
||||
if (state.selectedItem) {
|
||||
state.setInputValue("");
|
||||
state.setSelectedKey(null);
|
||||
} else {
|
||||
if (allowsCustomValue) {
|
||||
state.setInputValue("");
|
||||
}
|
||||
}
|
||||
state.setInputValue("");
|
||||
state.open();
|
||||
},
|
||||
"data-visible": !!state.selectedItem || state.inputValue?.length > 0,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user