Skip to content

Commit b601c63

Browse files
author
刘欢
committed
feat: use hooks => useSearchConfig
1 parent f52d3a5 commit b601c63

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

src/Select.tsx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import type { FlattenOptionData } from './interface';
5656
import { hasValue, isComboNoValue, toArray } from './utils/commonUtil';
5757
import { fillFieldNames, flattenOptions, injectPropsWithOption } from './utils/valueUtil';
5858
import warningProps, { warningNullOptions } from './utils/warningPropsUtil';
59+
import useSearchConfig from './hooks/useSearchConfig';
5960

6061
const OMIT_DOM_PROPS = ['inputValue'];
6162

@@ -110,7 +111,7 @@ type ArrayElementType<T> = T extends (infer E)[] ? E : T;
110111

111112
export type SemanticName = BaseSelectSemanticName;
112113
export type PopupSemantic = 'listItem' | 'list';
113-
interface SearchConfig<OptionType> {
114+
export interface SearchConfig<OptionType> {
114115
searchValue?: string;
115116
autoClearSearchValue?: boolean;
116117
onSearch?: (value: string) => void;
@@ -226,21 +227,22 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
226227
...restProps
227228
} = props;
228229

229-
const legacySearchProps = [
230-
'filterOption',
231-
'searchValue',
232-
'optionFilterProp',
233-
'optionLabelProp',
234-
'filterSort',
235-
'onSearch',
236-
'autoClearSearchValue',
237-
'tokenSeparators',
238-
];
239-
const legacyShowSearch: SearchConfig<DefaultOptionType> = {};
240-
legacySearchProps.forEach((propsName) => {
241-
legacyShowSearch[propsName] = props?.[propsName];
242-
});
243-
const mergedShowSearch = typeof showSearch === 'object' ? showSearch : legacyShowSearch;
230+
// const legacySearchProps = [
231+
// 'filterOption',
232+
// 'searchValue',
233+
// 'optionFilterProp',
234+
// 'optionLabelProp',
235+
// 'filterSort',
236+
// 'onSearch',
237+
// 'autoClearSearchValue',
238+
// 'tokenSeparators',
239+
// ];
240+
// const legacyShowSearch: SearchConfig<DefaultOptionType> = {};
241+
// legacySearchProps.forEach((propsName) => {
242+
// legacyShowSearch[propsName] = props?.[propsName];
243+
// });
244+
const [mergedShowSearch, searchConfig] = useSearchConfig(showSearch, props);
245+
// const mergedShowSearch = typeof showSearch === 'object' ? showSearch : legacyShowSearch;
244246
const {
245247
filterOption,
246248
searchValue,
@@ -250,7 +252,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
250252
onSearch,
251253
autoClearSearchValue = true,
252254
tokenSeparators,
253-
} = mergedShowSearch;
255+
} = searchConfig;
254256

255257
const mergedId = useId(id);
256258
const multiple = isMultiple(mode);
@@ -734,7 +736,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
734736
// >>> Trigger
735737
direction={direction}
736738
// >>> Search
737-
showSearch={showSearch === undefined ? undefined : !!showSearch}
739+
showSearch={mergedShowSearch}
738740
searchValue={mergedSearchValue}
739741
onSearch={onInternalSearch}
740742
autoClearSearchValue={autoClearSearchValue}

src/hooks/useSearchConfig.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { SearchConfig, DefaultOptionType } from '@/Select';
2+
import * as React from 'react';
3+
const legacySearchProps = [
4+
'filterOption',
5+
'searchValue',
6+
'optionFilterProp',
7+
'optionLabelProp',
8+
'filterSort',
9+
'onSearch',
10+
'autoClearSearchValue',
11+
'tokenSeparators',
12+
];
13+
// Convert `showSearch` to unique config
14+
export default function useSearchConfig(showSearch, props) {
15+
return React.useMemo<[boolean, SearchConfig<DefaultOptionType>]>(() => {
16+
const legacyShowSearch: SearchConfig<DefaultOptionType> = {};
17+
legacySearchProps.forEach((propsName) => {
18+
legacyShowSearch[propsName] = props?.[propsName];
19+
});
20+
const searchConfig: SearchConfig<DefaultOptionType> =
21+
typeof showSearch === 'object' ? showSearch : legacyShowSearch;
22+
if (showSearch === undefined) {
23+
return [undefined, searchConfig];
24+
}
25+
if (!showSearch) {
26+
return [false, {}];
27+
}
28+
return [true, searchConfig];
29+
}, [
30+
showSearch,
31+
props?.filterOption,
32+
props?.searchValue,
33+
props?.optionFilterProp,
34+
props?.optionLabelProp,
35+
props?.filterSort,
36+
props?.onSearch,
37+
props?.autoClearSearchValue,
38+
props?.tokenSeparators,
39+
]);
40+
}

0 commit comments

Comments
 (0)