-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
136 lines (125 loc) · 3.47 KB
/
index.jsx
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
import { Option, OptGroup } from '../vc-select'
import Select, { AbstractSelectProps, SelectValue } from '../select'
import Input from '../input'
import InputElement from './InputElement'
import PropTypes from '../_util/vue-types'
import { getComponentFromProp, getOptionProps, filterEmpty, isValidElement } from '../_util/props-util'
// const DataSourceItemObject = PropTypes.shape({
// value: String,
// text: String,
// }).loose
// const DataSourceItemType = PropTypes.oneOfType([
// PropTypes.string,
// DataSourceItemObject,
// ]).isRequired
// export interface AutoCompleteInputProps {
// onChange?: React.FormEventHandler<any>;
// value: any;
// }
const AutoCompleteProps = {
...AbstractSelectProps(),
value: SelectValue,
defaultValue: SelectValue,
dataSource: PropTypes.array,
optionLabelProp: String,
dropdownMatchSelectWidth: PropTypes.bool,
// onChange?: (value: SelectValue) => void;
// onSelect?: (value: SelectValue, option: Object) => any;
}
export default {
name: 'AAutoComplete',
props: {
...AutoCompleteProps,
prefixCls: PropTypes.string.def('ant-select'),
showSearch: PropTypes.bool.def(false),
transitionName: PropTypes.string.def('slide-up'),
choiceTransitionName: PropTypes.string.def('zoom'),
optionLabelProp: PropTypes.string.def('children'),
filterOption: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.func,
]).def(false),
defaultActiveFirstOption: PropTypes.bool.def(true),
},
Option: { ...Option, name: 'AAutoCompleteOption' },
OptGroup: { ...OptGroup, name: 'AAutoCompleteOptGroup' },
model: {
prop: 'value',
event: 'change',
},
methods: {
getInputElement () {
const { $slots } = this
const children = filterEmpty($slots.default)
const element = children.length ? children[0] : <Input />
return (
<InputElement>{element}</InputElement>
)
},
focus () {
if (this.$refs.select) {
this.$refs.select.focus()
}
},
blur () {
if (this.$refs.select) {
this.$refs.select.blur()
}
},
},
render () {
const {
size, prefixCls, optionLabelProp, dataSource, $slots, $listeners,
} = this
const cls = {
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-show-search`]: true,
[`${prefixCls}-auto-complete`]: true,
}
let options
const childArray = filterEmpty($slots.dataSource)
if (childArray.length) {
options = childArray
} else {
options = dataSource ? dataSource.map((item) => {
if (isValidElement(item)) {
return item
}
switch (typeof item) {
case 'string':
return <Option key={item}>{item}</Option>
case 'object':
return (
<Option key={item.value}>
{item.text}
</Option>
)
default:
throw new Error('AutoComplete[dataSource] only supports type `string[] | Object[]`.')
}
}) : []
}
const selectProps = {
props: {
...getOptionProps(this),
mode: 'combobox',
optionLabelProp,
getInputElement: this.getInputElement,
notFoundContent: getComponentFromProp(this, 'notFoundContent'),
},
class: cls,
ref: 'select',
on: {
...$listeners,
},
}
return (
<Select
{...selectProps}
>
{options}
</Select>
)
},
}