Skip to content

Commit 57caacd

Browse files
committed
add extra tests to verify that @click works on MenuItem as='button'
1 parent 46a33b2 commit 57caacd

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

packages/@headlessui-vue/src/components/menu/menu.test.tsx

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,12 @@ describe('Keyboard interactions', () => {
816816
<MenuButton>Trigger</MenuButton>
817817
<MenuItems>
818818
<MenuItem as="a">Item A</MenuItem>
819-
<MenuItem as="button" :onClick="clickHandler">
819+
<MenuItem as="button" @click="clickHandler">
820820
Item B
821821
</MenuItem>
822-
<MenuItem as="a">Item C</MenuItem>
822+
<MenuItem>
823+
<button @click="clickHandler">Item C</button>
824+
</MenuItem>
823825
</MenuItems>
824826
</Menu>
825827
`,
@@ -851,6 +853,18 @@ describe('Keyboard interactions', () => {
851853

852854
// Verify the button got "clicked"
853855
expect(clickHandler).toHaveBeenCalledTimes(1)
856+
857+
// Click the menu button again
858+
await click(getMenuButton())
859+
860+
// Active the last menu item
861+
await hover(getMenuItems()[2])
862+
863+
// Close menu, and invoke the item
864+
await press(Keys.Enter)
865+
866+
// Verify the button got "clicked"
867+
expect(clickHandler).toHaveBeenCalledTimes(2)
854868
})
855869

856870
describe('`Space` key', () => {
@@ -2388,6 +2402,46 @@ describe('Mouse interactions', () => {
23882402
assertMenu(getMenu(), { state: MenuState.Closed })
23892403
})
23902404

2405+
it('should be possible to click a menu item, which closes the menu and invokes the @click handler', async () => {
2406+
const clickHandler = jest.fn()
2407+
renderTemplate({
2408+
template: `
2409+
<Menu>
2410+
<MenuButton>Trigger</MenuButton>
2411+
<MenuItems>
2412+
<MenuItem as="a">alice</MenuItem>
2413+
<MenuItem as="button" @click="clickHandler">bob</MenuItem>
2414+
<MenuItem>
2415+
<button @click="clickHandler">charlie</button>
2416+
</MenuItem>
2417+
</MenuItems>
2418+
</Menu>
2419+
`,
2420+
setup: () => ({ clickHandler }),
2421+
})
2422+
2423+
// Open menu
2424+
await click(getMenuButton())
2425+
assertMenu(getMenu(), { state: MenuState.Open })
2426+
2427+
// We should be able to click the first item
2428+
await click(getMenuItems()[1])
2429+
assertMenu(getMenu(), { state: MenuState.Closed })
2430+
2431+
// Verify the callback has been called
2432+
expect(clickHandler).toHaveBeenCalledTimes(1)
2433+
2434+
// Let's re-open the window for now
2435+
await click(getMenuButton())
2436+
2437+
// Click the last item, which should close and invoke the handler
2438+
await click(getMenuItems()[2])
2439+
assertMenu(getMenu(), { state: MenuState.Closed })
2440+
2441+
// Verify the callback has been called
2442+
expect(clickHandler).toHaveBeenCalledTimes(2)
2443+
})
2444+
23912445
it('should be possible to click a disabled menu item, which is a no-op', async () => {
23922446
renderTemplate(`
23932447
<Menu>
@@ -2468,11 +2522,11 @@ describe('Mouse interactions', () => {
24682522
<Menu>
24692523
<MenuButton>Trigger</MenuButton>
24702524
<MenuItems>
2471-
<MenuItem as="a" :onClick="clickHandler">alice</MenuItem>
2472-
<MenuItem as="a" :onClick="clickHandler" disabled>
2525+
<MenuItem as="a" @click="clickHandler">alice</MenuItem>
2526+
<MenuItem as="a" @click="clickHandler" disabled>
24732527
bob
24742528
</MenuItem>
2475-
<MenuItem as="a" :onClick="clickHandler">charlie</MenuItem>
2529+
<MenuItem as="a" @click="clickHandler">charlie</MenuItem>
24762530
</MenuItems>
24772531
</Menu>
24782532
`,

packages/@headlessui-vue/src/components/menu/menu.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ export const MenuItem = defineComponent({
399399
disabled: { type: Boolean, default: false },
400400
class: { type: [String, Function], required: false },
401401
className: { type: [String, Function], required: false },
402-
onClick: { type: Function, required: false },
403402
},
404403
setup(props, { slots, attrs }) {
405404
const api = useMenuContext('MenuItem')
@@ -454,7 +453,6 @@ export const MenuItem = defineComponent({
454453

455454
function handleClick(event: MouseEvent) {
456455
if (disabled) return event.preventDefault()
457-
if (props.onClick) return props.onClick(event)
458456
}
459457

460458
return () => {

0 commit comments

Comments
 (0)