Skip to content

Commit d3b113a

Browse files
authored
fix: Backspace not update state (#441)
* fix: Backspace not update state * add engines
1 parent b7e0826 commit d3b113a

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "rc-select",
33
"version": "10.0.0-alpha.36",
44
"description": "React Select",
5+
"engines": {
6+
"node": ">=8.x"
7+
},
58
"keywords": [
69
"react",
710
"react-component",

src/generate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,13 +717,13 @@ export default function generateSelector<
717717
* - false: Search text is not empty when first time backspace down
718718
*/
719719
const [getClearLock, setClearLock] = useLock();
720-
const clearLock = getClearLock();
721720

722721
// KeyDown
723722
const onInternalKeyDown: React.KeyboardEventHandler<HTMLDivElement> = (
724723
event,
725724
...rest
726725
) => {
726+
const clearLock = getClearLock();
727727
const { which } = event;
728728

729729
// We only manage open state here, close logic should handle by list component

tests/Multiple.test.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { mount } from 'enzyme';
22
import KeyCode from 'rc-util/lib/KeyCode';
33
import React from 'react';
4-
import { resetWarned } from 'rc-util/lib/warning';
54
import Select, { Option, OptGroup } from '../src';
65
import focusTest from './shared/focusTest';
76
import blurTest from './shared/blurTest';
@@ -129,7 +128,10 @@ describe('Select.Multiple', () => {
129128

130129
expect(handleChange).toHaveBeenCalledWith(
131130
[1, 2],
132-
[expect.objectContaining({ value: 1 }), expect.objectContaining({ value: 2, testprop: 2 })],
131+
[
132+
expect.objectContaining({ value: 1 }),
133+
expect.objectContaining({ value: 2, testprop: 2 }),
134+
],
133135
);
134136
});
135137

@@ -239,4 +241,37 @@ describe('Select.Multiple', () => {
239241
});
240242
expect(wrapper.find('.rc-select-arrow-icon').length).toBeTruthy();
241243
});
244+
245+
it('block input when fast backspace', () => {
246+
jest.useFakeTimers();
247+
const onChange = jest.fn();
248+
249+
const wrapper = mount(
250+
<Select
251+
mode="multiple"
252+
value={['bamboo']}
253+
options={[{ value: 'bamboo' }, { value: 'light' }]}
254+
onChange={onChange}
255+
/>,
256+
);
257+
258+
// First type
259+
wrapper.find('input').simulate('keydown', { which: KeyCode.L });
260+
wrapper.find('input').simulate('change', { target: { value: 'l' } });
261+
262+
// Backspace
263+
wrapper.find('input').simulate('keydown', { which: KeyCode.BACKSPACE });
264+
wrapper.find('input').simulate('change', { target: { value: '' } });
265+
266+
onChange.mockReset();
267+
268+
wrapper.find('input').simulate('keydown', { which: KeyCode.BACKSPACE });
269+
expect(onChange).not.toHaveBeenCalled();
270+
271+
jest.runAllTimers();
272+
wrapper.find('input').simulate('keydown', { which: KeyCode.BACKSPACE });
273+
expect(onChange).toHaveBeenCalledWith([], expect.anything());
274+
275+
jest.useRealTimers();
276+
});
242277
});

0 commit comments

Comments
 (0)