2019-09-12 20:15:17 +08:00
|
|
|
/**
|
|
|
|
* TODO: 4.0
|
2021-01-13 21:00:30 +08:00
|
|
|
*
|
|
|
|
* - Remove `dataSource`
|
|
|
|
* - `size` not work with customizeInput
|
|
|
|
* - CustomizeInput not feedback `ENTER` key since accessibility enhancement
|
2019-09-12 20:15:17 +08:00
|
|
|
*/
|
|
|
|
|
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';
|
2016-07-25 17:46:45 +08:00
|
|
|
import classNames from 'classnames';
|
2021-01-13 21:00:30 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2021-12-21 21:38:11 +08:00
|
|
|
import type { BaseSelectRef } from 'rc-select';
|
|
|
|
import Select, {
|
|
|
|
BaseOptionType,
|
|
|
|
DefaultOptionType,
|
|
|
|
InternalSelectProps,
|
|
|
|
RefSelectProps,
|
|
|
|
} 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';
|
2022-02-17 16:41:24 +08:00
|
|
|
import { InputStatus } from '../_util/statusUtils';
|
2019-09-12 20:15:17 +08:00
|
|
|
|
|
|
|
const { Option } = Select;
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
export interface DataSourceItemObject {
|
|
|
|
value: string;
|
|
|
|
text: string;
|
|
|
|
}
|
2020-11-20 10:11:58 +08:00
|
|
|
export type DataSourceItemType = DataSourceItemObject | React.ReactNode;
|
2017-01-13 21:19:23 +08:00
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
export interface AutoCompleteProps<
|
|
|
|
ValueType = any,
|
|
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
|
|
> extends Omit<
|
|
|
|
InternalSelectProps<ValueType, OptionType>,
|
2020-05-14 15:57:04 +08:00
|
|
|
'inputIcon' | 'loading' | 'mode' | 'optionLabelProp' | 'labelInValue'
|
|
|
|
> {
|
2018-04-16 17:42:31 +08:00
|
|
|
dataSource?: DataSourceItemType[];
|
2022-02-17 16:41:24 +08:00
|
|
|
status?: InputStatus;
|
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-11-03 11:09:24 +08:00
|
|
|
const AutoComplete: React.ForwardRefRenderFunction<RefSelectProps, AutoCompleteProps> = (
|
2020-10-24 20:33:18 +08:00
|
|
|
props,
|
|
|
|
ref,
|
|
|
|
) => {
|
2019-09-12 20:15:17 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, children, dataSource } = props;
|
|
|
|
const childNodes: React.ReactElement[] = toArray(children);
|
|
|
|
|
|
|
|
// ============================= Input =============================
|
2020-09-16 11:51:55 +08:00
|
|
|
let customizeInput: React.ReactElement | undefined;
|
2019-09-12 20:15:17 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-16 11:51:55 +08:00
|
|
|
const getInputElement = customizeInput ? (): React.ReactElement => customizeInput! : undefined;
|
2019-09-12 20:15:17 +08:00
|
|
|
|
|
|
|
// ============================ 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 (
|
2020-10-24 20:33:18 +08:00
|
|
|
<Select
|
|
|
|
ref={ref}
|
2019-09-12 20:15:17 +08:00
|
|
|
{...omit(props, ['dataSource'])}
|
|
|
|
prefixCls={prefixCls}
|
2020-09-06 13:07:39 +08:00
|
|
|
className={classNames(`${prefixCls}-auto-complete`, className)}
|
2019-09-12 20:15:17 +08:00
|
|
|
mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE as any}
|
2021-01-13 21:00:30 +08:00
|
|
|
{...{
|
|
|
|
// Internal api
|
|
|
|
getInputElement,
|
|
|
|
}}
|
2019-09-12 20:15:17 +08:00
|
|
|
>
|
|
|
|
{optionChildren}
|
2020-10-24 20:33:18 +08:00
|
|
|
</Select>
|
2019-09-12 20:15:17 +08:00
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
const RefAutoComplete = React.forwardRef<RefSelectProps, AutoCompleteProps>(
|
|
|
|
AutoComplete,
|
|
|
|
) as unknown as (<
|
|
|
|
ValueType = any,
|
|
|
|
OptionType extends BaseOptionType | DefaultOptionType = DefaultOptionType,
|
|
|
|
>(
|
|
|
|
props: React.PropsWithChildren<AutoCompleteProps<ValueType, OptionType>> & {
|
|
|
|
ref?: React.Ref<BaseSelectRef>;
|
|
|
|
},
|
|
|
|
) => React.ReactElement) & {
|
|
|
|
Option: typeof Option;
|
2019-09-12 20:15:17 +08:00
|
|
|
};
|
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
RefAutoComplete.Option = Option;
|
2019-09-12 20:15:17 +08:00
|
|
|
|
2021-12-21 21:38:11 +08:00
|
|
|
export default RefAutoComplete;
|