diff --git a/src/hooks/useDataEntities.ts b/src/hooks/useDataEntities.ts
index c618ce7b..3aa1f2c4 100644
--- a/src/hooks/useDataEntities.ts
+++ b/src/hooks/useDataEntities.ts
@@ -27,7 +27,7 @@ export default (treeData: any, fieldNames: FieldNames) =>
warning(!isNil(val), 'TreeNode `value` is invalidate: undefined');
warning(!wrapper.valueEntities.has(val), `Same \`value\` exist in the tree: ${val}`);
warning(
- !key || String(key) === String(val),
+ isNil(key) || String(key) === String(val),
`\`key\` or \`value\` with TreeNode must be the same or you can remove one of them. key: ${key}, value: ${val}.`,
);
}
diff --git a/src/hooks/useTreeData.ts b/src/hooks/useTreeData.ts
index 58ea4b10..ca54accd 100644
--- a/src/hooks/useTreeData.ts
+++ b/src/hooks/useTreeData.ts
@@ -9,7 +9,7 @@ function buildTreeStructure(nodes: DataNode[], config: SimpleModeConfig): DataNo
nodes.forEach(node => {
const nodeKey = node[id];
- const clonedNode = { ...node, key: node.key || nodeKey };
+ const clonedNode = { ...node, key: node.key ?? nodeKey };
nodeMap.set(nodeKey, clonedNode);
});
diff --git a/tests/Select.spec.tsx b/tests/Select.spec.tsx
index fd22a4e2..0f037514 100644
--- a/tests/Select.spec.tsx
+++ b/tests/Select.spec.tsx
@@ -139,6 +139,21 @@ describe('TreeSelect.basic', () => {
);
expect(container.firstChild).toMatchSnapshot();
});
+
+ it('preserves a numeric zero key in treeDataSimpleMode', () => {
+ const onSelect = jest.fn();
+ const { container } = render(
+ ,
+ );
+
+ fireEvent.click(container.querySelector('.rc-tree-select-tree-node-content-wrapper'));
+ expect(onSelect).toHaveBeenCalledWith(0, expect.objectContaining({ key: 0 }));
+ });
});
it('sets default value', () => {
diff --git a/tests/Select.tree.spec.js b/tests/Select.tree.spec.js
index 9989bec2..5c0ee88a 100644
--- a/tests/Select.tree.spec.js
+++ b/tests/Select.tree.spec.js
@@ -87,6 +87,16 @@ describe('TreeSelect.tree', () => {
spy.mockRestore();
});
+ it('warning if numeric zero key is not same as value', () => {
+ resetWarned();
+ const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
+ render();
+ expect(spy).toHaveBeenCalledWith(
+ 'Warning: `key` or `value` with TreeNode must be the same or you can remove one of them. key: 0, value: different.',
+ );
+ spy.mockRestore();
+ });
+
it('warning if node undefined value', () => {
resetWarned();
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});