Skip to content

Commit 7e21e2b

Browse files
committed
Support multiple values for a key
1 parent eaae37d commit 7e21e2b

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22
- Fix issue with inherited hook in helper ([#33](https://github.com/avo-hq/class_variants/pull/33))
3+
- Support multiple values for a key ([#33](https://github.com/avo-hq/class_variants/pull/44))
34

45
## 1.1.0 (2025-01-20)
56
- Add support for merging ([#23](https://github.com/avo-hq/class_variants/pull/23))

lib/class_variants/instance.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def render(slot = :default, **overrides)
5353

5454
candidate.each_key do |key|
5555
next if key == :class || key == :slot
56-
match = criteria[key] == candidate[key]
56+
match = array_wrap(candidate[key]).include?(criteria[key])
5757
break unless match
5858
end
5959

@@ -139,5 +139,9 @@ def with_classess_processor(classes)
139139
classes
140140
end
141141
end
142+
143+
def array_wrap(value)
144+
value.is_a?(Array) ? value : [value]
145+
end
142146
end
143147
end

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ alert_classes = ClassVariants.build do
135135
# compound variant
136136
variant type: :button, color: :red, class: "..."
137137

138+
# compound variant with multiple values for a key
139+
variant type: %i[button link], color: :red, class: ""
140+
138141
# defaults
139142
defaults color: :red, type: :button
140143
end
@@ -237,6 +240,9 @@ alert_classes = ClassVariants.build(
237240
# compound variant without slots
238241
variant type: :button, color: :red, class: "..."
239242

243+
# compound variant with multiple values for a key
244+
variant type: %i[button link], color: :red, class: ""
245+
240246
# compound variant with slots
241247
variant type: :button, color: :red do
242248
slot :head, class: "..."

test/multiple_values_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require "test_helper"
2+
3+
class MultipleValuesTest < Minitest::Test
4+
def setup
5+
@cv = ClassVariants.build do
6+
variant color: %i[primary secondary], class: "shadow-sm"
7+
variant color: :primary, class: "bg-blue-500"
8+
variant color: :secondary, class: "bg-purple-500"
9+
variant color: :success, class: "bg-green-500"
10+
end
11+
end
12+
13+
def test_render
14+
assert_equal "shadow-sm bg-blue-500", @cv.render(color: :primary)
15+
assert_equal "shadow-sm bg-purple-500", @cv.render(color: :secondary)
16+
assert_equal "bg-green-500", @cv.render(color: :success)
17+
end
18+
end

0 commit comments

Comments
 (0)