Skip to content

Commit dfd24f3

Browse files
authored
Fix #6747: Tree/TreeSelect add filterDelay (#7549)
1 parent b7c9164 commit dfd24f3

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

components/lib/tree/Tree.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as React from 'react';
22
import { localeOption, PrimeReactContext } from '../api/Api';
33
import { useHandleStyle } from '../componentbase/ComponentBase';
4-
import { useMergeProps } from '../hooks/Hooks';
5-
import { useUpdateEffect } from '../hooks/useUpdateEffect';
4+
import { useDebounce, useMergeProps, useUpdateEffect } from '../hooks/Hooks';
65
import { SearchIcon } from '../icons/search';
76
import { SpinnerIcon } from '../icons/spinner';
87
import { classNames, DomHandler, IconUtils, ObjectUtils } from '../utils/Utils';
@@ -15,7 +14,7 @@ export const Tree = React.memo(
1514
const context = React.useContext(PrimeReactContext);
1615
const props = TreeBase.getProps(inProps, context);
1716

18-
const [filterValueState, setFilterValueState] = React.useState('');
17+
const [filterValue, filterValueState, setFilterValueState] = useDebounce('', props.filterDelay || 0);
1918
const [expandedKeysState, setExpandedKeysState] = React.useState(props.expandedKeys);
2019
const elementRef = React.useRef(null);
2120
const filteredNodes = React.useRef([]);
@@ -523,7 +522,9 @@ export const Tree = React.memo(
523522

524523
const createFilter = () => {
525524
if (props.filter) {
526-
const value = ObjectUtils.isNotEmpty(filteredValue) ? filteredValue : '';
525+
let value = props.onFilterValueChange ? props.filterValue : filterValue;
526+
527+
value = ObjectUtils.isNotEmpty(value) ? value : '';
527528
const searchIconProps = mergeProps(
528529
{
529530
className: cx('searchIcon')

components/lib/tree/TreeBase.js

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export const TreeBase = ComponentBase.extend({
129129
expandedKeys: null,
130130
filter: false,
131131
filterBy: 'label',
132+
filterDelay: 300,
132133
filterIcon: null,
133134
filterLocale: undefined,
134135
filterMode: 'lenient',

components/lib/tree/tree.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ export interface TreeProps {
542542
* @defaultValue false
543543
*/
544544
filter?: boolean | undefined;
545+
/**
546+
* Delay in milliseconds before filtering the data.
547+
* @defaultValue 300
548+
*/
549+
filterDelay?: number | undefined;
545550
/**
546551
* Icon of the filter.
547552
*/

components/lib/treeselect/TreeSelect.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
2-
import PrimeReact, { PrimeReactContext, localeOption, ariaLabel } from '../api/Api';
2+
import PrimeReact, { PrimeReactContext, ariaLabel, localeOption } from '../api/Api';
33
import { useHandleStyle } from '../componentbase/ComponentBase';
4-
import { useMergeProps, useMountEffect, useOverlayListener, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
4+
import { useDebounce, useMergeProps, useMountEffect, useOverlayListener, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
55
import { ChevronDownIcon } from '../icons/chevrondown';
66
import { SearchIcon } from '../icons/search';
77
import { TimesIcon } from '../icons/times';
@@ -22,7 +22,7 @@ export const TreeSelect = React.memo(
2222
const [focusedState, setFocusedState] = React.useState(false);
2323
const [overlayVisibleState, setOverlayVisibleState] = React.useState(false);
2424
const [expandedKeysState, setExpandedKeysState] = React.useState(props.expandedKeys);
25-
const [filterValueState, setFilterValueState] = React.useState('');
25+
const [filterValue, filterValueState, setFilterValueState] = useDebounce('', props.filterDelay || 0);
2626
const elementRef = React.useRef(null);
2727
const overlayRef = React.useRef(null);
2828
const filterInputRef = React.useRef(null);
@@ -667,6 +667,7 @@ export const TreeSelect = React.memo(
667667
expandedKeys={expandedKeys}
668668
filter={props.filter}
669669
filterBy={props.filterBy}
670+
filterDelay={props.filterDelay}
670671
filterLocale={props.filterLocale}
671672
filterMode={props.filterMode}
672673
filterPlaceholder={props.filterPlaceholder}
@@ -696,7 +697,9 @@ export const TreeSelect = React.memo(
696697

697698
const createFilterElement = () => {
698699
if (props.filter) {
699-
const filterValue = ObjectUtils.isNotEmpty(filteredValue) ? filteredValue : '';
700+
let filterValue = props.onFilterValueChange ? props.filterValue : filterValue;
701+
702+
filterValue = ObjectUtils.isNotEmpty(filterValue) ? filterValue : '';
700703
const filterContainerProps = mergeProps(
701704
{
702705
className: cx('filterContainer')

components/lib/treeselect/TreeSelectBase.js

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export const TreeSelectBase = ComponentBase.extend({
150150
expandedKeys: null,
151151
filter: false,
152152
filterBy: 'label',
153+
filterDelay: 300,
153154
filterIcon: null,
154155
filterInputAutoFocus: true,
155156
filterLocale: undefined,

components/lib/treeselect/treeselect.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@ export interface TreeSelectProps extends Omit<React.DetailedHTMLProps<React.Inpu
428428
* @defaultValue label
429429
*/
430430
filterBy?: string | undefined;
431+
/**
432+
* Delay in milliseconds before filtering the data.
433+
* @defaultValue 300
434+
*/
435+
filterDelay?: number | undefined;
431436
/**
432437
* Icon of the filter.
433438
*/

0 commit comments

Comments
 (0)