Add disabled class to disabled input

This commit is contained in:
Rafael Mendonça França 2011-09-11 18:46:47 -03:00
parent c2249590af
commit 8dc707a7c2
5 changed files with 32 additions and 24 deletions

View File

@ -57,11 +57,7 @@ module SimpleForm
end
def input_html_classes
[input_type, required_class]
end
def has_disabled?
options[:disabled] == true
[input_type, required_class, disabled_class]
end
def has_autofocus?
@ -70,6 +66,14 @@ module SimpleForm
private
def disabled_class
'disabled' if has_disabled?
end
def has_disabled?
options[:disabled] == true
end
def add_size!
input_html_options[:size] ||= [limit, SimpleForm.default_input_size].compact.min
end

View File

@ -65,4 +65,4 @@ module SimpleForm
end
end
end
end
end

View File

@ -18,9 +18,8 @@ module SimpleForm
css = options[:wrapper_class] ? Array.wrap(options[:wrapper_class]) : @defaults[:class]
css += input.input_html_classes
css << (options[:wrapper_error_class] || @defaults[:error_class]) if input.has_errors?
css << "disabled" if input.has_disabled?
css
css
end
end
end
end
end

View File

@ -21,6 +21,11 @@ class WrapperTest < ActionView::TestCase
assert_no_select 'div.disabled'
end
test 'wrapper should have disabled class when input is disabled' do
with_form_for @user, :active, :disabled => true
assert_select 'div.disabled'
end
test 'wrapper should support no wrapping when wrapper is false' do
with_form_for @user, :name, :wrapper => false
assert_select 'form > label[for=user_name]'

View File

@ -17,37 +17,37 @@ class InputTest < ActionView::TestCase
test 'input should generate disabled elements based on the disabled option' do
with_input_for @user, :name, :string, :disabled => true
assert_select 'input.string[disabled]'
assert_select 'input.string.disabled[disabled]'
with_input_for @user, :description, :text, :disabled => true
assert_select 'textarea.text[disabled]'
assert_select 'textarea.text.disabled[disabled]'
with_input_for @user, :age, :integer, :disabled => true
assert_select 'input.integer[disabled]'
assert_select 'input.integer.disabled[disabled]'
with_input_for @user, :born_at, :date, :disabled => true
assert_select 'select.date[disabled]'
assert_select 'select.date.disabled[disabled]'
with_input_for @user, :created_at, :datetime, :disabled => true
assert_select 'select.datetime[disabled]'
assert_select 'select.datetime.disabled[disabled]'
with_input_for @user, :name, :string, :disabled => false
assert_select 'input.string:not([disabled])'
assert_select 'input.string:not(.disabled[disabled])'
with_input_for @user, :description, :text, :disabled => false
assert_select 'textarea.text:not([disabled])'
assert_select 'textarea.text:not(.disabled[disabled])'
with_input_for @user, :age, :integer, :disabled => false
assert_select 'input.integer:not([disabled])'
assert_select 'input.integer:not(.disabled[disabled])'
with_input_for @user, :born_at, :date, :disabled => false
assert_select 'select.date:not([disabled])'
assert_select 'select.date:not(.disabled[disabled])'
with_input_for @user, :created_at, :datetime, :disabled => false
assert_select 'select.datetime:not([disabled])'
assert_select 'select.datetime:not(.disabled[disabled])'
with_input_for @user, :name, :string
assert_select 'input.string:not([disabled])'
assert_select 'input.string:not(.disabled[disabled])'
with_input_for @user, :description, :text
assert_select 'textarea.text:not([disabled])'
assert_select 'textarea.text:not(.disabled[disabled])'
with_input_for @user, :age, :integer
assert_select 'input.integer:not([disabled])'
assert_select 'input.integer:not(.disabled[disabled])'
with_input_for @user, :born_at, :date
assert_select 'select.date:not([disabled])'
assert_select 'select.date:not(.disabled[disabled])'
with_input_for @user, :created_at, :datetime
assert_select 'select.datetime:not([disabled])'
assert_select 'select.datetime:not(.disabled[disabled])'
end
test 'input should generate autofocus attribute based on the autofocus option' do