Skip to content

[WIP]feat: combine search props #1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
67 changes: 45 additions & 22 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import type { FlattenOptionData } from './interface';
import { hasValue, isComboNoValue, toArray } from './utils/commonUtil';
import { fillFieldNames, flattenOptions, injectPropsWithOption } from './utils/valueUtil';
import warningProps, { warningNullOptions } from './utils/warningPropsUtil';
import useSearchConfig from './hooks/useSearchConfig';

const OMIT_DOM_PROPS = ['inputValue'];

Expand Down Expand Up @@ -110,34 +111,52 @@ type ArrayElementType<T> = T extends (infer E)[] ? E : T;

export type SemanticName = BaseSelectSemanticName;
export type PopupSemantic = 'listItem' | 'list';
export interface SearchConfig<OptionType> {
searchValue?: string;
autoClearSearchValue?: boolean;
onSearch?: (value: string) => void;
tokenSeparators?: string[];
filterOption?: boolean | FilterFunc<OptionType>;
filterSort?: (optionA: OptionType, optionB: OptionType, info: { searchValue: string }) => number;
optionFilterProp?: string;
optionLabelProp?: string;
}
export interface SelectProps<ValueType = any, OptionType extends BaseOptionType = DefaultOptionType>
extends BaseSelectPropsWithoutPrivate {
extends Omit<BaseSelectPropsWithoutPrivate, 'showSearch'> {
prefixCls?: string;
id?: string;

backfill?: boolean;

// >>> Field Names
fieldNames?: FieldNames;

searchValue?: string;
onSearch?: (value: string) => void;
showSearch?: boolean | SearchConfig<OptionType>;
/** @deprecated pleace use SearchConfig.searchValue */
searchValue?: SearchConfig<OptionType>['searchValue'];
/** @deprecated pleace use SearchConfig.autoClearSearchValue */
autoClearSearchValue?: boolean;

// >>> Select
onSelect?: SelectHandler<ArrayElementType<ValueType>, OptionType>;
onDeselect?: SelectHandler<ArrayElementType<ValueType>, OptionType>;

/** @deprecated pleace use SearchConfig.onSearch */
onSearch?: SearchConfig<OptionType>['onSearch'];
/** @deprecated pleace use SearchConfig.tokenSeparators */
tokenSeparators?: string[];
// >>> Options
/**
* In Select, `false` means do nothing.
* In TreeSelect, `false` will highlight match item.
* It's by design.
*/
filterOption?: boolean | FilterFunc<OptionType>;
filterSort?: (optionA: OptionType, optionB: OptionType, info: { searchValue: string }) => number;
/** @deprecated pleace use SearchConfig.filterOption */
filterOption?: SearchConfig<OptionType>['filterOption'];
/** @deprecated pleace use SearchConfig.filterSort */
filterSort?: SearchConfig<OptionType>['filterSort'];
/** @deprecated pleace use SearchConfig.optionFilterProp */
optionFilterProp?: string;
/** @deprecated pleace use SearchConfig.optionLabelProp */
optionLabelProp?: string;
// >>> Select
onSelect?: SelectHandler<ArrayElementType<ValueType>, OptionType>;
onDeselect?: SelectHandler<ArrayElementType<ValueType>, OptionType>;

children?: React.ReactNode;
options?: OptionType[];
optionRender?: (
Expand Down Expand Up @@ -176,22 +195,12 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
prefixCls = 'rc-select',
backfill,
fieldNames,

// Search
searchValue,
onSearch,
autoClearSearchValue = true,

showSearch,
// Select
onSelect,
onDeselect,
popupMatchSelectWidth = true,

// Options
filterOption,
filterSort,
optionFilterProp,
optionLabelProp,
options,
optionRender,
children,
Expand All @@ -214,6 +223,18 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
...restProps
} = props;

const [mergedShowSearch, searchConfig] = useSearchConfig(showSearch, props);
const {
filterOption,
searchValue,
optionFilterProp,
optionLabelProp,
filterSort,
onSearch,
autoClearSearchValue = true,
tokenSeparators,
} = searchConfig;

const mergedId = useId(id);
const multiple = isMultiple(mode);
const childrenAsData = !!(!options && children);
Expand Down Expand Up @@ -685,10 +706,12 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
// >>> Trigger
direction={direction}
// >>> Search
showSearch={mergedShowSearch}
searchValue={mergedSearchValue}
onSearch={onInternalSearch}
autoClearSearchValue={autoClearSearchValue}
onSearchSplit={onInternalSearchSplit}
tokenSeparators={tokenSeparators}
popupMatchSelectWidth={popupMatchSelectWidth}
// >>> OptionList
OptionList={OptionList}
Expand Down
40 changes: 40 additions & 0 deletions src/hooks/useSearchConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { SearchConfig, DefaultOptionType } from '@/Select';
import * as React from 'react';
const legacySearchProps = [
'filterOption',
'searchValue',
'optionFilterProp',
'optionLabelProp',
'filterSort',
'onSearch',
'autoClearSearchValue',
'tokenSeparators',
];
// Convert `showSearch` to unique config
export default function useSearchConfig(showSearch, props) {
return React.useMemo<[boolean, SearchConfig<DefaultOptionType>]>(() => {
const legacyShowSearch: SearchConfig<DefaultOptionType> = {};
legacySearchProps.forEach((propsName) => {
legacyShowSearch[propsName] = props?.[propsName];
});
const searchConfig: SearchConfig<DefaultOptionType> =
typeof showSearch === 'object' ? showSearch : legacyShowSearch;
if (showSearch === undefined) {
return [undefined, searchConfig];
}
if (!showSearch) {
return [false, {}];
}
return [true, searchConfig];
}, [
showSearch,
props?.filterOption,
props?.searchValue,
props?.optionFilterProp,
props?.optionLabelProp,
props?.filterSort,
props?.onSearch,
props?.autoClearSearchValue,
props?.tokenSeparators,
]);
}