ant-design/components/select/index.tsx
nitinknolder b2c359cb34
fix: Add interface for Option props. (#24870)
* nitinknolder:export option props

* Add interface for option props in select component

* resolve pr comment
2020-06-10 11:00:01 +08:00

179 lines
4.9 KiB
TypeScript
Executable File

// TODO: 4.0 - codemod should help to change `filterOption` to support node props.
import * as React from 'react';
import omit from 'omit.js';
import classNames from 'classnames';
import RcSelect, { Option, OptGroup, SelectProps as RcSelectProps } from 'rc-select';
import { OptionProps } from 'rc-select/lib/Option';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import getIcons from './utils/iconUtil';
import SizeContext, { SizeType } from '../config-provider/SizeContext';
type RawValue = string | number;
export { OptionProps };
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'> {
suffixIcon?: React.ReactNode;
size?: SizeType;
mode?: 'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
bordered?: boolean;
}
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;
static SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
static defaultProps = {
transitionName: 'slide-up',
choiceTransitionName: 'zoom',
bordered: true,
};
selectRef = React.createRef<RcSelect<ValueType>>();
public focus = () => {
if (this.selectRef.current) {
this.selectRef.current.focus();
}
};
public blur = () => {
if (this.selectRef.current) {
this.selectRef.current.blur();
}
};
getMode = () => {
const { mode } = this.props as InternalSelectProps<ValueType>;
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,
direction,
virtual,
dropdownMatchSelectWidth,
}: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
notFoundContent,
className,
size: customizeSize,
listHeight = 256,
listItemHeight = 24,
getPopupContainer,
dropdownClassName,
bordered,
} = this.props as InternalSelectProps<ValueType>;
const prefixCls = getPrefixCls('select', customizePrefixCls);
const mode = this.getMode();
const isMultiple = mode === 'multiple' || mode === 'tags';
// ===================== Empty =====================
let mergedNotFound: React.ReactNode;
if (notFoundContent !== undefined) {
mergedNotFound = notFoundContent;
} else if (mode === 'combobox') {
mergedNotFound = null;
} else {
mergedNotFound = renderEmpty('Select');
}
// ===================== Icons =====================
const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons({
...this.props,
multiple: isMultiple,
});
const selectProps = omit(this.props, [
'prefixCls',
'suffixIcon',
'itemIcon',
'removeIcon',
'clearIcon',
'size',
'bordered',
]);
const rcSelectRtlDropDownClassName = classNames(dropdownClassName, {
[`${prefixCls}-dropdown-${direction}`]: direction === 'rtl',
});
return (
<SizeContext.Consumer>
{size => {
const mergedSize = customizeSize || size;
const mergedClassName = classNames(className, {
[`${prefixCls}-lg`]: mergedSize === 'large',
[`${prefixCls}-sm`]: mergedSize === 'small',
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-borderless`]: !bordered,
});
return (
<RcSelect<ValueType>
ref={this.selectRef}
virtual={virtual}
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
{...selectProps}
listHeight={listHeight}
listItemHeight={listItemHeight}
mode={mode}
prefixCls={prefixCls}
direction={direction}
inputIcon={suffixIcon}
menuItemSelectedIcon={itemIcon}
removeIcon={removeIcon}
clearIcon={clearIcon}
notFoundContent={mergedNotFound}
className={mergedClassName}
getPopupContainer={getPopupContainer || getContextPopupContainer}
dropdownClassName={rcSelectRtlDropDownClassName}
/>
);
}}
</SizeContext.Consumer>
);
};
render() {
return <ConfigConsumer>{this.renderSelect}</ConfigConsumer>;
}
}
export default Select;