Skip to content

Commit

Permalink
Fix pop-value being sent for undefined values. (#5960)
Browse files Browse the repository at this point in the history
* Fix pop-value being sent for undefined values.

* Fix unit tests.

* Fix linting.

* Add changeset.
  • Loading branch information
leonaves authored Sep 19, 2024
1 parent 06e3488 commit dd740ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-snakes-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-select': patch
---

No longer send pop-value action when multi-select is empty. This correctly resolves typings with that event, where removedValue cannot be undefined.
10 changes: 6 additions & 4 deletions packages/react-select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1096,10 +1096,12 @@ export default class Select<
newValueArray[0] || null
);

this.onChange(newValue, {
action: 'pop-value',
removedValue: lastSelectedValue,
});
if (lastSelectedValue) {
this.onChange(newValue, {
action: 'pop-value',
removedValue: lastSelectedValue,
});
}
};

// ==============================
Expand Down
21 changes: 20 additions & 1 deletion packages/react-select/src/__tests__/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,7 @@ test('should call onChange with an array on hitting backspace when backspaceRemo
isClearable
isMulti
onChange={onChangeSpy}
value={[OPTIONS[0]]}
/>
);
fireEvent.keyDown(container.querySelector('.react-select__control')!, {
Expand All @@ -1915,10 +1916,28 @@ test('should call onChange with an array on hitting backspace when backspaceRemo
expect(onChangeSpy).toHaveBeenCalledWith([], {
action: 'pop-value',
name: 'test-input-name',
removedValue: undefined,
removedValue: OPTIONS[0],
});
});

test('should call not call onChange on hitting backspace when backspaceRemovesValue is true and isMulti is true and there are no values', () => {
let onChangeSpy = jest.fn();
let { container } = render(
<Select
{...BASIC_PROPS}
backspaceRemovesValue
isClearable
isMulti
onChange={onChangeSpy}
/>
);
fireEvent.keyDown(container.querySelector('.react-select__control')!, {
keyCode: 8,
key: 'Backspace',
});
expect(onChangeSpy).not.toHaveBeenCalled();
});

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

0 comments on commit dd740ce

Please sign in to comment.