Skip to content

Commit 19b7634

Browse files
committed
Add removedValues into onChange clear action meta
The similar actions `remove-value` and `pop-value` have `removedValue` in their `onChange` meta arguments. It would be reasonable to pass the whole `this.state.selectValue` as `removedValues` assuming that all selected values are removed upon `clear`. This will help some client code to add additional logic in `onChange` handlers like filtering of fixed values to preseve after `clear` without referring to `options` array.
1 parent 4da6ee0 commit 19b7634

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

.changeset/pink-cats-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-select": patch
3+
---
4+
5+
Add `removedValues` that points to the current selection into a meta of onChange callback when clearing value.

packages/react-select/src/Select.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,11 @@ export default class Select extends Component<Props, State> {
872872
this.focusInput();
873873
};
874874
clearValue = () => {
875-
this.onChange(this.props.isMulti ? [] : null, { action: 'clear' });
875+
const { selectValue } = this.state;
876+
this.onChange(this.props.isMulti ? [] : null, {
877+
action: 'clear',
878+
removedValues: selectValue,
879+
});
876880
};
877881
popValue = () => {
878882
const { isMulti } = this.props;

packages/react-select/src/__tests__/Select.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,7 @@ test('should call onChange with `null` on hitting backspace when backspaceRemove
16611661
expect(onChangeSpy).toHaveBeenCalledWith(null, {
16621662
action: 'clear',
16631663
name: 'test-input-name',
1664+
removedValues: [],
16641665
});
16651666
});
16661667

@@ -2308,6 +2309,7 @@ test('clear select by clicking on clear button > should not call onMenuOpen', ()
23082309
expect(onChangeSpy).toBeCalledWith([], {
23092310
action: 'clear',
23102311
name: BASIC_PROPS.name,
2312+
removedValues: [{ label: '0', value: 'zero' }],
23112313
});
23122314
});
23132315

@@ -2631,6 +2633,7 @@ test('to clear value when hitting escape if escapeClearsValue and isClearable ar
26312633
expect(onInputChangeSpy).toHaveBeenCalledWith(null, {
26322634
action: 'clear',
26332635
name: BASIC_PROPS.name,
2636+
removedValues: [{ label: '0', value: 'zero' }],
26342637
});
26352638
});
26362639

0 commit comments

Comments
 (0)