Skip to content

Commit 6fda954

Browse files
QdabuliuQafc163
andauthored
fix: improve useDelayReset logic and add focus test for Select (#1160)
Co-authored-by: afc163 <[email protected]>
1 parent 48e2457 commit 6fda954

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/hooks/useDelayReset.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@ export default function useDelayReset(
1414
window.clearTimeout(delayRef.current);
1515
};
1616

17-
React.useEffect(() => cancelLatest, []);
17+
React.useEffect(() => {
18+
return () => {
19+
cancelLatest();
20+
};
21+
}, []);
1822

19-
const delaySetBool = (value: boolean, callback: () => void) => {
23+
const delaySetBool = (value: boolean, callback?: () => void) => {
2024
cancelLatest();
2125

22-
delayRef.current = window.setTimeout(() => {
23-
setBool(value);
24-
if (callback) {
25-
callback();
26-
}
27-
}, timeout);
26+
if (value === true) {
27+
// true 值立即设置
28+
setBool(true);
29+
callback?.();
30+
} else {
31+
// false 值延迟设置
32+
delayRef.current = window.setTimeout(() => {
33+
setBool(false);
34+
callback?.();
35+
}, timeout);
36+
}
2837
};
2938

3039
return [bool, delaySetBool, cancelLatest];

tests/Select.test.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import KeyCode from '@rc-component/util/lib/KeyCode';
1010
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
1111
import { resetWarned } from '@rc-component/util/lib/warning';
1212
import type { ScrollConfig } from 'rc-virtual-list/lib/List';
13-
import React from 'react';
13+
import React, { StrictMode } from 'react';
1414
import type { SelectProps } from '../src';
1515
import Select, { OptGroup, Option, useBaseProps } from '../src';
1616
import BaseSelect from '../src/BaseSelect';
@@ -2670,5 +2670,17 @@ describe('Select.Basic', () => {
26702670
expect(inputNode).toHaveAttribute('readonly');
26712671
}
26722672
});
2673+
2674+
it('should has focus class when focus', () => {
2675+
const { container } = render(
2676+
<StrictMode>
2677+
<Select options={[{ value: 'a', label: '1' }]} />
2678+
</StrictMode>,
2679+
);
2680+
const inputNode = container.querySelector('input');
2681+
fireEvent.focus(inputNode);
2682+
const select = container.querySelector('.rc-select');
2683+
expect(select).toHaveClass('rc-select-focused');
2684+
});
26732685
});
26742686
});

0 commit comments

Comments
 (0)