Skip to content

Commit

Permalink
Fix select with nil option (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlfonsoUceda authored and jodosha committed Nov 29, 2017
1 parent 3fe773b commit d07bf17
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hanami-helpers.gemspec
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'hanami-utils', '~> 1.1'

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'dry-struct', '~> 0.3'
spec.add_development_dependency 'rake', '~> 12'
spec.add_development_dependency 'rspec', '~> 3.7'
spec.add_development_dependency 'dry-struct', '~> 0.3'
end
9 changes: 6 additions & 3 deletions lib/hanami/helpers/form_helper/form_builder.rb
Expand Up @@ -1206,8 +1206,9 @@ def select(name, values, attributes = {}) # rubocop:disable Metrics/AbcSize, Met
super(attributes) do
option(prompt) unless prompt.nil?

already_selected = nil
values.each do |content, value|
if _select_option_selected?(value, selected, _value(name), attributes[:multiple])
if (attributes[:multiple] || !already_selected) && (already_selected = _select_option_selected?(value, selected, _value(name), attributes[:multiple]))
option(content, { value: value, selected: SELECTED }.merge(options))
else
option(content, { value: value }.merge(options))
Expand Down Expand Up @@ -1547,8 +1548,10 @@ def _select_input_name(name, multiple)
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def _select_option_selected?(value, selected, input_value, multiple)
value == selected || (multiple && (selected.is_a?(Array) && selected.include?(value))) ||
value.to_s == input_value.to_s || (multiple && (input_value.is_a?(Array) && input_value.include?(value)))
value == selected ||
(multiple && (selected.is_a?(Array) && selected.include?(value))) ||
value.to_s == input_value.to_s ||
(multiple && (input_value.is_a?(Array) && input_value.include?(value)))
end
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
Expand Down
28 changes: 28 additions & 0 deletions spec/unit/hanami/helpers/form_helper_spec.rb
Expand Up @@ -2427,6 +2427,34 @@
expect(actual).to include(%(<select name="book[store]" id="book-store">\n<option value="it" selected="selected">Italy</option>\n<option value="us">United States</option>\n</select>))
end
end

describe 'with nil as a value' do
let(:option_values) { Hash['Italy' => 'it', 'United States' => 'us', 'N/A' => nil] }

it "sets nil option as selected by default" do
actual = view.form_for(:book, action) do
select :store, option_values
end.to_s

expect(actual).to include(%(<select name="book[store]" id="book-store">\n<option value="it">Italy</option>\n<option value="us">United States</option>\n<option value="" selected="selected">N&#x2F;A</option>\n</select>))
end

it "set as selected the option with nil value" do
actual = view.form_for(:book, action) do
select :store, option_values, options: { selected: nil }
end.to_s

expect(actual).to include(%(<select name="book[store]" id="book-store">\n<option value="it">Italy</option>\n<option value="us">United States</option>\n<option value="" selected="selected">N&#x2F;A</option>\n</select>))
end

it "set as selected the option with a value" do
actual = view.form_for(:book, action) do
select :store, option_values, options: { selected: 'it' }
end.to_s

expect(actual).to include(%(<select name="book[store]" id="book-store">\n<option value="it" selected="selected">Italy</option>\n<option value="us">United States</option>\n<option value="">N&#x2F;A</option>\n</select>))
end
end
end

describe '#datalist' do
Expand Down

0 comments on commit d07bf17

Please sign in to comment.