diff --git a/app/components/itacomp/alert_component.rb b/app/components/itacomp/alert_component.rb index 28256bb..2a4a9b3 100644 --- a/app/components/itacomp/alert_component.rb +++ b/app/components/itacomp/alert_component.rb @@ -27,7 +27,7 @@ module Itacomp # @example with other key params # <%= render ItacompAlertComponent.new(id: 'my-id', data: {test: 'test'}) %> # - class AlertComponent < BaseComponent + class AlertComponent < ApplicationComponent # @param [String,Sym] :type of alert, default 'primary' # @param [Boolean] :close if true is added close button # @param [String] :class add other class after "alsert alert-#{type}" classes diff --git a/app/components/itacomp/application_component.rb b/app/components/itacomp/application_component.rb new file mode 100644 index 0000000..ef2d01e --- /dev/null +++ b/app/components/itacomp/application_component.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Itacomp + # Common method and incluion for Itacomp components + # + # ==== Inclusion + # * include ApplicationHelper + # * include CommonHelper + class ApplicationComponent < ViewComponent::Base + include ApplicationHelper + include CommonHelper + + # retur value if key is true + # + # ==== Options + # Value [Any], mandatory, return value if key is true + # Key [Boolean], if true, return value + def if_key(value, key) + value if key == true + end + end +end diff --git a/app/components/itacomp/avatar_component.rb b/app/components/itacomp/avatar_component.rb index 6b6970b..3de38bd 100644 --- a/app/components/itacomp/avatar_component.rb +++ b/app/components/itacomp/avatar_component.rb @@ -34,7 +34,7 @@ module Itacomp # With other options # <%= render itacomp::avatar(id: "my_avatar", data: {turbo_frame: 'main'}).new.with_content('a') %> #
a
- class AvatarComponent < BaseComponent + class AvatarComponent < ApplicationComponent # Initialize avatar component # # ==== Options diff --git a/app/components/itacomp/base_component.rb b/app/components/itacomp/base_component.rb deleted file mode 100644 index b15c4d4..0000000 --- a/app/components/itacomp/base_component.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -module Itacomp - class BaseComponent < ViewComponent::Base - include ApplicationHelper - include CommonHelper - end -end diff --git a/app/components/itacomp/dimmer_component.rb b/app/components/itacomp/dimmer_component.rb index fbd92af..6ebe8cc 100644 --- a/app/components/itacomp/dimmer_component.rb +++ b/app/components/itacomp/dimmer_component.rb @@ -72,7 +72,7 @@ module Itacomp # #

my dimmable content

