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';
|
|
|
|
import RcSelect, { Option, OptGroup, SelectProps as RcSelectProps } from 'rc-select';
|
2020-06-10 11:00:01 +08:00
|
|
|
import { OptionProps } from 'rc-select/lib/Option';
|
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';
|
2020-01-03 13:38:16 +08:00
|
|
|
import SizeContext, { SizeType } from '../config-provider/SizeContext';
|
2021-02-08 17:09:13 +08:00
|
|
|
import { getTransitionName } from '../_util/motion';
|
2018-12-22 21:21:42 +08:00
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
type RawValue = string | number;
|
2015-06-09 21:16:59 +08:00
|
|
|
|
2020-06-10 11:00:01 +08:00
|
|
|
export { OptionProps };
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
export type OptionType = typeof Option;
|
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
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
export interface InternalSelectProps<VT> extends Omit<RcSelectProps<VT>, 'mode'> {
|
2018-09-16 19:43:53 +08:00
|
|
|
suffixIcon?: React.ReactNode;
|
2020-01-03 13:38:16 +08:00
|
|
|
size?: SizeType;
|
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
|
|
|
}
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
export interface SelectProps<VT>
|
2021-08-02 12:07:26 +08:00
|
|
|
extends Omit<
|
|
|
|
InternalSelectProps<VT>,
|
|
|
|
'inputIcon' | 'mode' | 'getInputElement' | 'getRawInputElement' | 'backfill'
|
|
|
|
> {
|
2019-09-12 20:15:17 +08:00
|
|
|
mode?: 'multiple' | 'tags';
|
2017-11-20 17:03:08 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 11:09:24 +08:00
|
|
|
export interface RefSelectProps {
|
|
|
|
focus: () => void;
|
|
|
|
blur: () => void;
|
|
|
|
}
|
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
const SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
|
|
|
|
|
|
|
|
const InternalSelect = <VT extends SelectValue = SelectValue>(
|
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
bordered = true,
|
|
|
|
className,
|
|
|
|
getPopupContainer,
|
|
|
|
dropdownClassName,
|
|
|
|
listHeight = 256,
|
|
|
|
listItemHeight = 24,
|
|
|
|
size: customizeSize,
|
|
|
|
notFoundContent,
|
|
|
|
...props
|
|
|
|
}: SelectProps<VT>,
|
2020-11-03 11:09:24 +08:00
|
|
|
ref: React.Ref<RefSelectProps>,
|
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
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
const mode = React.useMemo(() => {
|
|
|
|
const { mode: m } = props as InternalSelectProps<VT>;
|
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';
|
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,
|
|
|
|
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
|
|
|
|
|
|
|
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<VT>
|
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}
|
2021-02-08 17:09:13 +08:00
|
|
|
transitionName={getTransitionName(rootPrefixCls, 'slide-up', props.transitionName)}
|
2020-10-24 20:33:18 +08:00
|
|
|
listHeight={listHeight}
|
|
|
|
listItemHeight={listItemHeight}
|
|
|
|
mode={mode}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
direction={direction}
|
|
|
|
inputIcon={suffixIcon}
|
|
|
|
menuItemSelectedIcon={itemIcon}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
clearIcon={clearIcon}
|
|
|
|
notFoundContent={mergedNotFound}
|
|
|
|
className={mergedClassName}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
|
|
|
dropdownClassName={rcSelectRtlDropDownClassName}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const SelectRef = React.forwardRef(InternalSelect) as <VT extends SelectValue = SelectValue>(
|
2020-11-06 09:52:42 +08:00
|
|
|
props: SelectProps<VT> & { ref?: React.Ref<RefSelectProps> },
|
2020-10-24 20:33:18 +08:00
|
|
|
) => React.ReactElement;
|
|
|
|
|
|
|
|
type InternalSelectType = typeof SelectRef;
|
|
|
|
|
2020-11-18 17:34:30 +08:00
|
|
|
interface SelectInterface extends InternalSelectType {
|
2020-10-24 20:33:18 +08:00
|
|
|
SECRET_COMBOBOX_MODE_DO_NOT_USE: string;
|
|
|
|
Option: typeof Option;
|
|
|
|
OptGroup: typeof OptGroup;
|
2016-03-21 09:22:14 +08:00
|
|
|
}
|
2019-09-12 20:15:17 +08:00
|
|
|
|
2020-10-24 20:33:18 +08:00
|
|
|
const Select = SelectRef as SelectInterface;
|
|
|
|
|
|
|
|
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;
|