Add autofocus attribute support.

This commit is contained in:
Jeroen Zwartepoorte 2010-12-17 19:28:47 +08:00 committed by José Valim
parent 965d449fb4
commit 07650728fd
2 changed files with 42 additions and 2 deletions

View File

@ -28,8 +28,9 @@ module SimpleForm
@reflection = options.delete(:reflection)
@options = options
@input_html_options = html_options_for(:input, input_html_classes).tap do |o|
o[:required] = true if has_required?
o[:disabled] = true if disabled?
o[:required] = true if has_required?
o[:disabled] = true if disabled?
o[:autofocus] = true if has_autofocus?
end
end
@ -76,6 +77,10 @@ module SimpleForm
attribute_required?
end
def has_autofocus?
options[:autofocus]
end
def has_validators?
attribute_name && object.class.respond_to?(:validators_on)
end

View File

@ -62,6 +62,41 @@ class InputTest < ActionView::TestCase
assert_select 'select.datetime:not([disabled])'
end
test 'input should generate autofocus attribute based on the autofocus option' do
with_input_for @user, :name, :string, :autofocus => true
assert_select 'input.string[autofocus]'
with_input_for @user, :description, :text, :autofocus => true
assert_select 'textarea.text[autofocus]'
with_input_for @user, :age, :integer, :autofocus => true
assert_select 'input.integer[autofocus]'
with_input_for @user, :born_at, :date, :autofocus => true
assert_select 'select.date[autofocus]'
with_input_for @user, :created_at, :datetime, :autofocus => true
assert_select 'select.datetime[autofocus]'
with_input_for @user, :name, :string, :autofocus => false
assert_select 'input.string:not([autofocus])'
with_input_for @user, :description, :text, :autofocus => false
assert_select 'textarea.text:not([autofocus])'
with_input_for @user, :age, :integer, :autofocus => false
assert_select 'input.integer:not([autofocus])'
with_input_for @user, :born_at, :date, :autofocus => false
assert_select 'select.date:not([autofocus])'
with_input_for @user, :created_at, :datetime, :autofocus => false
assert_select 'select.datetime:not([autofocus])'
with_input_for @user, :name, :string
assert_select 'input.string:not([autofocus])'
with_input_for @user, :description, :text
assert_select 'textarea.text:not([autofocus])'
with_input_for @user, :age, :integer
assert_select 'input.integer:not([autofocus])'
with_input_for @user, :born_at, :date
assert_select 'select.date:not([autofocus])'
with_input_for @user, :created_at, :datetime
assert_select 'select.datetime:not([autofocus])'
end
test 'input should render components according to an optional :components option' do
with_input_for @user, :name, :string, :components => [:input, :label]
assert_select 'input + label'