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 state data attribute in Vue #2787

Merged
merged 2 commits into from
Oct 4, 2023
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
49 changes: 48 additions & 1 deletion packages/@headlessui-vue/src/utils/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@ import { render } from './render'
let Dummy = defineComponent({
props: {
as: { type: [Object, String], default: 'div' },
slot: { type: Object, default: () => ({}) },
},
setup(props, { attrs, slots }) {
return () => render({ theirProps: props, ourProps: {}, slots, attrs, slot: {}, name: 'Dummy' })
return () => {
let { slot, ...rest } = props

return render({
theirProps: rest,
ourProps: {},
slots,
attrs,
slot,
name: 'Dummy',
})
}
},
})

Expand Down Expand Up @@ -120,3 +132,38 @@ describe('Validation', () => {
expect(document.getElementById('result')).toHaveAttribute('data-test', '123')
})
})

describe('State Data Attributes', () => {
it('as=element', () => {
renderTemplate({
template: html`
<Dummy id="result" as="div" :slot="{active: true, selected: true}">
<div>test</div>
</Dummy>
`,
})

expect(document.getElementById('result')).toHaveAttribute(
'data-headlessui-state',
'active selected'
)
})

it('as=template', () => {
renderTemplate({
template: html`
<Dummy as="template" class="abc" :slot="{active: true, selected: true}">
<div id="result">test</div>
</Dummy>
`,
})

expect(document.getElementById('result')).toHaveClass('abc')

// NOTE: Removing class="abc" causes this assertion to fail
expect(document.getElementById('result')).toHaveAttribute(
'data-headlessui-state',
'active selected'
)
})
})
3 changes: 2 additions & 1 deletion packages/@headlessui-vue/src/utils/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function _render({
)
}

let mergedProps = mergeProps(firstChild.props ?? {}, incomingProps)
let mergedProps = mergeProps(firstChild.props ?? {}, incomingProps, dataAttributes)
let cloned = cloneVNode(firstChild, mergedProps, true)
// Explicitly override props starting with `on`. This is for event handlers, but there are
// scenario's where we set them to `undefined` explicitly (when `aria-disabled="true"` is
Expand All @@ -155,6 +155,7 @@ function _render({
}

if (Array.isArray(children) && children.length === 1) {
// TODO: Do we need to cloneVNode + dataAttributes here?
return children[0]
}

Expand Down