Skip to content

Commit

Permalink
TEST: Allow adding custom modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
davidalejandroaguilar committed Sep 3, 2024
1 parent 5e4b83c commit 2baf96f
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/phlexy_ui/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def generate_classes!(
modifiers_map: {}
)
ClassList.new(
component: self,
component_html_class:,
base_modifiers:,
options:,
Expand Down
25 changes: 22 additions & 3 deletions lib/phlexy_ui/class_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ module PhlexyUI
class ClassList
RESPONSIVE_PREFIXES = %i[sm md lg].freeze

def initialize(component_html_class: nil, base_modifiers: [], options: {}, modifiers_map: {})
def initialize(
component:,
component_html_class: nil,
base_modifiers: [],
options: {},
modifiers_map: {}
)
@component_html_class = component_html_class
@base_modifiers = base_modifiers
@options = options
Expand All @@ -22,10 +28,17 @@ def to_a

attr_reader :component_html_class, :base_modifiers, :options, :modifiers_map

def selected_base_modifiers_classes
def selected_base_modifiers
base_modifiers.select { |modifier| modifiers_map.key?(modifier) }
end

def selected_custom_modifiers
base_modifiers.select do |modifier|
PhlexyUI.configuration.modifiers.key?(modifier, component:) ||
PhlexyUI.configuration.modifiers.key?(modifier)
end
end

def add_component_class(classes)
return unless component_html_class

Expand All @@ -35,7 +48,13 @@ def add_component_class(classes)
def add_selected_modifiers_classes(classes)
classes.concat(
html_classes_for_modifiers(
selected_base_modifiers_classes
selected_base_modifiers
)
)

classes.concat(
html_classes_for_modifiers(
selected_custom_modifiers
)
)
end
Expand Down
29 changes: 29 additions & 0 deletions lib/phlexy_ui/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,41 @@ def configuration
@configuration ||= Configuration.new
end

class Modifiers
def initialize
@modifiers = {}
end

def add(modifier, classes:, component: nil)
@modifiers[component] ||= {}
@modifiers[component][modifier] = classes
end

def remove(modifier, component: nil)
@modifiers[component] ||= {}
@modifiers[component]&.delete(modifier)
end

def key?(modifier, component: nil)
@modifiers[component]&.key?(modifier)
end

def reset
@modifiers = {}
true
end
end

class Configuration
attr_accessor :prefix

def initialize
@prefix = nil
end

def modifiers
@modifiers ||= Modifiers.new
end
end
end
end
120 changes: 120 additions & 0 deletions spec/lib/phlexy_ui/card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,81 @@
end
end

describe "custom modifiers" do
context "when there's a custom modifier for the component" do
around do |example|
PhlexyUI.configure do |config|
config.modifiers.add(
:my_modifier,
component: PhlexyUI::Card,
classes: "w-96 shadow-xl"
)
end

example.run

PhlexyUI.configure do |config|
config.modifiers.remove(:my_modifier, component: PhlexyUI::Card)
end
end

subject(:output) do
render described_class.new(:my_modifier)
end

it "renders it correctly" do
expected_html = html <<~HTML
<section class="card w-96 shadow-xl"></section>
HTML

expect(output).to eq(expected_html)
end
end

context "when there's a custom modifier without a specific component" do
around do |example|
PhlexyUI.configure do |config|
config.modifiers.add(
:my_modifier,
classes: "w-96 shadow-xl"
)
end

example.run

PhlexyUI.configure do |config|
config.modifiers.remove(:my_modifier)
end
end

subject(:output) do
render described_class.new(:my_modifier)
end

it "renders it correctly" do
expected_html = html <<~HTML
<section class="card w-96 shadow-xl"></section>
HTML

expect(output).to eq(expected_html)
end
end

context "when there's no custom modifier" do
subject(:output) do
render described_class.new(:my_modifier)
end

it "does not render it" do
expected_html = html <<~HTML
<section class="card"></section>
HTML

expect(output).to eq(expected_html)
end
end
end

describe "responsiveness" do
%i[sm md lg].each do |viewport|
context "when given an :#{viewport} responsive option as a single argument" do
Expand Down Expand Up @@ -181,6 +256,51 @@
expect(output).to eq(expected_html)
end
end

context "when there are custom modifiers" do
around do |example|
PhlexyUI.configure do |config|
config.modifiers.add(
:my_modifier,
component: PhlexyUI::Card,
classes: "w-96 shadow-xl"
)

config.modifiers.add(
:my_other_modifier,
classes: "p-4 rounded-box"
)
end

example.run

PhlexyUI.configure do |config|
config.modifiers.remove(:my_modifier, component: PhlexyUI::Card)
config.modifiers.remove(:my_other_modifier)
end
end

subject(:output) do
render described_class.new(
:my_modifier,
viewport => :my_other_modifier
)
end

it "renders it separately with a responsive prefix" do
expected_html = html <<~HTML
<section class="
card
w-96
shadow-xl
#{viewport}:rounded-box
#{viewport}:p-4>
</section>
HTML

expect(output).to eq(expected_html)
end
end
end
end

Expand Down

0 comments on commit 2baf96f

Please sign in to comment.