2017-04-12 04:49:08 +08:00
|
|
|
import React from 'react';
|
|
|
|
import 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';
|
2017-03-28 21:27:58 +08:00
|
|
|
import warning from '../_util/warning';
|
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;
|
2017-03-28 15:10:07 +08:00
|
|
|
size?: 'default' | 'large' | 'small';
|
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;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
placeholder?: string;
|
2017-05-19 11:29:16 +08:00
|
|
|
dropdownClassName?: string;
|
|
|
|
dropdownStyle?: React.CSSProperties;
|
|
|
|
dropdownMenuStyle?: React.CSSProperties;
|
|
|
|
onSearch?: (value: string) => any;
|
|
|
|
filterOption?: boolean | ((inputValue: string, option: Object) => any);
|
2017-01-12 09:23:56 +08:00
|
|
|
}
|
|
|
|
|
2017-03-28 15:10:07 +08:00
|
|
|
export interface LabeledValue {
|
|
|
|
key: string;
|
|
|
|
label: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type SelectValue = string | any[] | LabeledValue | LabeledValue[];
|
|
|
|
|
2017-01-12 09:23:56 +08:00
|
|
|
export interface SelectProps extends AbstractSelectProps {
|
|
|
|
value?: SelectValue;
|
|
|
|
defaultValue?: SelectValue;
|
2017-03-28 21:27:58 +08:00
|
|
|
mode?: 'default' | 'multiple' | 'tags' | 'combobox';
|
2017-01-12 09:23:56 +08:00
|
|
|
multiple?: boolean;
|
2016-08-10 09:46:56 +08:00
|
|
|
tags?: boolean;
|
2017-03-28 21:27:58 +08:00
|
|
|
combobox?: boolean;
|
2017-03-28 15:10:07 +08:00
|
|
|
optionLabelProp?: string;
|
|
|
|
onChange?: (value: SelectValue) => void;
|
2016-08-10 09:46:56 +08:00
|
|
|
onSelect?: (value: SelectValue, option: Object) => any;
|
|
|
|
onDeselect?: (value: SelectValue) => any;
|
2017-08-06 16:45:10 +08:00
|
|
|
onBlur?: () => any;
|
|
|
|
onFocus?: () => any;
|
2016-08-10 09:46:56 +08:00
|
|
|
dropdownMatchSelectWidth?: boolean;
|
|
|
|
optionFilterProp?: string;
|
|
|
|
defaultActiveFirstOption?: boolean;
|
|
|
|
labelInValue?: boolean;
|
2017-03-28 13:20:05 +08:00
|
|
|
getPopupContainer?: (triggerNode: Element) => HTMLElement;
|
2017-01-02 19:11:13 +08:00
|
|
|
tokenSeparators?: string[];
|
2017-01-13 21:19:23 +08:00
|
|
|
getInputElement?: () => React.ReactElement<any>;
|
2016-08-10 09:46:56 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 16:58:14 +08:00
|
|
|
export interface OptionProps {
|
|
|
|
disabled?: boolean;
|
|
|
|
value?: any;
|
2017-08-29 08:58:51 +08:00
|
|
|
title?: string;
|
2016-09-28 16:58:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface OptGroupProps {
|
|
|
|
label?: string | React.ReactElement<any>;
|
|
|
|
}
|
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
export interface SelectContext {
|
|
|
|
antLocale?: {
|
|
|
|
Select?: any,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-17 09:57:07 +08:00
|
|
|
const SelectPropTypes = {
|
2017-08-06 16:45:10 +08:00
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
className: PropTypes.string,
|
|
|
|
size: PropTypes.oneOf(['default', 'large', 'small']),
|
|
|
|
combobox: PropTypes.bool,
|
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
showSearch: PropTypes.bool,
|
|
|
|
optionLabelProp: PropTypes.string,
|
|
|
|
transitionName: PropTypes.string,
|
|
|
|
choiceTransitionName: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
export default class Select extends React.Component<SelectProps, any> {
|
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
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-select',
|
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-04-07 19:21:18 +08:00
|
|
|
static contextTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
antLocale: PropTypes.object,
|
2017-04-07 19:21:18 +08:00
|
|
|
};
|
|
|
|
|
2016-08-10 09:46:56 +08:00
|
|
|
context: SelectContext;
|
|
|
|
|
2017-03-28 21:27:58 +08:00
|
|
|
getLocale() {
|
|
|
|
const { antLocale } = this.context;
|
|
|
|
if (antLocale && antLocale.Select) {
|
|
|
|
return antLocale.Select;
|
|
|
|
}
|
|
|
|
return {
|
2017-04-07 19:21:18 +08:00
|
|
|
notFoundContent: '无匹配结果',
|
2017-03-28 21:27:58 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-07-24 16:05:56 +08:00
|
|
|
render() {
|
2016-10-09 22:55:50 +08:00
|
|
|
const {
|
2016-08-10 09:46:56 +08:00
|
|
|
prefixCls,
|
2016-10-24 16:30:38 +08:00
|
|
|
className = '',
|
2016-08-10 09:46:56 +08:00
|
|
|
size,
|
2017-03-28 21:27:58 +08:00
|
|
|
mode,
|
|
|
|
// @deprecated
|
|
|
|
multiple,
|
|
|
|
tags,
|
2016-08-10 09:46:56 +08:00
|
|
|
combobox,
|
2017-03-28 21:27:58 +08:00
|
|
|
...restProps,
|
2015-12-06 17:13:06 +08:00
|
|
|
} = this.props;
|
2017-03-28 21:27:58 +08:00
|
|
|
warning(
|
|
|
|
!multiple && !tags && !combobox,
|
|
|
|
'`Select[multiple|tags|combobox]` is deprecated, please use `Select[mode]` instead.',
|
|
|
|
);
|
2016-10-09 22:55:50 +08:00
|
|
|
|
2015-12-06 17:13:06 +08:00
|
|
|
const cls = classNames({
|
2016-04-06 15:50:58 +08:00
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
2016-11-30 10:20:23 +08:00
|
|
|
}, className);
|
2015-11-12 21:28:02 +08:00
|
|
|
|
2017-03-28 21:27:58 +08:00
|
|
|
const locale = this.getLocale();
|
|
|
|
let { notFoundContent = locale.notFoundContent, optionLabelProp } = this.props;
|
|
|
|
const isCombobox = mode === 'combobox' || combobox;
|
|
|
|
if (isCombobox) {
|
2015-12-06 17:13:06 +08:00
|
|
|
notFoundContent = null;
|
2016-05-04 20:31:12 +08:00
|
|
|
// children 带 dom 结构时,无法填入输入框
|
2016-05-19 15:36:35 +08:00
|
|
|
optionLabelProp = optionLabelProp || 'value';
|
2015-08-23 16:00:05 +08:00
|
|
|
}
|
2015-12-06 17:13:06 +08:00
|
|
|
|
2017-03-28 21:27:58 +08:00
|
|
|
const modeConfig = {
|
|
|
|
multiple: mode === 'multiple' || multiple,
|
|
|
|
tags: mode === 'tags' || tags,
|
|
|
|
combobox: isCombobox,
|
|
|
|
};
|
|
|
|
|
2015-06-15 17:17:23 +08:00
|
|
|
return (
|
2016-10-09 22:55:50 +08:00
|
|
|
<RcSelect
|
2017-03-28 21:27:58 +08:00
|
|
|
{...restProps}
|
|
|
|
{...modeConfig}
|
|
|
|
prefixCls={prefixCls}
|
2015-12-06 17:13:06 +08:00
|
|
|
className={cls}
|
2016-05-19 15:36:35 +08:00
|
|
|
optionLabelProp={optionLabelProp || 'children'}
|
2016-06-06 13:54:10 +08:00
|
|
|
notFoundContent={notFoundContent}
|
|
|
|
/>
|
2015-06-15 17:17:23 +08:00
|
|
|
);
|
|
|
|
}
|
2016-03-21 09:22:14 +08:00
|
|
|
}
|