Skip to content

Commit

Permalink
Support search field type. Fixes #330.
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Jan 8, 2025
1 parent dd06294 commit 7ffd927
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Changelog

## [2.0.1]

* Fixed `search` field type support [#330](https://github.com/bogdan/datagrid/issues/330)

``` ruby
class UsersGrid
scope { User }

filter(
:query, :string, input_options: { type: "search" }
) do |value, scope|
scope.magic_search(value)
end
end
```

Renders filter as:

``` html
<input type="search" name="users_grid[query]" id="users_grid_query"/>
```

## [2.0.0]

Version 2 is a major update implementing a lot of major improvements
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ group :development do
gem "rubocop", "~> 1.68"
gem "rubocop-yard", "~> 0.9.3", require: false
gem "sequel"
gem "sqlite3", "~> 1.7.0"
gem "sqlite3", "~> 2.1.0"

group :mongo do
gem "bson"
Expand Down
26 changes: 18 additions & 8 deletions lib/datagrid/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

module Datagrid
module FormBuilder
# @!visibility private
TYPE_METHOD_MAP = {
search: :search_field,
textarea: :text_area,
hidden: :hidden_field,
number: :number_field,
}.with_indifferent_access

# @param filter_or_attribute [Datagrid::Filters::BaseFilter, String, Symbol] filter object or filter name
# @param options [Hash] options of rails form input helper
# @return [String] a form input html for the corresponding filter name
Expand Down Expand Up @@ -56,16 +64,12 @@ def datagrid_filter_input(attribute_or_filter, **options, &block)
datetime_local_field filter.name, **options, &block
when :date
date_field filter.name, **options, &block
when :textarea
text_area filter.name, value: object.filter_value_as_string(filter), **options, &block
when :checkbox
value = options.fetch(:value, 1).to_s
options = { checked: true, **options } if filter.enum_checkboxes? && enum_checkbox_checked?(filter, value)
if filter.enum_checkboxes? && enum_checkbox_checked?(filter, value) && !options.key?(:checked)
options[:checked] = true
end
check_box filter.name, options, value
when :hidden
hidden_field filter.name, **options
when :number
number_field filter.name, **options
when :select
select(
filter.name,
Expand All @@ -80,7 +84,13 @@ def datagrid_filter_input(attribute_or_filter, **options, &block)
&block
)
else
text_field filter.name, value: object.filter_value_as_string(filter), **options, &block
public_send(
TYPE_METHOD_MAP[type] || :text_field,
filter.name,
value: object.filter_value_as_string(filter),
**options,
&block
)
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/datagrid/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ class MyTemplate
}
end

context "string filter type is search" do
let(:_filter) { :name }
let(:_grid) do
test_grid_filter(:name, :string, input_options: { type: :search })
end

it {
expect(subject).to equal_to_dom(
'<input type="search" name="report[name]" id="report_name"/>',
)
}
end

context "datetime filter type is text" do
let(:_filter) { :created_at }
let(:_grid) do
Expand Down

0 comments on commit 7ffd927

Please sign in to comment.