Skip to content
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
25 changes: 17 additions & 8 deletions src/hooks/useDelayReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ export default function useDelayReset(
window.clearTimeout(delayRef.current);
};

React.useEffect(() => cancelLatest, []);
React.useEffect(() => {
return () => {
cancelLatest();
};
}, []);

const delaySetBool = (value: boolean, callback: () => void) => {
const delaySetBool = (value: boolean, callback?: () => void) => {
cancelLatest();

delayRef.current = window.setTimeout(() => {
setBool(value);
if (callback) {
callback();
}
}, timeout);
if (value === true) {
// true 值立即设置
setBool(true);
callback?.();
} else {
// false 值延迟设置
delayRef.current = window.setTimeout(() => {
setBool(false);
callback?.();
}, timeout);
}
};

return [bool, delaySetBool, cancelLatest];
Expand Down
14 changes: 13 additions & 1 deletion tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import KeyCode from '@rc-component/util/lib/KeyCode';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import { resetWarned } from '@rc-component/util/lib/warning';
import type { ScrollConfig } from 'rc-virtual-list/lib/List';
import React from 'react';
import React, { StrictMode } from 'react';
import type { SelectProps } from '../src';
import Select, { OptGroup, Option, useBaseProps } from '../src';
import BaseSelect from '../src/BaseSelect';
Expand Down Expand Up @@ -2670,5 +2670,17 @@ describe('Select.Basic', () => {
expect(inputNode).toHaveAttribute('readonly');
}
});

it('should has focus class when focus', () => {
const { container } = render(
<StrictMode>
<Select options={[{ value: 'a', label: '1' }]} />
</StrictMode>,
);
const inputNode = container.querySelector('input');
fireEvent.focus(inputNode);
const select = container.querySelector('.rc-select');
expect(select).toHaveClass('rc-select-focused');
});
});
});
Loading