Skip to content

Commit f3e9082

Browse files
committed
Fix missing options in select tags
A small improvement on changes from solidusio#6091 where in Rails 7 calling `tag.method(@tag).parameters.any? { |_type, name| name == :content }` would always return false when @tag == :select, since `tag.select` method's signature has only `*` (:rest) as its parameter, thus not passing any `<option>`s for select tag. Somehow the problem was not being flagged by CI builds, but admin specs failed locally. Instead, use explicit void elements list to understand whether given element is void, therefore does not accept `content`.
1 parent 95a9ea8 commit f3e9082

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

admin/app/components/solidus_admin/base_component.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module SolidusAdmin
77
# BaseComponent is the base class for all components in Solidus Admin.
88
class BaseComponent < ViewComponent::Base
99
include SolidusAdmin::ComponentsHelper
10+
include SolidusAdmin::VoidElementsHelper
1011
include Turbo::FramesHelper
1112

1213
def icon_tag(name, **attrs)

admin/app/components/solidus_admin/ui/forms/input/component.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,24 @@ def call
9393
with_content options_for_select(@attributes.delete(:choices), @attributes.delete(:value))
9494
end
9595

96-
options = {
96+
build_tag
97+
end
98+
99+
private
100+
101+
def build_tag
102+
args = [@tag]
103+
args << content unless void_element?(@tag)
104+
105+
tag.public_send(*args, **tag_options)
106+
end
107+
108+
def tag_options
109+
@tag_options ||= {
97110
"data-controller": stimulus_id,
98111
"data-#{stimulus_id}-custom-validity-value": @error.presence,
99112
"data-action": "#{stimulus_id}#clearCustomValidity",
100113
**@attributes
101114
}
102-
103-
if tag.method(@tag).parameters.any? { |_type, name| name == :content }
104-
tag.public_send(
105-
@tag,
106-
content,
107-
**options
108-
)
109-
else
110-
tag.public_send(
111-
@tag,
112-
**options
113-
)
114-
end
115115
end
116116
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module SolidusAdmin
4+
module VoidElementsHelper
5+
# https://github.com/rails/rails/blob/194d697036c61af0caa66de5659721ded2478ce9/actionview/lib/action_view/helpers/tag_helper.rb#L84
6+
HTML_VOID_ELEMENTS = %i(area base br col embed hr img input keygen link meta source track wbr)
7+
8+
# @param el [Symbol]
9+
def void_element?(el)
10+
HTML_VOID_ELEMENTS.include?(el)
11+
end
12+
end
13+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe SolidusAdmin::VoidElementsHelper, type: :helper do
6+
describe '#void_element?' do
7+
subject { helper.void_element?(element) }
8+
9+
context 'when element is void' do
10+
let(:element) { :input }
11+
12+
it { is_expected.to be true }
13+
end
14+
15+
context 'when element is not void' do
16+
let(:element) { :div }
17+
18+
it { is_expected.to be false }
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)