Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the HTML id in to the select. #992

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions spec/lucky/ext/select_helpers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@ end
describe Lucky::SelectHelpers do
it "renders select" do
view.render_select(form.company_id).html.to_s.should eq <<-HTML
<select name="company:company_id"></select>
<select id="company_company_id" name="company:company_id"></select>
HTML

view.render_disabled_select(form.company_id).html.to_s.should eq <<-HTML
<select name="company:company_id" disabled></select>
<select id="company_company_id" name="company:company_id" disabled></select>
HTML
end

it "renders multi-select" do
view.render_multi_select(form.tags).html.to_s.should eq <<-HTML
<select name="company:tags[]" multiple></select>
<select id="company_tags_0" name="company:tags[]" multiple></select>
HTML

view.render_disabled_multi_select(form.tags).html.to_s.should eq <<-HTML
<select name="company:tags[]" multiple disabled></select>
<select id="company_tags_0" name="company:tags[]" multiple disabled></select>
HTML
end

Expand Down
12 changes: 10 additions & 2 deletions src/lucky/ext/select_helpers.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Lucky::SelectHelpers
def select_input(field : Avram::PermittedAttribute, attrs : Array(Symbol) = [] of Symbol, **html_options, &) : Nil
select_tag attrs, merge_options(html_options, {"name" => input_name(field)}) do
select_tag attrs, merge_options(html_options, {"id" => input_id(field), "name" => input_name(field)}) do
yield
end
end

def multi_select_input(field : Avram::PermittedAttribute(Array), attrs : Array(Symbol) = [] of Symbol, **html_options, &) : Nil
merged_attrs = [:multiple].concat(attrs)
select_tag merged_attrs, merge_options(html_options, {"name" => input_name(field)}) do
select_tag merged_attrs, merge_options(html_options, {"id" => input_id(field), "name" => input_name(field)}) do
yield
end
end
Expand Down Expand Up @@ -44,6 +44,14 @@ module Lucky::SelectHelpers
option(label, value: "")
end

private def input_id(field)
"#{field.param_key}_#{field.name}"
end

private def input_id(field : Avram::PermittedAttribute(Array))
"#{field.param_key}_#{field.name}_#{array_id_counter[field.name]}"
end

private def input_name(field)
"#{field.param_key}:#{field.name}"
end
Expand Down