Skip to content

Commit

Permalink
Add color input
Browse files Browse the repository at this point in the history
Add an input for color picker

Browser support for color picker is still limited but I don't see a reason not to support it within simple_form
  • Loading branch information
Gustavo Bazan committed Nov 13, 2018
1 parent f5758fc commit 7595be6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/simple_form/inputs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Inputs
autoload :CollectionInput
autoload :CollectionRadioButtonsInput
autoload :CollectionSelectInput
autoload :ColorInput
autoload :DateTimeInput
autoload :FileInput
autoload :GroupedCollectionSelectInput
Expand Down
14 changes: 14 additions & 0 deletions lib/simple_form/inputs/color_input.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions test/inputs/color_input_test.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 7595be6

Please sign in to comment.