Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gwyneplaine committed Dec 18, 2018
2 parents 3b9bccf + 79efe79 commit 680a42d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,20 +586,24 @@ export default class Select extends Component<Props, State> {
focusedValue: null,
});
}
onChange = (newValue: ValueType, actionMeta: ActionMeta) => {
const { onChange, name } = this.props;
onChange(newValue, { ...actionMeta, name });
};
setValue = (
newValue: ValueType,
action: ActionTypes = 'set-value',
option?: OptionType
) => {
const { closeMenuOnSelect, isMulti, onChange } = this.props;
const { closeMenuOnSelect, isMulti } = this.props;
this.onInputChange('', { action: 'set-value' });
if (closeMenuOnSelect) {
this.inputIsHiddenAfterUpdate = !isMulti;
this.onMenuClose();
}
// when the select value should change, we should reset focusedValue
this.clearFocusValueOnUpdate = true;
onChange(newValue, { action, option });
this.onChange(newValue, { action, option });
};
selectOption = (newValue: OptionType) => {
const { blurInputOnSelect, isMulti } = this.props;
Expand Down Expand Up @@ -637,10 +641,9 @@ export default class Select extends Component<Props, State> {
}
};
removeValue = (removedValue: OptionType) => {
const { onChange } = this.props;
const { selectValue } = this.state;
const candidate = this.getOptionValue(removedValue);
onChange(selectValue.filter(i => this.getOptionValue(i) !== candidate), {
this.onChange(selectValue.filter(i => this.getOptionValue(i) !== candidate), {
action: 'remove-value',
removedValue,
});
Expand All @@ -653,11 +656,10 @@ export default class Select extends Component<Props, State> {
this.focusInput();
};
clearValue = () => {
const { isMulti, onChange } = this.props;
onChange(isMulti ? [] : null, { action: 'clear' });
const { isMulti } = this.props;
this.onChange(isMulti ? [] : null, { action: 'clear' });
};
popValue = () => {
const { onChange } = this.props;
const { selectValue } = this.state;
const lastSelectedValue = selectValue[selectValue.length - 1];
this.announceAriaLiveSelection({
Expand All @@ -668,7 +670,7 @@ export default class Select extends Component<Props, State> {
: undefined,
},
});
onChange(selectValue.slice(0, selectValue.length - 1), {
this.onChange(selectValue.slice(0, selectValue.length - 1), {
action: 'pop-value',
removedValue: lastSelectedValue,
});
Expand Down
19 changes: 11 additions & 8 deletions src/__tests__/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,8 @@ cases(
selectWrapper.update();
expect(onChangeSpy).toHaveBeenCalledWith(expectedSelectedOption, {
action: 'select-option',
option: expectedActionMetaOption
option: expectedActionMetaOption,
name: BASIC_PROPS.name
});
},
{
Expand Down Expand Up @@ -594,7 +595,8 @@ cases(
selectWrapper.update();
expect(onChangeSpy).toHaveBeenCalledWith(expectedSelectedOption, {
action: 'deselect-option',
option: expectedMetaOption
option: expectedMetaOption,
name: BASIC_PROPS.name
});
},
{
Expand Down Expand Up @@ -1381,7 +1383,7 @@ test('multi select > call onChange with all values but last selected value and r
.simulate('keyDown', { keyCode: 8, key: 'Backspace' });
expect(onChangeSpy).toHaveBeenCalledWith(
[{ label: '0', value: 'zero' }, { label: '1', value: 'one' }],
{ action: 'pop-value', removedValue: { label: '2', value: 'two' } },
{ action: 'pop-value', removedValue: { label: '2', value: 'two' }, name: BASIC_PROPS.name },
);
});

Expand Down Expand Up @@ -1472,7 +1474,7 @@ test('multi select > clicking on X next to option will call onChange with all op

expect(onChangeSpy).toHaveBeenCalledWith(
[{ label: '0', value: 'zero' }, { label: '2', value: 'two' }],
{ action: 'remove-value', removedValue: { label: '4', value: 'four' } }
{ action: 'remove-value', removedValue: { label: '4', value: 'four' }, name: BASIC_PROPS.name }
);
});

Expand Down Expand Up @@ -1921,7 +1923,7 @@ test('clear select by clicking on clear button > should not call onMenuOpen', ()
selectWrapper
.find('div.react-select__clear-indicator')
.simulate('mousedown', { button: 0 });
expect(onChangeSpy).toBeCalledWith([], { action: 'clear' });
expect(onChangeSpy).toBeCalledWith([], { action: 'clear', name: BASIC_PROPS.name });
});

test('clearing select using clear button to not call onMenuOpen or onMenuClose', () => {
Expand Down Expand Up @@ -1954,7 +1956,8 @@ test('multi select > calls onChange when option is selected and isSearchable is
const selectedOption = { label: '0', value: 'zero' };
expect(onChangeSpy).toHaveBeenCalledWith([selectedOption], {
action: 'select-option',
option: selectedOption
option: selectedOption,
name: BASIC_PROPS.name
});
});

Expand Down Expand Up @@ -2065,7 +2068,7 @@ test('hitting spacebar should select option if isSearchable is false', () => {
selectWrapper.simulate('keyDown', { keyCode: 32, key: ' ' });
expect(onChangeSpy).toHaveBeenCalledWith(
{ label: '0', value: 'zero' },
{ action: 'select-option' }
{ action: 'select-option', name: BASIC_PROPS.name }
);
});

Expand Down Expand Up @@ -2178,7 +2181,7 @@ test('to clear value when hitting escape if escapeClearsValue and isClearable ar
);

selectWrapper.simulate('keyDown', { keyCode: 27, key: 'Escape' });
expect(onInputChangeSpy).toHaveBeenCalledWith(null, { action: 'clear' });
expect(onInputChangeSpy).toHaveBeenCalledWith(null, { action: 'clear', name: BASIC_PROPS.name });
});

cases(
Expand Down

0 comments on commit 680a42d

Please sign in to comment.