Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/BaseSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export type Placement = 'bottomLeft' | 'bottomRight' | 'topLeft' | 'topRight';

export type RawValueType = string | number;

export type DisplayInfoType = 'add' | 'remove' | 'clear';

export interface RefOptionListProps {
onKeyDown: React.KeyboardEventHandler;
onKeyUp: React.KeyboardEventHandler;
Expand Down Expand Up @@ -81,7 +83,7 @@ export interface BaseSelectPrivateProps {
onDisplayValuesChange: (
values: DisplayValueType[],
info: {
type: 'add' | 'remove' | 'clear';
type: DisplayInfoType;
values: DisplayValueType[];
},
) => void;
Expand Down
12 changes: 7 additions & 5 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
BaseSelectProps,
BaseSelectPropsWithoutPrivate,
BaseSelectRef,
DisplayInfoType,
DisplayValueType,
RenderNode,
} from './BaseSelect';
Expand Down Expand Up @@ -455,7 +456,7 @@ const Select = React.forwardRef(
);

// ========================= OptionList =========================
const triggerSelect = (val: RawValueType, selected: boolean) => {
const triggerSelect = (val: RawValueType, selected: boolean, type?: DisplayInfoType) => {
const getSelectEnt = (): [RawValueType | LabelInValueType, DefaultOptionType] => {
const option = getMixedOption(val);
return [
Expand All @@ -473,7 +474,7 @@ const Select = React.forwardRef(
if (selected && onSelect) {
const [wrappedValue, option] = getSelectEnt();
onSelect(wrappedValue, option);
} else if (!selected && onDeselect) {
} else if (!selected && onDeselect && type !== 'clear') {
const [wrappedValue, option] = getSelectEnt();
onDeselect(wrappedValue, option);
}
Expand Down Expand Up @@ -509,10 +510,11 @@ const Select = React.forwardRef(
// BaseSelect display values change
const onDisplayValuesChange: BaseSelectProps['onDisplayValuesChange'] = (nextValues, info) => {
triggerChange(nextValues);
const { type, values } = info;

if (info.type === 'remove' || info.type === 'clear') {
info.values.forEach((item) => {
triggerSelect(item.value, false);
if (type === 'remove' || type === 'clear') {
values.forEach((item) => {
triggerSelect(item.value, false, type);
});
}
};
Expand Down
8 changes: 4 additions & 4 deletions tests/shared/allowClearTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function allowClearTest(mode: any, value: any) {
it('clears value', () => {
const onClear = jest.fn();
const onChange = jest.fn();
const onDeselect = jest.fn();
const useArrayValue = ['tags', 'multiple'].includes(mode);
const wrapper = mount(
<Select
Expand All @@ -19,6 +20,7 @@ export default function allowClearTest(mode: any, value: any) {
mode={mode}
onClear={onClear}
onChange={onChange}
onDeselect={onDeselect}
>
<Option value="1">1</Option>
<Option value="2">2</Option>
Expand All @@ -31,15 +33,13 @@ export default function allowClearTest(mode: any, value: any) {

// enabled
wrapper.setProps({ disabled: false });
wrapper
.find('.rc-select-clear')
.last()
.simulate('mousedown');
wrapper.find('.rc-select-clear').last().simulate('mousedown');
if (useArrayValue) {
expect(onChange).toHaveBeenCalledWith([], []);
} else {
expect(onChange).toHaveBeenCalledWith(undefined, undefined);
}
expect(onDeselect).not.toBeCalled();
expect(wrapper.find('input').props().value).toEqual('');
expect(onClear).toHaveBeenCalled();
});
Expand Down