Skip to content

Commit

Permalink
Fix attribute value duplication (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
akodkod authored Aug 4, 2024
1 parent d210ae0 commit 74c9811
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/phlex_ui/attribute_merger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module PhlexUI
class AttributeMerger
attr_reader :default_attrs, :user_attrs
OVERRIDE_KEY = "!".freeze

def initialize(default_attrs, user_attrs)
@default_attrs = flatten_hash(default_attrs)
Expand All @@ -12,8 +13,8 @@ def initialize(default_attrs, user_attrs)
# ex: if default_attrs = { class: "text-right" }, user_attrs = { class!: "text-left" }
# the result will be { class: "text-left }
def call
merged_attrs = merge_hashes(default_attrs, user_attrs)
mix(merged_attrs, user_attrs)
merged_attrs = merge_hashes(default_attrs, non_override_attrs)
mix(merged_attrs, override_attrs)
end

private
Expand All @@ -40,6 +41,18 @@ def mix(*args)
end
end

def override_attrs
user_attrs.select do |key, value|
key.to_s[-1] == OVERRIDE_KEY
end
end

def non_override_attrs
user_attrs.reject do |key, value|
key.to_s[-1] == OVERRIDE_KEY
end
end

def flatten_hash(hash, parent_key = "", result_hash = {})
hash.each do |key, value|
new_key = parent_key.empty? ? key : :"#{parent_key}_#{key}"
Expand Down
2 changes: 1 addition & 1 deletion test/phlex_ui/input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_render_with_all_items
PhlexUI.Input(type: "email", placeholder: "Email")
end

assert_match(/Email/, output)
assert_match(/placeholder="Email"/, output)
end

def test_no_destructive_classes_when_error_absent
Expand Down

0 comments on commit 74c9811

Please sign in to comment.