2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
2017-01-13 21:19:23 +08:00
|
|
|
import { findDOMNode } from 'react-dom';
|
2017-01-12 09:23:56 +08:00
|
|
|
import Select, { AbstractSelectProps, OptionProps, OptGroupProps } from '../select';
|
2017-01-13 21:19:23 +08:00
|
|
|
import Input from '../input';
|
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';
|
|
|
|
|
2016-09-19 10:22:52 +08:00
|
|
|
export interface SelectedValue {
|
2016-09-19 10:17:07 +08:00
|
|
|
key: string;
|
|
|
|
label: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2016-09-22 14:40:34 +08:00
|
|
|
export interface DataSourceItemObject { value: string; text: string; };
|
|
|
|
export type DataSourceItemType = string | DataSourceItemObject;
|
|
|
|
|
2017-01-13 21:19:23 +08:00
|
|
|
export interface InputProps {
|
|
|
|
onChange?: React.FormEventHandler<any>;
|
|
|
|
value: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ValidInputElement =
|
|
|
|
HTMLInputElement |
|
|
|
|
HTMLTextAreaElement |
|
|
|
|
React.ReactElement<InputProps>;
|
|
|
|
|
2017-01-12 09:23:56 +08:00
|
|
|
export interface AutoCompleteProps extends AbstractSelectProps {
|
2016-08-22 17:26:14 +08:00
|
|
|
size?: 'large' | 'small' | 'default';
|
2016-07-25 17:46:45 +08:00
|
|
|
className?: string;
|
|
|
|
notFoundContent?: Element;
|
2016-09-22 14:40:34 +08:00
|
|
|
dataSource: DataSourceItemType[];
|
2016-09-19 10:17:07 +08:00
|
|
|
defaultValue?: string | Array<any> | SelectedValue | Array<SelectedValue>;
|
|
|
|
value?: string | Array<any> | SelectedValue | Array<SelectedValue>;
|
2016-10-18 11:55:00 +08:00
|
|
|
onChange?: (value: string | Array<any> | SelectedValue | Array<SelectedValue>) => void;
|
2017-01-12 09:23:56 +08:00
|
|
|
onSelect?: (value: string | Array<any> | SelectedValue | Array<SelectedValue>, option: Object) => any;
|
2016-09-19 10:17:07 +08:00
|
|
|
disabled?: boolean;
|
2017-01-13 21:19:23 +08:00
|
|
|
children: ValidInputElement |
|
|
|
|
React.ReactElement<OptionProps> |
|
|
|
|
Array<React.ReactElement<OptionProps>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
class InputElement extends React.Component<any, any> {
|
|
|
|
private ele: Element;
|
|
|
|
focus = () => {
|
|
|
|
(findDOMNode(this.ele) as HTMLInputElement).focus();
|
|
|
|
}
|
|
|
|
blur = () => {
|
|
|
|
(findDOMNode(this.ele) as HTMLInputElement).blur();
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return React.cloneElement(this.props.children, {
|
|
|
|
...this.props,
|
|
|
|
ref: ele => this.ele = ele,
|
|
|
|
}, null);
|
|
|
|
}
|
2016-07-25 17:46:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class AutoComplete extends React.Component<AutoCompleteProps, any> {
|
2016-10-18 11:55:00 +08:00
|
|
|
static Option = Option as React.ClassicComponentClass<OptionProps>;
|
|
|
|
static OptGroup = OptGroup as React.ClassicComponentClass<OptGroupProps>;
|
2016-07-25 17:46:45 +08:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-select',
|
|
|
|
transitionName: 'slide-up',
|
|
|
|
optionLabelProp: 'children',
|
|
|
|
choiceTransitionName: 'zoom',
|
|
|
|
showSearch: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
antLocale: React.PropTypes.object,
|
|
|
|
};
|
|
|
|
|
2017-01-13 21:19:23 +08:00
|
|
|
getInputElement = () => {
|
|
|
|
const { children } = this.props;
|
|
|
|
const element = children && React.isValidElement(children) && children.type !== Option ?
|
|
|
|
React.Children.only(this.props.children) :
|
|
|
|
<Input/>;
|
|
|
|
return <InputElement className="ant-input">{element}</InputElement>;
|
|
|
|
}
|
2017-01-20 18:28:09 +08:00
|
|
|
|
2016-07-25 17:46:45 +08:00
|
|
|
render() {
|
|
|
|
let {
|
2016-10-21 18:02:37 +08:00
|
|
|
size, className = '', notFoundContent, prefixCls, optionLabelProp, dataSource, children,
|
2016-07-25 17:46:45 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (childArray.length && (childArray[0] as React.ReactElement<any>).type === Option) {
|
2017-02-09 10:45:42 +08:00
|
|
|
options = children;
|
2017-01-13 21:19:23 +08:00
|
|
|
} else {
|
|
|
|
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[]`.');
|
|
|
|
}
|
|
|
|
}) : [];
|
|
|
|
}
|
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}
|
|
|
|
optionLabelProp={optionLabelProp}
|
|
|
|
combobox
|
2017-01-13 21:19:23 +08:00
|
|
|
getInputElement={this.getInputElement}
|
2016-11-23 17:53:10 +08:00
|
|
|
notFoundContent={notFoundContent}
|
|
|
|
>
|
2016-07-25 17:46:45 +08:00
|
|
|
{options}
|
|
|
|
</Select>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|