From 219fc2f3bc76f16e6ecc1ec7df491971d3dc0f73 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Sun, 8 May 2022 12:33:27 +0200 Subject: [PATCH 1/2] fix(turbo): patch turbo to make link_to work without global turbo enabled --- app/views/layouts/application.html.haml | 14 +++++++---- patches/@hotwired+turbo+7.1.0.patch | 32 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 patches/@hotwired+turbo+7.1.0.patch diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 8a7048d6b..81caa3ebb 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -42,8 +42,12 @@ = yield :charts_js - // Container for custom turbo-stream actions - %turbo-events - // Container for turbo form that we can submit from inside other forms - %div{ 'data-turbo': 'true' } - = form_tag('', id: :turbo_form) + // Container for custom turbo-stream actions + %turbo-events + + // We patch `@hotwired/turbo` to attach forms generated from links to this + // container instead of the body to avoid conflicts with `@rails/ujs`. We also + // patch `@hotwired/turbo` to add a timeout before removing the form because in + // order to be accepted as a valid `turbo form`` either global `turbo drive`` should + // be enabled or the form needs to have a parent with `data-turbo="true"` on it. + %div{ 'data-turbo': 'true' } diff --git a/patches/@hotwired+turbo+7.1.0.patch b/patches/@hotwired+turbo+7.1.0.patch new file mode 100644 index 000000000..d6b551b3e --- /dev/null +++ b/patches/@hotwired+turbo+7.1.0.patch @@ -0,0 +1,32 @@ +diff --git a/node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js b/node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js +index 963422f..65364f1 100644 +--- a/node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js ++++ b/node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js +@@ -2621,9 +2621,9 @@ class Session { + form.addEventListener("turbo:submit-start", () => form.remove()); + } + else { +- form.addEventListener("submit", () => form.remove()); ++ form.addEventListener("submit", () => setTimeout(() => form.remove(), 500)); + } +- document.body.appendChild(form); ++ (document.querySelector('body > [data-turbo="true"]') || document.body).appendChild(form); + return dispatch("submit", { cancelable: true, target: form }); + } + else { +diff --git a/node_modules/@hotwired/turbo/dist/turbo.es2017-umd.js b/node_modules/@hotwired/turbo/dist/turbo.es2017-umd.js +index 101db1f..ce43031 100644 +--- a/node_modules/@hotwired/turbo/dist/turbo.es2017-umd.js ++++ b/node_modules/@hotwired/turbo/dist/turbo.es2017-umd.js +@@ -2627,9 +2627,9 @@ Copyright © 2021 Basecamp, LLC + form.addEventListener("turbo:submit-start", () => form.remove()); + } + else { +- form.addEventListener("submit", () => form.remove()); ++ form.addEventListener("submit", () => setTimeout(() => form.remove(), 500)); + } +- document.body.appendChild(form); ++ (document.querySelector('body > [data-turbo="true"]') || document.body).appendChild(form); + return dispatch("submit", { cancelable: true, target: form }); + } + else { From c5addb48291f14d0b29ebcb06f378c14a8675c13 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 10 May 2022 09:01:56 +0200 Subject: [PATCH 2/2] fix(turbo): use link_to instead of submit button --- app/views/shared/dossiers/editable_champs/_repetition.html.haml | 2 +- .../shared/dossiers/editable_champs/_repetition_row.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/shared/dossiers/editable_champs/_repetition.html.haml b/app/views/shared/dossiers/editable_champs/_repetition.html.haml index 54e58d46f..e72156bed 100644 --- a/app/views/shared/dossiers/editable_champs/_repetition.html.haml +++ b/app/views/shared/dossiers/editable_champs/_repetition.html.haml @@ -3,6 +3,6 @@ = render partial: 'shared/dossiers/editable_champs/repetition_row', locals: { form: form, champ: champ, row: champs } .actions{ 'data-turbo': 'true' } - = button_tag type: :submit, form: :turbo_form, formaction: champs_repetition_path(champ.id), formmethod: :post, class: 'button add-row' do + = link_to champs_repetition_path(champ.id), data: { turbo_method: :post }, class: 'button add-row' do %span.icon.add Ajouter un élément pour « #{champ.libelle} » diff --git a/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml b/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml index 62a825530..47a4ea613 100644 --- a/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml +++ b/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml @@ -5,5 +5,5 @@ = render partial: 'shared/dossiers/editable_champs/editable_champ', locals: { form: form, champ: champ } .flex.row-reverse{ 'data-turbo': 'true' } - = button_tag type: :submit, form: :turbo_form, formaction: champs_repetition_path(champ.id, champ_ids: row.map(&:id), row_id: row_dom_id), formmethod: :delete, class: 'button danger remove-row' do + = link_to champs_repetition_path(champ.id, champ_ids: row.map(&:id), row_id: row_dom_id), data: { turbo_method: :delete }, class: 'button danger remove-row' do Supprimer l’élément