Skip to content

Commit

Permalink
Fixed #2037 - Improve onFilterValueChange event on Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed May 13, 2021
1 parent b9c0622 commit 7198da0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/components/tree/Tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ interface TreeDragDropParams {
dropIndex: number;
}

interface TreeFilterValueChangeParams {
originalEvent: React.FormEvent<HTMLInputElement>;
value: string;
}

export interface TreeProps {
id?: string;
value?: TreeNode[];
Expand Down Expand Up @@ -116,7 +121,7 @@ export interface TreeProps {
onToggle?(e: TreeExpandedParams): void;
onDragDrop?(e: TreeDragDropParams): void;
onContextMenu?(e: TreeEventNodeParams): void;
onFilterValueChange?(e: React.FormEvent<HTMLInputElement>): void;
onFilterValueChange?(e: TreeFilterValueChangeParams): void;
}

export declare class Tree extends React.Component<TreeProps, any> {
Expand Down
24 changes: 16 additions & 8 deletions src/components/tree/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,18 @@ 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 });
}
}

filter(value) {
this.setState({ filterValue: value||'' }, this._filter);
this.setState({ filterValue: ObjectUtils.isNotEmpty(value) ? value : '' }, this._filter);
}

_filter() {
Expand All @@ -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 {
Expand Down Expand Up @@ -442,11 +445,16 @@ export class Tree extends Component {

renderFilter() {
if (this.props.filter) {
return <div className="p-tree-filter-container">
<input type="text" autoComplete="off" className="p-tree-filter p-inputtext p-component" placeholder={this.props.filterPlaceholder}
onKeyDown={this.onFilterInputKeyDown} onChange={this.onFilterInputChange} disabled={this.props.disabled} />
<span className="p-tree-filter-icon pi pi-search"></span>
</div>;
let filterValue = this.getFilterValue();
filterValue = ObjectUtils.isNotEmpty(filterValue) ? filterValue : '';

return (
<div className="p-tree-filter-container">
<input type="text" value={filterValue} autoComplete="off" className="p-tree-filter p-inputtext p-component" placeholder={this.props.filterPlaceholder}
onKeyDown={this.onFilterInputKeyDown} onChange={this.onFilterInputChange} disabled={this.props.disabled} />
<span className="p-tree-filter-icon pi pi-search"></span>
</div>
);
}

return null;
Expand Down
12 changes: 12 additions & 0 deletions src/components/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 4 additions & 2 deletions src/showcase/tree/TreeDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,10 @@ export const TreeContextMenuDemo = () => {
</tr>
<tr>
<td>onFilterValueChange</td>
<td>event: browser event</td>
<td>Callback to invoke when the filter value is changed.</td>
<td>event.originalEvent: Browser event <br/>
event.value: the filtered value <br/>
</td>
<td>Callback to invoke when filter value changes.</td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit 7198da0

Please sign in to comment.