diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b0c659..b928556 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.8.3 * Fix rails hooking for version 7.1. [#327](https://github.com/bogdan/datagrid/issues/327) +* Fix formatting of value for `date` and `datetime-local` input types ## 1.8.2 diff --git a/lib/datagrid/form_builder.rb b/lib/datagrid/form_builder.rb index d5a26eb..d3c6a3f 100644 --- a/lib/datagrid/form_builder.rb +++ b/lib/datagrid/form_builder.rb @@ -28,6 +28,10 @@ def datagrid_filter_input(attribute_or_filter, **options) filter = datagrid_get_filter(attribute_or_filter) value = object.filter_value_as_string(filter) type = options[:type]&.to_sym + if options.has_key?(:value) && options[:value].nil? && [:"datetime-local", :"date"].include?(type) + # https://github.com/rails/rails/pull/53387 + options[:value] = "" + end if type == :"datetime-local" datetime_local_field filter.name, **options elsif type == :"date" diff --git a/spec/datagrid/form_builder_spec.rb b/spec/datagrid/form_builder_spec.rb index 56c9423..c374330 100644 --- a/spec/datagrid/form_builder_spec.rb +++ b/spec/datagrid/form_builder_spec.rb @@ -124,6 +124,15 @@ class MyTemplate it { should equal_to_dom( '<input type="datetime-local" class="created_at date_time_filter" value="2024-01-01T09:25:15" name="report[created_at]" id="report_created_at"/>' )} + + context "nil value option" do + let(:_filter_options) do + { value: nil } + end + it { should equal_to_dom( + '<input type="datetime-local" value="" class="created_at date_time_filter" name="report[created_at]" id="report_created_at"/>' + )} + end end context "type is date" do diff --git a/spec/support/matchers.rb b/spec/support/matchers.rb index 38b123b..a0ae6cd 100644 --- a/spec/support/matchers.rb +++ b/spec/support/matchers.rb @@ -20,7 +20,7 @@ def matches?(text) end def normalize(text) - Nokogiri::HTML::DocumentFragment.parse(force_encoding(text.split("\n").map(&:strip).join(""))).to_s + Nokogiri::HTML::DocumentFragment.parse(text.split("\n").map(&:strip).join("")).to_s end def failure_message @@ -30,12 +30,6 @@ def failure_message def description "equal to dom #{@expectation[0..20]}" end - - private - - def force_encoding(text) - "1.9.3".respond_to?(:force_encoding) ? text.clone.force_encoding("UTF-8") : text - end end