ant-design/components/select/index.tsx

153 lines
3.9 KiB
TypeScript
Raw Normal View History

// TODO: 4.0 - codemod should help to change `filterOption` to support node props.
import * as React from 'react';
2018-09-15 00:00:16 +08:00
import omit from 'omit.js';
import classNames from 'classnames';
import RcSelect, { Option, OptGroup, SelectProps as RcSelectProps } from 'rc-select';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import getIcons from './utils/iconUtil';
type RawValue = string | number;
2015-06-09 21:16:59 +08:00
export type Size = 'large' | 'default' | 'small';
export type OptionType = typeof Option;
export interface LabeledValue {
key?: string;
value: RawValue;
label: React.ReactNode;
}
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
export interface InternalSelectProps<VT> extends Omit<RcSelectProps<VT>, 'mode'> {
2018-09-16 19:43:53 +08:00
suffixIcon?: React.ReactNode;
size?: Size;
mode?: 'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
}
export interface SelectProps<VT>
extends Omit<InternalSelectProps<VT>, 'inputIcon' | 'mode' | 'getInputElement' | 'backfill'> {
mode?: 'multiple' | 'tags';
}
// We still use class here since `forwardRef` not support generic in typescript
class Select<ValueType extends SelectValue = SelectValue> extends React.Component<
SelectProps<ValueType>
> {
static Option = Option;
static OptGroup = OptGroup;
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 = {
transitionName: 'slide-up',
choiceTransitionName: 'zoom',
2016-07-13 11:14:24 +08:00
};
selectRef = React.createRef<RcSelect<ValueType>>();
2018-06-19 14:37:35 +08:00
public focus = () => {
if (this.selectRef.current) {
this.selectRef.current.focus();
2018-12-26 16:01:00 +08:00
}
};
2018-12-26 16:01:00 +08:00
public blur = () => {
if (this.selectRef.current) {
this.selectRef.current.blur();
}
2019-08-05 18:38:10 +08:00
};
getMode = () => {
const { mode } = this.props as InternalSelectProps<ValueType>;
2019-08-05 18:38:10 +08:00
if ((mode as any) === 'combobox') {
return undefined;
}
if (mode === Select.SECRET_COMBOBOX_MODE_DO_NOT_USE) {
return 'combobox';
}
return mode;
};
renderSelect = ({
getPopupContainer: getContextPopupContainer,
getPrefixCls,
renderEmpty,
}: ConfigConsumerProps) => {
2018-12-26 16:01:00 +08:00
const {
prefixCls: customizePrefixCls,
notFoundContent,
className,
size,
listHeight = 256,
listItemHeight = 32,
getPopupContainer,
} = this.props as InternalSelectProps<ValueType>;
2018-12-26 16:01:00 +08:00
const prefixCls = getPrefixCls('select', customizePrefixCls);
const mode = this.getMode();
const isMultiple = mode === 'multiple' || mode === 'tags';
2018-12-26 16:01:00 +08:00
// ===================== Empty =====================
let mergedNotFound;
if (notFoundContent !== undefined) {
mergedNotFound = notFoundContent;
} else if (mode === 'combobox') {
mergedNotFound = null;
} else {
mergedNotFound = renderEmpty('Select');
2018-12-26 16:01:00 +08:00
}
// ===================== Icons =====================
const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons({
...this.props,
multiple: isMultiple,
});
2018-12-26 16:01:00 +08:00
const selectProps = omit(this.props, [
'prefixCls',
'suffixIcon',
'itemIcon',
'removeIcon',
'clearIcon',
'size',
]);
const mergedClassName = classNames(className, {
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-sm`]: size === 'small',
});
return (
<RcSelect<ValueType>
ref={this.selectRef}
{...selectProps}
listHeight={listHeight}
listItemHeight={listItemHeight}
mode={mode}
2018-12-26 16:01:00 +08:00
prefixCls={prefixCls}
inputIcon={suffixIcon}
menuItemSelectedIcon={itemIcon}
removeIcon={removeIcon}
clearIcon={clearIcon}
notFoundContent={mergedNotFound}
className={mergedClassName}
getPopupContainer={getPopupContainer || getContextPopupContainer}
2018-12-26 16:01:00 +08:00
/>
);
2018-12-26 16:01:00 +08:00
};
render() {
return <ConfigConsumer>{this.renderSelect}</ConfigConsumer>;
}
}
export default Select;