You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
6.5 KiB

import * as React from 'react';
import * as PropTypes from 'prop-types';
import RcSelect, { Option, OptGroup } from 'rc-select';
import classNames from 'classnames';
import LocaleReceiver from '../locale-provider/LocaleReceiver';
import defaultLocale from '../locale-provider/default';
import omit from 'omit.js';
import warning from 'warning';
6 years ago
import Icon from '../icon';
export interface AbstractSelectProps {
prefixCls?: string;
className?: string;
size?: 'default' | 'large' | 'small';
notFoundContent?: React.ReactNode | null;
transitionName?: string;
choiceTransitionName?: string;
showSearch?: boolean;
allowClear?: boolean;
disabled?: boolean;
showArrow?: boolean;
style?: React.CSSProperties;
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&#39;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.
6 years ago
autoClearSearchValue?: boolean;
}
export interface LabeledValue {
key: string;
label: React.ReactNode;
}
export type SelectValue = string | string[] | number | number[] | LabeledValue | LabeledValue[];
export interface SelectProps extends AbstractSelectProps {
value?: SelectValue;
defaultValue?: SelectValue;
7 years ago
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;
onDeselect?: (value: SelectValue) => any;
onBlur?: (value: SelectValue) => void;
onFocus?: () => void;
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
onInputKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
onMouseEnter?: (e: React.MouseEvent<HTMLInputElement>) => any;
onMouseLeave?: (e: React.MouseEvent<HTMLInputElement>) => any;
maxTagCount?: number;
maxTagPlaceholder?: React.ReactNode | ((omittedValues: SelectValue[]) => React.ReactNode);
optionFilterProp?: string;
labelInValue?: boolean;
tokenSeparators?: string[];
getInputElement?: () => React.ReactElement<any>;
autoFocus?: boolean;
suffixIcon?: React.ReactNode;
}
export interface OptionProps {
disabled?: boolean;
value?: string | number;
title?: string;
children?: React.ReactNode;
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 };
export default class Select extends React.Component<SelectProps, {}> {
static Option = Option as React.ClassicComponentClass<OptionProps>;
static OptGroup = OptGroup as React.ClassicComponentClass<OptGroupProps>;
7 years ago
static SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
7 years ago
static defaultProps = {
prefixCls: 'ant-select',
showSearch: false,
transitionName: 'slide-up',
choiceTransitionName: 'zoom',
};
static propTypes = SelectPropTypes;
private rcSelect: any;
constructor(props: SelectProps) {
super(props);
7 years ago
warning(
props.mode !== 'combobox',
'The combobox mode of Select is deprecated,' +
'it will be removed in next major version,' +
'please use AutoComplete instead',
);
}
focus() {
this.rcSelect.focus();
}
blur() {
this.rcSelect.blur();
}
saveSelect = (node: any) => {
this.rcSelect = node;
}
getNotFoundContent(locale: SelectLocale) {
7 years ago
const { notFoundContent } = this.props;
if (this.isCombobox()) {
// AutoComplete don't have notFoundContent defaultly
return notFoundContent === undefined ? null : notFoundContent;
}
return notFoundContent === undefined ? locale.notFoundContent : notFoundContent;
}
7 years ago
isCombobox() {
const { mode } = this.props;
7 years ago
return mode === 'combobox' || mode === Select.SECRET_COMBOBOX_MODE_DO_NOT_USE;
7 years ago
}
renderSelect = (locale: SelectLocale) => {
const {
prefixCls,
className = '',
size,
mode,
suffixIcon,
...restProps
} = this.props;
const rest = omit(restProps, ['inputIcon', 'removeIcon', 'clearIcon']);
const cls = classNames({
9 years ago
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-sm`]: size === 'small',
}, className);
let { optionLabelProp } = this.props;
7 years ago
if (this.isCombobox()) {
// children 带 dom 结构时,无法填入输入框
optionLabelProp = optionLabelProp || 'value';
}
const modeConfig = {
multiple: mode === 'multiple',
tags: mode === 'tags',
7 years ago
combobox: this.isCombobox(),
};
const inputIcon = suffixIcon && (
React.isValidElement<{ className?: string }>(suffixIcon)
? React.cloneElement(suffixIcon) : suffixIcon) || (
<Icon type="down" className={`${prefixCls}-arrow-icon`} />
);
6 years ago
const removeIcon = (
<Icon type="close" className={`${prefixCls}-remove-icon`} />
);
const clearIcon = (
<Icon type="close-circle" theme="filled" className={`${prefixCls}-clear-icon`} />
);
const menuItemSelectedIcon = (
<Icon type="check" className={`${prefixCls}-selected-icon`} />
);
10 years ago
return (
<RcSelect
6 years ago
inputIcon={inputIcon}
removeIcon={removeIcon}
clearIcon={clearIcon}
menuItemSelectedIcon={menuItemSelectedIcon}
{...rest}
{...modeConfig}
prefixCls={prefixCls}
className={cls}
optionLabelProp={optionLabelProp || 'children'}
notFoundContent={this.getNotFoundContent(locale)}
ref={this.saveSelect}
/>
10 years ago
);
}
render() {
return (
<LocaleReceiver
componentName="Select"
defaultLocale={defaultLocale.Select}
>
{this.renderSelect}
</LocaleReceiver>
);
}
}