fix(components): [select] display exception when label is 0 or "" (#20905)

* fix(components): [option] display exception when label is `0` or `""`

* test: add test case
This commit is contained in:
伊墨
2025-06-03 09:07:37 +08:00
committed by GitHub
parent b290b3b1af
commit b31e727edd
2 changed files with 31 additions and 1 deletions

View File

@@ -3246,4 +3246,34 @@ describe('Select', () => {
await nextTick()
expect(selectVm.selectedLabel).toBe('')
})
// #20905
test('display correct when label is 0 or ""', async () => {
wrapper = _mount(
`
<el-select>
<el-option
v-for="item in options"
:label="item.label"
:key="item.value"
:value="item.value">
</el-option>
</el-select>
`,
() => ({
options: [
{ value: '黄金糕', label: '' },
{ value: '双皮奶', label: 0 },
{ value: '蚵仔煎', label: '蚵仔煎' },
{ value: '北京烤鸭', label: undefined },
],
})
)
await nextTick()
const options = getOptions()
expect(options[0].textContent).toBe('')
expect(options[1].textContent).toBe('0')
expect(options[2].textContent).toBe('蚵仔煎')
expect(options[3].textContent).toBe('北京烤鸭')
})
})

View File

@@ -38,7 +38,7 @@ export function useOption(props: OptionProps, states: OptionStates) {
})
const currentLabel = computed(() => {
return props.label || (isObject(props.value) ? '' : props.value)
return props.label ?? (isObject(props.value) ? '' : props.value)
})
const currentValue = computed(() => {