Skip to content

Commit 7595be6

Browse files
author
Gustavo Bazan
committed
Add color input
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
1 parent f5758fc commit 7595be6

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Guess input type more carefully. [@sringling](https://github.com/sringling)
55
* Allow custom error on forms without model. [@victorperez](https://github.com/victorperez)
66
* Do not support Ruby < 2.3 anymore. [@gssbzn](https://github.com/gssbzn)
7+
* Add color inout type. [@gssbzn](https://github.com/gssbzn)
78

89
### Bug fix
910
* Improve disabled option to input_field. [@betelgeuse](https://github.com/betelgeuse)

lib/simple_form/inputs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Inputs
1010
autoload :CollectionInput
1111
autoload :CollectionRadioButtonsInput
1212
autoload :CollectionSelectInput
13+
autoload :ColorInput
1314
autoload :DateTimeInput
1415
autoload :FileInput
1516
autoload :GroupedCollectionSelectInput

lib/simple_form/inputs/color_input.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
module SimpleForm
3+
module Inputs
4+
class ColorInput < Base
5+
def input(wrapper_options = nil)
6+
input_html_options[:type] ||= "color" if html5?
7+
8+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
9+
10+
@builder.text_field(attribute_name, merged_input_options)
11+
end
12+
end
13+
end
14+
end

test/inputs/color_input_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class ColorInputTest < ActionView::TestCase
6+
test 'input generates a color field' do
7+
with_input_for @user, :favorite_color, :color
8+
assert_select 'input[type=color].color#user_favorite_color'
9+
end
10+
end

test/support/models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class User
9191
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender,
9292
:extra_special_company_id, :pictures, :picture_ids, :special_pictures,
9393
:special_picture_ids, :uuid, :friends, :friend_ids, :special_tags, :special_tag_ids,
94-
:citext, :hstore, :json, :jsonb, :hourly
94+
:citext, :hstore, :json, :jsonb, :hourly, :favorite_color
9595

9696
def self.build(extra_attributes = {})
9797
attributes = {

0 commit comments

Comments
 (0)