From 7ffd9275166384049486c0b694323937fd753238 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Wed, 8 Jan 2025 15:48:03 +0100 Subject: [PATCH] Support search field type. Fixes #330. --- CHANGELOG.md | 22 ++++++++++++++++++++++ Gemfile | 2 +- lib/datagrid/form_builder.rb | 26 ++++++++++++++++++-------- spec/datagrid/form_builder_spec.rb | 13 +++++++++++++ 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2614e10..c9860ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 + +``` + ## [2.0.0] Version 2 is a major update implementing a lot of major improvements diff --git a/Gemfile b/Gemfile index 71afccb..2547332 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/lib/datagrid/form_builder.rb b/lib/datagrid/form_builder.rb index 4910621..858e3cf 100644 --- a/lib/datagrid/form_builder.rb +++ b/lib/datagrid/form_builder.rb @@ -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 @@ -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, @@ -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 diff --git a/spec/datagrid/form_builder_spec.rb b/spec/datagrid/form_builder_spec.rb index 7b097c7..b8d3988 100644 --- a/spec/datagrid/form_builder_spec.rb +++ b/spec/datagrid/form_builder_spec.rb @@ -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( + '', + ) + } + end + context "datetime filter type is text" do let(:_filter) { :created_at } let(:_grid) do