Skip to content

Commit

Permalink
fix(ld-button): stop propagation of click event if disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
borisdiakur committed Jan 19, 2023
1 parent d2e823d commit 4b97613
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/liquid/components/ld-button/ld-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ export class LdButton implements InnerFocusable, ClonesAttributes {

if (this.disabled || (ariaDisabled && ariaDisabled !== 'false')) {
ev.preventDefault()
// Stopping propagation is important for clicks on child elements,
// because otherwise event handlers attached to the ld-button
// are still called (no matter if the event was prevented or not).
ev.stopPropagation()
return
}

Expand Down
34 changes: 34 additions & 0 deletions src/liquid/components/ld-button/test/ld-button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,40 @@ describe('ld-button', () => {
expect(clickHandler).not.toHaveBeenCalled()
})

it('prevents default and propagation of click events, if disabled', async () => {
const page = await newSpecPage({
components: [LdButton],
html: `<ld-button disabled>Text</ld-button>`,
})
const ldButton = page.root

const ev = new MouseEvent('click', { bubbles: true, cancelable: true })

ldButton.dispatchEvent(ev)

await page.waitForChanges()

expect(ev.defaultPrevented).toBeTruthy()
expect(ev.cancelBubble).toBeTruthy()
})

it('prevents default and propagation of click events, if aria-disabled', async () => {
const page = await newSpecPage({
components: [LdButton],
html: `<ld-button aria-disabled="true">Text</ld-button>`,
})
const ldButton = page.root

const ev = new MouseEvent('click', { bubbles: true, cancelable: true })

ldButton.dispatchEvent(ev)

await page.waitForChanges()

expect(ev.defaultPrevented).toBeTruthy()
expect(ev.cancelBubble).toBeTruthy()
})

it('allows to set inner focus', async () => {
const { root } = await newSpecPage({
components: [LdButton],
Expand Down

1 comment on commit 4b97613

@vercel
Copy link

@vercel vercel bot commented on 4b97613 Jan 19, 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:

liquid – ./

liquid-git-main-uxsd.vercel.app
liquid-oxygen.vercel.app
liquid-uxsd.vercel.app

Please sign in to comment.