@@ -53,7 +53,7 @@ const CascaderProps = {
53
53
disabled : PropTypes . bool . def ( false ) ,
54
54
/** 是否支持清除*/
55
55
allowClear : PropTypes . bool . def ( true ) ,
56
- showSearch : PropTypes . oneOfType ( [ PropTypes . bool , ShowSearchType ] ) ,
56
+ showSearch : PropTypes . oneOfType ( [ Boolean , ShowSearchType ] ) ,
57
57
notFoundContent : PropTypes . any . def ( 'Not Found' ) ,
58
58
loadData : PropTypes . func ,
59
59
/** 次级菜单的展开方式,可选 'click' 和 'hover' */
@@ -66,6 +66,7 @@ const CascaderProps = {
66
66
inputPrefixCls : PropTypes . string . def ( 'ant-input' ) ,
67
67
getPopupContainer : PropTypes . func ,
68
68
popupVisible : PropTypes . bool ,
69
+ autoFocus : PropTypes . bool ,
69
70
}
70
71
71
72
function defaultFilterOption ( inputValue , path ) {
@@ -102,6 +103,13 @@ export default {
102
103
flattenOptions : showSearch && flattenTree ( options , changeOnSelect ) ,
103
104
}
104
105
} ,
106
+ mounted ( ) {
107
+ this . $nextTick ( ( ) => {
108
+ if ( this . autoFocus && ! this . showSearch && ! this . disabled ) {
109
+ this . $refs . picker . focus ( )
110
+ }
111
+ } )
112
+ } ,
105
113
watch : {
106
114
value ( val ) {
107
115
this . setState ( { sValue : val || [ ] } )
@@ -119,7 +127,7 @@ export default {
119
127
highlightKeyword ( str , keyword , prefixCls ) {
120
128
return str . split ( keyword )
121
129
. map ( ( node , index ) => index === 0 ? node : [
122
- < span class = { `${ prefixCls } -menu-item-keyword` } key = 'seperator' > { keyword } </ span > ,
130
+ < span class = { `${ prefixCls } -menu-item-keyword` } > { keyword } </ span > ,
123
131
node ,
124
132
] )
125
133
} ,
@@ -152,19 +160,23 @@ export default {
152
160
}
153
161
this . $emit ( 'popupVisibleChange' , popupVisible )
154
162
} ,
163
+ handleInputFocus ( e ) {
164
+ this . $emit ( 'focus' , e )
165
+ } ,
155
166
156
- handleInputBlur ( ) {
167
+ handleInputBlur ( e ) {
157
168
this . setState ( {
158
169
inputFocused : false ,
159
170
} )
171
+ this . $emit ( 'blur' , e )
160
172
} ,
161
173
162
174
handleInputClick ( e ) {
163
175
const { inputFocused, sPopupVisible } = this
164
176
// Prevent `Trigger` behaviour.
165
177
if ( inputFocused || sPopupVisible ) {
166
178
e . stopPropagation ( )
167
- e . nativeEvent . stopImmediatePropagation ( )
179
+ e . nativeEvent && e . nativeEvent . stopImmediatePropagation ( )
168
180
}
169
181
} ,
170
182
@@ -249,16 +261,24 @@ export default {
249
261
} ,
250
262
251
263
focus ( ) {
252
- this . $refs . input . focus ( )
264
+ if ( this . showSearch ) {
265
+ this . $refs . input . focus ( )
266
+ } else {
267
+ this . $refs . picker . focus ( )
268
+ }
253
269
} ,
254
270
255
271
blur ( ) {
256
- this . $refs . input . blur ( )
272
+ if ( this . showSearch ) {
273
+ this . $refs . input . blur ( )
274
+ } else {
275
+ this . $refs . picker . blur ( )
276
+ }
257
277
} ,
258
278
} ,
259
279
260
280
render ( ) {
261
- const { $slots, sValue : value , sPopupVisible, inputValue } = this
281
+ const { $slots, sValue : value , sPopupVisible, inputValue, $listeners } = this
262
282
const props = getOptionProps ( this )
263
283
const {
264
284
prefixCls, inputPrefixCls, placeholder, size, disabled,
@@ -329,6 +349,7 @@ export default {
329
349
if ( resultListMatchInputWidth && inputValue && this . input ) {
330
350
dropdownMenuColumnStyle . width = this . input . input . offsetWidth
331
351
}
352
+ // showSearch时,focus、blur在input上触发,反之在ref='picker'上触发
332
353
const inputProps = {
333
354
props : {
334
355
...tempInputProps ,
@@ -342,6 +363,7 @@ export default {
342
363
class : `${ prefixCls } -input ${ sizeCls } ` ,
343
364
ref : 'input' ,
344
365
on : {
366
+ focus : showSearch ? this . handleInputFocus : noop ,
345
367
click : showSearch ? this . handleInputClick : noop ,
346
368
blur : showSearch ? this . handleInputBlur : noop ,
347
369
keydown : this . handleKeyDown ,
@@ -354,6 +376,7 @@ export default {
354
376
< span
355
377
class = { pickerCls }
356
378
style = { getStyle ( this ) }
379
+ ref = 'picker'
357
380
>
358
381
{ showSearch ? < span class = { `${ prefixCls } -picker-label` } >
359
382
{ this . getLabel ( ) }
@@ -375,6 +398,7 @@ export default {
375
398
dropdownMenuColumnStyle : dropdownMenuColumnStyle ,
376
399
} ,
377
400
on : {
401
+ ...$listeners ,
378
402
popupVisibleChange : this . handlePopupVisibleChange ,
379
403
change : this . handleChange ,
380
404
} ,
0 commit comments