Skip to content

Commit

Permalink
fix: 修复Select组件部分事件的函数签名不正确的问题。修复Select组件多选模式下取消选择时也会触发onSelect事件的问题;…
Browse files Browse the repository at this point in the history
…同时添加onDeselect作为该事件的触发承载。
  • Loading branch information
SummerOverture committed Aug 9, 2021
1 parent 8db7fb2 commit 36f5293
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions site/docs/zh-CN/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ render(){
| multipleSelectAllText | 在多选模式下选项全部选中时,在已选框中显示的特定的文案 | String | '全部选中' |
| notFoundContent | 当下拉列表为空时显示的内容 | String \| ReactNode | '无匹配结果' |
| onChange | 选中 option时,调用此函数 | (value) => Void | - |
| onDeselect | 多选模式下取消选定时的回调 | (value) => Void | - |
| onMouseEnter | 鼠标移入时回调 | (value) => Void | - |
| onMouseLeave | 鼠标移出时回调 | (value) => Void | - |
| onPopupScroll | 下拉列表滚动时的回调 | (value) => Void | - |
Expand Down
18 changes: 13 additions & 5 deletions source/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ interface SelectProps {
multipleSelectAllText?: string;
notFoundContent?: string | React.ReactNode;
onChange?: (value?: any) => void;
onMouseEnter?: () => void;
onMouseLeave?: () => void;
onPopupScroll?: () => void;
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
onMouseLeave?: React.MouseEventHandler<HTMLDivElement>;
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
onSearch?: (value: any) => void;
onSelect?: (value: any) => void;
onDeselect?: (value: any) => void;
onVisibleChange?: (value: any) => void;
placeholder?: string;
placement?: 'bottomLeft' | 'bottomCenter' | 'bottomRight' | 'topLeft' | 'topCenter' | 'topRight';
Expand Down Expand Up @@ -126,6 +127,7 @@ class Select extends React.Component<SelectProps, SelectState> {
onPopupScroll: PropTypes.func,
onSearch: PropTypes.func,
onSelect: PropTypes.func,
onDeselect: PropTypes.func,
onVisibleChange: PropTypes.func,
placeholder: PropTypes.string,
placement: PropTypes.oneOf([
Expand Down Expand Up @@ -176,6 +178,7 @@ class Select extends React.Component<SelectProps, SelectState> {
onPopupScroll: noop,
onSearch: noop,
onSelect: noop,
onDeselect: noop,
onVisibleChange: noop,
placement: 'bottomLeft',
prefixCls: 'fishd-select',
Expand Down Expand Up @@ -449,7 +452,7 @@ class Select extends React.Component<SelectProps, SelectState> {
//处理 label、option的click操作
onOptionClick = (e, obj, clickInLabel) => {
e && e.stopPropagation();
const { onChange, mode, onSelect, labelInValue } = this.props;
const { onChange, mode, onSelect, onDeselect, labelInValue } = this.props;
const { selectValue } = this.state;
const index = selectValue.findIndex(selected => selected.key === obj.key);
if (mode === 'single') {
Expand Down Expand Up @@ -501,7 +504,12 @@ class Select extends React.Component<SelectProps, SelectState> {
}
}
//fire onSelect event => option/label click
onSelect(obj);
if(clickInLabel) {
onDeselect(obj);
} else {
onSelect(obj);
}

};

//获取加料后的children
Expand Down
20 changes: 20 additions & 0 deletions source/components/Select/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,25 @@ describe('<Select />', () => {

it('Select选项个数为3', () => {
expect(wrapper.find('.fishd-select-dropdown-option-item').length).toBe(3)
});

it('Label可删除的多选操作事件触发正常', () => {
const triggerValue = {label: '1', title: undefined, key: 1};
const onSelect = jest.fn();
const onDeselect = jest.fn();
wrapper = mount(
<Select labelClear mode={"multiple"} value={[1]} onSelect={onSelect} onDeselect={onDeselect}>
<Select.Option value={1}>1</Select.Option>
<Select.Option value={2}>2</Select.Option>
<Select.Option value={3}>3</Select.Option>
</Select>
);

wrapper.find('.fishd-select').simulate('click');
wrapper.find('.fishd-select-dropdown-option-item').at(0).simulate('click');
expect(onSelect).toBeCalledWith(expect.objectContaining(triggerValue));
wrapper.find('.fishd-select-option-clearable-option-close').simulate('click');
expect(onSelect).toBeCalledTimes(1);
expect(onDeselect).toBeCalledWith(expect.objectContaining(triggerValue));
})
});

0 comments on commit 36f5293

Please sign in to comment.