Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kdropdownitem): attribute handling #2157

Merged
merged 7 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sandbox/pages/SandboxDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,23 @@
</KButton>
<template #items>
<KDropdownItem
data-testid="router-link"
:item="{ label: 'Home', to: { name: 'home' } }"
target="_blank"
>
<KongIcon />
Router link
</KDropdownItem>
<KDropdownItem
data-testid="external-link"
:item="{ label: 'External link', to: 'https://kongponents.konghq.com/' }"
target="_blank"
>
<ExternalLinkIcon />
External link
</KDropdownItem>
<KDropdownItem
data-testid="disabled-button"
disabled
has-divider
@click="handleItemClick"
Expand All @@ -186,13 +189,15 @@
Disabled button
</KDropdownItem>
<KDropdownItem
data-testid="disabled-router-link"
disabled
:item="{ label: 'Home', to: { name: 'home' } }"
>
<KongIcon />
Disabled router link
</KDropdownItem>
<KDropdownItem
data-testid="disabled-external-link"
disabled
:item="{ label: 'External link', to: 'https://kongponents.konghq.com/' }"
>
Expand All @@ -201,6 +206,7 @@
</KDropdownItem>
<KDropdownItem
danger
data-testid="button"
has-divider
@click="handleItemClick"
>
Expand Down
20 changes: 10 additions & 10 deletions src/components/KDropdown/KDropdown.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ describe('KDropdown', () => {
cy.getTestId('dropdown-list').eq(0).find('.has-divider').should('have.length', 2)
cy.getTestId('dropdown-list').eq(0).find('.danger').should('have.length', 1)

cy.get('[data-testid="button"] button').should('be.visible')
cy.get('[data-testid="disabled-button"] button').should('be.visible')
cy.get('button[data-testid="button"]').should('be.visible')
cy.get('button[data-testid="disabled-button"]').should('be.visible')

cy.get('[data-testid="router-link"] router-link').should('be.visible')
cy.get('[data-testid="disabled-router-link"] router-link').should('be.visible')
cy.get('router-link[data-testid="router-link"]').should('be.visible')
cy.get('router-link[data-testid="disabled-router-link"]').should('be.visible')

cy.get('[data-testid="external-link"] a').should('be.visible')
cy.get('[data-testid="disabled-external-link"] a').should('be.visible')
cy.get('a[data-testid="external-link"]').should('be.visible')
cy.get('a[data-testid="disabled-external-link"]').should('be.visible')
})
})

Expand All @@ -228,11 +228,11 @@ describe('KDropdownItem', () => {
},
})

cy.getTestId('dropdown-item').should('not.exist')
cy.getTestId(testIdAttr).should('be.visible')
cy.getTestId(testIdAttr).find('[data-testid="dropdown-item-trigger"]').should('have.attr', 'target', '_blank')
cy.get(`li[data-testid="dropdown-item"].${boundClass}`).should('be.visible')
cy.get(`li[data-testid="dropdown-item"] [data-testid="${testIdAttr}"]`).should('be.visible')
cy.get(`li[data-testid="dropdown-item"] [data-testid="${testIdAttr}"]`).should('have.attr', 'target', '_blank')
// making sure classes don't leak to trigger element
cy.getTestId('dropdown-item-trigger').not(`.${boundClass}`).should('have.length', 1)
cy.get(`li[data-testid="dropdown-item"] [data-testid="${testIdAttr}"]`).not(`.${boundClass}`).should('have.length', 1)
})

it('correctly handles disabled state on links', () => {
Expand Down
12 changes: 7 additions & 5 deletions src/components/KDropdown/KDropdownItem.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<li
class="k-dropdown-item"
:class="{
:class="[{
'has-divider': hasDivider,
'disabled': disabled,
'danger': danger || isDangerous,
'dropdown-selected-option': selected
}"
'dropdown-selected-option': selected,
}, $attrs.class, $attrs.style]"
data-testid="dropdown-item"
>
<component
Expand All @@ -29,6 +29,10 @@ import type { DropdownItem, DropdownItemRenderedRecord, DropdownItemRenderedType
import type { PropType } from 'vue'
import { computed, useAttrs } from 'vue'

defineOptions({
inheritAttrs: false,
})

const attrs = useAttrs()

const props = defineProps({
Expand Down Expand Up @@ -134,8 +138,6 @@ const strippedAttrs = computed((): typeof attrs => {
delete modifiedAttrs.class
delete modifiedAttrs.disabled
delete modifiedAttrs.style
// Ensure the `data-testid` attribute is only applied to the top-most element
delete modifiedAttrs['data-testid']

return modifiedAttrs
})
Expand Down