Add input type range. Closes #308 and #290

This commit is contained in:
Carlos Antonio da Silva 2011-09-03 11:25:53 -03:00
parent 6b0249812e
commit 5b1471fdee
4 changed files with 115 additions and 36 deletions

View File

@ -10,6 +10,7 @@ module SimpleForm
map_type :string, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
map_type :password, :to => SimpleForm::Inputs::PasswordInput
map_type :integer, :decimal, :float, :to => SimpleForm::Inputs::NumericInput
map_type :range, :to => SimpleForm::Inputs::RangeInput
map_type :select, :radio, :check_boxes, :to => SimpleForm::Inputs::CollectionInput
map_type :date, :time, :datetime, :to => SimpleForm::Inputs::DateTimeInput
map_type :country, :time_zone, :to => SimpleForm::Inputs::PriorityInput

View File

@ -10,7 +10,9 @@ module SimpleForm
autoload :NumericInput, 'simple_form/inputs/numeric_input'
autoload :PasswordInput, 'simple_form/inputs/password_input'
autoload :PriorityInput, 'simple_form/inputs/priority_input'
autoload :TextInput, 'simple_form/inputs/text_input'
autoload :PasswordInput, 'simple_form/inputs/password_input'
autoload :RangeInput, 'simple_form/inputs/range_input'
autoload :StringInput, 'simple_form/inputs/string_input'
autoload :TextInput, 'simple_form/inputs/text_input'
end
end
end

View File

@ -0,0 +1,16 @@
module SimpleForm
module Inputs
class RangeInput < NumericInput
disable :placeholder
def input
if SimpleForm.html5
input_html_options[:type] ||= "range"
input_html_options[:step] ||= 1
end
super
end
end
end
end

View File

@ -229,6 +229,22 @@ class InputTest < ActionView::TestCase
end
end
test 'input should infer pattern from attributes when pattern is true' do
with_input_for @other_validating_user, :country, :string, :pattern => true
assert_select 'input[pattern="\w+"]'
end
test 'input should use given pattern from attributes' do
with_input_for @other_validating_user, :country, :string, :pattern => "\\d+"
assert_select 'input[pattern="\d+"]'
end
test 'input should fail if pattern is true but no pattern exists' do
assert_raise RuntimeError do
with_input_for @other_validating_user, :name, :string, :pattern => true
end
end
# NumericInput
test 'input should generate an integer text field for integer attributes ' do
with_input_for @user, :age, :integer
@ -335,22 +351,6 @@ class InputTest < ActionView::TestCase
assert_select 'input[max=119]'
end
test 'input should infer pattern from attributes when pattern is true' do
with_input_for @other_validating_user, :country, :string, :pattern => true
assert_select 'input[pattern="\w+"]'
end
test 'input should use given pattern from attributes' do
with_input_for @other_validating_user, :country, :string, :pattern => "\\d+"
assert_select 'input[pattern="\d+"]'
end
test 'input should fail if pattern is true but no pattern exists' do
assert_raise RuntimeError do
with_input_for @other_validating_user, :name, :string, :pattern => true
end
end
test 'input should have step value of any except for integer attribute' do
with_input_for @validating_user, :age, :float
assert_select 'input[step="any"]'
@ -378,24 +378,6 @@ class InputTest < ActionView::TestCase
end
end
# Numeric input but HTML5 disabled
test ' when not using HTML5 input should not generate field with type number and use text instead' do
swap SimpleForm, :html5 => false do
with_input_for @user, :age, :integer
assert_no_select "input[type=number]"
assert_no_select "input#user_age[text]"
end
end
test 'when not using HTML5 input should not use min or max or step attributes' do
swap SimpleForm, :html5 => false do
with_input_for @validating_user, :age, :integer
assert_no_select "input[min]"
assert_no_select "input[max]"
assert_no_select "input[step]"
end
end
[:integer, :float, :decimal].each do |type|
test "#{type} input should infer min value from attributes with greater than or equal validation" do
with_input_for @validating_user, :age, type
@ -408,6 +390,84 @@ class InputTest < ActionView::TestCase
end
end
# Numeric input but HTML5 disabled
test 'when not using HTML5 input should not generate field with type number and use text instead' do
swap SimpleForm, :html5 => false do
with_input_for @user, :age, :integer
assert_no_select "input[type=number]"
assert_select "input#user_age[type=text]"
end
end
test 'when not using HTML5 input should not use min or max or step attributes for numeric type' do
swap SimpleForm, :html5 => false do
with_input_for @validating_user, :age, :integer
assert_no_select "input[min]"
assert_no_select "input[max]"
assert_no_select "input[step]"
end
end
# RangeInput
test 'range input generates a input type range, based on numeric input' do
with_input_for @user, :age, :range
assert_select "input#user_age.range[type=range]"
end
test 'range input does not generate placeholder' do
with_input_for @user, :age, :range, :placeholder => "Select your age"
assert_select "input[type=range]"
assert_no_select "input[placeholder]"
end
test 'range input allows givin min and max attributes' do
with_input_for @user, :age, :range, :input_html => { :min => 10, :max => 50 }
assert_select "input[type=range][min=10][max=50]"
end
test 'range input infers min and max attributes from validations' do
with_input_for @validating_user, :age, :range
assert_select "input[type=range][min=18][max=99]"
end
test 'range input add default step attribute' do
with_input_for @validating_user, :age, :range
assert_select "input[type=range][step=1]"
end
test 'range input allows givin a step through input html options' do
with_input_for @validating_user, :age, :range, :input_html => { :step => 2 }
assert_select "input[type=range][step=2]"
end
test 'range input should not generate min attribute by default' do
with_input_for @user, :age, :range
assert_no_select 'input[min]'
end
test 'range input should not generate max attribute by default' do
with_input_for @user, :age, :range
assert_no_select 'input[max]'
end
# RangeInput iwth HTML5 disabled
test 'when not using HTML5, range input does not generate field with range type, and use text instead' do
swap SimpleForm, :html5 => false do
with_input_for @user, :age, :range
assert_no_select "input[type=number]"
assert_select "input[type=text]"
end
end
test 'when not using HTML5, range input should not use min or max or step attributes' do
swap SimpleForm, :html5 => false do
with_input_for @validating_user, :age, :range
assert_no_select "input[min]"
assert_no_select "input[max]"
assert_no_select "input[step]"
end
end
# BooleanInput
test 'input should generate a checkbox by default for boolean attributes' do
with_input_for @user, :active, :boolean