Skip to content

Commit de3f3a6

Browse files
committed
chore: replace components
1 parent e81f2f5 commit de3f3a6

File tree

5 files changed

+35
-37
lines changed

5 files changed

+35
-37
lines changed

src/BaseSelect/index.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type { AlignType, BuildInPlacements } from '@rc-component/trigger/lib/interface';
22
import { clsx } from 'clsx';
3-
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
4-
import useControlledState from '@rc-component/util/lib/hooks/useControlledState';
53
import isMobile from '@rc-component/util/lib/isMobile';
64
import { useComposeRef } from '@rc-component/util/lib/ref';
75
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
@@ -10,7 +8,6 @@ import * as React from 'react';
108
import { useAllowClear } from '../hooks/useAllowClear';
119
import { BaseSelectContext } from '../hooks/useBaseProps';
1210
import type { BaseSelectContextProps } from '../hooks/useBaseProps';
13-
import useDelayReset from '../hooks/useDelayReset';
1411
import useLock from '../hooks/useLock';
1512
import useSelectTriggerControl from '../hooks/useSelectTriggerControl';
1613
import useComponents, { type ComponentsConfig } from '../hooks/useComponents';
@@ -24,14 +21,13 @@ import type {
2421
RenderNode,
2522
} from '../interface';
2623
import type { RefSelectorProps } from '../Selector';
27-
import Selector from '../Selector';
2824
import type { RefTriggerProps } from '../SelectTrigger';
2925
import SelectTrigger from '../SelectTrigger';
30-
import TransBtn from '../TransBtn';
3126
import { getSeparatedContent, isValidCount } from '../utils/valueUtil';
3227
import Polite from './Polite';
3328
import useOpen from '../hooks/useOpen';
3429
import { useEvent } from '@rc-component/util';
30+
import type { SelectInputRef } from '../SelectInput';
3531
export type BaseSelectSemanticName = 'prefix' | 'suffix' | 'input' | 'clear';
3632

