2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2017-08-07 11:35:22 +08:00
|
|
|
import RcSelect, { Option, OptGroup } from 'rc-select';
|
2015-12-06 17:13:06 +08:00
|
|
|
import classNames from 'classnames';
|
2018-12-26 16:01:00 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps, RenderEmptyHandler } from '../config-provider';
|
2018-09-15 00:00:16 +08:00
|
|
|
import omit from 'omit.js';
|
2019-01-12 22:19:38 +08:00
|
|
|
import warning from '../_util/warning';
|
2018-08-09 12:07:27 +08:00
|
|
|
import Icon from '../icon';
|
2018-12-22 21:21:42 +08:00
|
|
|
import { tuple } from '../_util/type';
|
|
|
|
|
|
|
|
const SelectSizes = tuple('default', 'large', 'small');
|
2015-06-09 21:16:59 +08:00
|
|
|
|
2017-01-12 09:23:56 +08:00
|
|
|
export interface AbstractSelectProps {
|
2017-03-28 15:10:07 +08:00
|
|
|
prefixCls?: string;
|
2017-01-12 09:23:56 +08:00
|
|
|
className?: string;
|
2018-11-14 20:50:21 +08:00
|
|
|
showAction?: string | string[];
|
2018-12-22 21:21:42 +08:00
|
|
|
size?: (typeof SelectSizes)[number];
|
2016-10-24 16:30:38 +08:00
|
|
|
notFoundContent?: React.ReactNode | null;
|
2016-08-10 09:46:56 +08:00
|
|
|
transitionName?: string;
|
|
|
|
choiceTransitionName?: string;
|
2017-01-12 09:23:56 +08:00
|
|
|
showSearch?: boolean;
|
2016-08-10 09:46:56 +08:00
|
|
|
allowClear?: boolean;
|
2017-01-12 09:23:56 +08:00
|
|
|
disabled?: boolean;
|
2018-06-28 18:03:48 +08:00
|
|
|
showArrow?: boolean;
|
2017-01-12 09:23:56 +08:00
|
|
|
style?: React.CSSProperties;
|
2018-01-27 14:27:27 +08:00
|
|
|
tabIndex?: number;
|
2018-04-27 18:22:10 +08:00
|
|
|
placeholder?: string | React.ReactNode;
|
2017-12-06 15:37:12 +08:00
|
|
|
defaultActiveFirstOption?: boolean;
|
2017-05-19 11:29:16 +08:00
|
|
|
dropdownClassName?: string;
|
|
|
|
dropdownStyle?: React.CSSProperties;
|
|
|
|
dropdownMenuStyle?: React.CSSProperties;
|
2018-06-07 10:24:12 +08:00
|
|
|
dropdownMatchSelectWidth?: boolean;
|
2017-05-19 11:29:16 +08:00
|
|
|
onSearch?: (value: string) => any;
|
2019-01-15 18:17:28 +08:00
|
|
|
getPopupContainer?: (triggerNode?: Element) => HTMLElement;
|
2017-11-07 17:24:27 +08:00
|
|
|
filterOption?: boolean | ((inputValue: string, option: React.ReactElement<OptionProps>) => any);
|
2018-07-27 16:47:07 +08:00
|
|
|
id?: string;
|
2018-09-19 22:50:53 +08:00
|
|
|
defaultOpen?: boolean;
|
2018-08-25 23:41:30 +08:00
|
|
|
open?: boolean;
|
|
|
|
onDropdownVisibleChange?: (open: boolean) => void;
|
2018-09-30 15:04:03 +08:00
|
|
|
autoClearSearchValue?: boolean;
|
2018-12-20 17:19:01 +08:00
|
|
|
dropdownRender?: (menu?: React.ReactNode, props?: SelectProps) => React.ReactNode;
|
2018-11-27 12:01:30 +08:00
|
|
|
loading?: boolean;
|
2017-01-12 09:23:56 +08:00
|
|
|
}
|
|
|
|
|
2017-03-28 15:10:07 +08:00
|
|
|
export interface LabeledValue {
|
|
|
|
key: string;
|
|
|
|
label: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2018-05-03 11:28:30 +08:00
|
|
|
export type SelectValue = string | string[] | number | number[] | LabeledValue | LabeledValue[];
|
2017-03-28 15:10:07 +08:00
|
|
|
|
2018-11-22 17:50:05 +08:00
|
|
|
export interface SelectProps<T = SelectValue> extends AbstractSelectProps {
|
|
|
|
value?: T;
|
|
|
|
defaultValue?: T;
|
2018-06-19 17:10:35 +08:00
|
|
|
mode?: 'default' | 'multiple' | 'tags' | 'combobox' | string;
|
2017-03-28 15:10:07 +08:00
|
|
|
optionLabelProp?: string;
|
2018-05-23 19:13:11 +08:00
|
|
|
firstActiveValue?: string | string[];
|
2018-11-22 17:50:05 +08:00
|
|
|
onChange?: (value: T, option: React.ReactElement<any> | React.ReactElement<any>[]) => void;
|
|
|
|
onSelect?: (value: T, option: React.ReactElement<any>) => any;
|
|
|
|
onDeselect?: (value: T) => any;
|
|
|
|
onBlur?: (value: T) => void;
|
2018-10-30 10:52:27 +08:00
|
|
|
onFocus?: () => void;
|
2018-11-02 15:38:04 +08:00
|
|
|
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
|
2017-12-13 18:34:54 +08:00
|
|
|
onInputKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
|
2018-07-18 00:06:27 +08:00
|
|
|
onMouseEnter?: (e: React.MouseEvent<HTMLInputElement>) => any;
|
|
|
|
onMouseLeave?: (e: React.MouseEvent<HTMLInputElement>) => any;
|
2018-01-10 23:37:23 +08:00
|
|
|
maxTagCount?: number;
|
2018-11-22 17:50:05 +08:00
|
|
|
maxTagPlaceholder?: React.ReactNode | ((omittedValues: T[]) => React.ReactNode);
|
2016-08-10 09:46:56 +08:00
|
|
|
optionFilterProp?: string;
|
|
|
|
labelInValue?: boolean;
|
2017-01-02 19:11:13 +08:00
|
|
|
tokenSeparators?: string[];
|
2017-01-13 21:19:23 +08:00
|
|
|
getInputElement?: () => React.ReactElement<any>;
|
2017-11-11 00:07:03 +08:00
|
|
|
autoFocus?: boolean;
|
2018-09-16 19:43:53 +08:00
|
|
|
suffixIcon?: React.ReactNode;
|
2018-11-05 14:57:11 +08:00
|
|
|
removeIcon?: React.ReactNode;
|
|
|
|
clearIcon?: React.ReactNode;
|
|
|
|
menuItemSelectedIcon?: React.ReactNode;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 16:58:14 +08:00
|
|
|
export interface OptionProps {
|
|
|
|
disabled?: boolean;
|
2018-05-03 11:28:30 +08:00
|
|
|
value?: string | number;
|
2017-08-29 08:58:51 +08:00
|
|
|
title?: string;
|
2017-11-07 17:24:27 +08:00
|
|
|
children?: React.ReactNode;
|
2018-10-02 04:28:18 +08:00
|
|
|
className?: string;
|
2018-10-11 23:47:53 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-09-28 16:58:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface OptGroupProps {
|
2017-11-07 17:24:27 +08:00
|
|
|
label?: React.ReactNode;
|
2016-09-28 16:58:14 +08:00
|
|
|
}
|
|
|
|
|
2017-11-20 17:03:08 +08:00
|
|
|
export interface SelectLocale {
|
|
|
|
notFoundContent?: string;
|
|
|
|
}
|
|
|
|
|
2017-08-17 09:57:07 +08:00
|
|
|
const SelectPropTypes = {
|
2017-08-06 16:45:10 +08:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
className: PropTypes.string,
|
2018-12-22 21:21:42 +08:00
|
|
|
size: PropTypes.oneOf(SelectSizes),
|
2017-08-06 16:45:10 +08:00
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
showSearch: PropTypes.bool,
|
|
|
|
optionLabelProp: PropTypes.string,
|
|
|
|
transitionName: PropTypes.string,
|
|
|
|
choiceTransitionName: PropTypes.string,
|
2018-07-27 16:47:07 +08:00
|
|
|
id: PropTypes.string,
|
2017-08-06 16:45:10 +08:00
|
|
|
};
|
|
|
|
|
2016-10-18 11:55:00 +08:00
|
|
|
// => It is needless to export the declaration of below two inner components.
|
|
|
|
// export { Option, OptGroup };
|
2016-08-22 17:26:14 +08:00
|
|
|
|
2018-11-22 17:50:05 +08:00
|
|
|
export default class Select<T = SelectValue> extends React.Component<SelectProps<T>, {}> {
|
2016-09-28 16:58:14 +08:00
|
|
|
static Option = Option as React.ClassicComponentClass<OptionProps>;
|
|
|
|
static OptGroup = OptGroup as React.ClassicComponentClass<OptGroupProps>;
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2018-06-25 11:58:00 +08:00
|
|
|
static SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
|
2018-06-19 17:10:35 +08:00
|
|
|
|
2016-03-29 14:01:10 +08:00
|
|
|
static defaultProps = {
|
2016-08-10 09:46:56 +08:00
|
|
|
showSearch: false,
|
2016-03-29 14:01:10 +08:00
|
|
|
transitionName: 'slide-up',
|
|
|
|
choiceTransitionName: 'zoom',
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2017-08-06 16:45:10 +08:00
|
|
|
static propTypes = SelectPropTypes;
|
2016-05-08 16:05:36 +08:00
|
|
|
|
2017-11-11 00:07:03 +08:00
|
|
|
private rcSelect: any;
|
|
|
|
|
2018-11-22 17:50:05 +08:00
|
|
|
constructor(props: SelectProps<T>) {
|
2018-06-19 14:37:35 +08:00
|
|
|
super(props);
|
|
|
|
|
2018-06-19 17:10:35 +08:00
|
|
|
warning(
|
|
|
|
props.mode !== 'combobox',
|
2019-02-27 15:32:29 +08:00
|
|
|
'Select',
|
|
|
|
'The combobox mode is deprecated, ' +
|
2018-12-22 17:40:25 +08:00
|
|
|
'it will be removed in next major version, ' +
|
2018-12-07 20:02:01 +08:00
|
|
|
'please use AutoComplete instead',
|
2018-06-19 17:10:35 +08:00
|
|
|
);
|
2018-06-19 14:37:35 +08:00
|
|
|
}
|
|
|
|
|
2017-11-11 00:07:03 +08:00
|
|
|
focus() {
|
|
|
|
this.rcSelect.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.rcSelect.blur();
|
|
|
|
}
|
|
|
|
|
2017-11-21 14:53:25 +08:00
|
|
|
saveSelect = (node: any) => {
|
2017-11-11 00:07:03 +08:00
|
|
|
this.rcSelect = node;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-11-11 00:07:03 +08:00
|
|
|
|
2018-12-26 16:01:00 +08:00
|
|
|
getNotFoundContent(renderEmpty: RenderEmptyHandler) {
|
2018-06-19 17:10:35 +08:00
|
|
|
const { notFoundContent } = this.props;
|
2018-12-26 16:01:00 +08:00
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
return notFoundContent;
|
|
|
|
}
|
|
|
|
|
2018-06-19 17:10:35 +08:00
|
|
|
if (this.isCombobox()) {
|
2018-12-26 16:01:00 +08:00
|
|
|
return null;
|
2018-01-04 20:43:56 +08:00
|
|
|
}
|
2018-12-26 16:01:00 +08:00
|
|
|
|
|
|
|
return renderEmpty('Select');
|
|
|
|
|
|
|
|
// if (this.isCombobox()) {
|
|
|
|
// // AutoComplete don't have notFoundContent defaultly
|
|
|
|
// return notFoundContent === undefined ? null : notFoundContent;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return renderEmpty('Select');
|
|
|
|
// // return notFoundContent === undefined ? locale.notFoundContent : notFoundContent;
|
2018-01-04 20:43:56 +08:00
|
|
|
}
|
|
|
|
|
2018-06-19 17:10:35 +08:00
|
|
|
isCombobox() {
|
|
|
|
const { mode } = this.props;
|
2018-06-25 11:58:00 +08:00
|
|
|
return mode === 'combobox' || mode === Select.SECRET_COMBOBOX_MODE_DO_NOT_USE;
|
2018-06-19 17:10:35 +08:00
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderSuffixIcon(prefixCls: string) {
|
2018-12-07 20:02:01 +08:00
|
|
|
const { loading, suffixIcon } = this.props;
|
2018-11-27 12:01:30 +08:00
|
|
|
if (suffixIcon) {
|
2018-12-07 20:02:01 +08:00
|
|
|
return React.isValidElement<{ className?: string }>(suffixIcon)
|
|
|
|
? React.cloneElement(suffixIcon, {
|
|
|
|
className: classNames(suffixIcon.props.className, `${prefixCls}-arrow-icon`),
|
|
|
|
})
|
|
|
|
: suffixIcon;
|
2018-11-27 12:01:30 +08:00
|
|
|
}
|
|
|
|
if (loading) {
|
|
|
|
return <Icon type="loading" />;
|
|
|
|
}
|
|
|
|
return <Icon type="down" className={`${prefixCls}-arrow-icon`} />;
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:01:00 +08:00
|
|
|
renderSelect = ({
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
renderEmpty,
|
|
|
|
}: ConfigConsumerProps) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className = '',
|
|
|
|
size,
|
|
|
|
mode,
|
|
|
|
getPopupContainer,
|
|
|
|
removeIcon,
|
|
|
|
clearIcon,
|
|
|
|
menuItemSelectedIcon,
|
2019-02-28 12:26:43 +08:00
|
|
|
showArrow,
|
2018-12-26 16:01:00 +08:00
|
|
|
...restProps
|
|
|
|
} = this.props;
|
|
|
|
const rest = omit(restProps, ['inputIcon']);
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
|
|
|
const cls = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
2019-02-28 12:26:43 +08:00
|
|
|
[`${prefixCls}-show-arrow`]: showArrow,
|
2018-12-26 16:01:00 +08:00
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
|
|
|
|
let { optionLabelProp } = this.props;
|
|
|
|
if (this.isCombobox()) {
|
|
|
|
// children 带 dom 结构时,无法填入输入框
|
|
|
|
optionLabelProp = optionLabelProp || 'value';
|
|
|
|
}
|
|
|
|
|
|
|
|
const modeConfig = {
|
|
|
|
multiple: mode === 'multiple',
|
|
|
|
tags: mode === 'tags',
|
|
|
|
combobox: this.isCombobox(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const finalRemoveIcon = (removeIcon &&
|
|
|
|
(React.isValidElement<{ className?: string }>(removeIcon)
|
|
|
|
? React.cloneElement(removeIcon, {
|
|
|
|
className: classNames(removeIcon.props.className, `${prefixCls}-remove-icon`),
|
|
|
|
})
|
|
|
|
: removeIcon)) || <Icon type="close" className={`${prefixCls}-remove-icon`} />;
|
|
|
|
|
|
|
|
const finalClearIcon = (clearIcon &&
|
|
|
|
(React.isValidElement<{ className?: string }>(clearIcon)
|
|
|
|
? React.cloneElement(clearIcon, {
|
|
|
|
className: classNames(clearIcon.props.className, `${prefixCls}-clear-icon`),
|
|
|
|
})
|
|
|
|
: clearIcon)) || (
|
|
|
|
<Icon type="close-circle" theme="filled" className={`${prefixCls}-clear-icon`} />
|
|
|
|
);
|
|
|
|
|
|
|
|
const finalMenuItemSelectedIcon = (menuItemSelectedIcon &&
|
|
|
|
(React.isValidElement<{ className?: string }>(menuItemSelectedIcon)
|
|
|
|
? React.cloneElement(menuItemSelectedIcon, {
|
|
|
|
className: classNames(
|
|
|
|
menuItemSelectedIcon.props.className,
|
|
|
|
`${prefixCls}-selected-icon`,
|
|
|
|
),
|
|
|
|
})
|
|
|
|
: menuItemSelectedIcon)) || <Icon type="check" className={`${prefixCls}-selected-icon`} />;
|
2017-10-20 14:54:38 +08:00
|
|
|
|
|
|
|
return (
|
2018-12-26 16:01:00 +08:00
|
|
|
<RcSelect
|
|
|
|
inputIcon={this.renderSuffixIcon(prefixCls)}
|
|
|
|
removeIcon={finalRemoveIcon}
|
|
|
|
clearIcon={finalClearIcon}
|
|
|
|
menuItemSelectedIcon={finalMenuItemSelectedIcon}
|
2019-02-28 12:26:43 +08:00
|
|
|
showArrow={showArrow}
|
2018-12-26 16:01:00 +08:00
|
|
|
{...rest}
|
|
|
|
{...modeConfig}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={cls}
|
|
|
|
optionLabelProp={optionLabelProp || 'children'}
|
|
|
|
notFoundContent={this.getNotFoundContent(renderEmpty)}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
|
|
|
ref={this.saveSelect}
|
|
|
|
/>
|
2017-10-20 14:54:38 +08:00
|
|
|
);
|
2018-12-26 16:01:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <ConfigConsumer>{this.renderSelect}</ConfigConsumer>;
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
2016-03-21 09:22:14 +08:00
|
|
|
}
|