Skip to content

Commit

Permalink
Improve native browser submission while disabling formset on submit (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Dec 17, 2024
1 parent f29a51d commit 9430008
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/alpine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default function (Alpine: TAlpine) {
*/
const form = Alpine.reactive(createForm()) as Data & Form<Data>

syncWithDom(el, resolveMethod(method), resolveUrl(url), form)
syncWithDom(Alpine, el, resolveMethod(method), resolveUrl(url), form)

return form
})
Expand All @@ -174,15 +174,15 @@ export default function (Alpine: TAlpine) {
/**
* Sync the DOM form with the Precognitive form.
*/
const syncWithDom = <Data extends Record<string, unknown>>(el: Node, method: RequestMethod, url: string, form: Form<Data>): void => {
const syncWithDom = <Data extends Record<string, unknown>>(Alpine: TAlpine, el: Node, method: RequestMethod, url: string, form: Form<Data>): void => {
if (! (el instanceof Element && el.nodeName === 'FORM')) {
return
}

syncSyntheticMethodInput(el, method)
syncMethodAttribute(el, method)
syncActionAttribute(el, url)
addProcessingListener(el, form)
addProcessingListener(Alpine, el, form)
}

/**
Expand Down Expand Up @@ -231,4 +231,6 @@ const syncSyntheticMethodInput = (el: Element, method: RequestMethod) => {
/**
* Add processing listener.
*/
const addProcessingListener = <Data extends Record<string, unknown>>(el: Element, form: Form<Data>) => el.addEventListener('submit', () => (form.processing = true))
const addProcessingListener = <Data extends Record<string, unknown>>(Alpine: TAlpine, el: Element, form: Form<Data>) => el.addEventListener('submit', () => {
Alpine.nextTick(() => form.processing = true)
})

0 comments on commit 9430008

Please sign in to comment.