2014-01-23 16:17:16 +01:00
|
|
|
|
class DeltaInput < SimpleForm::Inputs::StringInput
|
|
|
|
|
# for now, need to pass id or it won't work
|
2014-12-10 22:03:17 +01:00
|
|
|
|
def input(wrapper_options)
|
|
|
|
|
options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
|
|
|
options[:type] = 'text'
|
|
|
|
|
options[:data] ||= {}
|
|
|
|
|
options[:data][:delta] ||= 1
|
|
|
|
|
options[:autocomplete] ||= 'off'
|
2023-05-12 13:01:12 +02:00
|
|
|
|
# TODO: get generated id, don't know how yet - `add_default_name_and_id_for_value` might be an option
|
2014-01-23 16:17:16 +01:00
|
|
|
|
|
|
|
|
|
template.content_tag :div, class: 'delta-input input-prepend input-append' do
|
2015-11-18 22:35:23 +01:00
|
|
|
|
delta_button(content_tag(:i, nil, class: 'icon icon-minus'), -1, options) +
|
|
|
|
|
@builder.text_field(attribute_name, options) +
|
|
|
|
|
delta_button(content_tag(:i, nil, class: 'icon icon-plus'), 1, options)
|
2014-01-23 16:17:16 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2021-03-01 15:27:26 +01:00
|
|
|
|
# template.button_tag('−', type: :submit, data: {decrement: options[:id]}, tabindex: -1, class: 'btn') +
|
2014-01-23 16:17:16 +01:00
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2014-12-10 22:03:17 +01:00
|
|
|
|
def delta_button(title, direction, options)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
data = { (direction > 0 ? 'increment' : 'decrement') => options[:id] }
|
2014-12-10 22:03:17 +01:00
|
|
|
|
delta = direction * options[:data][:delta]
|
2014-01-23 16:17:16 +01:00
|
|
|
|
template.button_tag(title, type: :button, name: 'delta', value: delta, data: data, tabindex: -1, class: 'btn')
|
|
|
|
|
end
|
|
|
|
|
end
|