From 929b878266a5c91ff62dc768bafe1adab0d19452 Mon Sep 17 00:00:00 2001 From: bryanadvisory Date: Wed, 8 Jul 2026 07:26:01 -0600 Subject: [PATCH] Adds support for kwargs for select options when using tuples --- README.md | 8 +++++--- lib/superform/rails/choices/mapper.rb | 2 ++ lib/superform/rails/components/select.rb | 5 +++-- spec/superform/rails/components/select_spec.rb | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 14d1117..5330af6 100644 --- a/README.md +++ b/README.md @@ -366,8 +366,10 @@ class JobPostingForm < Components::Form end end - # Select options can be [value, label] pairs, single values, hashes, or nil - # for a blank option. Pass nil first to prepend a blank . + # Select options can be [value, label] tuples, single values, hashes, or nil + # for a blank option. Pass nil first to prepend a blank . Optionally, + # you can pass a third value inside the option tuple with an argument hash for your + # options div do Field(:experience_level).label Field(:experience_level).select( @@ -375,7 +377,7 @@ class JobPostingForm < Components::Form ["junior", "Junior"], ["mid", "Mid-level"], ["senior", "Senior"], - ["lead", "Lead"] + ["lead", "Lead", {"data-value": "something lead relevant"}] ) end diff --git a/lib/superform/rails/choices/mapper.rb b/lib/superform/rails/choices/mapper.rb index bee8c88..a7bfd19 100644 --- a/lib/superform/rails/choices/mapper.rb +++ b/lib/superform/rails/choices/mapper.rb @@ -19,6 +19,8 @@ def each(&options) hash.each { |id, value| options.call id, value } in id, value options.call id, value + in id, value, args + options.call id, value, args in value options.call value, value.to_s end diff --git a/lib/superform/rails/components/select.rb b/lib/superform/rails/components/select.rb index 5ea3967..8cd51c7 100644 --- a/lib/superform/rails/components/select.rb +++ b/lib/superform/rails/components/select.rb @@ -44,11 +44,12 @@ def view_template(&block) def options(*collection) # Handle both single values and arrays (for multiple selects) selected_values = Array(field.value) - map_options(collection).each do |key, value| + map_options(collection).each do |key, value, args = {}| if key.nil? blank_option else - option(selected: selected_values.include?(key), value: key) { value } + option_args = mix({selected: selected_values.include?(key), value: key}, args) + option(**option_args) { value } end end end diff --git a/spec/superform/rails/components/select_spec.rb b/spec/superform/rails/components/select_spec.rb index ad485b3..e6b9863 100644 --- a/spec/superform/rails/components/select_spec.rb +++ b/spec/superform/rails/components/select_spec.rb @@ -40,6 +40,22 @@ end end + describe 'with options args' do + let(:options) { [[1, 'Admin', {data: {arg: "some-admin-value"}}], [2, 'Editor', {disabled: true}], [3, 'Viewer']] } + + subject { render(component) } + + it 'passes the arguments to the appropriate options' do + expect(subject).to eq( + '' + ) + end + end + describe 'with multiple: true' do let(:component) do described_class.new(