2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-10-18 11:55:00 +08:00
|
|
|
import { Option, OptGroup } from 'rc-select';
|
2016-07-25 17:46:45 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-28 15:10:07 +08:00
|
|
|
import InputElement from './InputElement';
|
2018-12-13 22:03:12 +08:00
|
|
|
import Input, { InputProps } from '../input';
|
2018-12-05 19:12:18 +08:00
|
|
|
import Select, { AbstractSelectProps, SelectValue, OptionProps, OptGroupProps } from '../select';
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2019-07-03 16:55:30 +08:00
|
|
|
import { Omit } from '../_util/type';
|
2016-09-19 10:17:07 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
export interface DataSourceItemObject {
|
|
|
|
value: string;
|
|
|
|
text: string;
|
|
|
|
}
|
2018-06-09 15:58:32 +08:00
|
|
|
export type DataSourceItemType =
|
2018-12-07 20:02:01 +08:00
|
|
|
| string
|
|
|
|
| DataSourceItemObject
|
|
|
|
| React.ReactElement<OptionProps>
|
|
|
|
| React.ReactElement<OptGroupProps>;
|
2016-09-22 14:40:34 +08:00
|
|
|
|
2017-12-14 10:49:44 +08:00
|
|
|
export interface AutoCompleteInputProps {
|
2019-06-24 11:29:58 +08:00
|
|
|
onChange?: React.FormEventHandler<any>;
|
|
|
|
value: any;
|
2017-01-13 21:19:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export type ValidInputElement =
|
2018-12-07 20:02:01 +08:00
|
|
|
| HTMLInputElement
|
|
|
|
| HTMLTextAreaElement
|
|
|
|
| React.ReactElement<AutoCompleteInputProps>;
|
2017-01-13 21:19:23 +08:00
|
|
|
|
2019-07-03 16:55:30 +08:00
|
|
|
export interface AutoCompleteProps extends Omit<AbstractSelectProps, 'loading'> {
|
2017-03-28 15:10:07 +08:00
|
|
|
value?: SelectValue;
|
|
|
|
defaultValue?: SelectValue;
|
2018-04-16 17:42:31 +08:00
|
|
|
dataSource?: DataSourceItemType[];
|
2019-09-11 20:27:54 +08:00
|
|
|
dropdownMenuStyle?: React.CSSProperties;
|
2018-08-02 09:13:20 +08:00
|
|
|
autoFocus?: boolean;
|
2018-06-14 14:45:46 +08:00
|
|
|
backfill?: boolean;
|
2017-03-28 15:10:07 +08:00
|
|
|
optionLabelProp?: string;
|
|
|
|
onChange?: (value: SelectValue) => void;
|
2019-06-24 11:29:58 +08:00
|
|
|
onSelect?: (value: SelectValue, option: Object) => any;
|
2018-10-30 10:52:27 +08:00
|
|
|
onBlur?: (value: SelectValue) => void;
|
|
|
|
onFocus?: () => void;
|
2018-12-07 20:02:01 +08:00
|
|
|
children?:
|
|
|
|
| ValidInputElement
|
2018-12-13 22:03:12 +08:00
|
|
|
| React.ReactElement<InputProps>
|
2018-12-07 20:02:01 +08:00
|
|
|
| React.ReactElement<OptionProps>
|
|
|
|
| Array<React.ReactElement<OptionProps>>;
|
2017-01-13 21:19:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-03 22:10:21 +08:00
|
|
|
function isSelectOptionOrSelectOptGroup(child: any): Boolean {
|
|
|
|
return child && child.type && (child.type.isSelectOption || child.type.isSelectOptGroup);
|
|
|
|
}
|
|
|
|
|
2017-11-22 11:26:04 +08:00
|
|
|
export default class AutoComplete extends React.Component<AutoCompleteProps, {}> {
|
2016-10-18 11:55:00 +08:00
|
|
|
static Option = Option as React.ClassicComponentClass<OptionProps>;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2016-10-18 11:55:00 +08:00
|
|
|
static OptGroup = OptGroup as React.ClassicComponentClass<OptGroupProps>;
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
optionLabelProp: 'children',
|
|
|
|
choiceTransitionName: 'zoom',
|
|
|
|
showSearch: false,
|
2017-05-03 15:33:40 +08:00
|
|
|
filterOption: false,
|
2016-07-25 17:46:45 +08:00
|
|
|
};
|
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
private select: any;
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
saveSelect = (node: any) => {
|
|
|
|
this.select = node;
|
|
|
|
};
|
|
|
|
|
2017-01-13 21:19:23 +08:00
|
|
|
getInputElement = () => {
|
|
|
|
const { children } = this.props;
|
2018-12-07 20:02:01 +08:00
|
|
|
const element =
|
|
|
|
children && React.isValidElement(children) && children.type !== Option ? (
|
|
|
|
React.Children.only(this.props.children)
|
|
|
|
) : (
|
|
|
|
<Input />
|
|
|
|
);
|
2019-01-28 11:10:56 +08:00
|
|
|
const elementProps = { ...(element as React.ReactElement<any>).props };
|
2017-09-30 21:17:49 +08:00
|
|
|
// https://github.com/ant-design/ant-design/pull/7742
|
|
|
|
delete elementProps.children;
|
2018-12-07 20:02:01 +08:00
|
|
|
return <InputElement {...elementProps}>{element}</InputElement>;
|
|
|
|
};
|
2017-01-20 18:28:09 +08:00
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
focus() {
|
|
|
|
this.select.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.select.blur();
|
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderAutoComplete = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2018-11-08 18:02:31 +08:00
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
size,
|
|
|
|
className = '',
|
|
|
|
notFoundContent,
|
|
|
|
optionLabelProp,
|
|
|
|
dataSource,
|
|
|
|
children,
|
2016-07-25 17:46:45 +08:00
|
|
|
} = this.props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
const cls = classNames({
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[className]: !!className,
|
|
|
|
[`${prefixCls}-show-search`]: true,
|
2017-01-13 21:19:23 +08:00
|
|
|
[`${prefixCls}-auto-complete`]: true,
|
2016-07-25 17:46:45 +08:00
|
|
|
});
|
|
|
|
|
2017-01-13 21:19:23 +08:00
|
|
|
let options;
|
|
|
|
const childArray = React.Children.toArray(children);
|
2018-12-07 20:02:01 +08:00
|
|
|
if (childArray.length && isSelectOptionOrSelectOptGroup(childArray[0])) {
|
2017-02-09 10:45:42 +08:00
|
|
|
options = children;
|
2017-01-13 21:19:23 +08:00
|
|
|
} else {
|
2018-12-07 20:02:01 +08:00
|
|
|
options = dataSource
|
|
|
|
? dataSource.map(item => {
|
|
|
|
if (React.isValidElement(item)) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
switch (typeof item) {
|
|
|
|
case 'string':
|
|
|
|
return <Option key={item}>{item}</Option>;
|
|
|
|
case 'object':
|
|
|
|
return (
|
|
|
|
<Option key={(item as DataSourceItemObject).value}>
|
|
|
|
{(item as DataSourceItemObject).text}
|
|
|
|
</Option>
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
throw new Error(
|
|
|
|
'AutoComplete[dataSource] only supports type `string[] | Object[]`.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: [];
|
2017-01-13 21:19:23 +08:00
|
|
|
}
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
return (
|
2016-11-23 17:53:10 +08:00
|
|
|
<Select
|
|
|
|
{...this.props}
|
2016-07-25 17:46:45 +08:00
|
|
|
className={cls}
|
2018-06-25 11:58:00 +08:00
|
|
|
mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE}
|
2016-07-25 17:46:45 +08:00
|
|
|
optionLabelProp={optionLabelProp}
|
2017-01-13 21:19:23 +08:00
|
|
|
getInputElement={this.getInputElement}
|
2016-11-23 17:53:10 +08:00
|
|
|
notFoundContent={notFoundContent}
|
2017-11-19 01:41:40 +08:00
|
|
|
ref={this.saveSelect}
|
2016-11-23 17:53:10 +08:00
|
|
|
>
|
2016-07-25 17:46:45 +08:00
|
|
|
{options}
|
|
|
|
</Select>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderAutoComplete}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-07-25 17:46:45 +08:00
|
|
|
}
|