3733
/**
@@ -347,7 +343,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
347343
}, []);
348344

349345
// ============================== Refs ==============================
350-
const containerRef = React.useRef<HTMLDivElement>(null);
346+
const containerRef = React.useRef<SelectInputRef>(null);
351347
const triggerRef = React.useRef<RefTriggerProps>(null);
352348
const selectorRef = React.useRef<RefSelectorProps>(null);
353349
const listRef = React.useRef<RefOptionListProps>(null);

src/SelectInput/Input.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,16 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
3434
autoComplete,
3535
...restProps
3636
} = props;
37-
const { prefixCls, mode, onSearch, onSearchSubmit, onInputBlur, autoFocus, tokenWithEnter } =
38-
useSelectInputContext();
37+
const {
38+
prefixCls,
39+
mode,
40+
onSearch,
41+
onSearchSubmit,
42+
onInputBlur,
43+
autoFocus,
44+
tokenWithEnter,
45+
components: { input: InputComponent },
46+
} = useSelectInputContext();
3947
const { id, classNames, styles, open, activeDescendantId, role, disabled } = useBaseProps() || {};
4048

4149
const inputCls = clsx(`${prefixCls}-input`, classNames?.input, className);
@@ -144,7 +152,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>((props, ref) => {
144152

145153
// ============================= Render =============================
146154
return (
147-
<input
155+
<InputComponent
148156
id={id}
149157
type={mode === 'combobox' ? 'text' : 'search'}
150158
{...restProps}

src/SelectInput/index.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import Affix from './Affix';
33
import SelectContent from './Content';
44
import SelectInputContext from './context';
55
import type { DisplayValueType, Mode } from '../interface';
6+
import useBaseProps from '../hooks/useBaseProps';
7+
import { omit, useEvent } from '@rc-component/util';
8+
import KeyCode from '@rc-component/util/lib/KeyCode';
9+
import { isValidateOpenKey } from '../utils/keyUtil';
10+
import clsx from 'clsx';
11+
import type { FilledComponentsConfig } from '../hooks/useComponents';
12+
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
613

714
export interface SelectInputRef {
815
focus: (options?: FocusOptions) => void;
@@ -36,13 +43,8 @@ export interface SelectInputProps extends Omit<React.HTMLAttributes<HTMLDivEleme
3643
className?: string;
3744
style?: React.CSSProperties;
3845
focused?: boolean;
39-
[key: string]: any;
46+
components: FilledComponentsConfig;
4047
}
41-
import useBaseProps from '../hooks/useBaseProps';
42-
import { omit, useEvent } from '@rc-component/util';
43-
import KeyCode from '@rc-component/util/lib/KeyCode';
44-
import { isValidateOpenKey } from '../utils/keyUtil';
45-
import clsx from 'clsx';
4648

4749
const DEFAULT_OMIT_PROPS = [
4850
'value',
@@ -172,7 +174,9 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
172174
// ====================== Open ======================
173175
const onInternalMouseDown: SelectInputProps['onMouseDown'] = useEvent((event) => {
174176
if (!disabled) {
175-
event.preventDefault();
177+
if (event.target !== getDOM(inputRef.current)) {
178+
event.preventDefault();
179+
}
176180

177181
// Check if we should prevent closing when clicking on selector
178182
// Don't close if: open && not multiple && (combobox mode || showSearch)
@@ -181,13 +185,6 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
181185
if (!(event.nativeEvent as any)._select_lazy) {
182186
inputRef.current?.focus();
183187

184-
// Clear search value if autoClearSearchValue is not false when closing
185-
if ((mode !== 'combobox' && (!showSearch || shouldPreventClose)) || !triggerOpen) {
186-
if (triggerOpen && autoClearSearchValue !== false) {
187-
onSearch?.('', true, false);
188-
}
189-
}
190-
191188
// Only toggle open if we should not prevent close
192189
if (!shouldPreventClose) {
193190
toggleOpen();

src/hooks/useComponents.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import * as React from 'react';
2-
import SelectInput, { type SelectInputProps } from '../SelectInput';
2+
import SelectInput, { type SelectInputRef, type SelectInputProps } from '../SelectInput';
33

44
export interface ComponentsConfig {
55
root?: React.ComponentType<any> | string;
6-
input?:
7-
| React.ComponentType<
8-
| React.TextareaHTMLAttributes<HTMLTextAreaElement>
9-
| React.InputHTMLAttributes<HTMLInputElement>
10-
>
11-
| string;
6+
input?: React.ComponentType<any> | string;
127
}
138

149
export interface FilledComponentsConfig {
15-
root: React.ComponentType<SelectInputProps>;
16-
input: React.ComponentType<
17-
React.TextareaHTMLAttributes<HTMLTextAreaElement> | React.InputHTMLAttributes<HTMLInputElement>
10+
root: React.ForwardRefExoticComponent<SelectInputProps & React.RefAttributes<SelectInputRef>>;
11+
input: React.ForwardRefExoticComponent<
12+
| React.TextareaHTMLAttributes<HTMLTextAreaElement>
13+
| (React.InputHTMLAttributes<HTMLInputElement> &
14+
React.RefAttributes<HTMLInputElement | HTMLTextAreaElement>)
1815
>;
1916
}
2017

tests/Multiple.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ describe('Select.Multiple', () => {
678678
const { container } = render(
679679
<Select mode="multiple" open={false} showSearch={true} searchValue="test" />,
680680
);
681-
expect(container.querySelector('input').value).toBe('');
681+
expect(container.querySelector('input')).toHaveValue('');
682682
});
683683
it('search value should show when autoClearSearchValue is false', () => {
684684
const { container } = render(
@@ -690,7 +690,7 @@ describe('Select.Multiple', () => {
690690
searchValue="test"
691691
/>,
692692
);
693-
expect(container.querySelector('input').value).toBe('test');
693+
expect(container.querySelector('input')).toHaveValue('test');
694694
});
695695
it('search value should no clear when autoClearSearchValue is false', () => {
696696
const { container } = render(
@@ -704,15 +704,15 @@ describe('Select.Multiple', () => {
704704

705705
toggleOpen(container);
706706
toggleOpen(container);
707-
expect(container.querySelector('input').value).toBe('test');
707+
expect(container.querySelector('input')).toHaveValue('test');
708708
});
709709
it('search value should clear when autoClearSearchValue is true', () => {
710710
const { container } = render(
711711
<Select mode="multiple" autoClearSearchValue={true} showSearch={true} searchValue="test" />,
712712
);
713713
toggleOpen(container);
714714
toggleOpen(container);
715-
expect(container.querySelector('input').value).toBe('');
715+
expect(container.querySelector('input')).toHaveValue('');
716716
});
717717
});
718718
});

0 commit comments

Comments
 (0)