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
|
|
|
*/
|
|
|
|
|
2016-07-25 17:46:45 +08:00
|
|
|
import classNames from 'classnames';
|
2021-12-21 21:38:11 +08:00
|
|
|
import type { BaseSelectRef } from 'rc-select';
|
2022-06-22 14:57:09 +08:00
|
|
|
import toArray from 'rc-util/lib/Children/toArray';
|
|
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
import * as React from 'react';
|
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigConsumer } from '../config-provider';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type {
|
2021-12-21 21:38:11 +08:00
|
|
|
BaseOptionType,
|
|
|
|
DefaultOptionType,
|
|
|
|
InternalSelectProps,
|
|
|
|
RefSelectProps,
|
|
|
|
} from '../select';
|
2022-05-07 14:31:54 +08:00
|
|
|
import Select from '../select';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { isValidElement } from '../_util/reactNode';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import warning from '../_util/warning';
|
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;
|
2022-08-05 11:21:07 +08:00
|
|
|
/**
|
|
|
|
* @deprecated `dropdownClassName` is deprecated which will be removed in next major version.
|
|
|
|
* Please use `popupClassName` instead.
|
|
|
|
*/
|
|
|
|
dropdownClassName?: string;
|
|
|
|
popupClassName?: string;
|
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,
|
|
|
|
) => {
|
2022-08-05 11:21:07 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
popupClassName,
|
|
|
|
dropdownClassName,
|
|
|
|
children,
|
|
|
|
dataSource,
|
|
|
|
} = props;
|
2019-09-12 20:15:17 +08:00
|
|
|
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:
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
|
|
|
false,
|
|
|
|
'AutoComplete',
|
|
|
|
'`dataSource` is only supports type `string[] | Object[]`.',
|
|
|
|
);
|
|
|
|
return undefined;
|
2019-09-12 20:15:17 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
: [];
|
2017-11-19 01:41:40 +08:00
|
|
|
}
|
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
|
|
|
!('dataSource' in props),
|
|
|
|
'AutoComplete',
|
|
|
|
'`dataSource` is deprecated, please use `options` instead.',
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
|
2022-08-05 11:21:07 +08:00
|
|
|
warning(
|
|
|
|
!dropdownClassName,
|
|
|
|
'AutoComplete',
|
|
|
|
'`dropdownClassName` is deprecated which will be removed in next major version. Please use `popupClassName` instead.',
|
|
|
|
);
|
|
|
|
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
|
|
|
!customizeInput || !('size' in props),
|
|
|
|
'AutoComplete',
|
|
|
|
'You need to control style self instead of setting `size` when using customize input.',
|
|
|
|
);
|
2019-09-12 20:15:17 +08:00
|
|
|
|
|
|
|
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}
|
2022-10-12 18:02:04 +08:00
|
|
|
popupClassName={popupClassName || dropdownClassName}
|
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;
|