Skip to content

Commit 26a9817

Browse files
committed
clean up unused code
1 parent 4e69578 commit 26a9817

File tree

4 files changed

+67
-46
lines changed

4 files changed

+67
-46
lines changed

src/Tree.jsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ class Tree extends React.Component {
6969
filterTreeNode: PropTypes.func,
7070
openTransitionName: PropTypes.string,
7171
openAnimation: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
72-
73-
// Tree will parse treeNode as entities map,
74-
// This prop enable user to process the Tree with additional entities
75-
// This function may be remove in future if we start to remove the dependency on key
76-
// So any user should not relay on this function.
77-
// If you are refactor this code, you can remove it as your wish
78-
unstable_processTreeEntity: PropTypes.shape({
79-
initWrapper: PropTypes.func.isRequired,
80-
processEntity: PropTypes.func.isRequired,
81-
onProcessFinished: PropTypes.func.isRequired,
82-
}),
8372
switcherIcon: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
8473
};
8574

@@ -191,7 +180,7 @@ class Tree extends React.Component {
191180
newState.treeNode = treeNode;
192181

193182
// Calculate the entities data for quick match
194-
const entitiesMap = convertTreeToEntities(treeNode, props.unstable_processTreeEntity);
183+
const entitiesMap = convertTreeToEntities(treeNode);
195184
newState.posEntities = entitiesMap.posEntities;
196185
newState.keyEntities = entitiesMap.keyEntities;
197186
}

src/util.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,15 @@ function keyListToString(keyList) {
150150
return keyList.map(key => String(key));
151151
}
152152

153-
export function convertDataToTree(treeData) {
153+
const internalProcessProps = props => props;
154+
export function convertDataToTree(treeData, { processProps = internalProcessProps } = {}) {
154155
if (!treeData) return [];
155156
const list = Array.isArray(treeData) ? treeData : [treeData];
156157
return list.map(({ children, ...props }) => {
157158
const childrenNodes = (children || []).map(convertDataToTree);
158159

159160
return (
160-
<TreeNode {...props}>
161+
<TreeNode {...processProps(props)}>
161162
{childrenNodes}
162163
</TreeNode>
163164
);
@@ -166,7 +167,7 @@ export function convertDataToTree(treeData) {
166167

167168
// TODO: ========================= NEW LOGIC =========================
168169
/**
169-
* Calculate treeNodes entities.
170+
* Calculate treeNodes entities. `processTreeEntity` is used for `rc-tree-select`
170171
* @param treeNodes
171172
* @param processTreeEntity User can customize the entity
172173
*/

tests/TreeProps.spec.js

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ describe('Tree Props', () => {
460460
});
461461
};
462462

463-
mount(
463+
const tree = mount(
464464
<Tree
465465
loadData={loadData}
466466
expandedKeys={['0', '1', '2']}
@@ -471,6 +471,42 @@ describe('Tree Props', () => {
471471
</Tree>
472472
);
473473

474+
tree.setProps({ expandedKeys: ['0', '1', '2'] });
475+
476+
return timeoutPromise().then(() => {
477+
expect(called).toBe(3);
478+
expect(keys[0]).toBe(1);
479+
expect(keys[1]).toBe(1);
480+
expect(keys[2]).toBe(1);
481+
});
482+
});
483+
484+
it('with defaultExpandedKeys', () => {
485+
let called = 0;
486+
const keys = {};
487+
const loadData = ({ props: { eventKey } }) => {
488+
keys[eventKey] = (keys[eventKey] || 0) + 1;
489+
490+
return new Promise(() => {
491+
called += 1;
492+
});
493+
};
494+
495+
const wrapper = mount(
496+
<Tree
497+
loadData={loadData}
498+
defaultExpandedKeys={['0', '1', '2']}
499+
>
500+
<TreeNode key="0" />
501+
<TreeNode key="1" />
502+
<TreeNode key="2" />
503+
</Tree>
504+
);
505+
506+
// Do not trigger loadData
507+
wrapper.find('.rc-tree-switcher').at(0).simulate('click');
508+
wrapper.find('.rc-tree-switcher').at(0).simulate('click');
509+
474510
return timeoutPromise().then(() => {
475511
expect(called).toBe(3);
476512
expect(keys[0]).toBe(1);
@@ -612,35 +648,6 @@ describe('Tree Props', () => {
612648
expect(wrapper.render()).toMatchSnapshot();
613649
});
614650

615-
describe('unstable_processTreeEntity', () => {
616-
const onProcessFinished = jest.fn();
617-
618-
const handler = {
619-
initWrapper(wrapper) {
620-
return { ...wrapper, valueEntities: {} };
621-
},
622-
processEntity(entity, { valueEntities }) {
623-
valueEntities[entity.node.props.value] = entity;
624-
},
625-
onProcessFinished,
626-
};
627-
628-
mount(
629-
<Tree unstable_processTreeEntity={handler}>
630-
<TreeNode key="K0" title="T0" value={0} />
631-
<TreeNode key="K1" title="T1" value={1}>
632-
<TreeNode key="K10" title="T10" value={10} />
633-
<TreeNode key="K11" title="T11" value={11} />
634-
</TreeNode>
635-
</Tree>
636-
);
637-
638-
expect(onProcessFinished).toBeCalled();
639-
640-
const valueList = Object.keys(onProcessFinished.mock.calls[0][0].valueEntities);
641-
expect(valueList).toEqual(['0', '1', '10', '11']);
642-
});
643-
644651
describe('disabled', () => {
645652
it('basic', () => {
646653
const wrapper = render(
@@ -696,7 +703,7 @@ describe('Tree Props', () => {
696703
const sfc = ({ isLeaf }) => {
697704
if (testLeaf) {
698705
return isLeaf ? <span>{text}</span> : null;
699-
}
706+
}
700707
return isLeaf ? null : <span>{text}</span>;
701708
};
702709

tests/util.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ describe('Util', () => {
6969
expect(convertTreeToData($treeNode)).toEqual(treeData);
7070
});
7171

72+
it('convertTreeToEntities with additional handler', () => {
73+
const onProcessFinished = jest.fn();
74+
75+
const tree = (
76+
<Tree>
77+
<TreeNode key="key" title="test" value="ttt" />
78+
</Tree>
79+
);
80+
81+
const { keyEntities, valueEntities } = convertTreeToEntities(tree.props.children, {
82+
initWrapper: wrapper => ({
83+
...wrapper,
84+
valueEntities: {},
85+
}),
86+
processEntity: (entity, wrapper) => {
87+
wrapper.valueEntities[entity.node.props.value] = entity;
88+
},
89+
onProcessFinished,
90+
});
91+
92+
expect(onProcessFinished).toBeCalled();
93+
expect(valueEntities.ttt).toBe(keyEntities.key);
94+
});
95+
7296
// You can remove this test if refactor remove conductCheck function
7397
describe('conductCheck', () => {
7498
describe('basic', () => {

0 commit comments

Comments
 (0)