Skip to content

Commit 242592e

Browse files
authored
fix: onSelect should keep trigger (#413)
* always trigger onSelect * add test case
1 parent e243dcc commit 242592e

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/Select.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,14 @@ class Select extends React.Component<Partial<ISelectProps>, ISelectState> {
443443
const selectedValue = getValuePropValue(item);
444444
const lastValue = value[value.length - 1];
445445

446+
let skipTrigger = false;
447+
446448
if (isMultipleOrTags(props)) {
447449
if (findIndexInValueBySingleValue(value, selectedValue) !== -1) {
448-
return;
450+
skipTrigger = true;
451+
} else {
452+
value = value.concat([selectedValue]);
449453
}
450-
value = value.concat([selectedValue]);
451454
} else {
452455
if (
453456
!isCombobox(props) &&
@@ -456,18 +459,24 @@ class Select extends React.Component<Partial<ISelectProps>, ISelectState> {
456459
selectedValue !== this.state.backfillValue
457460
) {
458461
this.setOpenState(false, { needFocus: true, fireSearch: false });
459-
return;
462+
skipTrigger = true;
463+
} else {
464+
value = [selectedValue];
465+
this.setOpenState(false, { needFocus: true, fireSearch: false });
460466
}
461-
value = [selectedValue];
462-
this.setOpenState(false, { needFocus: true, fireSearch: false });
463467
}
464468

465-
this.fireChange(value);
469+
if (!skipTrigger) {
470+
this.fireChange(value);
471+
}
466472
this.fireSelect(selectedValue);
467-
const inputValue = isCombobox(props) ? getPropValue(item, props.optionLabelProp) : '';
468473

469-
if (props.autoClearSearchValue) {
470-
this.setInputValue(inputValue, false);
474+
if (!skipTrigger) {
475+
const inputValue = isCombobox(props) ? getPropValue(item, props.optionLabelProp) : '';
476+
477+
if (props.autoClearSearchValue) {
478+
this.setInputValue(inputValue, false);
479+
}
471480
}
472481
};
473482

tests/Select.spec.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,4 +1176,22 @@ describe('Select', () => {
11761176
});
11771177
expect(wrapper.find('.rc-select-arrow-loading').length).toBe(1);
11781178
});
1179+
1180+
it('should keep trigger onSelect by select', () => {
1181+
const onSelect = jest.fn();
1182+
1183+
const wrapper = mount<Select>(
1184+
<Select backfill={true} open={true} onSelect={onSelect} optionLabelProp="children">
1185+
<Option value="1">One</Option>
1186+
</Select>,
1187+
);
1188+
1189+
const input = wrapper.find('input');
1190+
1191+
for (let i = 0; i < 10; i += 1) {
1192+
onSelect.mockReset();
1193+
input.simulate('keyDown', { keyCode: KeyCode.ENTER });
1194+
expect(onSelect).toBeCalledWith('1', expect.anything());
1195+
}
1196+
});
11791197
});

0 commit comments

Comments
 (0)