2019-09-12 20:15:17 +08:00
|
|
|
// TODO: 4.0 - codemod should help to change `filterOption` to support node props.
|
|
|
|
|
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { SelectProps as RcSelectProps } from 'rc-select';
|
2022-11-09 12:28:04 +08:00
|
|
|
import RcSelect, { type BaseSelectRef, OptGroup, Option } from 'rc-select';
|
|
|
|
import type { OptionProps } from 'rc-select/lib/Option';
|
2022-06-06 23:39:00 +08:00
|
|
|
import type { BaseOptionType, DefaultOptionType } from 'rc-select/lib/Select';
|
|
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
import * as React from 'react';
|
2020-10-24 20:33:18 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-22 14:57:09 +08:00
|
|
|
import defaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
|
|
|
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-06-06 23:39:00 +08:00
|
|
|
import type { SelectCommonPlacement } from '../_util/motion';
|
|
|
|
import { getTransitionDirection, getTransitionName } from '../_util/motion';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
2022-06-06 23:39:00 +08:00
|
|
|
import getIcons from './utils/iconUtil';
|
2018-12-22 21:21:42 +08:00
|
|
|
|
2022-03-08 10:29:00 +08:00
|
|
|
import useStyle from './style';
|
2022-07-13 23:06:28 +08:00
|
|
|
import genPurePanel from '../_util/PurePanel';
|
2022-09-08 14:33:11 +08:00
|
|
|
import warning from '../_util/warning';
|
2022-10-18 16:23:10 +08:00
|
|
|
import { useCompactItemContext } from '../space/Compact';
|
2022-03-08 10:29:00 +08:00
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
type RawValue = string | number;
|
2015-06-09 21:16:59 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export type { 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;
|
2022-08-05 11:21:07 +08:00
|
|
|
popupClassName?: string;
|
2022-09-08 14:33:11 +08:00
|
|
|
/** @deprecated Please use `popupClassName` instead */
|
|
|
|
dropdownClassName?: string;
|
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,
|
2022-08-05 11:21:07 +08:00
|
|
|
popupClassName,
|
2022-09-08 14:33:11 +08:00
|
|
|
dropdownClassName,
|
2020-10-24 20:33:18 +08:00
|
|
|
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,
|
2022-12-14 10:22:17 +08:00
|
|
|
select,
|
2020-10-24 20:33:18 +08:00
|
|
|
} = 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();
|
2022-10-18 16:23:10 +08:00
|
|
|
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
|
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,
|
2022-12-06 21:09:59 +08:00
|
|
|
} = React.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 {
|
2022-05-16 16:34:42 +08:00
|
|
|
mergedNotFound = (renderEmpty || defaultRenderEmpty)('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-09-08 14:33:11 +08:00
|
|
|
popupClassName || dropdownClassName,
|
2022-03-08 10:29:00 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-dropdown-${direction}`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
hashId,
|
|
|
|
);
|
2020-10-24 20:33:18 +08:00
|
|
|
|
2022-10-18 16:23:10 +08:00
|
|
|
const mergedSize = compactSize || customizeSize || size;
|
2022-04-29 20:48:10 +08:00
|
|
|
|
|
|
|
// ===================== Disabled =====================
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
2022-09-20 16:48:59 +08:00
|
|
|
const mergedDisabled = customDisabled ?? disabled;
|
2022-04-29 20:48:10 +08:00
|
|
|
|
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),
|
2022-10-18 16:23:10 +08:00
|
|
|
compactItemClassnames,
|
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-09-08 14:33:11 +08:00
|
|
|
// ====================== Warning ======================
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(
|
|
|
|
!dropdownClassName,
|
|
|
|
'Select',
|
|
|
|
'`dropdownClassName` is deprecated. Please use `popupClassName` instead.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ====================== Render =======================
|
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}
|
2022-12-14 10:22:17 +08:00
|
|
|
showSearch={select?.showSearch}
|
2020-10-24 20:33:18 +08:00
|
|
|
{...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;
|
2022-07-07 20:05:19 +08:00
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
2021-12-21 21:38:11 +08:00
|
|
|
};
|
2020-10-24 20:33:18 +08:00
|
|
|
|
2022-07-07 20:05:19 +08:00
|
|
|
// We don't care debug panel
|
|
|
|
/* istanbul ignore next */
|
|
|
|
const PurePanel = genPurePanel(Select);
|
|
|
|
|
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;
|
2022-07-07 20:05:19 +08:00
|
|
|
Select._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
|
2020-10-24 20:33:18 +08:00
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
export default Select;
|