Skip to content

Commit be69bd9

Browse files
committed
fix: cascader autoFocus not work
1 parent 62b698c commit be69bd9

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

components/cascader/index.jsx

+31-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const CascaderProps = {
5353
disabled: PropTypes.bool.def(false),
5454
/** 是否支持清除*/
5555
allowClear: PropTypes.bool.def(true),
56-
showSearch: PropTypes.oneOfType([PropTypes.bool, ShowSearchType]),
56+
showSearch: PropTypes.oneOfType([Boolean, ShowSearchType]),
5757
notFoundContent: PropTypes.any.def('Not Found'),
5858
loadData: PropTypes.func,
5959
/** 次级菜单的展开方式,可选 'click' 和 'hover' */
@@ -66,6 +66,7 @@ const CascaderProps = {
6666
inputPrefixCls: PropTypes.string.def('ant-input'),
6767
getPopupContainer: PropTypes.func,
6868
popupVisible: PropTypes.bool,
69+
autoFocus: PropTypes.bool,
6970
}
7071

7172
function defaultFilterOption (inputValue, path) {
@@ -102,6 +103,13 @@ export default {
102103
flattenOptions: showSearch && flattenTree(options, changeOnSelect),
103104
}
104105
},
106+
mounted () {
107+
this.$nextTick(() => {
108+
if (this.autoFocus && !this.showSearch && !this.disabled) {
109+
this.$refs.picker.focus()
110+
}
111+
})
112+
},
105113
watch: {
106114
value (val) {
107115
this.setState({ sValue: val || [] })
@@ -119,7 +127,7 @@ export default {
119127
highlightKeyword (str, keyword, prefixCls) {
120128
return str.split(keyword)
121129
.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>,
123131
node,
124132
])
125133
},
@@ -152,19 +160,23 @@ export default {
152160
}
153161
this.$emit('popupVisibleChange', popupVisible)
154162
},
163+
handleInputFocus (e) {
164+
this.$emit('focus', e)
165+
},
155166

156-
handleInputBlur () {
167+
handleInputBlur (e) {
157168
this.setState({
158169
inputFocused: false,
159170
})
171+
this.$emit('blur', e)
160172
},
161173

162174
handleInputClick (e) {
163175
const { inputFocused, sPopupVisible } = this
164176
// Prevent `Trigger` behaviour.
165177
if (inputFocused || sPopupVisible) {
166178
e.stopPropagation()
167-
e.nativeEvent.stopImmediatePropagation()
179+
e.nativeEvent && e.nativeEvent.stopImmediatePropagation()
168180
}
169181
},
170182

@@ -249,16 +261,24 @@ export default {
249261
},
250262

251263
focus () {
252-
this.$refs.input.focus()
264+
if (this.showSearch) {
265+
this.$refs.input.focus()
266+
} else {
267+
this.$refs.picker.focus()
268+
}
253269
},
254270

255271
blur () {
256-
this.$refs.input.blur()
272+
if (this.showSearch) {
273+
this.$refs.input.blur()
274+
} else {
275+
this.$refs.picker.blur()
276+
}
257277
},
258278
},
259279

260280
render () {
261-
const { $slots, sValue: value, sPopupVisible, inputValue } = this
281+
const { $slots, sValue: value, sPopupVisible, inputValue, $listeners } = this
262282
const props = getOptionProps(this)
263283
const {
264284
prefixCls, inputPrefixCls, placeholder, size, disabled,
@@ -329,6 +349,7 @@ export default {
329349
if (resultListMatchInputWidth && inputValue && this.input) {
330350
dropdownMenuColumnStyle.width = this.input.input.offsetWidth
331351
}
352+
// showSearch时,focus、blur在input上触发,反之在ref='picker'上触发
332353
const inputProps = {
333354
props: {
334355
...tempInputProps,
@@ -342,6 +363,7 @@ export default {
342363
class: `${prefixCls}-input ${sizeCls}`,
343364
ref: 'input',
344365
on: {
366+
focus: showSearch ? this.handleInputFocus : noop,
345367
click: showSearch ? this.handleInputClick : noop,
346368
blur: showSearch ? this.handleInputBlur : noop,
347369
keydown: this.handleKeyDown,
@@ -354,6 +376,7 @@ export default {
354376
<span
355377
class={pickerCls}
356378
style={getStyle(this)}
379+
ref='picker'
357380
>
358381
{ showSearch ? <span class={`${prefixCls}-picker-label`}>
359382
{this.getLabel()}
@@ -375,6 +398,7 @@ export default {
375398
dropdownMenuColumnStyle: dropdownMenuColumnStyle,
376399
},
377400
on: {
401+
...$listeners,
378402
popupVisibleChange: this.handlePopupVisibleChange,
379403
change: this.handleChange,
380404
},

components/input/Input.jsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ export default {
2727
stateValue: fixControlledValue(!hasProp(this, 'value') ? defaultValue : value),
2828
}
2929
},
30-
computed: {
30+
mounted () {
31+
this.$nextTick(() => {
32+
if (this.autoFocus) {
33+
this.focus()
34+
}
35+
})
3136
},
3237
watch: {
3338
value (val) {

components/trigger/Trigger.jsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,11 @@ export default {
524524
newChildProps.on.blur = this.onBlur
525525
} else {
526526
newChildProps.on.focus = this.createTwoChains('focus')
527-
newChildProps.on.blur = this.createTwoChains('blur')
527+
newChildProps.on.blur = (e) => {
528+
if (!e.relatedTarget || !contains(e.target, e.relatedTarget)) {
529+
this.createTwoChains('blur')(e)
530+
}
531+
}
528532
}
529533
const { sPopupVisible, forceRender } = this
530534
const trigger = cloneElement(child, newChildProps)

components/vc-cascader/Cascader.jsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export default {
257257
render () {
258258
const {
259259
$props, $slots, sValue, sActiveValue, handleMenuSelect,
260-
sPopupVisible, handlePopupVisibleChange, handleKeyDown,
260+
sPopupVisible, handlePopupVisibleChange, handleKeyDown, $listeners,
261261
} = this
262262
const {
263263
prefixCls, transitionName, popupClassName, options, disabled,
@@ -274,6 +274,7 @@ export default {
274274
visible: sPopupVisible,
275275
},
276276
on: {
277+
...$listeners,
277278
select: handleMenuSelect,
278279
},
279280
}
@@ -298,6 +299,7 @@ export default {
298299
popupClassName: popupClassName + emptyMenuClassName,
299300
},
300301
on: {
302+
...$listeners,
301303
popupVisibleChange: handlePopupVisibleChange,
302304
},
303305
ref: 'trigger',

0 commit comments

Comments
 (0)