|
1 |
| -import { useMemo, useState } from "react"; |
| 1 | +import { useMemo } from "react"; |
2 | 2 | import { useSelect } from "../../../hooks";
|
3 |
| -import { AutoComplete } from "@douyinfe/semi-ui"; |
| 3 | +import { TreeSelect } from "@douyinfe/semi-ui"; |
4 | 4 | import { IconSearch } from "@douyinfe/semi-icons";
|
5 | 5 | import { ObjectType } from "../../../data/constants";
|
6 | 6 | import { useTranslation } from "react-i18next";
|
7 | 7 |
|
8 | 8 | export default function SearchBar({ tables }) {
|
9 | 9 | const { setSelectedElement } = useSelect();
|
10 |
| - const [searchText, setSearchText] = useState(""); |
11 | 10 | const { t } = useTranslation();
|
12 |
| - const filteredTable = useMemo( |
13 |
| - () => tables.map((t) => t.name).filter((i) => i.includes(searchText)), |
14 |
| - [tables, searchText], |
15 |
| - ); |
| 11 | + |
| 12 | + const treeData = useMemo(() => { |
| 13 | + return tables.map(({ id, name: parentName, fields }, i) => { |
| 14 | + const children = fields.map(({ name }, j) => ({ |
| 15 | + tableId: id, |
| 16 | + id: `${j}`, |
| 17 | + label: name, |
| 18 | + value: name, |
| 19 | + key: `${i}-${j}`, |
| 20 | + })); |
| 21 | + |
| 22 | + return { |
| 23 | + tableId: id, |
| 24 | + id: `${i}`, |
| 25 | + label: parentName, |
| 26 | + value: parentName, |
| 27 | + key: `${i}`, |
| 28 | + children, |
| 29 | + }; |
| 30 | + }); |
| 31 | + }, [tables]); |
16 | 32 |
|
17 | 33 | return (
|
18 |
| - <AutoComplete |
19 |
| - data={filteredTable} |
20 |
| - value={searchText} |
21 |
| - showClear |
| 34 | + <TreeSelect |
| 35 | + searchPosition="trigger" |
| 36 | + dropdownStyle={{ maxHeight: 400, overflow: "auto" }} |
| 37 | + treeData={treeData} |
22 | 38 | prefix={<IconSearch />}
|
23 |
| - placeholder={t("search")} |
24 | 39 | emptyContent={<div className="p-3 popover-theme">{t("not_found")}</div>}
|
25 |
| - onChange={(v) => setSearchText(v)} |
26 |
| - onSelect={(v) => { |
27 |
| - const { id } = tables.find((t) => t.name === v); |
| 40 | + filterTreeNode |
| 41 | + placeholder={t("search")} |
| 42 | + onChange={(node) => { |
| 43 | + const { tableId, id, children } = node; |
| 44 | + |
28 | 45 | setSelectedElement((prev) => ({
|
29 | 46 | ...prev,
|
30 |
| - id: id, |
| 47 | + id: tableId, |
31 | 48 | open: true,
|
32 | 49 | element: ObjectType.TABLE,
|
33 | 50 | }));
|
34 | 51 | document
|
35 |
| - .getElementById(`scroll_table_${id}`) |
| 52 | + .getElementById(`scroll_table_${tableId}`) |
36 | 53 | .scrollIntoView({ behavior: "smooth" });
|
| 54 | + |
| 55 | + if (!children) { |
| 56 | + document |
| 57 | + .getElementById(`scroll_table_${tableId}_input_${id}`) |
| 58 | + .focus(); |
| 59 | + } |
37 | 60 | }}
|
| 61 | + onChangeWithObject |
38 | 62 | className="w-full"
|
39 | 63 | />
|
40 | 64 | );
|
|
0 commit comments