Skip to content
Open
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: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,18 @@ 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 <option></option>.
# Select options can be [value, label] tuples, single values, hashes, or nil
# for a blank option. Pass nil first to prepend a blank <option></option>. 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(
nil,
["junior", "Junior"],
["mid", "Mid-level"],
["senior", "Senior"],
["lead", "Lead"]
["lead", "Lead", {"data-value": "something lead relevant"}]
)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/superform/rails/choices/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/superform/rails/components/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/superform/rails/components/select_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<select id="role_ids" name="role_ids">' \
'<option value="1" data-arg="some-admin-value">Admin</option>' \
'<option value="2" disabled>Editor</option>' \
'<option value="3">Viewer</option>' \
'</select>'
)
end
end

describe 'with multiple: true' do
let(:component) do
described_class.new(
Expand Down