Skip to content

Commit dd740ce

Browse files
authoredSep 19, 2024··
Fix pop-value being sent for undefined values. (#5960)
* Fix pop-value being sent for undefined values. * Fix unit tests. * Fix linting. * Add changeset.
1 parent 06e3488 commit dd740ce

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed
 

‎.changeset/calm-snakes-collect.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-select': patch
3+
---
4+
5+
No longer send pop-value action when multi-select is empty. This correctly resolves typings with that event, where removedValue cannot be undefined.

‎packages/react-select/src/Select.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -1096,10 +1096,12 @@ export default class Select<
10961096
newValueArray[0] || null
10971097
);
10981098

1099-
this.onChange(newValue, {
1100-
action: 'pop-value',
1101-
removedValue: lastSelectedValue,
1102-
});
1099+
if (lastSelectedValue) {
1100+
this.onChange(newValue, {
1101+
action: 'pop-value',
1102+
removedValue: lastSelectedValue,
1103+
});
1104+
}
11031105
};
11041106

11051107
// ==============================

‎packages/react-select/src/__tests__/Select.test.tsx

+20-1
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,7 @@ test('should call onChange with an array on hitting backspace when backspaceRemo
19061906
isClearable
19071907
isMulti
19081908
onChange={onChangeSpy}
1909+
value={[OPTIONS[0]]}
19091910
/>
19101911
);
19111912
fireEvent.keyDown(container.querySelector('.react-select__control')!, {
@@ -1915,10 +1916,28 @@ test('should call onChange with an array on hitting backspace when backspaceRemo
19151916
expect(onChangeSpy).toHaveBeenCalledWith([], {
19161917
action: 'pop-value',
19171918
name: 'test-input-name',
1918-
removedValue: undefined,
1919+
removedValue: OPTIONS[0],
19191920
});
19201921
});
19211922

1923+
test('should call not call onChange on hitting backspace when backspaceRemovesValue is true and isMulti is true and there are no values', () => {
1924+
let onChangeSpy = jest.fn();
1925+
let { container } = render(
1926+
<Select
1927+
{...BASIC_PROPS}
1928+
backspaceRemovesValue
1929+
isClearable
1930+
isMulti
1931+
onChange={onChangeSpy}
1932+
/>
1933+
);
1934+
fireEvent.keyDown(container.querySelector('.react-select__control')!, {
1935+
keyCode: 8,
1936+
key: 'Backspace',
1937+
});
1938+
expect(onChangeSpy).not.toHaveBeenCalled();
1939+
});
1940+
19221941
test('multi select > clicking on X next to option will call onChange with all options other that the clicked option', () => {
19231942
let onChangeSpy = jest.fn();
19241943
let { container } = render(

0 commit comments

Comments
 (0)
Please sign in to comment.