From 0efd78b979b06575d3b3111b0a0f07e805f3dab3 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sun, 3 Dec 2023 23:01:56 +0500 Subject: [PATCH] #11068: update confirm_modal_component to accept controller values --- app/components/confirm_modal_component.rb | 18 +++++++++++++++++- .../confirm_modal_component.html.haml | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/components/confirm_modal_component.rb b/app/components/confirm_modal_component.rb index 39ed7f90e0..32cdebe5ba 100644 --- a/app/components/confirm_modal_component.rb +++ b/app/components/confirm_modal_component.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class ConfirmModalComponent < ModalComponent + # @param controller_data_values [Array(Hash)] format: {: value1, : value2} def initialize( id:, reflex: nil, @@ -10,7 +11,8 @@ class ConfirmModalComponent < ModalComponent confirm_reflexes: nil, confirm_button_color: :primary, confirm_button_text: I18n.t('js.admin.modals.confirm'), - cancel_button_text: I18n.t('js.admin.modals.cancel') + cancel_button_text: I18n.t('js.admin.modals.cancel'), + controller_data_values: {} ) super(id:, close_button: true) @confirm_actions = confirm_actions @@ -21,6 +23,7 @@ class ConfirmModalComponent < ModalComponent @confirm_button_color = confirm_button_color @confirm_button_text = confirm_button_text @cancel_button_text = cancel_button_text + @controller_data_values = transform_values_for_controller(controller_data_values) end private @@ -28,4 +31,17 @@ class ConfirmModalComponent < ModalComponent def close_button_class "secondary" end + + def modal_attrs + @modal_attrs ||= { + id: @id, + "data-controller": "modal #{@controller}", + "data-action": "keyup@document->modal#closeIfEscapeKey", + "data-#{@controller}-reflex-value": @reflex + }.merge(@controller_data_values) + end + + def transform_values_for_controller(values) + values.transform_keys { |value_name| "data-#{@controller}-#{value_name}-value" } + end end diff --git a/app/components/confirm_modal_component/confirm_modal_component.html.haml b/app/components/confirm_modal_component/confirm_modal_component.html.haml index 5049e9e52c..3ab7e1b281 100644 --- a/app/components/confirm_modal_component/confirm_modal_component.html.haml +++ b/app/components/confirm_modal_component/confirm_modal_component.html.haml @@ -1,4 +1,4 @@ -%div{ id: @id, "data-controller": "modal #{@controller}", "data-action": "keyup@document->modal#closeIfEscapeKey", "data-#{@controller}-reflex-value": @reflex } +%div{ **modal_attrs } .reveal-modal-bg.fade{ "data-modal-target": "background", "data-action": "click->modal#close" } .reveal-modal.fade.tiny.help-modal{ "data-modal-target": "modal" } = content