This repository has been archived by the owner on Nov 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpensiveFunction.js
57 lines (52 loc) · 1.62 KB
/
ExpensiveFunction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { useState, useMemo } from "react";
import { useCombobox } from "../hooks/useCombobox";
import { useForceRerender } from "../hooks/useForceRerender";
import { getItems } from "../utils/filterCities";
import Menu from "../components/Menu";
function ExpensiveFunction() {
const forceRerender = useForceRerender();
const [inputValue, setInputValue] = useState("");
const allItems = useMemo(() => getItems(inputValue), [inputValue]);
const items = allItems.slice(0, 100);
const {
selectedItem,
highlightedIndex,
getComboboxProps,
getInputProps,
getItemProps,
getLabelProps,
getMenuProps,
selectItem,
} = useCombobox({
items,
inputValue,
onInputValueChange: ({ inputValue: newValue }) => setInputValue(newValue),
onSelectedItemChange: ({ selectedItem }) =>
alert(
selectedItem ? `You selected ${selectedItem.name}` : "Selection Cleared"
),
itemToString: (item) => (item ? item.name : ""),
});
return (
<div className="city-app">
<button onClick={forceRerender}>force rerender</button>
<div>
<label {...getLabelProps()}>Find a city</label>
<div {...getComboboxProps()}>
<input {...getInputProps({ type: "text" })} />
<button onClick={() => selectItem(null)} aria-label="toggle menu">
✕
</button>
</div>
<Menu
items={items}
getMenuProps={getMenuProps}
getItemProps={getItemProps}
highlightedIndex={highlightedIndex}
selectedItem={selectedItem}
/>
</div>
</div>
);
}
export { ExpensiveFunction };