fix: [el-date-picker] focus input box when focus method is called (#4343)

fix #4327
This commit is contained in:
msidolphin 2021-12-11 06:56:40 +08:00 committed by GitHub
parent 21d41aed87
commit 4f6cac4252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 6 deletions

View File

@ -217,9 +217,9 @@ Note, date time locale (month name, first day of the week ...) are also configur
## Methods
| Method | Description | Parameters |
| ------ | ------------------------- | ---------- |
| focus | focus the Input component | |
| Method | Description | Parameters |
| ------ | ------------------------- | --------------- |
| focus | focus the Input component | focusStartInput |
## Slots

View File

@ -58,8 +58,8 @@ export default defineComponent({
const commonPicker = ref(null)
const refProps = {
...props,
focus: () => {
commonPicker.value?.handleFocus()
focus: (focusStartInput = true) => {
commonPicker.value?.focus(focusStartInput)
},
}
ctx.expose(refProps)

View File

@ -270,7 +270,7 @@ export default defineComponent({
ctx.emit('update:modelValue', val ? formatValue : val, lang.value)
}
}
const refInput = computed(() => {
const refInput = computed<HTMLInputElement[]>(() => {
if (refPopper.value.triggerRef) {
const _r = isRangeInput.value
? refPopper.value.triggerRef
@ -279,6 +279,12 @@ export default defineComponent({
}
return []
})
const refStartInput = computed(() => {
return refInput?.value[0]
})
const refEndInput = computed(() => {
return refInput?.value[1]
})
const setSelectionRange = (start, end, pos) => {
const _inputs = refInput.value
if (!_inputs.length) return
@ -302,6 +308,17 @@ export default defineComponent({
userInput.value = null
emitInput(result)
}
const focus = (focusStartInput = true) => {
let input = refStartInput.value
if (!focusStartInput && isRangeInput.value) {
input = refEndInput.value
}
if (input) {
input.focus()
}
}
const handleFocus = (e) => {
if (props.readonly || pickerDisabled.value || pickerVisible.value) return
pickerVisible.value = true
@ -610,6 +627,7 @@ export default defineComponent({
pickerDisabled,
onSetPickerOption,
onCalendarChange,
focus,
}
},
})