fix(components): [el-dropdown] use custom attributes for dropdown items (#5779)

* fix(components): [el-dropdown] use custom attributes for dropdown items

* fix: forward props and attrs
This commit is contained in:
mawi1512 2022-02-09 08:07:20 +01:00 committed by GitHub
parent 99d3f0902b
commit 129a2d72e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 4 deletions

View File

@ -487,4 +487,28 @@ describe('Dropdown', () => {
expect(popperElement.classList.contains('custom-popper-class')).toBe(true)
})
test('custom attributes for dropdown items', async () => {
const wrapper = _mount(
`
<el-dropdown>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item data-custom-attribute="hello">Item</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
`,
() => ({})
)
await nextTick()
expect(
wrapper
.findComponent({
name: 'DropdownItemImpl',
})
.find('.el-dropdown-menu__item')
.element.getAttribute('data-custom-attribute')
).toBe('hello')
})
})

View File

@ -1,8 +1,12 @@
<template>
<div v-if="divided" class="el-dropdown-menu__item--divided"></div>
<div
v-if="divided"
class="el-dropdown-menu__item--divided"
v-bind="$attrs"
></div>
<div
:ref="itemRef"
v-bind="dataset"
v-bind="{ ...dataset, ...$attrs }"
:aria-disabled="disabled"
:class="{
'el-dropdown-menu__item': true,

View File

@ -5,7 +5,7 @@
>
<el-roving-focus-item :focusable="!disabled">
<el-dropdown-item-impl
v-bind="$props"
v-bind="propsAndAttrs"
@pointerleave="handlePointerLeave"
@pointermove="handlePointerMove"
@click="handleClick"
@ -41,9 +41,10 @@ export default defineComponent({
ElRovingFocusItem,
ElDropdownItemImpl,
},
inheritAttrs: false,
props: dropdownItemProps,
emits: ['pointermove', 'pointerleave', 'click'],
setup(props, { emit }) {
setup(props, { emit, attrs }) {
const { elDropdown } = useDropdown()
const _instance = getCurrentInstance()
const itemRef = ref<HTMLElement | null>(null)
@ -97,11 +98,17 @@ export default defineComponent({
}
)
// direct usage of v-bind={ ...$props, ...$attrs } causes type errors
const propsAndAttrs = computed(() => {
return { ...props, ...attrs }
})
return {
handleClick,
handlePointerMove,
handlePointerLeave,
textContent,
propsAndAttrs,
}
},
})