Skip to content

Commit

Permalink
Fix state data attribute in Vue (#2787)
Browse files Browse the repository at this point in the history
* Add tests

* Fix state data attribute in Vue
  • Loading branch information
thecrypticace committed Oct 4, 2023
1 parent 99cdf91 commit 20a224a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
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

2 comments on commit 20a224a

@vercel
Copy link

@vercel vercel bot commented on 20a224a Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

headlessui-vue – ./packages/playground-vue

headlessui-vue.vercel.app
headlessui-vue-git-main-tailwindlabs.vercel.app
headlessui-vue-tailwindlabs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 20a224a Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

headlessui-react – ./packages/playground-react

headlessui-react-tailwindlabs.vercel.app
headlessui-react-git-main-tailwindlabs.vercel.app
headlessui-react.vercel.app

Please sign in to comment.