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

Fix pop-value being sent for undefined values. #5960

Merged
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
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