Skip to content

Commit d5d1564

Browse files
authored
fix: Multiple selector with customize tag not match the key of item (#782)
* test: test driven * fix: key missing
1 parent 746c570 commit d5d1564

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/Selector/MultipleSelector.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import Input from './Input';
99
import useLayoutEffect from '../hooks/useLayoutEffect';
1010
import type { DisplayValueType, RenderNode, CustomTagProps, RawValueType } from '../BaseSelect';
1111

12+
function itemKey(value: DisplayValueType) {
13+
return value.key ?? value.value;
14+
}
15+
1216
interface SelectorProps extends InnerSelectorProps {
1317
// Icon
1418
removeIcon?: RenderNode;
@@ -224,7 +228,7 @@ const SelectSelector: React.FC<SelectorProps> = (props) => {
224228
renderItem={renderItem}
225229
renderRest={renderRest}
226230
suffix={inputNode}
227-
itemKey="key"
231+
itemKey={itemKey}
228232
maxCount={maxTagCount}
229233
/>
230234
);

tests/Tags.test.tsx

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/* eslint-disable import/no-named-as-default-member */
21
import { mount } from 'enzyme';
32
import KeyCode from 'rc-util/lib/KeyCode';
43
import classNames from 'classnames';
54
import * as React from 'react';
6-
import Select, { OptGroup, Option } from '../src';
5+
import Select, { BaseSelect, OptGroup, Option } from '../src';
76
import allowClearTest from './shared/allowClearTest';
87
import blurTest from './shared/blurTest';
98
import dynamicChildrenTest from './shared/dynamicChildrenTest';
@@ -329,6 +328,63 @@ describe('Select.Tags', () => {
329328

330329
expect(tagRender).toHaveBeenCalledWith(expect.objectContaining({ closable: false }));
331330
});
331+
332+
it('should keep key', () => {
333+
const MyTag = ({ onClose, label }: any) => {
334+
const [closed, setClosed] = React.useState(false);
335+
336+
return (
337+
<span
338+
className="my-tag"
339+
onClick={() => {
340+
setClosed(true);
341+
onClose();
342+
}}
343+
>
344+
{label}
345+
{String(closed)}
346+
</span>
347+
);
348+
};
349+
350+
const onDisplayValuesChange = jest.fn();
351+
352+
const wrapper = mount(
353+
<BaseSelect
354+
mode="tags"
355+
maxTagCount={99}
356+
tagRender={(props) => <MyTag {...props} />}
357+
onDisplayValuesChange={onDisplayValuesChange}
358+
displayValues={[
359+
{
360+
label: '1',
361+
value: 1,
362+
},
363+
{
364+
label: '2',
365+
value: 2,
366+
},
367+
]}
368+
{...({} as any)}
369+
/>,
370+
);
371+
372+
wrapper.find('.my-tag').at(0).simulate('click');
373+
expect(onDisplayValuesChange).toHaveBeenCalled();
374+
375+
// Update
376+
wrapper.setProps({
377+
displayValues: [
378+
{
379+
label: '2',
380+
value: 2,
381+
},
382+
],
383+
});
384+
385+
expect(wrapper.find('span.my-tag')).toHaveLength(1);
386+
expect(wrapper.find('span.my-tag').text()).toEqual('2false');
387+
});
332388
});
333389

334390
describe('OptGroup', () => {

0 commit comments

Comments
 (0)