ant-design/components/select/index.tsx

163 lines
4.2 KiB
TypeScript
Raw Normal View History

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';
import enUS from '../locale-provider/en_US';
import warning from '../_util/warning';
2015-06-09 21:16:59 +08:00
export interface AbstractSelectProps {
prefixCls?: string;
className?: string;
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;
showSearch?: boolean;
2016-08-10 09:46:56 +08:00
allowClear?: boolean;
disabled?: boolean;
style?: React.CSSProperties;
placeholder?: string;
dropdownClassName?: string;
dropdownStyle?: React.CSSProperties;
dropdownMenuStyle?: React.CSSProperties;
onSearch?: (value: string) => any;
filterOption?: boolean | ((inputValue: string, option: Object) => any);
}
export interface LabeledValue {
key: string;
label: React.ReactNode;
}
export type SelectValue = string | any[] | LabeledValue | LabeledValue[];
export interface SelectProps extends AbstractSelectProps {
value?: SelectValue;
defaultValue?: SelectValue;
mode?: 'default' | 'multiple' | 'tags' | 'combobox';
multiple?: boolean;
2016-08-10 09:46:56 +08:00
tags?: boolean;
combobox?: boolean;
optionLabelProp?: string;
onChange?: (value: SelectValue) => void;
2016-08-10 09:46:56 +08:00
onSelect?: (value: SelectValue, option: Object) => any;
onDeselect?: (value: SelectValue) => any;
onBlur?: () => any;
onFocus?: () => any;
2016-08-10 09:46:56 +08:00
dropdownMatchSelectWidth?: boolean;
optionFilterProp?: string;
defaultActiveFirstOption?: boolean;
labelInValue?: boolean;
getPopupContainer?: (triggerNode: Element) => HTMLElement;
tokenSeparators?: string[];
getInputElement?: () => React.ReactElement<any>;
2016-08-10 09:46:56 +08:00
}
export interface OptionProps {
disabled?: boolean;
value?: any;
title?: string;
}
export interface OptGroupProps {
label?: string | React.ReactElement<any>;
}
2016-08-10 09:46:56 +08:00
export interface SelectContext {
antLocale?: {
Select?: any,
};
}
const SelectPropTypes = {
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,
};
// => 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> {
static Option = Option as React.ClassicComponentClass<OptionProps>;
static OptGroup = OptGroup as React.ClassicComponentClass<OptGroupProps>;
static defaultProps = {
prefixCls: 'ant-select',
2016-08-10 09:46:56 +08:00
showSearch: false,
transitionName: 'slide-up',
choiceTransitionName: 'zoom',
2016-07-13 11:14:24 +08:00
};
static propTypes = SelectPropTypes;
static contextTypes = {
antLocale: PropTypes.object,
};
2016-08-10 09:46:56 +08:00
context: SelectContext;
getLocale() {
const { antLocale } = this.context;
if (antLocale && antLocale.Select) {
return antLocale.Select;
}
return enUS.Select;
}
2015-07-24 16:05:56 +08:00
render() {
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,
mode,
// @deprecated
multiple,
tags,
2016-08-10 09:46:56 +08:00
combobox,
...restProps,
2015-12-06 17:13:06 +08:00
} = this.props;
warning(
!multiple && !tags && !combobox,
'`Select[multiple|tags|combobox]` is deprecated, please use `Select[mode]` instead.',
);
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',
}, className);
2015-11-12 21:28:02 +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;
// children 带 dom 结构时,无法填入输入框
optionLabelProp = optionLabelProp || 'value';
2015-08-23 16:00:05 +08:00
}
2015-12-06 17:13:06 +08:00
const modeConfig = {
multiple: mode === 'multiple' || multiple,
tags: mode === 'tags' || tags,
combobox: isCombobox,
};
2015-06-15 17:17:23 +08:00
return (
<RcSelect
{...restProps}
{...modeConfig}
prefixCls={prefixCls}
2015-12-06 17:13:06 +08:00
className={cls}
optionLabelProp={optionLabelProp || 'children'}
notFoundContent={notFoundContent}
/>
2015-06-15 17:17:23 +08:00
);
}
}