Skip to content

Commit d35afa8

Browse files
authored
Merge pull request #3416 from JedWatson/normalise-value-removal
3.x Normalise value removal
2 parents 8ac29a9 + 7b1f642 commit d35afa8

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/Select.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,9 @@ export default class Select extends Component<Props, State> {
663663
removeValue = (removedValue: OptionType) => {
664664
const { selectValue } = this.state;
665665
const candidate = this.getOptionValue(removedValue);
666+
const newValue = selectValue.filter(i => this.getOptionValue(i) !== candidate);
666667
this.onChange(
667-
selectValue.filter(i => this.getOptionValue(i) !== candidate),
668+
newValue.length ? newValue : null,
668669
{
669670
action: 'remove-value',
670671
removedValue,
@@ -685,13 +686,14 @@ export default class Select extends Component<Props, State> {
685686
popValue = () => {
686687
const { selectValue } = this.state;
687688
const lastSelectedValue = selectValue[selectValue.length - 1];
689+
const newValue = selectValue.slice(0, selectValue.length - 1);
688690
this.announceAriaLiveSelection({
689691
event: 'pop-value',
690692
context: {
691693
value: lastSelectedValue ? this.getOptionLabel(lastSelectedValue) : '',
692694
},
693695
});
694-
this.onChange(selectValue.slice(0, selectValue.length - 1), {
696+
this.onChange(newValue.length ? newValue : null, {
695697
action: 'pop-value',
696698
removedValue: lastSelectedValue,
697699
});

src/__tests__/Select.test.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,39 +1491,44 @@ test('should not call onChange on hitting backspace even when backspaceRemovesVa
14911491
expect(onChangeSpy).not.toHaveBeenCalled();
14921492
});
14931493

1494-
test('should call onChange with `null` on hitting backspace when backspaceRemovesValue is true and isMulti is false', () => {
1494+
cases('should call onChange with `null` on hitting backspace when backspaceRemovesValue is true', ({ props = { ...BASIC_PROPS }, expectedValue }) => {
14951495
let onChangeSpy = jest.fn();
14961496
let selectWrapper = mount(
14971497
<Select
1498-
{...BASIC_PROPS}
1498+
{...props}
14991499
backspaceRemovesValue
15001500
isClearable
1501-
isMulti={false}
15021501
onChange={onChangeSpy}
15031502
/>
15041503
);
15051504
selectWrapper
15061505
.find(Control)
15071506
.simulate('keyDown', { keyCode: 8, key: 'Backspace' });
1508-
expect(onChangeSpy).toHaveBeenCalledWith(null, { action: 'clear', name: 'test-input-name' });
1507+
expect(onChangeSpy).toHaveBeenCalledWith(null, expectedValue);
1508+
}, {
1509+
'and isMulti is false': {
1510+
props: {
1511+
...BASIC_PROPS,
1512+
isMulti: false,
1513+
},
1514+
expectedValue: {
1515+
action: 'clear',
1516+
name: 'test-input-name',
1517+
}
1518+
},
1519+
'and isMulti is true': {
1520+
props: {
1521+
...BASIC_PROPS,
1522+
isMulti: true,
1523+
},
1524+
expectedValue: {
1525+
action: 'pop-value',
1526+
name: 'test-input-name',
1527+
removedValue: undefined
1528+
}
1529+
},
15091530
});
15101531

1511-
test('should call onChange with an array on hitting backspace when backspaceRemovesValue is true and isMulti is true', () => {
1512-
let onChangeSpy = jest.fn();
1513-
let selectWrapper = mount(
1514-
<Select
1515-
{...BASIC_PROPS}
1516-
backspaceRemovesValue
1517-
isClearable
1518-
isMulti
1519-
onChange={onChangeSpy}
1520-
/>
1521-
);
1522-
selectWrapper
1523-
.find(Control)
1524-
.simulate('keyDown', { keyCode: 8, key: 'Backspace' });
1525-
expect(onChangeSpy).toHaveBeenCalledWith([], { action: 'pop-value', name: 'test-input-name', removedValue: undefined });
1526-
});
15271532

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

0 commit comments

Comments
 (0)