Skip to content

Commit

Permalink
feat: enable overwrite default html params (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
right9alt authored Jun 24, 2024
1 parent 71fc005 commit fa247d2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/active_dry_form/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def fields_for(association_name, fields_options = {}, &block)

private def wrap_input(method_type, field, options, wrapper_options = {})
config = ActiveDryForm.config.html_options._settings[method_type] ? ActiveDryForm.config.html_options[method_type] : EMPTY_HASH
options = options.merge(config) { |_key, oldval, newval| Array.wrap(newval) + Array.wrap(oldval) }
options = config.merge(options)

options[:class] = Array.wrap(config[:class]) + Array.wrap(options[:class]) if config[:class]
options[:required] = object.info(field)[:required] unless options.key?(:required)

Input
Expand Down
5 changes: 4 additions & 1 deletion lib/active_dry_form/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module ActiveDryForm
setting :input, default: 'form-input'
setting :input_required, default: 'form-input-required'
setting :input_error, default: 'form-input-error'
setting :form, default: ['active-dry-form']
end

setting :html_options do
Expand All @@ -25,6 +26,8 @@ module ActiveDryForm
setting :input_email, default: EMPTY_HASH
setting :input_file, default: EMPTY_HASH
setting :input_integer, default: EMPTY_HASH

# If without 'any', the fractional part of the number is lost when step is an integer (1 by default)
setting :input_number, default: { step: 'any' }
setting :input_password, default: EMPTY_HASH
setting :input_select, default: EMPTY_HASH
Expand All @@ -33,7 +36,7 @@ module ActiveDryForm
setting :input_text, default: EMPTY_HASH
setting :input_url, default: EMPTY_HASH

setting :form, default: { class: ['active-dry-form'] }
setting :form, default: EMPTY_HASH
end

end
Expand Down
4 changes: 3 additions & 1 deletion lib/active_dry_form/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def active_dry_form_for(name, options = {}, &block)
end

private def html_options(options)
(options[:html] || {}).merge(ActiveDryForm.config.html_options.form) do |_key, oldval, newval|
classes = { class: ActiveDryForm.config.css_classes.form }

(options[:html] || {}).merge(ActiveDryForm.config.html_options.form, classes) do |_key, oldval, newval|
Array.wrap(newval) + Array.wrap(oldval)
end
end
Expand Down
14 changes: 13 additions & 1 deletion spec/active_dry_form/form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def polymorphic_path(*)
expect(html).to include_html(expected_html)
end

it 'renders float input' do
it 'renders float input default step' do
html = context.active_dry_form_for(form) { |f| f.input :balance }
expected_html = <<-HTML
<div class="form-input input_number">
Expand All @@ -137,6 +137,18 @@ def polymorphic_path(*)
expect(html).to include_html(expected_html)
end

it 'renders float input with step option' do
html = context.active_dry_form_for(form) { |f| f.input :balance, { step: 0.1 } }
expected_html = <<-HTML
<div class="form-input input_number">
<label for="user_balance">User Balance</label>
<input step="0.1" type="number" name="user[balance]" id="user_balance" />
</div>
HTML

expect(html).to include_html(expected_html)
end

it 'renders boolean checkbox' do
html = context.active_dry_form_for(form) { |f| f.input :is_retail }
expected_html = <<-HTML
Expand Down

0 comments on commit fa247d2

Please sign in to comment.