# - class DimmerComponent < BaseComponent + class DimmerComponent < ApplicationComponent renders_one :inner_content # Initialize dimmer component # diff --git a/app/components/itacomp/notification_component.rb b/app/components/itacomp/notification_component.rb index f79da83..64c58ea 100644 --- a/app/components/itacomp/notification_component.rb +++ b/app/components/itacomp/notification_component.rb @@ -9,7 +9,7 @@ module Itacomp # Mo dismissable and no show # <%= render Itacomp::NotificationComponent.new(title: 'test', dismissable: false, show: false) %> # - class NotificationComponent < BaseComponent + class NotificationComponent < ApplicationComponent # Initialize notification component # # ==== Options diff --git a/app/components/itacomp/overlay_component.rb b/app/components/itacomp/overlay_component.rb new file mode 100644 index 0000000..7a81a8e --- /dev/null +++ b/app/components/itacomp/overlay_component.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Itacomp + # make a strcture for [overlay] component](https://italia.github.io/bootstrap-italia/docs/componenti/ove/) + # ==== Example + # With only required img + # <%= render Itacomp::OverlayComponent.new('test.jpg') %> + #
+ # With text + # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text') %> + #
My text
+ # With black background + # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', black: true) %> + #
My text
+ # With full height + # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', full: true) %> + #
My text
+ # With icon and full height + # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', icon: 'lock', full: true) %> + #
my text
+ # With content + # <%= render Itacomp::OverlayComponent.new('test.jpg').with_content('My text') %> + #
My text
+ class OverlayComponent < ApplicationComponent + # Initialize Overlay component + # ==== Options + # * img [String], mandatory, default: nil, image url or asset; + # * text [String], default nil, Overlay span content + # * icon [String] default nil if present is add icon content in overlay + # * black [Boolean] default false if true add overlay-black class + # * full [Boolean] default false if true add overlay-panel-fullheight clas + # * **opts each key is delegated as image_tag options. Default: {}, useful for add class or alternate image description + # * yield figcaption content in a span tag + def initialize(img = nil, text: nil, icon: nil, black: false, full: false, **opts) + @opts = opts + @figcaption_opts = { class: [ "overlay-panel", if_key("overlay-panel-fullheight", full), if_key("overlay-black", black), if_key("overlay-icon", icon.present?) ] } + + @text = text + @img = img + @icon = icon + end + end +end diff --git a/app/components/itacomp/overlay_component/overlay_component.html.erb b/app/components/itacomp/overlay_component/overlay_component.html.erb new file mode 100644 index 0000000..44d97cc --- /dev/null +++ b/app/components/itacomp/overlay_component/overlay_component.html.erb @@ -0,0 +1,11 @@ +
+ <%= image_tag @img, **@opts %> + <%= tag.figcaption **@figcaption_opts do %> + <% if @icon.present? %> + <%= content || @text %> + <%= ita_icon @icon %> + <% else %> + <%= content? ? content : @text %> + <% end %> + <% end %> +
diff --git a/app/components/itacomp/tab_component.rb b/app/components/itacomp/tab_component.rb index b35cf0f..4efc78e 100644 --- a/app/components/itacomp/tab_component.rb +++ b/app/components/itacomp/tab_component.rb @@ -15,7 +15,7 @@ module Itacomp # # # - class TabComponent < ViewComponent::Base + class TabComponent < ApplicationComponent # Initialize tab component # # ==== options diff --git a/app/components/itacomp/turbo_frame_component.rb b/app/components/itacomp/turbo_frame_component.rb index c070496..c9f3443 100644 --- a/app/components/itacomp/turbo_frame_component.rb +++ b/app/components/itacomp/turbo_frame_component.rb @@ -34,7 +34,7 @@ module Itacomp # text content # # text content - class TurboFrameComponent < BaseComponent + class TurboFrameComponent < ApplicationComponent # Initialize TurboFrameComponent # ==== Options # * * [Symbol], each key going as tag option diff --git a/test/components/itacomp/application_component_test.rb b/test/components/itacomp/application_component_test.rb new file mode 100644 index 0000000..2e48edf --- /dev/null +++ b/test/components/itacomp/application_component_test.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "test_helper" + +class Itacomp::ApplicationComponentTest < ViewComponent::TestCase + test "if_key method" do + assert_equal "yes", Itacomp::ApplicationComponent.new.if_key("yes", true) + assert_nil Itacomp::ApplicationComponent.new.if_key("yes", false) + assert_nil Itacomp::ApplicationComponent.new.if_key("yes", "test") + assert_nil Itacomp::ApplicationComponent.new.if_key("yes", nil) + end +end diff --git a/test/components/itacomp/base_component_test.rb b/test/components/itacomp/base_component_test.rb deleted file mode 100644 index b412a4b..0000000 --- a/test/components/itacomp/base_component_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Itacomp::BaseComponentTest < ViewComponent::TestCase - def test_component_renders_something_useful - # assert_equal( - # %(Hello, components!), - # render_inline(BaseComponent.new(message: "Hello, components!")).css("span").to_html - # ) - end -end diff --git a/test/components/itacomp/overlay_component_test.rb b/test/components/itacomp/overlay_component_test.rb new file mode 100644 index 0000000..d23c80f --- /dev/null +++ b/test/components/itacomp/overlay_component_test.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "test_helper" + +class Itacomp::OverlayComponentTest < ViewComponent::TestCase + test "with only image" do + render_inline(Itacomp::OverlayComponent.new("/image.png")) + assert_selector 'figure.overlay-wrapper img[src="/image.png"]' + assert_selector "figure.overlay-wrapper figcaption span", text: nil + end + + test "with text fegcaption description" do + render_inline(Itacomp::OverlayComponent.new("/image.png", text: "test")) + assert_selector "figure.overlay-wrapper figcaption span", text: "test" + end + + test "with black figcaption background" do + render_inline(Itacomp::OverlayComponent.new("/image.png", text: "test", black: true)) + assert_selector "figure.overlay-wrapper figcaption.overlay-black span", text: "test" + end + + test "with icon, fullheight figcaption overlay and visually hidden text description" do + render_inline(Itacomp::OverlayComponent.new("/image.png", text: "test", full: true, icon: "test")) + assert_selector "figure.overlay-wrapper figcaption.overlay-panel-fullheight.overlay-icon span.visually-hidden", text: "test" + assert_selector "figure.overlay-wrapper figcaption.overlay-panel-fullheight.overlay-icon svg.icon use" + end +end