feat(turbo): add enable and disable mutations

This commit is contained in:
Paul Chavard 2022-05-04 12:42:10 +02:00
parent cfb83bbdda
commit 1500a1cf28
2 changed files with 19 additions and 1 deletions

View file

@ -31,5 +31,13 @@ module TurboStreamHelper
def focus_all(targets) def focus_all(targets)
dispatch('dom:mutation', { action: :focus, targets: targets }) dispatch('dom:mutation', { action: :focus, targets: targets })
end 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
end end

View file

@ -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<typeof MutationAction>; type MutationAction = z.infer<typeof MutationAction>;
const Mutation = z.union([ const Mutation = z.union([
z.object({ z.object({
@ -55,6 +55,16 @@ const Mutations: Record<MutationAction, (mutation: Mutation) => void> = {
for (const element of findElements(mutation)) { for (const element of findElements(mutation)) {
element.focus(); element.focus();
} }
},
disable: (mutation) => {
for (const element of findElements<HTMLInputElement>(mutation)) {
element.disabled = true;
}
},
enable: (mutation) => {
for (const element of findElements<HTMLInputElement>(mutation)) {
element.disabled = false;
}
} }
}; };