Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x Normalise value removal #3416

Merged
merged 2 commits into from
Apr 26, 2019
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