From 7198da099acc19a319d5decc32caa0d0eeea7e87 Mon Sep 17 00:00:00 2001 From: mertsincan Date: Fri, 14 May 2021 00:33:18 +0300 Subject: [PATCH] Fixed #2037 - Improve onFilterValueChange event on Tree --- src/components/tree/Tree.d.ts | 7 ++++++- src/components/tree/Tree.js | 24 ++++++++++++++++-------- src/components/utils/ObjectUtils.js | 12 ++++++++++++ src/showcase/tree/TreeDoc.js | 6 ++++-- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/components/tree/Tree.d.ts b/src/components/tree/Tree.d.ts index f81801b39f..d95cdffdde 100644 --- a/src/components/tree/Tree.d.ts +++ b/src/components/tree/Tree.d.ts @@ -78,6 +78,11 @@ interface TreeDragDropParams { dropIndex: number; } +interface TreeFilterValueChangeParams { + originalEvent: React.FormEvent; + value: string; +} + export interface TreeProps { id?: string; value?: TreeNode[]; @@ -116,7 +121,7 @@ export interface TreeProps { onToggle?(e: TreeExpandedParams): void; onDragDrop?(e: TreeDragDropParams): void; onContextMenu?(e: TreeEventNodeParams): void; - onFilterValueChange?(e: React.FormEvent): void; + onFilterValueChange?(e: TreeFilterValueChangeParams): void; } export declare class Tree extends React.Component { diff --git a/src/components/tree/Tree.js b/src/components/tree/Tree.js index 6cf3e0adfe..483b91704c 100644 --- a/src/components/tree/Tree.js +++ b/src/components/tree/Tree.js @@ -310,7 +310,10 @@ export class Tree extends Component { let filterValue = event.target.value; if (this.props.onFilterValueChange) { - this.props.onFilterValueChange(filterValue); + this.props.onFilterValueChange({ + originalEvent: event, + value: filterValue + }); } else { this.setState({ filterValue }); @@ -318,7 +321,7 @@ export class Tree extends Component { } filter(value) { - this.setState({ filterValue: value||'' }, this._filter); + this.setState({ filterValue: ObjectUtils.isNotEmpty(value) ? value : '' }, this._filter); } _filter() { @@ -328,7 +331,7 @@ export class Tree extends Component { const filterValue = this.getFilterValue(); - if (filterValue === '') { + if (ObjectUtils.isEmpty(filterValue)) { this.filteredNodes = this.props.value; } else { @@ -442,11 +445,16 @@ export class Tree extends Component { renderFilter() { if (this.props.filter) { - return
- - -
; + let filterValue = this.getFilterValue(); + filterValue = ObjectUtils.isNotEmpty(filterValue) ? filterValue : ''; + + return ( +
+ + +
+ ); } return null; diff --git a/src/components/utils/ObjectUtils.js b/src/components/utils/ObjectUtils.js index 0325b0cb72..b40be0eb6c 100644 --- a/src/components/utils/ObjectUtils.js +++ b/src/components/utils/ObjectUtils.js @@ -158,4 +158,16 @@ export default class ObjectUtils { return str; } + + static isEmpty(value) { + return ( + value === null || value === undefined || value === '' || + (Array.isArray(value) && value.length === 0) || + (typeof value === 'object' && Object.keys(value).length === 0) + ); + } + + static isNotEmpty(value) { + return !this.isEmpty(value); + } } diff --git a/src/showcase/tree/TreeDoc.js b/src/showcase/tree/TreeDoc.js index 95c40c841f..e5eabb8b73 100644 --- a/src/showcase/tree/TreeDoc.js +++ b/src/showcase/tree/TreeDoc.js @@ -935,8 +935,10 @@ export const TreeContextMenuDemo = () => { onFilterValueChange - event: browser event - Callback to invoke when the filter value is changed. + event.originalEvent: Browser event
+ event.value: the filtered value
+ + Callback to invoke when filter value changes.