2019-09-12 20:15:17 +08:00
|
|
|
/**
|
|
|
|
* TODO: 4.0
|
|
|
|
* - remove `dataSource`
|
|
|
|
* - `size` not work with customizeInput
|
|
|
|
* - customizeInput not feedback `ENTER` key since accessibility enhancement
|
|
|
|
*/
|
|
|
|
|
2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2019-09-12 20:15:17 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
|
|
import { SelectProps as RcSelectProps } from 'rc-select';
|
2016-07-25 17:46:45 +08:00
|
|
|
import classNames from 'classnames';
|
2019-09-12 20:15:17 +08:00
|
|
|
import omit from 'omit.js';
|
|
|
|
import Select, { InternalSelectProps, OptionType } from '../select';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { isValidElement } from '../_util/reactNode';
|
2019-09-12 20:15:17 +08:00
|
|
|
|
|
|
|
const { Option } = Select;
|
|
|
|
|
|
|
|
const InternalSelect = Select as React.ComponentClass<RcSelectProps>;
|
2016-09-19 10:17:07 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
export interface DataSourceItemObject {
|
|
|
|
value: string;
|
|
|
|
text: string;
|
|
|
|
}
|
2019-09-12 20:15:17 +08:00
|
|
|
export type DataSourceItemType = string | DataSourceItemObject;
|
2017-01-13 21:19:23 +08:00
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
export interface AutoCompleteProps
|
2020-05-14 15:57:04 +08:00
|
|
|
extends Omit<
|
|
|
|
InternalSelectProps<string>,
|
|
|
|
'inputIcon' | 'loading' | 'mode' | 'optionLabelProp' | 'labelInValue'
|
|
|
|
> {
|
2018-04-16 17:42:31 +08:00
|
|
|
dataSource?: DataSourceItemType[];
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:12:31 +08:00
|
|
|
const AutoComplete: React.ForwardRefRenderFunction<Select, AutoCompleteProps> = (props, ref) => {
|
2019-09-12 20:15:17 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, children, dataSource } = props;
|
|
|
|
const childNodes: React.ReactElement[] = toArray(children);
|
|
|
|
|
|
|
|
const selectRef = React.useRef<Select>();
|
|
|
|
|
|
|
|
React.useImperativeHandle<Select, Select>(ref, () => selectRef.current!);
|
|
|
|
|
|
|
|
// ============================= Input =============================
|
|
|
|
let customizeInput: React.ReactElement;
|
|
|
|
|
|
|
|
if (
|
|
|
|
childNodes.length === 1 &&
|
2020-05-14 20:54:49 +08:00
|
|
|
isValidElement(childNodes[0]) &&
|
2019-09-12 20:15:17 +08:00
|
|
|
!isSelectOptionOrSelectOptGroup(childNodes[0])
|
|
|
|
) {
|
2020-06-08 18:01:50 +08:00
|
|
|
[customizeInput] = childNodes;
|
2017-11-19 01:41:40 +08:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
const getInputElement = (): React.ReactElement => customizeInput;
|
|
|
|
|
|
|
|
// ============================ Options ============================
|
|
|
|
let optionChildren: React.ReactNode;
|
|
|
|
|
|
|
|
// [Legacy] convert `children` or `dataSource` into option children
|
|
|
|
if (childNodes.length && isSelectOptionOrSelectOptGroup(childNodes[0])) {
|
|
|
|
optionChildren = children;
|
|
|
|
} else {
|
|
|
|
optionChildren = dataSource
|
|
|
|
? dataSource.map(item => {
|
2020-05-14 20:54:49 +08:00
|
|
|
if (isValidElement(item)) {
|
2019-09-12 20:15:17 +08:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
switch (typeof item) {
|
|
|
|
case 'string':
|
|
|
|
return (
|
2019-11-06 11:18:11 +08:00
|
|
|
<Option key={item} value={item}>
|
|
|
|
{item}
|
|
|
|
</Option>
|
|
|
|
);
|
|
|
|
case 'object': {
|
|
|
|
const { value: optionValue } = item as DataSourceItemObject;
|
|
|
|
return (
|
|
|
|
<Option key={optionValue} value={optionValue}>
|
2019-09-12 20:15:17 +08:00
|
|
|
{(item as DataSourceItemObject).text}
|
|
|
|
</Option>
|
|
|
|
);
|
2019-11-06 11:18:11 +08:00
|
|
|
}
|
2019-09-12 20:15:17 +08:00
|
|
|
default:
|
|
|
|
throw new Error('AutoComplete[dataSource] only supports type `string[] | Object[]`.');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: [];
|
2017-11-19 01:41:40 +08:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:15:17 +08:00
|
|
|
// ============================ Warning ============================
|
|
|
|
React.useEffect(() => {
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2019-09-12 20:15:17 +08:00
|
|
|
!('dataSource' in props),
|
|
|
|
'AutoComplete',
|
|
|
|
'`dataSource` is deprecated, please use `options` instead.',
|
2016-07-25 17:46:45 +08:00
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2019-09-12 20:15:17 +08:00
|
|
|
!customizeInput || !('size' in props),
|
|
|
|
'AutoComplete',
|
|
|
|
'You need to control style self instead of setting `size` when using customize input.',
|
|
|
|
);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ConfigConsumer>
|
|
|
|
{({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<InternalSelect
|
|
|
|
ref={selectRef as any}
|
|
|
|
{...omit(props, ['dataSource'])}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={classNames(className, `${prefixCls}-auto-complete`)}
|
|
|
|
mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE as any}
|
|
|
|
getInputElement={getInputElement}
|
|
|
|
>
|
|
|
|
{optionChildren}
|
|
|
|
</InternalSelect>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const RefAutoComplete = React.forwardRef<Select, AutoCompleteProps>(AutoComplete);
|
|
|
|
|
|
|
|
type RefAutoComplete = typeof RefAutoComplete & {
|
|
|
|
Option: OptionType;
|
|
|
|
};
|
|
|
|
|
|
|
|
(RefAutoComplete as RefAutoComplete).Option = Option;
|
|
|
|
|
|
|
|
export default RefAutoComplete as RefAutoComplete;
|