From 2234e7b94f746554f2afe78d139fe45942ed14d3 Mon Sep 17 00:00:00 2001 From: Maxwell Kendall Date: Tue, 31 Mar 2020 14:49:09 -0400 Subject: [PATCH] [ftr/pass-node-properties-to-tree] flattenNodes returns an object with a new shape. The goal here is to pass the caller of CheckboxTree -- onClick/onExpand/onCheck -- a node w/ the same data passed to it at invocation inside the nodes array. We now return all properties of the object passed at invocation under the keyspace "extraKeys": { ...node } --- src/js/NodeModel.js | 3 +++ src/js/constants.js | 14 ++++++++++++++ test/CheckboxTree.js | 9 ++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/js/NodeModel.js b/src/js/NodeModel.js index 82797d56..aae6788e 100644 --- a/src/js/NodeModel.js +++ b/src/js/NodeModel.js @@ -51,6 +51,9 @@ class NodeModel { disabled: this.getDisabledState(node, parent, disabled, noCascade), treeDepth: depth, index, + extraKeys: Object.entries(node) + .filter(([key]) => !CheckModel.flatKeys.includes(key)) + .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}), }; this.flattenNodes(node.children, node, depth + 1); }); diff --git a/src/js/constants.js b/src/js/constants.js index b06e313f..5061240b 100644 --- a/src/js/constants.js +++ b/src/js/constants.js @@ -2,6 +2,20 @@ const CheckModel = { ALL: 'all', PARENT: 'parent', LEAF: 'leaf', + flatKeys: [ + 'label', + 'value', + 'children', + 'parent', + 'isChild', + 'isParent', + 'isLeaf', + 'showCheckbox', + 'disabled', + 'treeDepth', + 'index', + 'extraKeys', + ], }; export default { CheckModel }; diff --git a/test/CheckboxTree.js b/test/CheckboxTree.js index 4c7f3c26..1b24f6e2 100644 --- a/test/CheckboxTree.js +++ b/test/CheckboxTree.js @@ -859,8 +859,9 @@ describe('', () => { { value: 'jupiter', label: 'Jupiter', + test1: '123', children: [ - { value: 'io', label: 'Io' }, + { value: 'io', label: 'Io', test2: '456' }, { value: 'europa', label: 'Europa' }, ], }, @@ -884,6 +885,9 @@ describe('', () => { treeDepth: 1, index: 0, parentValue: 'jupiter', + extraKeys: { + test2: '456', + }, }; const expectedParentMetadata = { value: 'jupiter', @@ -893,6 +897,9 @@ describe('', () => { treeDepth: 0, index: 0, parentValue: undefined, + extraKeys: { + test1: '123', + }, }; // onCheck