Skip to content

Commit

Permalink
Add removedValues into onChange clear action meta
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eugenet8k committed Jan 22, 2021
1 parent 4da6ee0 commit 19b7634
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pink-cats-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-select": patch
---

Add `removedValues` that points to the current selection into a meta of onChange callback when clearing value.
6 changes: 5 additions & 1 deletion packages/react-select/src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,11 @@ export default class Select extends Component<Props, State> {
this.focusInput();
};
clearValue = () => {
this.onChange(this.props.isMulti ? [] : null, { action: 'clear' });
const { selectValue } = this.state;
this.onChange(this.props.isMulti ? [] : null, {
action: 'clear',
removedValues: selectValue,
});
};
popValue = () => {
const { isMulti } = this.props;
Expand Down
3 changes: 3 additions & 0 deletions packages/react-select/src/__tests__/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,7 @@ test('should call onChange with `null` on hitting backspace when backspaceRemove
expect(onChangeSpy).toHaveBeenCalledWith(null, {
action: 'clear',
name: 'test-input-name',
removedValues: [],
});
});

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

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

Expand Down

0 comments on commit 19b7634

Please sign in to comment.