2016-11-09 14:45:49 +08:00
|
|
|
---
|
|
|
|
order: 4
|
|
|
|
title:
|
|
|
|
zh-CN: 可搜索
|
|
|
|
en-US: Searchable
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
可搜索的树。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Searchable Tree.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-23 14:37:16 +08:00
|
|
|
import { Input, Tree } from 'antd';
|
2022-07-04 22:09:54 +08:00
|
|
|
import type { DataNode } from 'antd/es/tree';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React, { useMemo, useState } from 'react';
|
2016-11-09 14:45:49 +08:00
|
|
|
|
2019-06-19 19:09:08 +08:00
|
|
|
const { Search } = Input;
|
2016-11-09 14:45:49 +08:00
|
|
|
|
|
|
|
const x = 3;
|
|
|
|
const y = 2;
|
|
|
|
const z = 1;
|
2022-05-19 09:46:26 +08:00
|
|
|
const defaultData: DataNode[] = [];
|
2016-11-09 14:45:49 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const generateData = (_level: number, _preKey?: React.Key, _tns?: DataNode[]) => {
|
2016-11-09 14:45:49 +08:00
|
|
|
const preKey = _preKey || '0';
|
2022-05-19 09:46:26 +08:00
|
|
|
const tns = _tns || defaultData;
|
2016-11-09 14:45:49 +08:00
|
|
|
|
|
|
|
const children = [];
|
|
|
|
for (let i = 0; i < x; i++) {
|
|
|
|
const key = `${preKey}-${i}`;
|
|
|
|
tns.push({ title: key, key });
|
|
|
|
if (i < y) {
|
|
|
|
children.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_level < 0) {
|
|
|
|
return tns;
|
|
|
|
}
|
|
|
|
const level = _level - 1;
|
|
|
|
children.forEach((key, index) => {
|
|
|
|
tns[index].children = [];
|
|
|
|
return generateData(level, key, tns[index].children);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
generateData(z);
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const dataList: { key: React.Key; title: string }[] = [];
|
|
|
|
const generateList = (data: DataNode[]) => {
|
2016-11-09 14:45:49 +08:00
|
|
|
for (let i = 0; i < data.length; i++) {
|
|
|
|
const node = data[i];
|
2019-06-19 19:09:08 +08:00
|
|
|
const { key } = node;
|
2022-05-19 09:46:26 +08:00
|
|
|
dataList.push({ key, title: key as string });
|
2016-11-09 14:45:49 +08:00
|
|
|
if (node.children) {
|
2019-03-23 12:19:36 +08:00
|
|
|
generateList(node.children);
|
2016-11-09 14:45:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-05-19 09:46:26 +08:00
|
|
|
generateList(defaultData);
|
2016-11-09 14:45:49 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const getParentKey = (key: React.Key, tree: DataNode[]): React.Key => {
|
|
|
|
let parentKey: React.Key;
|
2016-11-09 14:45:49 +08:00
|
|
|
for (let i = 0; i < tree.length; i++) {
|
|
|
|
const node = tree[i];
|
|
|
|
if (node.children) {
|
|
|
|
if (node.children.some(item => item.key === key)) {
|
|
|
|
parentKey = node.key;
|
|
|
|
} else if (getParentKey(key, node.children)) {
|
|
|
|
parentKey = getParentKey(key, node.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-19 09:46:26 +08:00
|
|
|
return parentKey!;
|
2016-11-09 14:45:49 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [expandedKeys, setExpandedKeys] = useState<React.Key[]>([]);
|
|
|
|
const [searchValue, setSearchValue] = useState('');
|
|
|
|
const [autoExpandParent, setAutoExpandParent] = useState(true);
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const onExpand = (newExpandedKeys: string[]) => {
|
|
|
|
setExpandedKeys(newExpandedKeys);
|
|
|
|
setAutoExpandParent(false);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
2019-06-19 19:09:08 +08:00
|
|
|
const { value } = e.target;
|
2022-05-19 09:46:26 +08:00
|
|
|
const newExpandedKeys = dataList
|
2019-05-07 14:57:32 +08:00
|
|
|
.map(item => {
|
|
|
|
if (item.title.indexOf(value) > -1) {
|
2022-05-19 09:46:26 +08:00
|
|
|
return getParentKey(item.key, defaultData);
|
2019-05-07 14:57:32 +08:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.filter((item, i, self) => item && self.indexOf(item) === i);
|
2022-05-19 09:46:26 +08:00
|
|
|
setExpandedKeys(newExpandedKeys as React.Key[]);
|
|
|
|
setSearchValue(value);
|
|
|
|
setAutoExpandParent(true);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const treeData = useMemo(() => {
|
|
|
|
const loop = (data: DataNode[]): DataNode[] =>
|
2019-05-07 14:57:32 +08:00
|
|
|
data.map(item => {
|
2022-05-19 09:46:26 +08:00
|
|
|
const strTitle = item.title as string;
|
|
|
|
const index = strTitle.indexOf(searchValue);
|
|
|
|
const beforeStr = strTitle.substring(0, index);
|
|
|
|
const afterStr = strTitle.slice(index + searchValue.length);
|
2019-05-07 14:57:32 +08:00
|
|
|
const title =
|
|
|
|
index > -1 ? (
|
|
|
|
<span>
|
|
|
|
{beforeStr}
|
2019-12-24 20:58:42 +08:00
|
|
|
<span className="site-tree-search-value">{searchValue}</span>
|
2019-05-07 14:57:32 +08:00
|
|
|
{afterStr}
|
|
|
|
</span>
|
|
|
|
) : (
|
2022-06-06 23:23:11 +08:00
|
|
|
<span>{strTitle}</span>
|
2019-05-07 14:57:32 +08:00
|
|
|
);
|
|
|
|
if (item.children) {
|
2019-08-12 13:22:36 +08:00
|
|
|
return { title, key: item.key, children: loop(item.children) };
|
2019-05-07 14:57:32 +08:00
|
|
|
}
|
2019-08-12 13:22:36 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
title,
|
|
|
|
key: item.key,
|
|
|
|
};
|
2019-05-07 14:57:32 +08:00
|
|
|
});
|
2016-11-09 14:45:49 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return loop(defaultData);
|
|
|
|
}, [searchValue]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Search style={{ marginBottom: 8 }} placeholder="Search" onChange={onChange} />
|
|
|
|
<Tree
|
|
|
|
onExpand={onExpand}
|
|
|
|
expandedKeys={expandedKeys}
|
|
|
|
autoExpandParent={autoExpandParent}
|
|
|
|
treeData={treeData}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2019-12-24 20:58:42 +08:00
|
|
|
|
|
|
|
```css
|
|
|
|
.site-tree-search-value {
|
|
|
|
color: #f50;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
<style>
|
|
|
|
[data-theme="dark"] .site-tree-search-value {
|
|
|
|
color: #d84a1b;
|
|
|
|
}
|
|
|
|
</style>
|