Skip to content

Add hasNodes property to TreeNode for overriding #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ Note the difference between the state `active` and `focused`. ENTER is equivalen
| -------- | ---------------------------------------------------------------------------------------------------------------------- | --------------------------------- | ------- |
| label | the rendered text of a Node | string | '' |
| index | a number that defines the rendering order of this node on the same level; this is not needed if `data` is `TreeNode[]` | number | - |
| hasNodes | If passed, its value will be used to decide whether the node can be expanded. Otherwise, it will be decided based on if `nodes` contains any value | boolean | - |
| nodes | a node without this property means that it is the last child of its branch | {[string]:TreeNode} \| TreeNode[] | - |
| ...other | User defined props | any | - |

Expand All @@ -192,6 +193,7 @@ Note the difference between the state `active` and `focused`. ENTER is equivalen
| -------- | ---------------------------------------------------------------------------------------------------------------------- | --------------------------------- | ------- |
| key | Node name | string | - |
| label | the rendered text of a Node | string | '' |
| hasNodes | If passed, its value will be used to decide whether the node can be expanded. Otherwise, it will be decided based on if `nodes` contains any value | boolean
| nodes | a node without this property means that it is the last child of its branch | {[string]:TreeNode} \| TreeNode[] | - |
| ...other | User defined props | any | - |

Expand Down
98 changes: 97 additions & 1 deletion src/TreeMenu/__tests__/walk.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {slowWalk,fastWalk, TreeNode, TreeNodeObject, TreeNodeInArray } from '../walk';
import { slowWalk, fastWalk, TreeNode, TreeNodeObject, TreeNodeInArray } from '../walk';

const mockDataInObject: TreeNodeObject = {
atd: {
Expand Down Expand Up @@ -97,6 +97,68 @@ const expectedOutcome = [
},
];

const hasNodesMockDataInObject: TreeNodeObject = {
A: {
label: 'A',
index: 0,
nodes: {
B: {
label: 'B',
index: 0,
},
},
},
C: {
label: 'C',
index: 1,
hasNodes: true,
},
};

const hasNodesMockDataInArray: TreeNodeInArray[] = [
{
key: 'A',
label: 'A',
nodes: [
{
key: 'B',
label: 'B',
nodes: [],
},
],
},
{
key: 'C',
label: 'C',
hasNodes: true,
},
];

const hasNodesExpectedOutcome = [
{
index: 0,
isOpen: false,
key: 'A',
label: 'A',
level: 0,
hasNodes: true,
openNodes: [],
parent: '',
searchTerm: '',
},
{
index: 1,
isOpen: false,
key: 'C',
label: 'C',
level: 0,
hasNodes: true,
openNodes: [],
parent: '',
searchTerm: '',
},
];

describe('slowWalk', () => {
it('should transpose the data object to a desired shape', () => {
const result = slowWalk({ data: mockDataInObject, openNodes: [], searchTerm: '7' });
Expand All @@ -106,6 +168,23 @@ describe('slowWalk', () => {
const result = slowWalk({ data: mockDataInArray, openNodes: [], searchTerm: '7' });
expect(result).toEqual(expectedOutcome);
});

it('should transpose the data object with supplied hasNodes', () => {
const result = slowWalk({
data: hasNodesMockDataInObject,
openNodes: [],
searchTerm: '',
});
expect(result).toEqual(hasNodesExpectedOutcome);
});
it('should transpose the data array with supplied hasNodes', () => {
const result = slowWalk({
data: hasNodesMockDataInArray,
openNodes: [],
searchTerm: '',
});
expect(result).toEqual(hasNodesExpectedOutcome);
});
});

describe('fastWalk', () => {
Expand All @@ -117,4 +196,21 @@ describe('fastWalk', () => {
const result = fastWalk({ data: mockDataInArray, openNodes: [], searchTerm: '7' });
expect(result).toEqual(expectedOutcome);
});

it('should transpose the data object with supplied hasNodes', () => {
const result = fastWalk({
data: hasNodesMockDataInObject,
openNodes: [],
searchTerm: '',
});
expect(result).toEqual(hasNodesExpectedOutcome);
});
it('should transpose the data array with supplied hasNodes', () => {
const result = fastWalk({
data: hasNodesMockDataInArray,
openNodes: [],
searchTerm: '',
});
expect(result).toEqual(hasNodesExpectedOutcome);
});
});
19 changes: 14 additions & 5 deletions src/TreeMenu/walk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ interface LocaleFunctionProps {
[name: string]: any;
}

interface MatchSearchFunctionProps extends LocaleFunctionProps {
interface OverrideProps extends LocaleFunctionProps {
hasNodes?: boolean;
}

interface MatchSearchFunctionProps extends OverrideProps {
searchTerm: string;
}

export interface TreeNode extends LocaleFunctionProps {
export interface TreeNode extends OverrideProps {
index: number;
nodes?: TreeNodeObject;
}

export interface TreeNodeInArray extends LocaleFunctionProps {
export interface TreeNodeInArray extends OverrideProps {
key: string;
nodes?: TreeNodeInArray[];
}
Expand Down Expand Up @@ -106,9 +110,14 @@ const generateBranch = ({
}: BranchProps): Item[] => {
const { parent, level, openNodes, searchTerm } = props;

const { nodes, label: rawLabel = 'unknown', ...nodeProps } = node;
const {
nodes,
label: rawLabel = 'unknown',
hasNodes: nodeHasNodes,
...nodeProps
} = node;
const key = [parent, nodeName].filter(x => x).join('/');
const hasNodes = validateData(nodes);
const hasNodes = nodeHasNodes || validateData(nodes);
const isOpen = hasNodes && (openNodes.includes(key) || !!searchTerm);

const label = locale({ label: rawLabel, ...nodeProps });
Expand Down