Skip to content

Commit

Permalink
Fix for heartcombo#1739
Browse files Browse the repository at this point in the history
  • Loading branch information
jamezilla committed Feb 14, 2024
1 parent 8f77f59 commit 2346274
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 51 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
51 changes: 16 additions & 35 deletions test/inputs/datetime_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,37 @@

# 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

end

# Tests for datetime, date and time inputs when HTML5 compatibility is enabled 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
Expand All @@ -73,7 +55,7 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
end

test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enabled' do
swap_wrapper do
swap_wrapper :default, custom_wrapper_with_optional_html5_component do
with_input_for @user, :created_at, :datetime, html5: true
assert_select 'input[type="datetime-local"]'
end
Expand All @@ -91,7 +73,7 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase

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 @@ -105,9 +87,8 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
end

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

assert_select 'input[type="date"]'
end
end
Expand All @@ -125,17 +106,16 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase

test 'input is able to pass options to time select' do
with_input_for @user, :delivery_time, :time, required: true, html5: false,
disabled: true, prompt: { hour: 'hora', minute: 'minuto' }
disabled: true, prompt: { hour: 'hora', minute: 'minuto' }

assert_select 'select.time[disabled=disabled]'
assert_select 'select.time option', 'hora'
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
swap_wrapper :default, custom_wrapper_with_optional_html5_component do
with_input_for @user, :delivery_time, :time, html5: true

assert_select 'input[type="time"]'
end
end
Expand Down Expand Up @@ -173,4 +153,5 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
with_input_for :project, :created_at, :date, html5: true
assert_select 'label[for=project_created_at]'
end

end
8 changes: 8 additions & 0 deletions test/support/misc_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ def custom_wrapper
end
end

def custom_wrapper_with_optional_html5_component
SimpleForm.build tag: :section, class: "custom_wrapper" do |b|
b.optional :html5
b.use :label
b.use :input
end
end

def custom_wrapper_with_wrapped_optional_component
SimpleForm.build tag: :section, class: "custom_wrapper" do |b|
b.wrapper tag: :div, class: 'no_output_wrapper' do |ba|
Expand Down

0 comments on commit 2346274

Please sign in to comment.