mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-04 07:09:14 +00:00
Add the ability to pass options ModalComponent
Now you can add another stimulus controller or action to the modal
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ModalComponent < ViewComponent::Base
|
||||
def initialize(id:, close_button: true, instant: false, modal_class: :small)
|
||||
def initialize(id:, close_button: true, instant: false, modal_class: :small, **options)
|
||||
@id = id
|
||||
@close_button = close_button
|
||||
@instant = instant
|
||||
@modal_class = modal_class
|
||||
@options = options
|
||||
@data_controller = "modal #{@options.delete(:'data-controller')}".squish
|
||||
@data_action =
|
||||
"keyup@document->modal#closeIfEscapeKey #{@options.delete(:'data-action')}".squish
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
%div{ id: @id, "data-controller": "modal", "data-action": "keyup@document->modal#closeIfEscapeKey", "data-modal-instant-value": @instant }
|
||||
%div{ id: @id, "data-controller": @data_controller, "data-action": @data_action, "data-modal-instant-value": @instant, **@options }
|
||||
.reveal-modal-bg.fade{ "data-modal-target": "background", "data-action": "click->modal#close" }
|
||||
.reveal-modal.fade.modal-component{ "data-modal-target": "modal", class: @modal_class }
|
||||
= content
|
||||
|
||||
36
spec/components/modal_component_spec.rb
Normal file
36
spec/components/modal_component_spec.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
RSpec.describe ModalComponent, type: :component do
|
||||
it "renders default 'data-action' and 'data-controller'" do
|
||||
render_inline(described_class.new(id: "test-id"))
|
||||
|
||||
expect(page).to have_selector "#test-id"
|
||||
expect(page).to have_selector '[data-controller="modal"]'
|
||||
expect(page).to have_selector '[data-action="keyup@document->modal#closeIfEscapeKey"]'
|
||||
end
|
||||
|
||||
it "accepts html attributes options" do
|
||||
render_inline(described_class.new(id: "test-id", 'data-test': "some data"))
|
||||
|
||||
expect(page).to have_selector "#test-id"
|
||||
expect(page).to have_selector '[data-test="some data"]'
|
||||
end
|
||||
|
||||
it "merges 'data-controller' attribute when present in options" do
|
||||
render_inline(described_class.new(id: "test-id", 'data-controller': "other-controller"))
|
||||
|
||||
expect(page).to have_selector "#test-id"
|
||||
expect(page).to have_selector '[data-controller="modal other-controller"]'
|
||||
end
|
||||
|
||||
it "merges 'data-action' attribute when present in options" do
|
||||
render_inline(described_class.new(id: "test-id", 'data-action': "click->other-controller#test"))
|
||||
|
||||
expect(page).to have_selector "#test-id"
|
||||
expect(page).to have_selector(
|
||||
'[data-action="keyup@document->modal#closeIfEscapeKey click->other-controller#test"]'
|
||||
)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user