Skip to content

Commit

Permalink
fix: button submitters now work
Browse files Browse the repository at this point in the history
  • Loading branch information
KonnorRogers committed Nov 23, 2021
1 parent f275d80 commit 806bdc2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/elementDisabler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ export function disableElement (event: Event | HTMLFormElement | Submitter): voi

const { linkDisableSelector, buttonDisableSelector, formDisableSelector, formSubmitSelector } = window.mrujs
if (matches(element, linkDisableSelector)) {
console.log("here")
disableLinkElement(element)
} else if (matches(element, buttonDisableSelector) || matches(element, formDisableSelector)) {
console.log(element)
disableFormElement(element as HTMLFormElement)
console.log("here1")
} else if (matches(element, formSubmitSelector)) {
disableFormElements(element as HTMLFormElement)
console.log("here2")
}
}

Expand Down Expand Up @@ -90,8 +94,11 @@ function disableFormElement (element: HTMLFormElement): void {
}
}

element.disabled = true
element.dataset.ujsDisabled = 'true'

// Due to how FormSubmissions work in Chrome, if the button is disabled prior
// to submitting the form, then form "submit" event will never trigger.
setTimeout(() => element.disabled = true)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function handleMethod (event: Event): void {

if (href == null) return

stopEverything(event)
event.preventDefault()
const submitter = event.target

const linkSubmission = MethodSubmission(element)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const BASE_SELECTORS: QuerySelectorInterface = {
linkDisableSelector: `a[${disableWith}], a[${disable}]`,

// Button onClick disable selector with possible reenable after remote submission
buttonDisableSelector: `button[${disableWith}], button[${disable}]`,
buttonDisableSelector: `button[data-remote][${disableWith}], button[data-remote][${disable}]`,
fileInputSelector: 'fileInputSelector: \'input[name][type=file]:not([disabled])\''
}

Expand Down
8 changes: 6 additions & 2 deletions test/rails/dummy/app/javascript/packs/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mrujs from 'mrujs'
// import Racks from "@rails/ujs"
import Rails from 'mrujs'
import { ActiveStorage, JsErb } from "mrujs/plugins"
// import { JsErb } from "mrujs/plugins"
// import * as ActiveStorage from "@rails/activestorage"
Expand All @@ -14,12 +15,15 @@ import "../stylesheets/application.css"
}
})()

mrujs.start({
Rails.start({
plugins: [
JsErb(),
ActiveStorage()
]
})

// window.Racks = Racks
// Racks.start()
// ActiveStorage.start()

document.addEventListener("direct-upload:initialize", event => {
Expand Down
3 changes: 2 additions & 1 deletion test/rails/dummy/app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<%= render partial: "posts/form_body", locals: {form: form, post: post} %>

<div class="actions">
<%= form.submit nil, data: {"disable-with" => "Submitting..."} %>
<%# <%= form.submit nil, data: {"disable-with" => "Submitting..."} %1> %>
<%= form.button nil, data: {"disable-with" => "Submitting..."} %>
</div>
<% end %>

Expand Down

0 comments on commit 806bdc2

Please sign in to comment.