1
+ import { inject , provide } from 'vue' ;
1
2
import { Option , OptGroup } from '../vc-select' ;
2
3
import Select , { AbstractSelectProps , SelectValue } from '../select' ;
3
4
import Input from '../input' ;
4
5
import InputElement from './InputElement' ;
5
6
import PropTypes from '../_util/vue-types' ;
6
7
import { ConfigConsumerProps } from '../config-provider' ;
7
- import {
8
- getComponentFromProp ,
9
- getOptionProps ,
10
- filterEmpty ,
11
- isValidElement ,
12
- getListeners ,
13
- } from '../_util/props-util' ;
14
- import Base from '../base' ;
8
+ import { getComponent , getOptionProps , isValidElement , getSlot } from '../_util/props-util' ;
15
9
16
10
// const DataSourceItemObject = PropTypes.shape({
17
11
// value: String,
@@ -26,6 +20,9 @@ import Base from '../base';
26
20
// onChange?: React.FormEventHandler<any>;
27
21
// value: any;
28
22
// }
23
+ function isSelectOptionOrSelectOptGroup ( child ) {
24
+ return child && child . type && ( child . type . isSelectOption || child . type . isSelectOptGroup ) ;
25
+ }
29
26
30
27
const AutoCompleteProps = {
31
28
...AbstractSelectProps ( ) ,
@@ -41,6 +38,7 @@ const AutoCompleteProps = {
41
38
42
39
const AutoComplete = {
43
40
name : 'AAutoComplete' ,
41
+ inheritAttrs : false ,
44
42
props : {
45
43
...AutoCompleteProps ,
46
44
prefixCls : PropTypes . string . def ( 'ant-select' ) ,
@@ -55,59 +53,62 @@ const AutoComplete = {
55
53
} ,
56
54
Option : { ...Option , name : 'AAutoCompleteOption' } ,
57
55
OptGroup : { ...OptGroup , name : 'AAutoCompleteOptGroup' } ,
58
- model : {
59
- prop : 'value' ,
60
- event : 'change' ,
61
- } ,
62
- inject : {
63
- configProvider : { default : ( ) => ConfigConsumerProps } ,
64
- } ,
65
- provide ( ) {
56
+ // model: {
57
+ // prop: 'value',
58
+ // event: 'change',
59
+ // },
60
+ setup ( ) {
66
61
return {
67
- savePopupRef : this . savePopupRef ,
62
+ configProvider : inject ( 'configProvider' , ConfigConsumerProps ) ,
68
63
} ;
69
64
} ,
65
+ created ( ) {
66
+ provide ( 'savePopupRef' , this . savePopupRef ) ;
67
+ } ,
70
68
methods : {
71
69
savePopupRef ( ref ) {
72
70
this . popupRef = ref ;
73
71
} ,
74
-
72
+ saveSelect ( node ) {
73
+ this . select = node ;
74
+ } ,
75
75
getInputElement ( ) {
76
- const { $slots , placeholder } = this ;
77
- const children = filterEmpty ( $slots . default ) ;
76
+ const { placeholder } = this ;
77
+ const children = getSlot ( this ) ;
78
78
const element = children . length ? children [ 0 ] : < Input lazy = { false } /> ;
79
79
return < InputElement placeholder = { placeholder } > { element } </ InputElement > ;
80
80
} ,
81
81
82
82
focus ( ) {
83
- if ( this . $refs . select ) {
84
- this . $refs . select . focus ( ) ;
83
+ if ( this . select ) {
84
+ this . select . focus ( ) ;
85
85
}
86
86
} ,
87
87
88
88
blur ( ) {
89
- if ( this . $refs . select ) {
90
- this . $refs . select . blur ( ) ;
89
+ if ( this . select ) {
90
+ this . select . blur ( ) ;
91
91
}
92
92
} ,
93
93
} ,
94
94
95
95
render ( ) {
96
- const { size, prefixCls : customizePrefixCls , optionLabelProp, dataSource, $slots } = this ;
96
+ const { size, prefixCls : customizePrefixCls , optionLabelProp, dataSource } = this ;
97
97
98
98
const getPrefixCls = this . configProvider . getPrefixCls ;
99
99
const prefixCls = getPrefixCls ( 'select' , customizePrefixCls ) ;
100
-
100
+ const { class : className } = this . $attrs ;
101
101
const cls = {
102
+ [ className ] : ! ! className ,
102
103
[ `${ prefixCls } -lg` ] : size === 'large' ,
103
104
[ `${ prefixCls } -sm` ] : size === 'small' ,
104
105
[ `${ prefixCls } -show-search` ] : true ,
105
106
[ `${ prefixCls } -auto-complete` ] : true ,
106
107
} ;
107
108
108
109
let options ;
109
- const childArray = filterEmpty ( $slots . dataSource ) ;
110
- if ( childArray . length ) {
110
+ const childArray = getSlot ( this , ' dataSource' ) ;
111
+ if ( childArray . length && isSelectOptionOrSelectOptGroup ( childArray [ 0 ] ) ) {
111
112
options = childArray ;
112
113
} else {
113
114
options = dataSource
@@ -129,28 +130,25 @@ const AutoComplete = {
129
130
: [ ] ;
130
131
}
131
132
const selectProps = {
132
- props : {
133
- ...getOptionProps ( this ) ,
134
- mode : Select . SECRET_COMBOBOX_MODE_DO_NOT_USE ,
135
- optionLabelProp,
136
- getInputElement : this . getInputElement ,
137
- notFoundContent : getComponentFromProp ( this , 'notFoundContent' ) ,
138
- placeholder : '' ,
139
- } ,
133
+ ...getOptionProps ( this ) ,
134
+ ...this . $attrs ,
135
+ mode : Select . SECRET_COMBOBOX_MODE_DO_NOT_USE ,
136
+ optionLabelProp,
137
+ getInputElement : this . getInputElement ,
138
+ notFoundContent : getComponent ( this , 'notFoundContent' ) ,
139
+ placeholder : '' ,
140
140
class : cls ,
141
- ref : 'select' ,
142
- on : getListeners ( this ) ,
141
+ ref : this . saveSelect ,
143
142
} ;
144
143
return < Select { ...selectProps } > { options } </ Select > ;
145
144
} ,
146
145
} ;
147
146
148
147
/* istanbul ignore next */
149
- AutoComplete . install = function ( Vue ) {
150
- Vue . use ( Base ) ;
151
- Vue . component ( AutoComplete . name , AutoComplete ) ;
152
- Vue . component ( AutoComplete . Option . name , AutoComplete . Option ) ;
153
- Vue . component ( AutoComplete . OptGroup . name , AutoComplete . OptGroup ) ;
148
+ AutoComplete . install = function ( app ) {
149
+ app . component ( AutoComplete . name , AutoComplete ) ;
150
+ app . component ( AutoComplete . Option . name , AutoComplete . Option ) ;
151
+ app . component ( AutoComplete . OptGroup . name , AutoComplete . OptGroup ) ;
154
152
} ;
155
153
156
154
export default AutoComplete ;
0 commit comments