ant-design/components/select/index.tsx
二货机器人 ff88851c4c
chore: Bump Select related deps (#33364)
* chore: bump select related version

* test: Fix test case

* chore: bump rc-select version

* test: Update snapshot

* chore: Update Select ts

* test: Update snapshot

* test: Update snapshot

* test: Update snapshot

* chore: Update ts definition

* chore: fix internal ts def

* test: update auto complete snapshot

* chore: update snapshot

* chore: bump version

* chore: fix demo ts

* chore: Move ValueType to the first place

* test: Update test case
2021-12-21 21:38:11 +08:00

168 lines
5.0 KiB
TypeScript
Executable File

// TODO: 4.0 - codemod should help to change `filterOption` to support node props.
import * as React from 'react';
import omit from 'rc-util/lib/omit';
import classNames from 'classnames';
import RcSelect, { Option, OptGroup, SelectProps as RcSelectProps, BaseSelectRef } from 'rc-select';
import type { BaseOptionType, DefaultOptionType } from 'rc-select/lib/Select';
import { OptionProps } from 'rc-select/lib/Option';
import { ConfigContext } from '../config-provider';
import getIcons from './utils/iconUtil';
import SizeContext, { SizeType } from '../config-provider/SizeContext';
import { getTransitionName } from '../_util/motion';
type RawValue = string | number;
export { OptionProps, BaseSelectRef as RefSelectProps, BaseOptionType, DefaultOptionType };
export interface LabeledValue {
key?: string;
value: RawValue;
label: React.ReactNode;
}
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[] | undefined;
export interface InternalSelectProps<
ValueType = any,
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
> extends Omit<RcSelectProps<ValueType, OptionType>, 'mode'> {
suffixIcon?: React.ReactNode;
size?: SizeType;
mode?: 'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
bordered?: boolean;
}
export interface SelectProps<
ValueType = any,
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
> extends Omit<
InternalSelectProps<ValueType, OptionType>,
'inputIcon' | 'mode' | 'getInputElement' | 'getRawInputElement' | 'backfill'
> {
mode?: 'multiple' | 'tags';
}
const SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
const InternalSelect = <OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType>(
{
prefixCls: customizePrefixCls,
bordered = true,
className,
getPopupContainer,
dropdownClassName,
listHeight = 256,
listItemHeight = 24,
size: customizeSize,
notFoundContent,
...props
}: SelectProps<OptionType>,
ref: React.Ref<BaseSelectRef>,
) => {
const {
getPopupContainer: getContextPopupContainer,
getPrefixCls,
renderEmpty,
direction,
virtual,
dropdownMatchSelectWidth,
} = React.useContext(ConfigContext);
const size = React.useContext(SizeContext);
const prefixCls = getPrefixCls('select', customizePrefixCls);
const rootPrefixCls = getPrefixCls();
const mode = React.useMemo(() => {
const { mode: m } = props as InternalSelectProps<OptionType>;
if ((m as any) === 'combobox') {
return undefined;
}
if (m === SECRET_COMBOBOX_MODE_DO_NOT_USE) {
return 'combobox';
}
return m;
}, [props.mode]);
const isMultiple = mode === 'multiple' || mode === 'tags';
// ===================== Empty =====================
let mergedNotFound: React.ReactNode;
if (notFoundContent !== undefined) {
mergedNotFound = notFoundContent;
} else if (mode === 'combobox') {
mergedNotFound = null;
} else {
mergedNotFound = renderEmpty('Select');
}
// ===================== Icons =====================
const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons({
...props,
multiple: isMultiple,
prefixCls,
});
const selectProps = omit(props as typeof props & { itemIcon: any }, ['suffixIcon', 'itemIcon']);
const rcSelectRtlDropDownClassName = classNames(dropdownClassName, {
[`${prefixCls}-dropdown-${direction}`]: direction === 'rtl',
});
const mergedSize = customizeSize || size;
const mergedClassName = classNames(
{
[`${prefixCls}-lg`]: mergedSize === 'large',
[`${prefixCls}-sm`]: mergedSize === 'small',
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-borderless`]: !bordered,
},
className,
);
return (
<RcSelect<any, any>
ref={ref as any}
virtual={virtual}
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
{...selectProps}
transitionName={getTransitionName(rootPrefixCls, 'slide-up', props.transitionName)}
listHeight={listHeight}
listItemHeight={listItemHeight}
mode={mode as any}
prefixCls={prefixCls}
direction={direction}
inputIcon={suffixIcon}
menuItemSelectedIcon={itemIcon}
removeIcon={removeIcon}
clearIcon={clearIcon}
notFoundContent={mergedNotFound}
className={mergedClassName}
getPopupContainer={getPopupContainer || getContextPopupContainer}
dropdownClassName={rcSelectRtlDropDownClassName}
/>
);
};
const Select = React.forwardRef(InternalSelect) as unknown as (<
ValueType = any,
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
>(
props: React.PropsWithChildren<SelectProps<ValueType, OptionType>> & {
ref?: React.Ref<BaseSelectRef>;
},
) => React.ReactElement) & {
SECRET_COMBOBOX_MODE_DO_NOT_USE: string;
Option: typeof Option;
OptGroup: typeof OptGroup;
};
Select.SECRET_COMBOBOX_MODE_DO_NOT_USE = SECRET_COMBOBOX_MODE_DO_NOT_USE;
Select.Option = Option;
Select.OptGroup = OptGroup;
export default Select;