2016-11-09 14:45:49 +08:00
|
|
|
---
|
|
|
|
order: 4
|
|
|
|
title:
|
|
|
|
zh-CN: 可搜索
|
|
|
|
en-US: Searchable
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
可搜索的树。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Searchable Tree.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2016-11-09 14:45:49 +08:00
|
|
|
import { Tree, Input } from 'antd';
|
|
|
|
|
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;
|
|
|
|
const gData = [];
|
|
|
|
|
|
|
|
const generateData = (_level, _preKey, _tns) => {
|
|
|
|
const preKey = _preKey || '0';
|
|
|
|
const tns = _tns || gData;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
const dataList = [];
|
2019-05-07 14:57:32 +08:00
|
|
|
const generateList = data => {
|
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;
|
2016-11-09 14:45:49 +08:00
|
|
|
dataList.push({ key, title: key });
|
|
|
|
if (node.children) {
|
2019-03-23 12:19:36 +08:00
|
|
|
generateList(node.children);
|
2016-11-09 14:45:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
generateList(gData);
|
|
|
|
|
|
|
|
const getParentKey = (key, tree) => {
|
|
|
|
let parentKey;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parentKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SearchTree extends React.Component {
|
2016-12-15 21:45:56 +08:00
|
|
|
state = {
|
|
|
|
expandedKeys: [],
|
|
|
|
searchValue: '',
|
|
|
|
autoExpandParent: true,
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
onExpand = expandedKeys => {
|
2016-12-15 21:45:56 +08:00
|
|
|
this.setState({
|
|
|
|
expandedKeys,
|
|
|
|
autoExpandParent: false,
|
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
onChange = e => {
|
2019-06-19 19:09:08 +08:00
|
|
|
const { value } = e.target;
|
2019-05-07 14:57:32 +08:00
|
|
|
const expandedKeys = dataList
|
|
|
|
.map(item => {
|
|
|
|
if (item.title.indexOf(value) > -1) {
|
|
|
|
return getParentKey(item.key, gData);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.filter((item, i, self) => item && self.indexOf(item) === i);
|
2016-11-09 14:45:49 +08:00
|
|
|
this.setState({
|
2017-01-11 20:20:38 +08:00
|
|
|
expandedKeys,
|
2016-11-09 14:45:49 +08:00
|
|
|
searchValue: value,
|
2016-12-15 21:45:56 +08:00
|
|
|
autoExpandParent: true,
|
2016-11-09 14:45:49 +08:00
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2016-11-09 14:45:49 +08:00
|
|
|
render() {
|
2016-12-15 21:45:56 +08:00
|
|
|
const { searchValue, expandedKeys, autoExpandParent } = this.state;
|
2019-05-07 14:57:32 +08:00
|
|
|
const loop = data =>
|
|
|
|
data.map(item => {
|
|
|
|
const index = item.title.indexOf(searchValue);
|
2022-03-15 10:41:12 +08:00
|
|
|
const beforeStr = item.title.substring(0, index);
|
|
|
|
const afterStr = item.title.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>
|
|
|
|
) : (
|
|
|
|
<span>{item.title}</span>
|
|
|
|
);
|
|
|
|
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
|
|
|
return (
|
|
|
|
<div>
|
2017-09-13 22:18:44 +08:00
|
|
|
<Search style={{ marginBottom: 8 }} placeholder="Search" onChange={this.onChange} />
|
2016-11-09 14:45:49 +08:00
|
|
|
<Tree
|
|
|
|
onExpand={this.onExpand}
|
|
|
|
expandedKeys={expandedKeys}
|
2016-12-15 21:45:56 +08:00
|
|
|
autoExpandParent={autoExpandParent}
|
2019-08-12 13:22:36 +08:00
|
|
|
treeData={loop(gData)}
|
|
|
|
/>
|
2016-11-09 14:45:49 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 23:27:45 +08:00
|
|
|
export default () => <SearchTree />;
|
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>
|