mirror of
https://github.com/element-plus/element-plus.git
synced 2024-11-21 01:02:59 +08:00
fix(components): [select] error parameters of getValueIndex (#14173)
* fix(components): [select] error arguments of getValueIndex closed #14139 #18247 * test: fix test * style(components): [select] optimize code format
This commit is contained in:
parent
7bba5332f1
commit
a6430f5b98
@ -2803,4 +2803,132 @@ describe('Select', () => {
|
||||
.display
|
||||
).toBe('none')
|
||||
})
|
||||
|
||||
describe('check default first option after input', () => {
|
||||
it('defalut', async () => {
|
||||
vi.useFakeTimers()
|
||||
wrapper = getSelectVm({
|
||||
filterable: true,
|
||||
defaultFirstOption: true,
|
||||
})
|
||||
|
||||
const select = wrapper.findComponent({ name: 'ElSelect' })
|
||||
const selectVm = select.vm as any
|
||||
const input = wrapper.find('input')
|
||||
input.element.focus()
|
||||
|
||||
selectVm.onInput({
|
||||
target: {
|
||||
value: '蚵仔煎',
|
||||
},
|
||||
})
|
||||
|
||||
vi.runAllTimers()
|
||||
await nextTick()
|
||||
expect(selectVm.states.hoveringIndex).toBe(2)
|
||||
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('with multiple', async () => {
|
||||
vi.useFakeTimers()
|
||||
wrapper = getSelectVm({
|
||||
multiple: true,
|
||||
filterable: true,
|
||||
defaultFirstOption: true,
|
||||
})
|
||||
|
||||
const select = wrapper.findComponent({ name: 'ElSelect' })
|
||||
const selectVm = select.vm as any
|
||||
const input = wrapper.find('input')
|
||||
input.element.focus()
|
||||
|
||||
selectVm.onInput({
|
||||
target: {
|
||||
value: '蚵仔煎',
|
||||
},
|
||||
})
|
||||
|
||||
vi.runAllTimers()
|
||||
await nextTick()
|
||||
expect(selectVm.states.hoveringIndex).toBe(2)
|
||||
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('the value is string with value-key', async () => {
|
||||
vi.useFakeTimers()
|
||||
wrapper = getSelectVm({
|
||||
filterable: true,
|
||||
defaultFirstOption: true,
|
||||
valueKey: 'label',
|
||||
})
|
||||
|
||||
const select = wrapper.findComponent({ name: 'ElSelect' })
|
||||
const selectVm = select.vm as any
|
||||
const input = wrapper.find('input')
|
||||
input.element.focus()
|
||||
|
||||
selectVm.onInput({
|
||||
target: {
|
||||
value: '蚵仔煎',
|
||||
},
|
||||
})
|
||||
|
||||
vi.runAllTimers()
|
||||
await nextTick()
|
||||
expect(selectVm.states.hoveringIndex).toBe(2)
|
||||
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('the value is object with value-key', async () => {
|
||||
vi.useFakeTimers()
|
||||
wrapper = _mount(
|
||||
`
|
||||
<el-select v-model="value" value-key="id" filterable default-first-option>
|
||||
<el-option
|
||||
v-for="item in options"
|
||||
:label="item.name"
|
||||
:key="item.id"
|
||||
:value="item">
|
||||
</el-option>
|
||||
</el-select>
|
||||
`,
|
||||
() => ({
|
||||
options: [
|
||||
{
|
||||
id: 1,
|
||||
name: '黄金糕',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: '双皮奶',
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: '蚵仔煎',
|
||||
},
|
||||
],
|
||||
})
|
||||
)
|
||||
|
||||
const select = wrapper.findComponent({ name: 'ElSelect' })
|
||||
const selectVm = select.vm as any
|
||||
const input = wrapper.find('input')
|
||||
input.element.focus()
|
||||
|
||||
selectVm.onInput({
|
||||
target: {
|
||||
value: '蚵仔煎',
|
||||
},
|
||||
})
|
||||
|
||||
vi.runAllTimers()
|
||||
await nextTick()
|
||||
expect(selectVm.states.hoveringIndex).toBe(2)
|
||||
|
||||
vi.useRealTimers()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -5,7 +5,6 @@ import {
|
||||
onMounted,
|
||||
reactive,
|
||||
ref,
|
||||
toRaw,
|
||||
watch,
|
||||
watchEffect,
|
||||
} from 'vue'
|
||||
@ -396,8 +395,9 @@ export const useSelect = (props: ISelectProps, emit) => {
|
||||
)
|
||||
const userCreatedOption = optionsInDropdown.find((n) => n.created)
|
||||
const firstOriginOption = optionsInDropdown[0]
|
||||
const valueList = optionsArray.value.map((item) => item.value)
|
||||
states.hoveringIndex = getValueIndex(
|
||||
optionsArray.value,
|
||||
valueList,
|
||||
userCreatedOption || firstOriginOption
|
||||
)
|
||||
}
|
||||
@ -563,7 +563,7 @@ export const useSelect = (props: ISelectProps, emit) => {
|
||||
const handleOptionSelect = (option) => {
|
||||
if (props.multiple) {
|
||||
const value = ensureArray(props.modelValue ?? []).slice()
|
||||
const optionIndex = getValueIndex(value, option.value)
|
||||
const optionIndex = getValueIndex(value, option)
|
||||
if (optionIndex > -1) {
|
||||
value.splice(optionIndex, 1)
|
||||
} else if (
|
||||
@ -592,19 +592,12 @@ export const useSelect = (props: ISelectProps, emit) => {
|
||||
})
|
||||
}
|
||||
|
||||
const getValueIndex = (arr: any[] = [], value) => {
|
||||
if (!isObject(value)) return arr.indexOf(value)
|
||||
const getValueIndex = (arr: any[] = [], option) => {
|
||||
if (!isObject(option?.value)) return arr.indexOf(option.value)
|
||||
|
||||
const valueKey = props.valueKey
|
||||
let index = -1
|
||||
arr.some((item, i) => {
|
||||
if (toRaw(get(item, valueKey)) === get(value, valueKey)) {
|
||||
index = i
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return arr.findIndex((item) => {
|
||||
return isEqual(get(item, props.valueKey), getValueKey(option))
|
||||
})
|
||||
return index
|
||||
}
|
||||
|
||||
const scrollToOption = (option) => {
|
||||
|
Loading…
Reference in New Issue
Block a user