|
1 | 1 | import { useState } from "react";
|
2 | 2 | import type { JSX } from "react";
|
3 | 3 |
|
4 |
| -import { useComboboxFilter, useId } from "@lib-hooks"; |
5 |
| -import { Combobox, Field } from "@lib-components"; |
| 4 | +import { useId } from "@lib-hooks"; |
| 5 | +import { Combobox, Field, Option, Spinner } from "@lib-components"; |
6 | 6 | import type { TComboboxProps } from "@lib-components";
|
7 | 7 |
|
8 | 8 | import { OptionLayoutTemplate } from "../../templates";
|
9 | 9 | import useClasses from "./styles";
|
10 | 10 |
|
11 |
| -const options = [ |
12 |
| - // { children: "Alligator", value: "Alligator" }, |
13 |
| - // { children: "Bee", value: "Bee" }, |
14 |
| - // { children: "Bird", value: "Bird" }, |
15 |
| - // { children: "Dog", value: "Dog" }, |
16 |
| - // { children: "Dolphin", value: "Dolphin" }, |
17 |
| - // { children: "Ferret", value: "Ferret" }, |
18 |
| - // { children: "Firefly", value: "Firefly" }, |
19 |
| - // { children: "Fish", value: "Fish" }, |
20 |
| - // { children: "Goat", value: "Goat" }, |
21 |
| - // { children: "Horse", value: "Horse" }, |
22 |
| - // { children: "Lion", value: "Lion" }, |
23 |
| -]; |
| 11 | +type TProps = { |
| 12 | + results: string[]; |
| 13 | + isBouncing: boolean; |
| 14 | + isFetching: boolean; |
| 15 | + onChange: TComboboxProps["onChange"]; |
| 16 | + onOptionSelect: () => void; |
| 17 | +}; |
24 | 18 |
|
25 |
| -type TProps = {}; |
26 |
| - |
27 |
| -export default function OptionTrace({}: TProps): JSX.Element { |
| 19 | +export default function OptionTrace({ |
| 20 | + results, |
| 21 | + isBouncing, |
| 22 | + isFetching, |
| 23 | + onChange, |
| 24 | + onOptionSelect, |
| 25 | +}: TProps): JSX.Element { |
28 | 26 | const classes = useClasses();
|
29 |
| - const comboId = useId(); |
30 |
| - const [query, setQuery] = useState<string>(""); |
31 |
| - const currentQueryHasExactMatch = options.some( |
32 |
| - (option) => option.children === query, |
33 |
| - ); |
34 |
| - |
35 |
| - const children = useComboboxFilter(query, options, { |
36 |
| - noOptionsMessage: "No animals match your search.", |
37 |
| - }); |
38 |
| - const onOptionSelect: TComboboxProps["onOptionSelect"] = (_, data) => { |
39 |
| - setQuery(data.optionText ?? ""); |
40 |
| - }; |
| 27 | + const comboId = useId("combobox"); |
41 | 28 |
|
42 | 29 | return (
|
43 | 30 | <OptionLayoutTemplate
|
44 | 31 | header="Trace a Word"
|
45 | 32 | subtitle="Use a word as a anchor and find words that are related to it."
|
46 |
| - onClick={() => {}} |
47 | 33 | isLoading={false}
|
48 |
| - disableClick={false} |
49 |
| - buttonLabel="Construct trace tree" |
| 34 | + withoutButton |
50 | 35 | >
|
51 |
| - <Field |
52 |
| - label="Search" |
53 |
| - validationState={currentQueryHasExactMatch ? "success" : "error"} |
54 |
| - validationMessage={ |
55 |
| - currentQueryHasExactMatch |
56 |
| - ? "This word is known." |
57 |
| - : "This word is not known." |
58 |
| - } |
59 |
| - > |
| 36 | + <Field id={comboId} label="Select a known word"> |
60 | 37 | <Combobox
|
61 |
| - onOptionSelect={onOptionSelect} |
62 | 38 | aria-labelledby={comboId}
|
63 |
| - placeholder="Select a known word from the dictionary" |
64 |
| - onChange={(ev) => { |
65 |
| - setQuery(ev.target.value); |
66 |
| - }} |
67 |
| - value={query} |
| 39 | + placeholder="Start typing for suggestions" |
| 40 | + onChange={onChange} |
68 | 41 | >
|
69 |
| - {children} |
| 42 | + {isFetching ? ( |
| 43 | + <Option text="" disabled className={classes.loading}> |
| 44 | + <Spinner size="extra-small" label="Searching, hold on..." /> |
| 45 | + </Option> |
| 46 | + ) : ( |
| 47 | + results.map((word) => ( |
| 48 | + <Option |
| 49 | + key={word} |
| 50 | + text={word} |
| 51 | + disabled={isBouncing} |
| 52 | + onClick={onOptionSelect} |
| 53 | + > |
| 54 | + {word} |
| 55 | + </Option> |
| 56 | + )) |
| 57 | + )} |
| 58 | + |
| 59 | + {results.length === 0 && ( |
| 60 | + <Option text="" disabled className={classes.optionReadOnly}> |
| 61 | + No matching words found... |
| 62 | + </Option> |
| 63 | + )} |
70 | 64 | </Combobox>
|
71 | 65 | </Field>
|
72 | 66 | </OptionLayoutTemplate>
|
|
0 commit comments