2019-09-12 20:15:17 +08:00
|
|
|
// TODO: 4.0 - codemod should help to change `filterOption` to support node props.
|
|
|
|
|
2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2019-09-12 20:15:17 +08:00
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SelectProps as RcSelectProps } from 'rc-select';
|
|
|
|
import RcSelect, { Option, OptGroup, BaseSelectRef } from 'rc-select';
|
2021-12-21 21:38:11 +08:00
|
|
|
import type { BaseOptionType, DefaultOptionType } from 'rc-select/lib/Select';
|
2020-06-10 11:00:01 +08:00
|
|
|
import { OptionProps } from 'rc-select/lib/Option';
|
2022-02-16 21:39:48 +08:00
|
|
|
import { useContext } from 'react';
|
2020-10-24 20:33:18 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2019-09-28 11:31:28 +08:00
|
|
|
import getIcons from './utils/iconUtil';
|
2022-04-29 20:48:10 +08:00
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2022-03-24 21:54:20 +08:00
|
|
|
import { FormItemInputContext } from '../form/context';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
|
|
|
import type { SelectCommonPlacement } from '../_util/motion';
|
|
|
|
import { getTransitionName, getTransitionDirection } from '../_util/motion';
|
2018-12-22 21:21:42 +08:00
|
|
|
|
2022-03-08 10:29:00 +08:00
|
|
|
import useStyle from './style';
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
type RawValue = string | number;
|
2015-06-09 21:16:59 +08:00
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
export { OptionProps, BaseSelectRef as RefSelectProps, BaseOptionType, DefaultOptionType };
|
2017-01-12 09:23:56 +08:00
|
|
|
|
2017-03-28 15:10:07 +08:00
|
|
|
export interface LabeledValue {
|
2019-09-12 20:15:17 +08:00
|
|
|
key?: string;
|
|
|
|
value: RawValue;
|
2017-03-28 15:10:07 +08:00
|
|
|
label: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2021-04-10 13:35:08 +08:00
|
|
|
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[] | undefined;
|
2017-03-28 15:10:07 +08:00
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
export interface InternalSelectProps<
|
|
|
|
ValueType = any,
|
|
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
|
|
> extends Omit<RcSelectProps<ValueType, OptionType>, 'mode'> {
|
2018-09-16 19:43:53 +08:00
|
|
|
suffixIcon?: React.ReactNode;
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled?: boolean;
|
2019-09-12 20:15:17 +08:00
|
|
|
mode?: 'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
|
2020-02-06 10:02:02 +08:00
|
|
|
bordered?: boolean;
|
2016-09-28 16:58:14 +08:00
|
|
|
}
|
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
export interface SelectProps<
|
|
|
|
ValueType = any,
|
|
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
|
|
> extends Omit<
|
|
|
|
InternalSelectProps<ValueType, OptionType>,
|
2022-01-20 16:54:47 +08:00
|
|
|
'inputIcon' | 'mode' | 'getInputElement' | 'getRawInputElement' | 'backfill' | 'placement'
|
2021-08-02 12:07:26 +08:00
|
|
|
> {
|
2022-01-20 16:54:47 +08:00
|
|
|
placement?: SelectCommonPlacement;
|
2019-09-12 20:15:17 +08:00
|
|
|
mode?: 'multiple' | 'tags';
|
2022-02-16 21:39:48 +08:00
|
|
|
status?: InputStatus;
|
2017-11-20 17:03:08 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
const SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
|
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
const InternalSelect = <OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType>(
|
2020-10-24 20:33:18 +08:00
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
bordered = true,
|
|
|
|
className,
|
|
|
|
getPopupContainer,
|
|
|
|
dropdownClassName,
|
|
|
|
listHeight = 256,
|
2022-01-20 16:54:47 +08:00
|
|
|
placement,
|
2020-10-24 20:33:18 +08:00
|
|
|
listItemHeight = 24,
|
|
|
|
size: customizeSize,
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled: customDisabled,
|
2020-10-24 20:33:18 +08:00
|
|
|
notFoundContent,
|
2022-02-16 21:39:48 +08:00
|
|
|
status: customStatus,
|
|
|
|
showArrow,
|
2020-10-24 20:33:18 +08:00
|
|
|
...props
|
2021-12-21 21:38:11 +08:00
|
|
|
}: SelectProps<OptionType>,
|
|
|
|
ref: React.Ref<BaseSelectRef>,
|
2020-10-24 20:33:18 +08:00
|
|
|
) => {
|
|
|
|
const {
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
renderEmpty,
|
|
|
|
direction,
|
|
|
|
virtual,
|
|
|
|
dropdownMatchSelectWidth,
|
|
|
|
} = React.useContext(ConfigContext);
|
|
|
|
const size = React.useContext(SizeContext);
|
2018-06-19 14:37:35 +08:00
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2018-12-26 16:01:00 +08:00
|
|
|
|
2022-04-07 12:20:34 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-03-08 10:29:00 +08:00
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
const mode = React.useMemo(() => {
|
2021-12-21 21:38:11 +08:00
|
|
|
const { mode: m } = props as InternalSelectProps<OptionType>;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
if ((m as any) === 'combobox') {
|
2019-09-12 20:15:17 +08:00
|
|
|
return undefined;
|
2018-11-27 12:01:30 +08:00
|
|
|
}
|
2019-09-12 20:15:17 +08:00
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
if (m === SECRET_COMBOBOX_MODE_DO_NOT_USE) {
|
2019-09-12 20:15:17 +08:00
|
|
|
return 'combobox';
|
2018-11-27 12:01:30 +08:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
return m;
|
|
|
|
}, [props.mode]);
|
2019-09-12 20:15:17 +08:00
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
const isMultiple = mode === 'multiple' || mode === 'tags';
|
2022-02-16 21:39:48 +08:00
|
|
|
const mergedShowArrow =
|
|
|
|
showArrow !== undefined ? showArrow : props.loading || !(isMultiple || mode === 'combobox');
|
|
|
|
|
2022-03-24 21:54:20 +08:00
|
|
|
// ===================== Form Status =====================
|
2022-03-25 17:48:12 +08:00
|
|
|
const {
|
|
|
|
status: contextStatus,
|
|
|
|
hasFeedback,
|
|
|
|
isFormItemInput,
|
|
|
|
feedbackIcon,
|
|
|
|
} = useContext(FormItemInputContext);
|
2022-02-16 21:39:48 +08:00
|
|
|
const mergedStatus = getMergedStatus(contextStatus, customStatus);
|
2018-12-26 16:01:00 +08:00
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
// ===================== Empty =====================
|
|
|
|
let mergedNotFound: React.ReactNode;
|
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
mergedNotFound = notFoundContent;
|
|
|
|
} else if (mode === 'combobox') {
|
|
|
|
mergedNotFound = null;
|
|
|
|
} else {
|
|
|
|
mergedNotFound = renderEmpty('Select');
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
2020-10-24 20:33:18 +08:00
|
|
|
|
|
|
|
// ===================== Icons =====================
|
|
|
|
const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons({
|
|
|
|
...props,
|
|
|
|
multiple: isMultiple,
|
2022-02-16 21:39:48 +08:00
|
|
|
hasFeedback,
|
2022-03-25 17:48:12 +08:00
|
|
|
feedbackIcon,
|
2022-02-16 21:39:48 +08:00
|
|
|
showArrow: mergedShowArrow,
|
2020-10-24 20:33:18 +08:00
|
|
|
prefixCls,
|
|
|
|
});
|
|
|
|
|
2021-01-13 21:00:30 +08:00
|
|
|
const selectProps = omit(props as typeof props & { itemIcon: any }, ['suffixIcon', 'itemIcon']);
|
2020-10-24 20:33:18 +08:00
|
|
|
|
2022-04-22 13:36:51 +08:00
|
|
|
const rcSelectRtlDropdownClassName = classNames(
|
2022-03-08 10:29:00 +08:00
|
|
|
dropdownClassName,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-dropdown-${direction}`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
hashId,
|
|
|
|
);
|
2020-10-24 20:33:18 +08:00
|
|
|
|
|
|
|
const mergedSize = customizeSize || size;
|
2022-04-29 20:48:10 +08:00
|
|
|
|
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
|
|
|
const mergedDisabled = customDisabled || disabled;
|
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
const mergedClassName = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
|
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
2022-03-24 21:54:20 +08:00
|
|
|
[`${prefixCls}-in-form-item`]: isFormItemInput,
|
2020-10-24 20:33:18 +08:00
|
|
|
},
|
2022-02-16 21:39:48 +08:00
|
|
|
getStatusClassNames(prefixCls, mergedStatus, hasFeedback),
|
2020-10-24 20:33:18 +08:00
|
|
|
className,
|
2022-03-08 10:29:00 +08:00
|
|
|
hashId,
|
2020-10-24 20:33:18 +08:00
|
|
|
);
|
|
|
|
|
2022-01-20 16:54:47 +08:00
|
|
|
// ===================== Placement =====================
|
|
|
|
const getPlacement = () => {
|
|
|
|
if (placement !== undefined) {
|
|
|
|
return placement;
|
|
|
|
}
|
|
|
|
return direction === 'rtl'
|
|
|
|
? ('bottomRight' as SelectCommonPlacement)
|
|
|
|
: ('bottomLeft' as SelectCommonPlacement);
|
|
|
|
};
|
|
|
|
|
2022-03-08 10:29:00 +08:00
|
|
|
return wrapSSR(
|
2021-12-21 21:38:11 +08:00
|
|
|
<RcSelect<any, any>
|
2021-01-13 21:00:30 +08:00
|
|
|
ref={ref as any}
|
2020-10-24 20:33:18 +08:00
|
|
|
virtual={virtual}
|
|
|
|
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
|
|
|
|
{...selectProps}
|
2022-01-20 16:54:47 +08:00
|
|
|
transitionName={getTransitionName(
|
|
|
|
rootPrefixCls,
|
|
|
|
getTransitionDirection(placement),
|
|
|
|
props.transitionName,
|
|
|
|
)}
|
2020-10-24 20:33:18 +08:00
|
|
|
listHeight={listHeight}
|
|
|
|
listItemHeight={listItemHeight}
|
2021-12-21 21:38:11 +08:00
|
|
|
mode={mode as any}
|
2020-10-24 20:33:18 +08:00
|
|
|
prefixCls={prefixCls}
|
2022-01-20 16:54:47 +08:00
|
|
|
placement={getPlacement()}
|
2020-10-24 20:33:18 +08:00
|
|
|
direction={direction}
|
|
|
|
inputIcon={suffixIcon}
|
|
|
|
menuItemSelectedIcon={itemIcon}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
clearIcon={clearIcon}
|
|
|
|
notFoundContent={mergedNotFound}
|
|
|
|
className={mergedClassName}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
2022-04-21 22:09:59 +08:00
|
|
|
dropdownClassName={rcSelectRtlDropdownClassName}
|
2022-02-16 21:39:48 +08:00
|
|
|
showArrow={hasFeedback || showArrow}
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled={mergedDisabled}
|
2022-03-08 10:29:00 +08:00
|
|
|
/>,
|
2020-10-24 20:33:18 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
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) & {
|
2020-10-24 20:33:18 +08:00
|
|
|
SECRET_COMBOBOX_MODE_DO_NOT_USE: string;
|
|
|
|
Option: typeof Option;
|
|
|
|
OptGroup: typeof OptGroup;
|
2021-12-21 21:38:11 +08:00
|
|
|
};
|
2020-10-24 20:33:18 +08:00
|
|
|
|
|
|
|
Select.SECRET_COMBOBOX_MODE_DO_NOT_USE = SECRET_COMBOBOX_MODE_DO_NOT_USE;
|
|
|
|
Select.Option = Option;
|
|
|
|
Select.OptGroup = OptGroup;
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
export default Select;
|