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';
|
2018-09-15 00:00:16 +08:00
|
|
|
import omit from 'omit.js';
|
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';
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
|
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>
|
|
|
|
extends Omit<InternalSelectProps<VT>, 'inputIcon' | 'mode' | 'getInputElement' | 'backfill'> {
|
|
|
|
mode?: 'multiple' | 'tags';
|
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';
|
|
|
|
|
|
|
|
const InternalSelect = <VT extends SelectValue = SelectValue>(
|
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
bordered = true,
|
|
|
|
className,
|
|
|
|
getPopupContainer,
|
|
|
|
dropdownClassName,
|
|
|
|
listHeight = 256,
|
|
|
|
listItemHeight = 24,
|
|
|
|
size: customizeSize,
|
|
|
|
notFoundContent,
|
|
|
|
transitionName = 'slide-up',
|
|
|
|
...props
|
|
|
|
}: SelectProps<VT>,
|
|
|
|
ref: React.Ref<RcSelect<VT>>,
|
|
|
|
) => {
|
|
|
|
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);
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
const selectProps = omit(props, ['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<VT>
|
|
|
|
ref={ref}
|
|
|
|
virtual={virtual}
|
|
|
|
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
|
|
|
|
{...selectProps}
|
|
|
|
transitionName={transitionName}
|
|
|
|
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>(
|
|
|
|
props: SelectProps<VT> & { ref?: React.Ref<RcSelect<VT>> },
|
|
|
|
) => React.ReactElement;
|
|
|
|
|
|
|
|
type InternalSelectType = typeof SelectRef;
|
|
|
|
|
|
|
|
interface SelectInterface extends InternalSelectType {
|
|
|
|
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;
|