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.

287 lines
8.4 KiB

import * as React from 'react';
import * as PropTypes from 'prop-types';
import RcSelect, { Option, OptGroup } from 'rc-select';
import classNames from 'classnames';
import omit from 'omit.js';
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
5 years ago
import { Loading, Down, Close, CloseCircleFilled, Check } from '@ant-design/icons';
import { ConfigConsumer, ConfigConsumerProps, RenderEmptyHandler } from '../config-provider';
6 years ago
import warning from '../_util/warning';
import { tuple } from '../_util/type';
const SelectSizes = tuple('default', 'large', 'small');
export interface AbstractSelectProps {
prefixCls?: string;
className?: string;
showAction?: string | string[];
size?: (typeof SelectSizes)[number];
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) => void;
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
filterOption?:
| boolean
| ((inputValue: string, option: React.ReactElement<OptionProps>) => boolean);
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;
6 years ago
dropdownRender?: (menu?: React.ReactNode, props?: SelectProps) => React.ReactNode;
loading?: boolean;
}
export interface LabeledValue {
key: string;
label: React.ReactNode;
}
export type SelectValue = string | string[] | number | number[] | LabeledValue | LabeledValue[];
export interface SelectProps<T = SelectValue> extends AbstractSelectProps {
value?: T;
5 years ago
/** @deprecated Use `searchValue` instead. */
inputValue?: string;
searchValue?: string;
defaultValue?: T;
7 years ago
mode?: 'default' | 'multiple' | 'tags' | 'combobox' | string;
optionLabelProp?: string;
firstActiveValue?: string | string[];
onChange?: (value: T, option: React.ReactElement<any> | React.ReactElement<any>[]) => void;
onSelect?: (value: T extends (infer I)[] ? I : T, option: React.ReactElement<any>) => void;
onDeselect?: (value: T extends (infer I)[] ? I : T) => void;
onBlur?: (value: T) => void;
onFocus?: () => void;
onPopupScroll?: React.UIEventHandler<HTMLDivElement>;
onInputKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
onMouseEnter?: (e: React.MouseEvent<HTMLInputElement>) => void;
onMouseLeave?: (e: React.MouseEvent<HTMLInputElement>) => void;
maxTagCount?: number;
maxTagTextLength?: number;
maxTagPlaceholder?: React.ReactNode | ((omittedValues: T[]) => React.ReactNode);
optionFilterProp?: string;
labelInValue?: boolean;
tokenSeparators?: string[];
getInputElement?: () => React.ReactElement<any>;
autoFocus?: boolean;
suffixIcon?: React.ReactNode;
removeIcon?: React.ReactNode;
clearIcon?: React.ReactNode;
menuItemSelectedIcon?: React.ReactNode;
}
export interface OptionProps {
disabled?: boolean;
value?: string | number;
title?: string;
children?: React.ReactNode;
className?: 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(SelectSizes),
notFoundContent: PropTypes.any,
showSearch: PropTypes.bool,
optionLabelProp: PropTypes.string,
transitionName: PropTypes.string,
choiceTransitionName: PropTypes.string,
id: PropTypes.string,
};
export default class Select<T = SelectValue> extends React.Component<SelectProps<T>, {}> {
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 = {
showSearch: false,
transitionName: 'slide-up',
choiceTransitionName: 'zoom',
};
static propTypes = SelectPropTypes;
private rcSelect: any;
constructor(props: SelectProps<T>) {
super(props);
7 years ago
warning(
props.mode !== 'combobox',
'Select',
'The combobox mode is deprecated, ' +
'it will be removed in next major version, ' +
'please use AutoComplete instead',
7 years ago
);
warning(
!('inputValue' in props),
'Select',
'`inputValue` is deprecated. Please use `searchValue` instead.',
);
}
getNotFoundContent(renderEmpty: RenderEmptyHandler) {
7 years ago
const { notFoundContent } = this.props;
if (notFoundContent !== undefined) {
return notFoundContent;
}
7 years ago
if (this.isCombobox()) {
return null;
}
return renderEmpty('Select');
}
saveSelect = (node: any) => {
this.rcSelect = node;
};
focus() {
this.rcSelect.focus();
}
blur() {
this.rcSelect.blur();
}
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
}
renderSuffixIcon(prefixCls: string) {
const { loading, suffixIcon } = this.props;
if (suffixIcon) {
return React.isValidElement<{ className?: string }>(suffixIcon)
? React.cloneElement(suffixIcon, {
className: classNames(suffixIcon.props.className, `${prefixCls}-arrow-icon`),
})
: suffixIcon;
}
if (loading) {
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
5 years ago
return <Loading />;
}
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
5 years ago
return <Down className={`${prefixCls}-arrow-icon`} />;
}
renderSelect = ({
getPopupContainer: getContextPopupContainer,
getPrefixCls,
renderEmpty,
}: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
className = '',
size,
mode,
getPopupContainer,
removeIcon,
clearIcon,
menuItemSelectedIcon,
showArrow,
inputValue,
searchValue,
...restProps
} = this.props;
const rest = omit(restProps, ['inputIcon']);
const prefixCls = getPrefixCls('select', customizePrefixCls);
const cls = classNames(
{
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-show-arrow`]: showArrow,
},
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`),
})
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
5 years ago
: removeIcon)) || <Close className={`${prefixCls}-remove-icon`} />;
const finalClearIcon = (clearIcon &&
(React.isValidElement<{ className?: string }>(clearIcon)
? React.cloneElement(clearIcon, {
className: classNames(clearIcon.props.className, `${prefixCls}-clear-icon`),
})
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
5 years ago
: clearIcon)) || <CloseCircleFilled className={`${prefixCls}-clear-icon`} />;
const finalMenuItemSelectedIcon = (menuItemSelectedIcon &&
(React.isValidElement<{ className?: string }>(menuItemSelectedIcon)
? React.cloneElement(menuItemSelectedIcon, {
className: classNames(
menuItemSelectedIcon.props.className,
`${prefixCls}-selected-icon`,
),
})
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
5 years ago
: menuItemSelectedIcon)) || <Check className={`${prefixCls}-selected-icon`} />;
return (
<RcSelect
inputIcon={this.renderSuffixIcon(prefixCls)}
removeIcon={finalRemoveIcon}
clearIcon={finalClearIcon}
menuItemSelectedIcon={finalMenuItemSelectedIcon}
showArrow={showArrow}
{...rest}
{...modeConfig}
inputValue={searchValue || inputValue}
prefixCls={prefixCls}
className={cls}
optionLabelProp={optionLabelProp || 'children'}
notFoundContent={this.getNotFoundContent(renderEmpty)}
getPopupContainer={getPopupContainer || getContextPopupContainer}
ref={this.saveSelect}
/>
);
};
render() {
return <ConfigConsumer>{this.renderSelect}</ConfigConsumer>;
}
}