ant-design/components/tree/demo/search.md

143 lines
3.2 KiB
Markdown
Raw Normal View History

2016-11-09 14:45:49 +08:00
---
order: 4
title:
zh-CN: 可搜索
en-US: Searchable
---
## zh-CN
可搜索的树。
## en-US
Searchable Tree.
2017-02-13 10:55:53 +08:00
````jsx
2016-11-09 14:45:49 +08:00
import { Tree, Input } from 'antd';
const TreeNode = Tree.TreeNode;
2016-11-24 14:56:08 +08:00
const Search = Input.Search;
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 = [];
const generateList = (data) => {
for (let i = 0; i < data.length; i++) {
const node = data[i];
const key = node.key;
dataList.push({ key, title: key });
if (node.children) {
generateList(node.children, node.key);
}
}
};
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 {
state = {
expandedKeys: [],
searchValue: '',
autoExpandParent: true,
2016-11-09 14:45:49 +08:00
}
onExpand = (expandedKeys) => {
this.setState({
expandedKeys,
autoExpandParent: false,
});
2016-11-09 14:45:49 +08:00
}
onChange = (e) => {
const value = e.target.value;
2017-01-11 20:20:38 +08:00
const expandedKeys = dataList.map((item) => {
2016-11-09 14:45:49 +08:00
if (item.key.indexOf(value) > -1) {
2017-01-11 20:20:38 +08:00
return getParentKey(item.key, gData);
2016-11-09 14:45:49 +08:00
}
2017-01-11 20:20:38 +08:00
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,
autoExpandParent: true,
2016-11-09 14:45:49 +08:00
});
}
render() {
const { searchValue, expandedKeys, autoExpandParent } = this.state;
2016-11-09 14:45:49 +08:00
const loop = data => data.map((item) => {
const index = item.key.search(searchValue);
const beforeStr = item.key.substr(0, index);
const afterStr = item.key.substr(index + searchValue.length);
2016-11-15 15:21:13 +08:00
const title = index > -1 ? (
<span>
{beforeStr}
2017-01-11 20:20:38 +08:00
<span style={{ color: '#f50' }}>{searchValue}</span>
2016-11-15 15:21:13 +08:00
{afterStr}
</span>
) : <span>{item.key}</span>;
2016-11-09 14:45:49 +08:00
if (item.children) {
return (
<TreeNode key={item.key} title={title}>
{loop(item.children)}
</TreeNode>
);
}
return <TreeNode key={item.key} title={title} />;
});
return (
<div>
2017-01-11 20:20:38 +08:00
<Search style={{ width: 300 }} placeholder="Search" onChange={this.onChange} />
2016-11-09 14:45:49 +08:00
<Tree
onExpand={this.onExpand}
expandedKeys={expandedKeys}
autoExpandParent={autoExpandParent}
2016-11-09 14:45:49 +08:00
>
{loop(gData)}
</Tree>
</div>
);
}
}
ReactDOM.render(<SearchTree />, mountNode);
````