From 79e88217df8318cd2f31f526a6502057bf654671 Mon Sep 17 00:00:00 2001 From: Enzo Haguiwara Date: Thu, 3 Aug 2023 03:25:39 -0300 Subject: [PATCH] Add admin ui/forms/switch component --- .../ui/forms/switch/component.rb | 47 +++++++++++++++++++ .../ui/forms/switch/component_preview.rb | 34 ++++++++++++++ .../component_preview/overview.html.erb | 33 +++++++++++++ .../ui/forms/switch/component_spec.rb | 9 ++++ 4 files changed, 123 insertions(+) create mode 100644 admin/app/components/solidus_admin/ui/forms/switch/component.rb create mode 100644 admin/spec/components/previews/solidus_admin/ui/forms/switch/component_preview.rb create mode 100644 admin/spec/components/previews/solidus_admin/ui/forms/switch/component_preview/overview.html.erb create mode 100644 admin/spec/components/solidus_admin/ui/forms/switch/component_spec.rb diff --git a/admin/app/components/solidus_admin/ui/forms/switch/component.rb b/admin/app/components/solidus_admin/ui/forms/switch/component.rb new file mode 100644 index 00000000000..d233c2adf7c --- /dev/null +++ b/admin/app/components/solidus_admin/ui/forms/switch/component.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +class SolidusAdmin::UI::Forms::Switch::Component < SolidusAdmin::BaseComponent + SIZES = { + s: ' + w-8 h-5 + + after:w-4 after:h-4 + after:top-0.5 after:left-0.5 + + active:after:w-4 + after:checked:left-[1.875rem] + ', + m: ' + w-10 h-6 + + after:w-5 after:h-5 + after:top-0.5 after:left-0.5 + + active:after:w-5 + after:checked:left-[2.375rem] + ', + }.freeze + + def initialize(size: :m, **attributes) + @size = size + @attributes = attributes + end + + def call + tag.input( + type: 'checkbox', + class: " + #{SIZES.fetch(@size)} + appearance-none outline-0 cursor-pointer bg-gray-200 inline-block rounded-full relative + + after:content-[''] after:absolute after:bg-white + after:duration-300 after:rounded-full after:checked:-translate-x-full + + hover:bg-gray-300 + disabled:opacity-40 disabled:cursor-not-allowed + checked:bg-gray-500 checked:hover:bg-gray-700 + ", + **@attributes, + ) + end +end diff --git a/admin/spec/components/previews/solidus_admin/ui/forms/switch/component_preview.rb b/admin/spec/components/previews/solidus_admin/ui/forms/switch/component_preview.rb new file mode 100644 index 00000000000..c16c7f7887e --- /dev/null +++ b/admin/spec/components/previews/solidus_admin/ui/forms/switch/component_preview.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +# @component "ui/forms/switch" +class SolidusAdmin::UI::Forms::Switch::ComponentPreview < ViewComponent::Preview + include SolidusAdmin::Preview + + # **With a form builder** + # + # The switch component renders a standalone switch input. + # If the id attribute is not filled, it will be set as a random string. + # + # ```erb + # <%= form_for @product do |form| %> + # ... + # <%= render component('ui/forms/switch').new( + # id: "#{form.object_name}-switch", + # checked: form.object.accept_tos, + # ) %> + # ... + # <% end %> + # ``` + # + + def overview + render_with_template + end + + # @param size select { choices: [s, m] } + # @param checked toggle + # @param disabled toggle + def playground(size: :m, checked: false, disabled: false) + render current_component.new(id: 1, size: size.to_sym, checked: checked, disabled: disabled) + end +end diff --git a/admin/spec/components/previews/solidus_admin/ui/forms/switch/component_preview/overview.html.erb b/admin/spec/components/previews/solidus_admin/ui/forms/switch/component_preview/overview.html.erb new file mode 100644 index 00000000000..d6b9ba75f8b --- /dev/null +++ b/admin/spec/components/previews/solidus_admin/ui/forms/switch/component_preview/overview.html.erb @@ -0,0 +1,33 @@ +
+ + + + <% current_component::SIZES.keys.each do |size| %> + + <% end %> + + + + <% current_component::SIZES.keys.each do |size| %> + <% %i[default disabled].each do |state| %> + + <% end %> + <% end %> + + <% %i[off on].each do |checked| %> + + + <% current_component::SIZES.keys.each do |size| %> + <% %i[default disabled].each do |state| %> + <% cell_id = SecureRandom.uuid %> + + <% end %> + <% end %> + + <% end %> +
<%= size.to_s.humanize %>
<%= state.to_s.humanize %>
<%= checked.to_s.humanize %> +
+ <%= render current_component.new(id: cell_id, size: size, checked: checked == :on, disabled: state == :disabled) %> +
+
+
diff --git a/admin/spec/components/solidus_admin/ui/forms/switch/component_spec.rb b/admin/spec/components/solidus_admin/ui/forms/switch/component_spec.rb new file mode 100644 index 00000000000..c90239a1bde --- /dev/null +++ b/admin/spec/components/solidus_admin/ui/forms/switch/component_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require "spec_helper" + +RSpec.describe SolidusAdmin::UI::Forms::Switch::Component, type: :component do + it "renders the overview preview" do + render_preview(:overview) + end +end