Skip to content

Commit

Permalink
add support for and use the allowClear option for select2 #2020
Browse files Browse the repository at this point in the history
  • Loading branch information
stuzart committed Oct 28, 2024
1 parent 2d99efe commit 0c0f0c4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/assets/javascripts/objects_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ var ObjectsInput = {
const opts = {
placeholder: 'Search ...',
theme: "bootstrap",
width: '100%'
width: '100%',
allowClear: false
};

if ($j(this).data('placeholder')) {
opts.placeholder = $j(this).data('placeholder');
}

if ($j(this).data('allow-clear')) {
opts.allowClear = $j(this).data('allow-clear');
}

if ($j(this).data('tags-limit')) {
opts.maximumSelectionLength = $j(this).data('tags-limit');
}
Expand Down
1 change: 1 addition & 0 deletions app/helpers/bootstrap_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def objects_input(element_name, existing_objects = [], options = {}, value_metho
options['data-tags-limit'] = options.delete(:limit) if options[:limit]
options['data-allow-new-items'] = options.delete(:allow_new) if options[:allow_new]
options['data-placeholder'] = options.delete(:placeholder) if options[:placeholder]
options['data-allow-clear'] = options.delete(:allow_clear) if options[:allow_clear]
options[:include_blank] = ''
options[:multiple] = true unless options.key?(:multiple)
options[:name] = "#{element_name}#{options[:multiple] ? '[]': ''}" unless options.key?(:name)
Expand Down
1 change: 1 addition & 0 deletions app/helpers/observation_units_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module ObservationUnitsHelper
def observation_unit_selector(name, selected = nil, opts = {})
opts[:data] ||= {}
opts[:multiple] = false
opts[:allow_clear] = true
grouped_options = grouped_observation_unit_options
opts[:select_options] = grouped_options_for_select(grouped_options, selected&.id)

Expand Down

0 comments on commit 0c0f0c4

Please sign in to comment.