From ba07f96a7657ddbdf85a0a20506bb4e3455a02d6 Mon Sep 17 00:00:00 2001 From: zombiej Date: Thu, 25 Jul 2019 11:14:52 +0800 Subject: [PATCH 1/2] always trigger onSelect --- src/Select.tsx | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Select.tsx b/src/Select.tsx index 45ad09fb4..8dae1ca3d 100644 --- a/src/Select.tsx +++ b/src/Select.tsx @@ -443,11 +443,14 @@ class Select extends React.Component, ISelectState> { const selectedValue = getValuePropValue(item); const lastValue = value[value.length - 1]; + let skipTrigger = false; + if (isMultipleOrTags(props)) { if (findIndexInValueBySingleValue(value, selectedValue) !== -1) { - return; + skipTrigger = true; + } else { + value = value.concat([selectedValue]); } - value = value.concat([selectedValue]); } else { if ( !isCombobox(props) && @@ -456,18 +459,24 @@ class Select extends React.Component, ISelectState> { selectedValue !== this.state.backfillValue ) { this.setOpenState(false, { needFocus: true, fireSearch: false }); - return; + skipTrigger = true; + } else { + value = [selectedValue]; + this.setOpenState(false, { needFocus: true, fireSearch: false }); } - value = [selectedValue]; - this.setOpenState(false, { needFocus: true, fireSearch: false }); } - this.fireChange(value); + if (!skipTrigger) { + this.fireChange(value); + } this.fireSelect(selectedValue); - const inputValue = isCombobox(props) ? getPropValue(item, props.optionLabelProp) : ''; - if (props.autoClearSearchValue) { - this.setInputValue(inputValue, false); + if (!skipTrigger) { + const inputValue = isCombobox(props) ? getPropValue(item, props.optionLabelProp) : ''; + + if (props.autoClearSearchValue) { + this.setInputValue(inputValue, false); + } } }; From 3d70ee6b7620466578b9adaee9eab07b8e28566b Mon Sep 17 00:00:00 2001 From: zombiej Date: Thu, 25 Jul 2019 11:20:54 +0800 Subject: [PATCH 2/2] add test case --- tests/Select.spec.tsx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Select.spec.tsx b/tests/Select.spec.tsx index b59c60762..a9797bf49 100644 --- a/tests/Select.spec.tsx +++ b/tests/Select.spec.tsx @@ -1176,4 +1176,22 @@ describe('Select', () => { }); expect(wrapper.find('.rc-select-arrow-loading').length).toBe(1); }); + + it('should keep trigger onSelect by select', () => { + const onSelect = jest.fn(); + + const wrapper = mount + + , + ); + + const input = wrapper.find('input'); + + for (let i = 0; i < 10; i += 1) { + onSelect.mockReset(); + input.simulate('keyDown', { keyCode: KeyCode.ENTER }); + expect(onSelect).toBeCalledWith('1', expect.anything()); + } + }); });