Skip to content

Commit

Permalink
Bug fix for heartcombo#1739. Makes DatetimeInput respect wrapper conf…
Browse files Browse the repository at this point in the history
…iguration for the html5 component.
  • Loading branch information
jamezilla committed Feb 14, 2024
1 parent 8f77f59 commit c0c11c3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 92 deletions.
27 changes: 11 additions & 16 deletions lib/simple_form/inputs/date_time_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class DateTimeInput < Base
def input(wrapper_options = nil)
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)

if use_html5_inputs?
if html5?
@builder.send(:"#{input_type}_field", attribute_name, merged_input_options)
else
@builder.send(:"#{input_type}_select", attribute_name, input_options, merged_input_options)
Expand All @@ -15,25 +15,20 @@ def input(wrapper_options = nil)
private

def label_target
if use_html5_inputs?
attribute_name
else
position = case input_type
when :date, :datetime
date_order = input_options[:order] || I18n.t('date.order')
date_order.first.to_sym
else
:hour
end
return attribute_name if html5?

position = ActionView::Helpers::DateTimeSelector::POSITION[position]
"#{attribute_name}_#{position}i"
position = case input_type
when :date, :datetime
date_order = input_options[:order] || I18n.t('date.order')
date_order.first.to_sym
else
:hour
end
end

def use_html5_inputs?
input_options[:html5]
position = ActionView::Helpers::DateTimeSelector::POSITION[position]
"#{attribute_name}_#{position}i"
end

end
end
end
107 changes: 31 additions & 76 deletions test/inputs/datetime_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,47 @@

# Tests for datetime, date and time inputs when HTML5 compatibility is enabled in the wrapper.
class DateTimeInputWithHtml5Test < ActionView::TestCase
test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enbled' do
with_input_for @user, :created_at, :datetime, html5: true
assert_select 'input[type="datetime-local"]'
end

test 'input generates a datetime select for datetime attributes' do
test 'input generates a datetime input for datetime attributes' do
with_input_for @user, :created_at, :datetime

assert_select 'select.datetime'
end

test 'input generates a date input for date attributes if HTML5 compatibility is explicitly enbled' do
with_input_for @user, :born_at, :date, html5: true

assert_select 'input[type="date"]'
assert_select 'input[type="datetime-local"]'
end

test 'input generates a date select for date attributes' do
test 'input generates a date input for date attributes' do
with_input_for @user, :born_at, :date

assert_select 'select.date'
end

test 'input generates a time input for time attributes if HTML5 compatibility is explicitly enbled' do
with_input_for @user, :delivery_time, :time, html5: true

assert_select 'input[type="time"]'
assert_select 'input[type="date"]'
end

test 'input generates a time select for time attributes' do
test 'input generates a time input for time attributes' do
with_input_for @user, :delivery_time, :time

assert_select 'select.time'
assert_select 'input[type="time"]'
end

test 'input generates required html attribute' do
with_input_for @user, :delivery_time, :time, required: true, html5: true
with_input_for @user, :delivery_time, :time, required: true
assert_select 'input.required'
assert_select 'input[required]'
end

test 'input has an aria-required html attribute' do
with_input_for @user, :delivery_time, :time, required: true, html5: true
with_input_for @user, :delivery_time, :time, required: true
assert_select 'input[aria-required=true]'
end

test 'label points to attribute name' do
with_input_for :project, :created_at, :date
assert_select 'label[for=project_created_at]'
end

end

# Tests for datetime, date and time inputs when HTML5 compatibility is enabled in the wrapper.
# Tests for datetime, date and time inputs when HTML5 compatibility is disabled in the wrapper.
class DateTimeInputWithoutHtml5Test < ActionView::TestCase

test 'input generates a datetime select by default for datetime attributes' do
swap_wrapper do
with_input_for @user, :created_at, :datetime
1.upto(5) do |i|
assert_select "form select.datetime#user_created_at_#{i}i"
end
with_input_for @user, :created_at, :datetime, html5: false
1.upto(5) do |i|
assert_select "form select.datetime#user_created_at_#{i}i"
end
end

Expand All @@ -72,26 +58,17 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
assert_select 'select.datetime option', 'dia'
end

test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enabled' do
swap_wrapper do
with_input_for @user, :created_at, :datetime, html5: true
assert_select 'input[type="datetime-local"]'
end
end

test 'input generates a date select for date attributes' do
swap_wrapper do
with_input_for @user, :born_at, :date
assert_select 'select.date#user_born_at_1i'
assert_select 'select.date#user_born_at_2i'
assert_select 'select.date#user_born_at_3i'
assert_no_select 'select.date#user_born_at_4i'
end
with_input_for @user, :born_at, :date, html5: false
assert_select 'select.date#user_born_at_1i'
assert_select 'select.date#user_born_at_2i'
assert_select 'select.date#user_born_at_3i'
assert_no_select 'select.date#user_born_at_4i'
end

test 'input is able to pass options to date select' do
with_input_for @user, :born_at, :date, as: :date, html5: false,
disabled: true, prompt: { year: 'ano', month: 'mês', day: 'dia' }
disabled: true, prompt: { year: 'ano', month: 'mês', day: 'dia' }

assert_select 'select.date[disabled=disabled]'
assert_select 'select.date option', 'ano'
Expand All @@ -104,23 +81,13 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
assert_select "select.date option[value='#{Date.today.year}'][selected=selected]"
end

test 'input generates a date input for date attributes if HTML5 compatibility is explicitly enabled' do
swap_wrapper do
with_input_for @user, :born_at, :date, html5: true

assert_select 'input[type="date"]'
end
end

test 'input generates a time select for time attributes' do
swap_wrapper do
with_input_for @user, :delivery_time, :time
assert_select 'input[type=hidden]#user_delivery_time_1i'
assert_select 'input[type=hidden]#user_delivery_time_2i'
assert_select 'input[type=hidden]#user_delivery_time_3i'
assert_select 'select.time#user_delivery_time_4i'
assert_select 'select.time#user_delivery_time_5i'
end
with_input_for @user, :delivery_time, :time, html5: false
assert_select 'input[type=hidden]#user_delivery_time_1i'
assert_select 'input[type=hidden]#user_delivery_time_2i'
assert_select 'input[type=hidden]#user_delivery_time_3i'
assert_select 'select.time#user_delivery_time_4i'
assert_select 'select.time#user_delivery_time_5i'
end

test 'input is able to pass options to time select' do
Expand All @@ -132,14 +99,6 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
assert_select 'select.time option', 'minuto'
end

test 'input generates a time input for time attributes if HTML5 compatibility is explicitly enabled' do
swap_wrapper do
with_input_for @user, :delivery_time, :time, html5: true

assert_select 'input[type="time"]'
end
end

test 'label uses i18n to get target for date input type' do
store_translations(:en, date: { order: %w[month day year] }) do
with_input_for :project, :created_at, :date, html5: false
Expand Down Expand Up @@ -169,8 +128,4 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
assert_select 'label[for=project_created_at_4i]'
end

test 'label points to attribute name if HTML5 compatibility is explicitly enabled' do
with_input_for :project, :created_at, :date, html5: true
assert_select 'label[for=project_created_at]'
end
end

0 comments on commit c0c11c3

Please sign in to comment.