Skip to content

Commit 3884839

Browse files
wp-abergebgranger
andauthored
fix: allow controlled InputSearch to update onChange handler with temp text value (#654)
* fix: allow cotrolled InputSearch to update onChange handler with temp text value * chore: adding test * chore: run formatter --------- Co-authored-by: Edward Granger <edward.granger@washpost.com>
1 parent bdf21f9 commit 3884839

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

packages/kit/src/input-search/InputSearchInput.test.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,38 @@ describe("InputSearchInput", () => {
208208
expect(inputElement).toHaveValue("Test item");
209209
});
210210
});
211+
212+
describe("InputSearchInput - Controlled", () => {
213+
test("updates the value when an item is highlighted", async () => {
214+
const user = userEvent.setup();
215+
const changeSpy = jest.fn();
216+
render(
217+
<InputSearchRoot aria-label="Example-Search" openOnFocus>
218+
<InputSearchInput
219+
name="fruit"
220+
id="fruit"
221+
value={""}
222+
onChange={(event) => {
223+
changeSpy(event.target.value);
224+
}}
225+
/>
226+
<InputSearchPopover>
227+
<InputSearchList>
228+
<InputSearchListItem value="Apple" />
229+
<InputSearchListItem value="Banana" />
230+
<InputSearchListItem value="Orange" />
231+
<InputSearchListItem value="Kiwi" />
232+
<InputSearchListItem value="Pineapple" />
233+
</InputSearchList>
234+
</InputSearchPopover>
235+
</InputSearchRoot>
236+
);
237+
238+
const inputElement = screen.getByRole("combobox");
239+
await user.click(inputElement);
240+
await user.keyboard("T");
241+
await user.keyboard("[ArrowDown]");
242+
243+
expect(inputElement).toHaveValue("Apple");
244+
});
245+
});

packages/kit/src/input-search/InputSearchInput.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const InputSearchInput = React.forwardRef<
2525
label = "Search",
2626
autocomplete = true,
2727
id,
28+
value,
2829
...rest
2930
}: InputSearchInputProps,
3031
ref
@@ -61,8 +62,8 @@ export const InputSearchInput = React.forwardRef<
6162
}
6263
}, [state.selectionManager.focusedKey, setTempText]);
6364

64-
if (rest.value) {
65-
inputProps.value = rest.value;
65+
if (value) {
66+
inputProps.value = value;
6667
}
6768
if (autocomplete && tempText !== undefined) {
6869
inputProps.value = tempText;

packages/kit/src/input-search/play.stories.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,49 @@ export const Scroll = {
220220
},
221221
};
222222

223+
const ControlledTemplate: StoryFn<typeof InputSearch.Root> = (args) => {
224+
const [term, setTerm] = useState("");
225+
return (
226+
<Box css={{ width: "275px" }}>
227+
<InputSearch.Root
228+
{...args}
229+
aria-label="Example-Search"
230+
openOnFocus
231+
onSelect={(value) => {
232+
//setTerm(value);
233+
}}
234+
>
235+
<InputSearch.Input
236+
name="fruit"
237+
id="fruit"
238+
value={term}
239+
onChange={(event) => {
240+
setTerm(event.target.value);
241+
}}
242+
/>
243+
<InputSearch.Popover>
244+
<InputSearch.List>
245+
<InputSearch.ListItem value="Apple" />
246+
<InputSearch.ListItem value="Banana" />
247+
<InputSearch.ListItem value="Orange" />
248+
<InputSearch.ListItem value="Kiwi" />
249+
<InputSearch.ListItem value="Pineapple" />
250+
</InputSearch.List>
251+
</InputSearch.Popover>
252+
</InputSearch.Root>
253+
</Box>
254+
);
255+
};
256+
257+
export const Controlled = {
258+
render: ControlledTemplate,
259+
args: {},
260+
261+
parameters: {
262+
chromatic: { disableSnapshot: true },
263+
},
264+
};
265+
223266
const InteractionsTemplate: StoryFn<typeof InputSearch.Root> = () => (
224267
<Box css={{ width: "275px", height: "340px" }}>
225268
<InputSearch.Root aria-label="Example-Search" openOnFocus>

0 commit comments

Comments
 (0)