diff --git a/CHANGELOG.md b/CHANGELOG.md index 38a7ae0f..027b3c54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Guess input type more carefully. [@sringling](https://github.com/sringling) * Allow custom error on forms without model. [@victorperez](https://github.com/victorperez) * Do not support Ruby < 2.3 anymore. [@gssbzn](https://github.com/gssbzn) +* Add color inout type. [@gssbzn](https://github.com/gssbzn) ### Bug fix * Improve disabled option to input_field. [@betelgeuse](https://github.com/betelgeuse) diff --git a/lib/simple_form/inputs.rb b/lib/simple_form/inputs.rb index 1fbb5361..25cb7cbc 100644 --- a/lib/simple_form/inputs.rb +++ b/lib/simple_form/inputs.rb @@ -10,6 +10,7 @@ module SimpleForm autoload :CollectionInput autoload :CollectionRadioButtonsInput autoload :CollectionSelectInput + autoload :ColorInput autoload :DateTimeInput autoload :FileInput autoload :GroupedCollectionSelectInput diff --git a/lib/simple_form/inputs/color_input.rb b/lib/simple_form/inputs/color_input.rb new file mode 100644 index 00000000..239cd0e7 --- /dev/null +++ b/lib/simple_form/inputs/color_input.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true +module SimpleForm + module Inputs + class ColorInput < Base + def input(wrapper_options = nil) + input_html_options[:type] ||= "color" if html5? + + merged_input_options = merge_wrapper_options(input_html_options, wrapper_options) + + @builder.text_field(attribute_name, merged_input_options) + end + end + end +end diff --git a/test/inputs/color_input_test.rb b/test/inputs/color_input_test.rb new file mode 100644 index 00000000..19d8cb8a --- /dev/null +++ b/test/inputs/color_input_test.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'test_helper' + +class ColorInputTest < ActionView::TestCase + test 'input generates a color field' do + with_input_for @user, :favorite_color, :color + assert_select 'input[type=color].color#user_favorite_color' + end +end diff --git a/test/support/models.rb b/test/support/models.rb index 0dd7feed..28619767 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -91,7 +91,7 @@ class User :post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender, :extra_special_company_id, :pictures, :picture_ids, :special_pictures, :special_picture_ids, :uuid, :friends, :friend_ids, :special_tags, :special_tag_ids, - :citext, :hstore, :json, :jsonb, :hourly + :citext, :hstore, :json, :jsonb, :hourly, :favorite_color def self.build(extra_attributes = {}) attributes = {