Skip to content

Commit

Permalink
fix(tree): toggle node when filtering mode (#7282)
Browse files Browse the repository at this point in the history
* fix: toggle node when filtering mode

* remove: delete not working code

* refactor: change default value

* style: skip react-hooks/exhaustive-deps lint

* fix: prevent event bubbling when dropdown click

* chore: revert unrelated change
  • Loading branch information
KumJungMin authored Jan 3, 2025
1 parent 7e663aa commit e921655
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
33 changes: 22 additions & 11 deletions components/lib/tree/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ export const Tree = React.memo(

const [filterValue, filterValueState, setFilterValueState] = useDebounce('', props.filterDelay || 0);
const [expandedKeysState, setExpandedKeysState] = React.useState(props.expandedKeys);
const [filterExpandedKeys, setFilterExpandedKeys] = React.useState({});

const elementRef = React.useRef(null);
const filteredNodes = React.useRef([]);
const dragState = React.useRef(null);
const filterChanged = React.useRef(false);

const filteredValue = props.onFilterValueChange ? props.filterValue : filterValueState;
const expandedKeys = props.onToggle ? props.expandedKeys : expandedKeysState;
const isFiltering = props.filter && filteredValue;
const expandedKeys = isFiltering ? filterExpandedKeys : props.onToggle ? props.expandedKeys : expandedKeysState;
const currentFilterExpandedKeys = {};

const childFocusEvent = React.useRef(null);
const { ptm, cx, isUnstyled } = TreeBase.setMetaData({
props,
Expand Down Expand Up @@ -52,7 +58,11 @@ export const Tree = React.memo(
childFocusEvent.current = originalEvent;
}

setExpandedKeysState(value);
if (isFiltering) {
setFilterExpandedKeys(value);
} else {
setExpandedKeysState(value);
}
}
};

Expand Down Expand Up @@ -80,6 +90,11 @@ export const Tree = React.memo(
}
}, [expandedKeys]);

React.useEffect(() => {
if (props.filter) _filter();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filteredValue, props.value, props.filter]);

const onDragStart = (event) => {
dragState.current = {
path: event.path,
Expand Down Expand Up @@ -299,20 +314,18 @@ export const Tree = React.memo(

const filter = (value) => {
setFilterValueState(ObjectUtils.isNotEmpty(value) ? value : '');
_filter();
};

const childNodeFocus = (node) => {};

const _filter = () => {
if (!filterChanged.current) {
return;
}
if (!filterChanged.current) return;

if (ObjectUtils.isEmpty(filteredValue)) {
filteredNodes.current = props.value;
} else {
filteredNodes.current = [];

const searchFields = props.filterBy.split(',');
const filterText = filteredValue.toLocaleLowerCase(props.filterLocale);
const isStrictMode = props.filterMode === 'strict';
Expand All @@ -330,6 +343,7 @@ export const Tree = React.memo(
}
}

setFilterExpandedKeys(currentFilterExpandedKeys);
filterChanged.current = false;
};

Expand All @@ -353,7 +367,7 @@ export const Tree = React.memo(
}

if (matched) {
node.expanded = true;
currentFilterExpandedKeys[node.key] = true;

return true;
}
Expand Down Expand Up @@ -475,10 +489,7 @@ export const Tree = React.memo(

const createModel = () => {
if (props.value) {
if (props.filter) {
filterChanged.current = true;
_filter();
}
if (props.filter) filterChanged.current = true;

const value = getRootNode();

Expand Down
2 changes: 1 addition & 1 deletion components/lib/tree/UITreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const UITreeNode = React.memo((props) => {
const mergeProps = useMergeProps();
const isLeaf = props.isNodeLeaf(props.node);
const label = props.node.label;
const expanded = (props.expandedKeys ? props.expandedKeys[props.node.key] !== undefined : false) || props.node.expanded;
const expanded = props.expandedKeys ? props.expandedKeys[props.node.key] !== undefined : false;
const { ptm, cx } = props;

const getPTOptions = (key) => {
Expand Down

0 comments on commit e921655

Please sign in to comment.