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
6 changes: 4 additions & 2 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,9 @@ export default class Select extends Component<Props, State> {
removeValue = (removedValue: OptionType) => {
const { selectValue } = this.state;
const candidate = this.getOptionValue(removedValue);
const newValue = selectValue.filter(i => this.getOptionValue(i) !== candidate);
this.onChange(
selectValue.filter(i => this.getOptionValue(i) !== candidate),
newValue.length ? newValue : null,
{
action: 'remove-value',
removedValue,
Expand All @@ -667,13 +668,14 @@ export default class Select extends Component<Props, State> {
popValue = () => {
const { selectValue } = this.state;
const lastSelectedValue = selectValue[selectValue.length - 1];
const newValue = selectValue.slice(0, selectValue.length - 1);
this.announceAriaLiveSelection({
event: 'pop-value',
context: {
value: lastSelectedValue ? this.getOptionLabel(lastSelectedValue) : '',
},
});
this.onChange(selectValue.slice(0, selectValue.length - 1), {
this.onChange(newValue.length ? newValue : null, {
action: 'pop-value',
removedValue: lastSelectedValue,
});
Expand Down
45 changes: 25 additions & 20 deletions src/__tests__/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1448,39 +1448,44 @@ test('should not call onChange on hitting backspace even when backspaceRemovesVa
expect(onChangeSpy).not.toHaveBeenCalled();
});

test('should call onChange with `null` on hitting backspace when backspaceRemovesValue is true and isMulti is false', () => {
cases('should call onChange with `null` on hitting backspace when backspaceRemovesValue is true', ({ props = { ...BASIC_PROPS }, expectedValue }) => {
let onChangeSpy = jest.fn();
let selectWrapper = mount(
<Select
{...BASIC_PROPS}
{...props}
backspaceRemovesValue
isClearable
isMulti={false}
onChange={onChangeSpy}
/>
);
selectWrapper
.find(Control)
.simulate('keyDown', { keyCode: 8, key: 'Backspace' });
expect(onChangeSpy).toHaveBeenCalledWith(null, { action: 'clear', name: 'test-input-name' });
expect(onChangeSpy).toHaveBeenCalledWith(null, expectedValue);
}, {
'and isMulti is false': {
props: {
...BASIC_PROPS,
isMulti: false,
},
expectedValue: {
action: 'clear',
name: 'test-input-name',
}
},
'and isMulti is true': {
props: {
...BASIC_PROPS,
isMulti: true,
},
expectedValue: {
action: 'pop-value',
name: 'test-input-name',
removedValue: undefined
}
},
});

test('should call onChange with an array on hitting backspace when backspaceRemovesValue is true and isMulti is true', () => {
let onChangeSpy = jest.fn();
let selectWrapper = mount(
<Select
{...BASIC_PROPS}
backspaceRemovesValue
isClearable
isMulti
onChange={onChangeSpy}
/>
);
selectWrapper
.find(Control)
.simulate('keyDown', { keyCode: 8, key: 'Backspace' });
expect(onChangeSpy).toHaveBeenCalledWith([], { action: 'pop-value', name: 'test-input-name', removedValue: undefined });
});

test('multi select > clicking on X next to option will call onChange with all options other that the clicked option', () => {
let onChangeSpy = jest.fn();
Expand Down