Skip to content

Commit

Permalink
fix: BaseSelect uses key property of displayValues as itemKey (#265)
Browse files Browse the repository at this point in the history
* fix: 🐛 BaseSelect uses the `key` property as the default key

* fix: `tagRender` must receive a value attribute

* add test case
  • Loading branch information
Dunqing authored Mar 28, 2022
1 parent 2bb5097 commit 773c8f3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/hooks/useDisplayValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ export default (
valueOptions.map(({ option }) => option),
);

const value = toPathKey(valueCells);

return {
label,
value: toPathKey(valueCells),
value,
key: value,
valueCells,
};
});
Expand Down
51 changes: 50 additions & 1 deletion tests/selector.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { mount } from './enzyme';
import Cascader from '../src';
import { addressOptions } from './demoOptions';
Expand Down Expand Up @@ -34,6 +34,55 @@ describe('Cascader.Selector', () => {
expect(onChange).toHaveBeenCalledWith([['exist']], expect.anything());
});

it('avoid reuse', () => {
const Tag: React.FC<any> = ({ children, onClose }) => {
const [visible, setVisible] = useState(true);
return (
<button
onClick={() => {
setVisible(false);
onClose();
}}
className={visible ? '' : 'reuse'}
>
{children}
</button>
);
};

const wrapper = mount(
<Cascader
options={[
{ label: 'AA', value: 'aa' },
{ label: 'BB', value: 'bb' },
{ label: 'CC', value: 'cc' },
{ label: 'DD', value: 'dd' },
{ label: 'EE', value: 'ee' },
]}
value={[['aa'], ['bb'], ['cc'], ['dd'], ['ee']]}
onChange={values => {
wrapper.setProps({
value: values,
});
}}
tagRender={({ label, onClose }) => (
<Tag onClose={onClose} id={label}>
{label}
</Tag>
)}
checkable
/>,
);

for (let i = 5; i > 0; i--) {
const buttons = wrapper.find('button');
expect(buttons.length).toBe(i);
buttons.first().simulate('click');
wrapper.update();
expect(wrapper.find('.reuse').length).toBe(0);
}
});

it('when selected modify options', () => {
const wrapper = mount(<Cascader options={addressOptions} open />);

Expand Down

1 comment on commit 773c8f3

@vercel
Copy link

@vercel vercel bot commented on 773c8f3 Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.