Skip to content

Commit

Permalink
fix: fix tree label to accept jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
wildergd committed Jun 9, 2021
1 parent 2632a9b commit cc11cb9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/components/Tree/helpers/buildPlainListFromTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export default function buildPlainListFromTree(tree, parentName, parentPath) {
const nodeName = getNodeName({ parentName, index });
const level = getNodeLevel({ name: nodeName });
const nodePath = parentPath ? [...parentPath, index] : [index];
const nodeLabel = typeof node.label === 'string' ? node.label.toLowerCase() : node.label;
if (node.isExpanded) {
return [
...result,
{
name: nodeName,
label: node.label.toLowerCase(),
label: nodeLabel,
level,
nodePath,
isExpanded: node.isExpanded,
Expand All @@ -24,7 +25,7 @@ export default function buildPlainListFromTree(tree, parentName, parentPath) {
...result,
{
name: nodeName,
label: node.label.toLowerCase(),
label: nodeLabel,
level,
nodePath,
isExpanded: node.isExpanded,
Expand Down
7 changes: 6 additions & 1 deletion src/components/Tree/helpers/findNodeByFirstLetter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export default function findNodeByFirstLetter(nodes, searchTerm, fromIndex) {
const head = nodes.slice(0, fromIndex);
const tail = nodes.slice(fromIndex + 1);
return [...tail, ...head].find(node => node.label.indexOf(searchTerm.toLowerCase()) === 0);
return [...tail, ...head].find(node => {
if (typeof node.label === 'string') {
return node.label.indexOf(searchTerm.toLowerCase()) === 0;
}
return false;
});
}
3 changes: 2 additions & 1 deletion src/components/Tree/hooks/useTreeNodesAsPlainList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import buildPlainListFromTree from '../helpers/buildPlainListFromTree';
export default function useTreeNodesAsPlainList(data) {
const dataStr = JSON.stringify(data, ['label', 'isExpanded', 'children']);
return useMemo(() => {
return buildPlainListFromTree(JSON.parse(dataStr));
return buildPlainListFromTree(data);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dataStr]);
}

0 comments on commit cc11cb9

Please sign in to comment.