Skip to content

Commit

Permalink
feat(ld-button): implicit form submit
Browse files Browse the repository at this point in the history
  • Loading branch information
borisdiakur committed Oct 13, 2021
1 parent ec3775f commit 821c7ae
Show file tree
Hide file tree
Showing 3 changed files with 409 additions and 310 deletions.
27 changes: 26 additions & 1 deletion src/liquid/components/ld-button/ld-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,40 @@ export class LdButton implements InnerFocusable {
})
}

private clickFakeButton(
form: HTMLFormElement,
buttonType: 'submit' | 'reset'
) {
const btnFake = document.createElement('button')
btnFake.type = buttonType
btnFake.style.display = 'none'
form.appendChild(btnFake)
btnFake.click()
btnFake.remove()
}

private handleClick = (event: MouseEvent) => {
console.log(event.target)
const ariaDisabled = this.button.getAttribute('aria-disabled')

if (this.disabled || (ariaDisabled && ariaDisabled !== 'false')) {
event.preventDefault()
event.stopImmediatePropagation()
return
}

if (!this.href) {
const form = this.el.closest('form')
if (form) {
switch (this.el.getAttribute('type')) {
case 'reset':
this.clickFakeButton(form, 'reset')
break
case 'submit':
default:
this.clickFakeButton(form, 'submit')
}
}
}
}

componentWillLoad() {
Expand Down

0 comments on commit 821c7ae

Please sign in to comment.