Adding placeholder option to the numeric input
This commit is contained in:
parent
1654d95a85
commit
8a85618022
|
@ -76,6 +76,14 @@ module SimpleForm
|
|||
reflection ? object.class.validators_on(reflection.name) : []
|
||||
end
|
||||
|
||||
def has_placeholder?
|
||||
options[:placeholder] != false && placeholder.present?
|
||||
end
|
||||
|
||||
def placeholder
|
||||
@placeholder ||= options[:placeholder] || translate(:placeholders)
|
||||
end
|
||||
|
||||
def attribute_required_by_default?
|
||||
SimpleForm.required_by_default
|
||||
end
|
||||
|
|
|
@ -7,9 +7,10 @@ module SimpleForm
|
|||
|
||||
def input_html_options
|
||||
input_options = super
|
||||
input_options[:type] ||= "number"
|
||||
input_options[:size] ||= SimpleForm.default_input_size
|
||||
input_options[:step] ||= 1 if integer?
|
||||
input_options[:type] ||= "number"
|
||||
input_options[:size] ||= SimpleForm.default_input_size
|
||||
input_options[:step] ||= 1 if integer?
|
||||
input_options[:placeholder] ||= placeholder if has_placeholder?
|
||||
|
||||
infer_attributes_from_validations(input_options)
|
||||
|
||||
|
|
|
@ -23,14 +23,6 @@ module SimpleForm
|
|||
def limit
|
||||
column && column.limit
|
||||
end
|
||||
|
||||
def has_placeholder?
|
||||
options[:placeholder] != false && placeholder.present?
|
||||
end
|
||||
|
||||
def placeholder
|
||||
@placeholder ||= options[:placeholder] || translate(:placeholders)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -150,6 +150,25 @@ class InputTest < ActionView::TestCase
|
|||
assert_select 'input[step=1]'
|
||||
end
|
||||
|
||||
test 'numeric input should not generate placeholder by default' do
|
||||
with_input_for @user, :age, :integer
|
||||
assert_no_select 'input[placeholder]'
|
||||
end
|
||||
|
||||
test 'numeric input should accept the placeholder option' do
|
||||
with_input_for @user, :age, :integer, :placeholder => 'Put in some text'
|
||||
assert_select 'input.integer[placeholder=Put in some text]'
|
||||
end
|
||||
|
||||
test 'numeric input should use i18n to translate placeholder text' do
|
||||
store_translations(:en, :simple_form => { :placeholders => { :user => {
|
||||
:age => 'Age goes here'
|
||||
} } }) do
|
||||
with_input_for @user, :age, :integer
|
||||
assert_select 'input.integer[placeholder=Age goes here]'
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue