Skip to content

Ensure disabled state works as expected by changing links to buttons - #51

Merged
andreibondarev merged 4 commits into
mainfrom
fix-button-disable-state
Oct 23, 2026
Merged

andreibondarev merged 4 commits into
mainfrom
fix-button-disable-state

Conversation

@andreibondarev

Copy link
Copy Markdown
Member

Supersedes #48 by @joshmfrankel. His commits are included unchanged, plus a merge of main and one commit addressing the review feedback. I couldn't push to the fork directly.

From #48

  • Batch action links become button_to forms, disabled by default, so the disabled state actually works
  • WeakSets stop init() (DOMContentLoaded / turbo:load) from binding handlers twice
  • Selected ids are submitted as hidden batch_action_ids[] inputs

Follow-up fixes (review feedback)

  • Confirm is opt-in again: it only shows when confirm: is given. Before, every submission prompted "Are you sure you want to submit this form?"
  • The confirm message moves to data-batch-action-confirm so rails-ujs doesn't show a second dialog for button[data-confirm]
  • The native submit is allowed to proceed (only cancelled when the confirm is declined) instead of preventDefault() + form.submit()
  • Hidden ids from an earlier submission are replaced, not duplicated (e.g. after navigating back)
  • Fixed the inverted back/forward-cache reset (length === 0 → > 0) and re-sync button state afterwards
  • The form gets data-turbo="false" to keep the previous full-page POST behavior, so existing batch actions that redirect_to keep working under Turbo. This also covers the turbo_method TODO.
  • button.form replaces the form:has(...) selector
  • Query params in the path (Change adding query parameters #45) are kept, since they're in the form action; added a spec for this
  • Partial formatting nits from the review

Breaking for custom CSS/JS

  • The button is now a <button> inside a <form class="button_to">, not an <a class="btn disabled">
  • data-confirm on the button is now data-batch-action-confirm

Testing

  • bundle exec rspec: 10 examples, 0 failures
  • Headless Chrome against the rendered markup: buttons start disabled and enable on selection; cancelling the confirm blocks submit; ids aren't duplicated on resubmit; buttons without a confirm message don't prompt; a double init() doesn't double-bind

joshmfrankel and others added 4 commits April 2, 2026 15:34
* Use WeakSet to avoid attaching multiple event handlers to the same element from multiple init() calls
* Append hidden input within the button_to form to contain all selected batch_action_ids
* Set Button disabled as default state
* Ensure data-confirm is pulled into confirmation dialog (Rails 7+ doesn't confirm with UJS)
# Conflicts:
#	app/assets/javascripts/administrate_batch_actions/script.js
- Only show a confirm dialog when a confirm message is given, and use
  data-batch-action-confirm so rails-ujs doesn't prompt a second time
- Let the native form submission proceed instead of form.submit(), only
  preventing it when the confirm is cancelled
- Replace previously appended hidden ids so a resubmit doesn't duplicate them
- Fix inverted back/forward cache reset and re-sync button state
- Opt the form out of Turbo to keep the previous full-page POST behavior
- Find each button's form via button.form instead of form:has()
- Tidy partial formatting
@andreibondarev
andreibondarev merged commit 4a1d43e into main Oct 23, 2026
1 check passed
@andreibondarev
andreibondarev deleted the fix-button-disable-state branch October 23, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants