fix: searching bug fix

This commit is contained in:
duanhongbo committed 2020-11-26 12:29:40 +08:00
1 parent 6e56594e2d
commit 1240547930
1 file changed
+44 -27
+44 -27
View File
@@ -31,6 +31,7 @@ class FileDataNode implements DataNode {
style?: React.CSSProperties; style?: React.CSSProperties;
parent: FileDataNode; parent: FileDataNode;
visible: boolean = true;
public constructor(init?: Partial<FileDataNode>) { public constructor(init?: Partial<FileDataNode>) {
Object.assign(this, init); Object.assign(this, init);
@@ -48,6 +49,26 @@ class FileDataNode implements DataNode {
return this.key as string; return this.key as string;
} }
public getRenderData() {
if(!this.visible){
return null;
}
let ret = {
title: this.title,
key: this.key,
isLeaf: this.isLeaf,
children: new Array()
};
for (const child of this.children) {
const data = child.getRenderData();
if(data){
ret.children.push(data);
}
}
return ret;
}
loadChilds(recursive?: boolean) { loadChilds(recursive?: boolean) {
const folder = this.key as string; const folder = this.key as string;
if (folder == "" || !fs.existsSync(folder)) { if (folder == "" || !fs.existsSync(folder)) {
@@ -121,22 +142,19 @@ class FileDataNode implements DataNode {
return ret; return ret;
} }
removeChilds(exclude:string){ setVisible(keyWord:string){
for (let i = this.children.length-1;i >= 0;--i) { let ret = false;
for (let i = this.children.length-1;i >= 0;i--) {
const child = this.children[i]; const child = this.children[i];
if (child.isLeaf) { if (child.isLeaf) {
const keyStr = child.key as string; const keyStr = child.title as string;
if(!keyStr.includes(exclude)){ child.visible = keyStr.includes(keyWord);
this.children.splice(i,1);
}
} else { } else {
const childLen = child.removeChilds(exclude); child.visible = child.setVisible(keyWord);
if(childLen==0){
this.children.splice(i,1);
}
} }
ret = child.visible || ret;
} }
return this.children.length; return ret;
} }
// name:string; // name:string;
@@ -246,7 +264,7 @@ export default class Properties extends Component<PropertiesProps> {
// ); // );
// } // }
handleOnSearch(value: string, event: ChangeEvent) { handleOnSearch(value: string) {
const expandedKeys = this.state.root const expandedKeys = this.state.root
.getList() .getList()
.map((item) => { .map((item) => {
@@ -259,10 +277,10 @@ export default class Properties extends Component<PropertiesProps> {
return null; return null;
}) })
.filter((item, i, self) => item && self.indexOf(item) === i); .filter((item, i, self) => item && self.indexOf(item) === i);
const root = this.state.root;
root.setVisible(value||"");
if (value && value.length > 0) { if (value && value.length > 0) {
const root = this.state.root;
root.removeChilds(value);
this.setState({ this.setState({
root:root, root:root,
searchKey: value, searchKey: value,
@@ -270,8 +288,6 @@ export default class Properties extends Component<PropertiesProps> {
autoExpandParent: true, autoExpandParent: true,
}); });
} else { } else {
const root = this.getRootNode(this.props.workdir);
root.expandSelf();
this.setState({ this.setState({
root:root, root:root,
searchKey: "", searchKey: "",
@@ -283,12 +299,6 @@ export default class Properties extends Component<PropertiesProps> {
this.forceUpdate(); this.forceUpdate();
} }
filterNode(node: EventDataNode): boolean {
const searchKey = this.state.searchKey;
const nodeKey = node.title as string;
return nodeKey.includes(searchKey);
}
titleRender(node: DataNode): React.ReactNode { titleRender(node: DataNode): React.ReactNode {
const titleStr = node.title as string; const titleStr = node.title as string;
const searchKey = this.state.searchKey; const searchKey = this.state.searchKey;
@@ -297,7 +307,7 @@ export default class Properties extends Component<PropertiesProps> {
const afterStr = titleStr.substr(index + searchKey.length); const afterStr = titleStr.substr(index + searchKey.length);
//return (<span>{titleStr}</span>) //return (<span>{titleStr}</span>)
return index > -1 ? ( return node.isLeaf && index > -1 ? (
<span> <span>
{beforeStr} {beforeStr}
<span className="site-tree-search-value">{searchKey}</span> <span className="site-tree-search-value">{searchKey}</span>
@@ -328,13 +338,21 @@ export default class Properties extends Component<PropertiesProps> {
console.log("render Properties"); console.log("render Properties");
const { onOpenTree, onDeleteTree, workdir } = this.props; const { onOpenTree, onDeleteTree, workdir } = this.props;
const root = this.state.root; const root = this.state.root;
const nodes = root ? [root] : []; const nodes = root ? [root.getRenderData()] : [];
return nodes.length ? ( return nodes.length ? (
<div> <div>
<Search <Search
allowClear allowClear
placeholder="Search" placeholder="Search"
onSearch={this.handleOnSearch.bind(this)} onChange={(e)=>{
const value = e.target.value?.toLowerCase();
this.handleOnSearch(value);
}}
onSearch={
(value,event)=>{
this.handleOnSearch(value?.toLowerCase());
}
}
/> />
<DirectoryTree <DirectoryTree
selectable selectable
@@ -345,7 +363,6 @@ export default class Properties extends Component<PropertiesProps> {
autoExpandParent={this.state.autoExpandParent} autoExpandParent={this.state.autoExpandParent}
titleRender={this.titleRender.bind(this)} titleRender={this.titleRender.bind(this)}
treeData={nodes} treeData={nodes}
filterTreeNode={this.filterNode.bind(this)}
defaultExpandedKeys={[root.key]} defaultExpandedKeys={[root.key]}
onSelect={(keys, info) => { onSelect={(keys, info) => {
//console.log("onSelect", keys, info); //console.log("onSelect", keys, info);