Skip to content

Commit

Permalink
Fix nil value for date/time input
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Oct 20, 2024
1 parent 8f87157 commit b036189
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/datagrid/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions spec/datagrid/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions spec/support/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down

0 comments on commit b036189

Please sign in to comment.