Skip to content

Commit b3efa3e

Browse files
authored
fix: prevent frozen InputSearch after text editing (#658)
1 parent d819389 commit b3efa3e

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const InputSearchInput = React.forwardRef<
6565
if (value) {
6666
inputProps.value = value;
6767
}
68-
if (autocomplete && tempText !== undefined) {
68+
if (autocomplete && withKeyboard.current) {
6969
inputProps.value = tempText;
7070
}
7171

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,17 @@ export const InputSearchRoot = ({
100100
state
101101
);
102102

103+
const prevSelectedKey = React.useRef(state.selectedKey);
103104
React.useEffect(() => {
104-
if (state.selectedItem && onSelect) {
105+
if (
106+
state.selectedItem &&
107+
onSelect &&
108+
prevSelectedKey.current !== state.selectedKey
109+
) {
105110
onSelect(
106111
state.selectedItem.textValue || (state.selectedItem.rendered as string)
107112
);
113+
prevSelectedKey.current = state.selectedKey;
108114
}
109115
}, [state.selectedItem, onSelect]);
110116

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const useCityMatch = (term: string) => {
2828
term.trim() === ""
2929
? null
3030
: matchSorter(cities, term, {
31-
keys: [(item) => `${item.city}, ${item.state}`],
32-
}),
31+
keys: [(item) => `${item.city}, ${item.state}`],
32+
}),
3333
[term]
3434
);
3535
};
@@ -292,3 +292,23 @@ export const Interactions = {
292292
await expect(input).toHaveDisplayValue("Apple");
293293
},
294294
};
295+
296+
export const ControlledKeyboardInteractions = {
297+
render: ControlledTemplate,
298+
299+
play: async () => {
300+
const input = await screen.findByLabelText("Search");
301+
await userEvent.type(input, "app", {
302+
delay: 100,
303+
});
304+
await userEvent.keyboard("[ArrowDown]");
305+
await userEvent.keyboard("[ArrowDown]");
306+
await userEvent.keyboard("[ArrowDown]");
307+
await expect(input).toHaveDisplayValue("Orange");
308+
await userEvent.keyboard("[Backspace]");
309+
await expect(input).toHaveDisplayValue("Orang");
310+
const clearButton = await screen.findByText("Clear");
311+
await userEvent.click(clearButton);
312+
await expect(input).toHaveDisplayValue("");
313+
},
314+
};

0 commit comments

Comments
 (0)