-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
/
index.tsx
executable file
·137 lines (118 loc) · 4.11 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* TODO: 4.0
* - remove `dataSource`
* - `size` not work with customizeInput
* - customizeInput not feedback `ENTER` key since accessibility enhancement
*/
import * as React from 'react';
import toArray from 'rc-util/lib/Children/toArray';
import classNames from 'classnames';
import omit from 'omit.js';
import RcSelect from 'rc-select';
import Select, { InternalSelectProps, OptionType } from '../select';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import devWarning from '../_util/devWarning';
import { isValidElement } from '../_util/reactNode';
const { Option } = Select;
export interface DataSourceItemObject {
value: string;
text: string;
}
export type DataSourceItemType = string | DataSourceItemObject;
export interface AutoCompleteProps
extends Omit<
InternalSelectProps<string>,
'inputIcon' | 'loading' | 'mode' | 'optionLabelProp' | 'labelInValue'
> {
dataSource?: DataSourceItemType[];
}
function isSelectOptionOrSelectOptGroup(child: any): Boolean {
return child && child.type && (child.type.isSelectOption || child.type.isSelectOptGroup);
}
const AutoComplete: React.ForwardRefRenderFunction<RcSelect<any>, AutoCompleteProps> = (
props,
ref,
) => {
const { prefixCls: customizePrefixCls, className, children, dataSource } = props;
const childNodes: React.ReactElement[] = toArray(children);
// ============================= Input =============================
let customizeInput: React.ReactElement | undefined;
if (
childNodes.length === 1 &&
isValidElement(childNodes[0]) &&
!isSelectOptionOrSelectOptGroup(childNodes[0])
) {
[customizeInput] = childNodes;
}
const getInputElement = customizeInput ? (): React.ReactElement => customizeInput! : undefined;
// ============================ 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 => {
if (isValidElement(item)) {
return item;
}
switch (typeof item) {
case 'string':
return (
<Option key={item} value={item}>
{item}
</Option>
);
case 'object': {
const { value: optionValue } = item as DataSourceItemObject;
return (
<Option key={optionValue} value={optionValue}>
{(item as DataSourceItemObject).text}
</Option>
);
}
default:
throw new Error('AutoComplete[dataSource] only supports type `string[] | Object[]`.');
}
})
: [];
}
// ============================ Warning ============================
React.useEffect(() => {
devWarning(
!('dataSource' in props),
'AutoComplete',
'`dataSource` is deprecated, please use `options` instead.',
);
devWarning(
!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 (
<Select
ref={ref}
{...omit(props, ['dataSource'])}
prefixCls={prefixCls}
className={classNames(`${prefixCls}-auto-complete`, className)}
mode={Select.SECRET_COMBOBOX_MODE_DO_NOT_USE as any}
getInputElement={getInputElement}
>
{optionChildren}
</Select>
);
}}
</ConfigConsumer>
);
};
const RefAutoComplete = React.forwardRef<unknown, AutoCompleteProps>(AutoComplete);
type RefAutoCompleteWithOption = typeof RefAutoComplete & {
Option: OptionType;
};
(RefAutoComplete as RefAutoCompleteWithOption).Option = Option;
export default RefAutoComplete as RefAutoCompleteWithOption;