fix(select-v2): adjust the trigger conditions of focus and blur (#2650)

This commit is contained in:
msidolphin 2021-07-24 23:51:53 +08:00 committed by GitHub
parent 48fd68bdf1
commit 80a44d87a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 14 deletions

View File

@ -26,6 +26,7 @@ interface SelectProps {
disabled?: boolean disabled?: boolean
clearable?: boolean clearable?: boolean
multiple?: boolean multiple?: boolean
filterable?: boolean
multipleLimit?: number multipleLimit?: number
[key: string]: any [key: string]: any
} }
@ -34,6 +35,8 @@ interface SelectEvents {
onChange?: (value?: string) => void onChange?: (value?: string) => void
onVisibleChange?: (visible?: boolean) => void onVisibleChange?: (visible?: boolean) => void
onRemoveTag?: (tag?: string) => void onRemoveTag?: (tag?: string) => void
onFocus?: (event?: FocusEvent) => void
onBlur?: (event?) => void
[key: string]: (...args) => any [key: string]: (...args) => any
} }
@ -48,10 +51,13 @@ const createSelect = (options: {
:disabled="disabled" :disabled="disabled"
:clearable="clearable" :clearable="clearable"
:multiple="multiple" :multiple="multiple"
:filterable="filterable"
:multiple-limit="multipleLimit" :multiple-limit="multipleLimit"
@change="onChange" @change="onChange"
@visible-change="onVisibleChange" @visible-change="onVisibleChange"
@remove-tah="onRemoveTag" @remove-tah="onRemoveTag"
@focus="onFocus"
@blur="onBlur"
v-model="value"></el-select> v-model="value"></el-select>
`, { `, {
data () { data () {
@ -62,6 +68,7 @@ const createSelect = (options: {
disabled: false, disabled: false,
clearable: false, clearable: false,
multiple: false, multiple: false,
filterable: false,
multipleLimit: 0, multipleLimit: 0,
...options.data && options.data(), ...options.data && options.data(),
} }
@ -70,6 +77,8 @@ const createSelect = (options: {
onChange: NOOP, onChange: NOOP,
onVisibleChange: NOOP, onVisibleChange: NOOP,
onRemoveTag: NOOP, onRemoveTag: NOOP,
onFocus: NOOP,
onBlur: NOOP,
...options.methods, ...options.methods,
}, },
}) })
@ -381,4 +390,69 @@ describe('Select', () => {
expect(vm.value.length).toBe(2) expect(vm.value.length).toBe(2)
}) })
}) })
describe('event', () => {
it('focus & blur', async () => {
const onFocus = jest.fn()
const onBlur = jest.fn()
const wrapper = createSelect({
methods: {
onFocus,
onBlur,
},
})
const input = wrapper.find('input')
const select = wrapper.findComponent(Select)
await input.trigger('focus')
// Simulate focus state to trigger menu multiple times
select.vm.toggleMenu()
await nextTick
select.vm.toggleMenu()
await nextTick
// Simulate click the outside
select.vm.handleClickOutside()
await nextTick
expect(onFocus).toHaveBeenCalledTimes(1)
expect(onBlur).toHaveBeenCalled()
})
it('focus & blur for multiple & filterable select', async () => {
const onFocus = jest.fn()
const onBlur = jest.fn()
const wrapper = createSelect({
data() {
return {
multiple: true,
filterable: true,
value: [],
}
},
methods: {
onFocus,
onBlur,
},
})
const input = wrapper.find('input')
const select = wrapper.findComponent(Select)
await input.trigger('focus')
// Simulate focus state to trigger menu multiple times
select.vm.toggleMenu()
await nextTick
select.vm.toggleMenu()
await nextTick
// Select multiple items in multiple mode without triggering focus
const options = getOptions()
options[1].click()
await nextTick
options[2].click()
await nextTick
expect(onFocus).toHaveBeenCalledTimes(1)
// Simulate click the outside
select.vm.handleClickOutside()
await nextTick
await nextTick
expect(onBlur).toHaveBeenCalled()
})
})
}) })

View File

@ -221,8 +221,8 @@ const useSelect = (props: ExtractPropTypes<typeof SelectProps>, emit) => {
// if (expanded.value) { // if (expanded.value) {
// expanded.value = false // expanded.value = false
// } // }
if (states.isComposing) states.softFocus = true
expanded.value = !expanded.value expanded.value = !expanded.value
states.softFocus = true
inputRef.value?.focus?.() inputRef.value?.focus?.()
// } // }
} }
@ -388,14 +388,9 @@ const useSelect = (props: ExtractPropTypes<typeof SelectProps>, emit) => {
states.selectedLabel = option.label states.selectedLabel = option.label
update(option.value) update(option.value)
expanded.value = false expanded.value = false
states.isComposing = false
states.isSilentBlur = byClick
} }
states.isComposing = false
states.isSilentBlur = byClick
// setSoftFocus()
if (expanded.value) return
nextTick(() => {
// scrollToOption(option)
})
} }
const deleteTag = (event: MouseEvent, tag: Option) => { const deleteTag = (event: MouseEvent, tag: Option) => {
@ -423,15 +418,14 @@ const useSelect = (props: ExtractPropTypes<typeof SelectProps>, emit) => {
} }
const handleFocus = (event: FocusEvent) => { const handleFocus = (event: FocusEvent) => {
const focused = states.isComposing
states.isComposing = true states.isComposing = true
if (!states.softFocus) { if (!states.softFocus) {
if (props.automaticDropdown || props.filterable) { if (props.automaticDropdown || props.filterable) {
expanded.value = true expanded.value = true
// if (props.filterable) {
// states.menuVisibleOnFocus = true
// }
} }
emit('focus', event) // If already in the focus state, shouldn't trigger event
if (!focused) emit('focus', event)
} else { } else {
states.softFocus = false states.softFocus = false
} }
@ -444,7 +438,6 @@ const useSelect = (props: ExtractPropTypes<typeof SelectProps>, emit) => {
} }
} }
states.isComposing = false
states.softFocus = false states.softFocus = false
// reset input value when blurred // reset input value when blurred
@ -454,7 +447,6 @@ const useSelect = (props: ExtractPropTypes<typeof SelectProps>, emit) => {
if (calculatorRef.value) { if (calculatorRef.value) {
states.calculatedWidth = calculatorRef.value.getBoundingClientRect().width states.calculatedWidth = calculatorRef.value.getBoundingClientRect().width
} }
if (states.isSilentBlur) { if (states.isSilentBlur) {
states.isSilentBlur = false states.isSilentBlur = false
} else { } else {
@ -462,6 +454,7 @@ const useSelect = (props: ExtractPropTypes<typeof SelectProps>, emit) => {
emit('blur') emit('blur')
} }
} }
states.isComposing = false
}) })
} }