From 1500a1cf281c4d214ee4a5385cb54d845a7f9e18 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 4 May 2022 12:42:10 +0200 Subject: [PATCH] feat(turbo): add enable and disable mutations --- app/helpers/turbo_stream_helper.rb | 8 ++++++++ app/javascript/controllers/turbo_event_controller.ts | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/helpers/turbo_stream_helper.rb b/app/helpers/turbo_stream_helper.rb index 40699c66e..fa14dc358 100644 --- a/app/helpers/turbo_stream_helper.rb +++ b/app/helpers/turbo_stream_helper.rb @@ -31,5 +31,13 @@ module TurboStreamHelper def focus_all(targets) dispatch('dom:mutation', { action: :focus, targets: targets }) end + + def disable(target) + dispatch('dom:mutation', { action: :disable, target: target }) + end + + def enable(target) + dispatch('dom:mutation', { action: :enable, target: target }) + end end end diff --git a/app/javascript/controllers/turbo_event_controller.ts b/app/javascript/controllers/turbo_event_controller.ts index f66b63ffd..c42250d18 100644 --- a/app/javascript/controllers/turbo_event_controller.ts +++ b/app/javascript/controllers/turbo_event_controller.ts @@ -18,7 +18,7 @@ export class TurboEventController extends ApplicationController { } } -const MutationAction = z.enum(['show', 'hide', 'focus']); +const MutationAction = z.enum(['show', 'hide', 'focus', 'enable', 'disable']); type MutationAction = z.infer; const Mutation = z.union([ z.object({ @@ -55,6 +55,16 @@ const Mutations: Record void> = { for (const element of findElements(mutation)) { element.focus(); } + }, + disable: (mutation) => { + for (const element of findElements(mutation)) { + element.disabled = true; + } + }, + enable: (mutation) => { + for (const element of findElements(mutation)) { + element.disabled = false; + } } };