Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow controlled InputSearch to update onChange handler with temp text value #654

Merged
merged 4 commits into from
Aug 5, 2024
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
35 changes: 35 additions & 0 deletions packages/kit/src/input-search/InputSearchInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,38 @@ describe("InputSearchInput", () => {
expect(inputElement).toHaveValue("Test item");
});
});

describe("InputSearchInput - Controlled", () => {
test("updates the value when an item is highlighted", async () => {
const user = userEvent.setup();
const changeSpy = jest.fn();
render(
<InputSearchRoot aria-label="Example-Search" openOnFocus>
<InputSearchInput
name="fruit"
id="fruit"
value={""}
onChange={(event) => {
changeSpy(event.target.value);
}}
/>
<InputSearchPopover>
<InputSearchList>
<InputSearchListItem value="Apple" />
<InputSearchListItem value="Banana" />
<InputSearchListItem value="Orange" />
<InputSearchListItem value="Kiwi" />
<InputSearchListItem value="Pineapple" />
</InputSearchList>
</InputSearchPopover>
</InputSearchRoot>
);

const inputElement = screen.getByRole("combobox");
await user.click(inputElement);
await user.keyboard("T");
await user.keyboard("[ArrowDown]");

expect(inputElement).toHaveValue("Apple");
});
});
5 changes: 3 additions & 2 deletions packages/kit/src/input-search/InputSearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const InputSearchInput = React.forwardRef<
label = "Search",
autocomplete = true,
id,
value,
...rest
}: InputSearchInputProps,
ref
Expand Down Expand Up @@ -61,8 +62,8 @@ export const InputSearchInput = React.forwardRef<
}
}, [state.selectionManager.focusedKey, setTempText]);

if (rest.value) {
inputProps.value = rest.value;
if (value) {
inputProps.value = value;
}
if (autocomplete && tempText !== undefined) {
inputProps.value = tempText;
Expand Down
43 changes: 43 additions & 0 deletions packages/kit/src/input-search/play.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,49 @@ export const Scroll = {
},
};

const ControlledTemplate: StoryFn<typeof InputSearch.Root> = (args) => {
const [term, setTerm] = useState("");
return (
<Box css={{ width: "275px" }}>
<InputSearch.Root
{...args}
aria-label="Example-Search"
openOnFocus
onSelect={(value) => {
//setTerm(value);
}}
>
<InputSearch.Input
name="fruit"
id="fruit"
value={term}
onChange={(event) => {
setTerm(event.target.value);
}}
/>
<InputSearch.Popover>
<InputSearch.List>
<InputSearch.ListItem value="Apple" />
<InputSearch.ListItem value="Banana" />
<InputSearch.ListItem value="Orange" />
<InputSearch.ListItem value="Kiwi" />
<InputSearch.ListItem value="Pineapple" />
</InputSearch.List>
</InputSearch.Popover>
</InputSearch.Root>
</Box>
);
};

export const Controlled = {
render: ControlledTemplate,
args: {},

parameters: {
chromatic: { disableSnapshot: true },
},
};

const InteractionsTemplate: StoryFn<typeof InputSearch.Root> = () => (
<Box css={{ width: "275px", height: "340px" }}>
<InputSearch.Root aria-label="Example-Search" openOnFocus>
Expand Down
Loading