Skip to content

Commit 773c8f3

Browse files
authored
fix: BaseSelect uses key property of displayValues as itemKey (#265)
* fix: 🐛 BaseSelect uses the `key` property as the default key * fix: `tagRender` must receive a value attribute * add test case
1 parent 2bb5097 commit 773c8f3

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/hooks/useDisplayValues.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ export default (
4949
valueOptions.map(({ option }) => option),
5050
);
5151

52+
const value = toPathKey(valueCells);
53+
5254
return {
5355
label,
54-
value: toPathKey(valueCells),
56+
value,
57+
key: value,
5558
valueCells,
5659
};
5760
});

tests/selector.spec.tsx

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { mount } from './enzyme';
33
import Cascader from '../src';
44
import { addressOptions } from './demoOptions';
@@ -34,6 +34,55 @@ describe('Cascader.Selector', () => {
3434
expect(onChange).toHaveBeenCalledWith([['exist']], expect.anything());
3535
});
3636

37+
it('avoid reuse', () => {
38+
const Tag: React.FC<any> = ({ children, onClose }) => {
39+
const [visible, setVisible] = useState(true);
40+
return (
41+
<button
42+
onClick={() => {
43+
setVisible(false);
44+
onClose();
45+
}}
46+
className={visible ? '' : 'reuse'}
47+
>
48+
{children}
49+
</button>
50+
);
51+
};
52+
53+
const wrapper = mount(
54+
<Cascader
55+
options={[
56+
{ label: 'AA', value: 'aa' },
57+
{ label: 'BB', value: 'bb' },
58+
{ label: 'CC', value: 'cc' },
59+
{ label: 'DD', value: 'dd' },
60+
{ label: 'EE', value: 'ee' },
61+
]}
62+
value={[['aa'], ['bb'], ['cc'], ['dd'], ['ee']]}
63+
onChange={values => {
64+
wrapper.setProps({
65+
value: values,
66+
});
67+
}}
68+
tagRender={({ label, onClose }) => (
69+
<Tag onClose={onClose} id={label}>
70+
{label}
71+
</Tag>
72+
)}
73+
checkable
74+
/>,
75+
);
76+
77+
for (let i = 5; i > 0; i--) {
78+
const buttons = wrapper.find('button');
79+
expect(buttons.length).toBe(i);
80+
buttons.first().simulate('click');
81+
wrapper.update();
82+
expect(wrapper.find('.reuse').length).toBe(0);
83+
}
84+
});
85+
3786
it('when selected modify options', () => {
3887
const wrapper = mount(<Cascader options={addressOptions} open />);
3988

0 commit comments

Comments
 (0)