ant-design/components/select/index.tsx

277 lines
7.9 KiB
TypeScript
Raw Normal View History

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';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
2018-09-15 00:00:16 +08:00
import omit from 'omit.js';
2018-06-19 14:37:35 +08:00
import warning from 'warning';
2018-08-09 12:07:27 +08:00
import Icon from '../icon';
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;
2018-06-28 18:03:48 +08:00
showArrow?: boolean;
style?: React.CSSProperties;
2018-01-27 14:27:27 +08:00
tabIndex?: number;
placeholder?: string | React.ReactNode;
defaultActiveFirstOption?: boolean;
dropdownClassName?: string;
dropdownStyle?: React.CSSProperties;
dropdownMenuStyle?: React.CSSProperties;
dropdownMatchSelectWidth?: boolean;
onSearch?: (value: string) => any;
getPopupContainer?: (triggerNode: Element) => HTMLElement;
filterOption?: boolean | ((inputValue: string, option: React.ReactElement<OptionProps>) => any);
id?: string;
defaultOpen?: boolean;
open?: boolean;
onDropdownVisibleChange?: (open: boolean) => void;
fix #11312: add autoClearSearchValue to AbstractSelectProps (#12473) - extends the interface with an optional prop that can be provided to the RcSelect First of all, thank you for your contribution! :-) Please makes sure that these checkboxes are checked before submitting your PR, thank you! * [ x ] Make sure that you propose PR to right branch: bugfix for `master`, feature for branch `feature`. * [ x ] Make sure that you follow antd's [code convention](https://github.com/ant-design/ant-design/wiki/Code-convention-for-antd). * [ x ] Run `npm run lint` and fix those errors before submitting in order to keep consistent code style. * [ x ] Rebase before creating a PR to keep commit history clear. * [ x ] Add some descriptions and refer relative issues for you PR. Extra checklist: **if** *isBugFix* **:** * [ ] Make sure that you add at least one unit test for the bug which you had fixed. **elif** *isNewFeature* **:** * [ ] Update API docs for the component. * [ ] Update/Add demo to demonstrate new feature. * [ ] Update TypeScript definition for the component. * [ ] Add unit tests for the feature. This fix is related to Issue #11312 and adds the optional `autoClearSearchValue` prop which can be supplied to `RcSelect` to the Select´s props. This is not really a new feature and a relatively unsubstantial one a that, so I didn´t add a demo for it, but I could do so if that´s desirable. I updated the English API docs, but I´m not a Chinese speaker so there is no addition to the Chinese documentation. Maybe someone speaking Chinese would be willing to help out.
2018-09-30 15:04:03 +08:00
autoClearSearchValue?: boolean;
}
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[];
export interface SelectProps extends AbstractSelectProps {
value?: SelectValue;
defaultValue?: SelectValue;
2018-06-19 17:10:35 +08:00
mode?: 'default' | 'multiple' | 'tags' | 'combobox' | string;
optionLabelProp?: string;
firstActiveValue?: string | string[];
onChange?: (value: SelectValue, option: React.ReactElement<any> | React.ReactElement<any>[]) => void;
onSelect?: (value: SelectValue, option: React.ReactElement<any>) => any;
2016-08-10 09:46:56 +08:00
onDeselect?: (value: SelectValue) => any;
onBlur?: (value: SelectValue) => void;
onFocus?: () => void;
2018-11-02 15:38:04 +08:00
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
onInputKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
onMouseEnter?: (e: React.MouseEvent<HTMLInputElement>) => any;
onMouseLeave?: (e: React.MouseEvent<HTMLInputElement>) => any;
maxTagCount?: number;
2018-01-11 10:03:10 +08:00
maxTagPlaceholder?: React.ReactNode | ((omittedValues: SelectValue[]) => React.ReactNode);
2016-08-10 09:46:56 +08:00
optionFilterProp?: string;
labelInValue?: boolean;
tokenSeparators?: string[];
getInputElement?: () => React.ReactElement<any>;
autoFocus?: boolean;
2018-09-16 19:43:53 +08:00
suffixIcon?: React.ReactNode;
removeIcon?: React.ReactNode;
clearIcon?: React.ReactNode;
menuItemSelectedIcon?: React.ReactNode;
2016-08-10 09:46:56 +08:00
}
export interface OptionProps {
disabled?: boolean;
2018-05-03 11:28:30 +08:00
value?: string | number;
title?: string;
children?: React.ReactNode;
2018-10-02 04:28:18 +08:00
className?: string;
key?: string;
style?: React.CSSProperties;
}
export interface OptGroupProps {
label?: React.ReactNode;
}
export interface SelectLocale {
notFoundContent?: string;
}
const SelectPropTypes = {
prefixCls: PropTypes.string,
className: PropTypes.string,
size: PropTypes.oneOf(['default', 'large', 'small']),
notFoundContent: PropTypes.any,
showSearch: PropTypes.bool,
optionLabelProp: PropTypes.string,
transitionName: PropTypes.string,
choiceTransitionName: PropTypes.string,
id: 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
export default class Select extends React.Component<SelectProps, {}> {
static Option = Option as React.ClassicComponentClass<OptionProps>;
static OptGroup = OptGroup as React.ClassicComponentClass<OptGroupProps>;
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
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;
private rcSelect: any;
2018-06-19 14:37:35 +08:00
constructor(props: SelectProps) {
super(props);
2018-06-19 17:10:35 +08:00
warning(
props.mode !== 'combobox',
'The combobox mode of Select is deprecated,' +
'it will be removed in next major version,' +
'please use AutoComplete instead',
);
2018-06-19 14:37:35 +08:00
}
focus() {
this.rcSelect.focus();
}
blur() {
this.rcSelect.blur();
}
saveSelect = (node: any) => {
this.rcSelect = node;
}
getNotFoundContent(locale: SelectLocale) {
2018-06-19 17:10:35 +08:00
const { notFoundContent } = this.props;
if (this.isCombobox()) {
// AutoComplete don't have notFoundContent defaultly
return notFoundContent === undefined ? null : notFoundContent;
}
return notFoundContent === undefined ? locale.notFoundContent : notFoundContent;
}
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
}
renderSelect = (locale: SelectLocale) => {
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,
2018-09-16 19:43:53 +08:00
suffixIcon,
removeIcon,
clearIcon,
menuItemSelectedIcon,
...restProps
2015-12-06 17:13:06 +08:00
} = this.props;
const rest = omit(restProps, ['inputIcon']);
2018-09-15 00:00:16 +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',
}, className);
2015-11-12 21:28:02 +08:00
let { optionLabelProp } = this.props;
2018-06-19 17:10:35 +08:00
if (this.isCombobox()) {
// children 带 dom 结构时,无法填入输入框
optionLabelProp = optionLabelProp || 'value';
2015-08-23 16:00:05 +08:00
}
2015-12-06 17:13:06 +08:00
const modeConfig = {
2017-11-30 09:53:32 +08:00
multiple: mode === 'multiple',
tags: mode === 'tags',
2018-06-19 17:10:35 +08:00
combobox: this.isCombobox(),
};
2018-09-16 19:43:53 +08:00
const inputIcon = suffixIcon && (
React.isValidElement<{ className?: string }>(suffixIcon) ?
React.cloneElement(
suffixIcon,
{
className: classNames(
suffixIcon.props.className,
`${prefixCls}-arrow-icon`,
),
},
) : suffixIcon) || (
2018-09-15 00:00:16 +08:00
<Icon type="down" className={`${prefixCls}-arrow-icon`} />
);
2018-08-09 12:07:27 +08:00
const finalRemoveIcon = removeIcon && (
React.isValidElement<{ className?: string }>(removeIcon) ?
React.cloneElement(
removeIcon,
{
className: classNames(
removeIcon.props.className,
`${prefixCls}-remove-icon`,
),
},
) : removeIcon) || (
2018-08-09 12:07:27 +08:00
<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) || (
2018-09-06 21:26:17 +08:00
<Icon type="check" className={`${prefixCls}-selected-icon`} />
);
2015-06-15 17:17:23 +08:00
return (
<RcSelect
2018-08-24 19:47:07 +08:00
inputIcon={inputIcon}
removeIcon={finalRemoveIcon}
clearIcon={finalClearIcon}
menuItemSelectedIcon={finalMenuItemSelectedIcon}
2018-09-15 00:00:16 +08:00
{...rest}
{...modeConfig}
prefixCls={prefixCls}
2015-12-06 17:13:06 +08:00
className={cls}
optionLabelProp={optionLabelProp || 'children'}
notFoundContent={this.getNotFoundContent(locale)}
ref={this.saveSelect}
/>
2015-06-15 17:17:23 +08:00
);
}
render() {
return (
<LocaleReceiver
componentName="Select"
defaultLocale={defaultLocale.Select}
>
{this.renderSelect}
</LocaleReceiver>
);
}
}