feat: search nested fields in tables

This commit is contained in:
Huy Bui 2024-06-17 00:43:43 +07:00
parent 7716980be4
commit 960d8feec3
2 changed files with 42 additions and 17 deletions

View File

@ -1,40 +1,64 @@
import { useMemo, useState } from "react"; import { useMemo } from "react";
import { useSelect } from "../../../hooks"; import { useSelect } from "../../../hooks";
import { AutoComplete } from "@douyinfe/semi-ui"; import { TreeSelect } from "@douyinfe/semi-ui";
import { IconSearch } from "@douyinfe/semi-icons"; import { IconSearch } from "@douyinfe/semi-icons";
import { ObjectType } from "../../../data/constants"; import { ObjectType } from "../../../data/constants";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
export default function SearchBar({ tables }) { export default function SearchBar({ tables }) {
const { setSelectedElement } = useSelect(); const { setSelectedElement } = useSelect();
const [searchText, setSearchText] = useState("");
const { t } = useTranslation(); const { t } = useTranslation();
const filteredTable = useMemo(
() => tables.map((t) => t.name).filter((i) => i.includes(searchText)), const treeData = useMemo(() => {
[tables, searchText], return tables.map(({ id, name: parentName, fields }, i) => {
); const children = fields.map(({ name }, j) => ({
tableId: id,
id: `${j}`,
label: name,
value: name,
key: `${i}-${j}`,
}));
return {
tableId: id,
id: `${i}`,
label: parentName,
value: parentName,
key: `${i}`,
children,
};
});
}, [tables]);
return ( return (
<AutoComplete <TreeSelect
data={filteredTable} searchPosition="trigger"
value={searchText} dropdownStyle={{ maxHeight: 400, overflow: "auto" }}
showClear treeData={treeData}
prefix={<IconSearch />} prefix={<IconSearch />}
placeholder={t("search")}
emptyContent={<div className="p-3 popover-theme">{t("not_found")}</div>} emptyContent={<div className="p-3 popover-theme">{t("not_found")}</div>}
onChange={(v) => setSearchText(v)} filterTreeNode
onSelect={(v) => { placeholder={t("search")}
const { id } = tables.find((t) => t.name === v); onChange={(node) => {
const { tableId, id, children } = node;
setSelectedElement((prev) => ({ setSelectedElement((prev) => ({
...prev, ...prev,
id: id, id: tableId,
open: true, open: true,
element: ObjectType.TABLE, element: ObjectType.TABLE,
})); }));
document document
.getElementById(`scroll_table_${id}`) .getElementById(`scroll_table_${tableId}`)
.scrollIntoView({ behavior: "smooth" }); .scrollIntoView({ behavior: "smooth" });
if (!children) {
document
.getElementById(`scroll_table_${tableId}_input_${id}`)
.focus();
}
}} }}
onChangeWithObject
className="w-full" className="w-full"
/> />
); );

View File

@ -19,6 +19,7 @@ export default function TableField({ data, tid, index }) {
<Row gutter={6} className="hover-1 my-2"> <Row gutter={6} className="hover-1 my-2">
<Col span={7}> <Col span={7}>
<Input <Input
id={`scroll_table_${tid}_input_${index}`}
value={data.name} value={data.name}
validateStatus={data.name.trim() === "" ? "error" : "default"} validateStatus={data.name.trim() === "" ? "error" : "default"}
placeholder="Name" placeholder="Name"