diff --git a/app/assets/javascripts/fae/form/inputs/_select.js b/app/assets/javascripts/fae/form/inputs/_select.js index 6603941c1..f43341b27 100644 --- a/app/assets/javascripts/fae/form/inputs/_select.js +++ b/app/assets/javascripts/fae/form/inputs/_select.js @@ -92,11 +92,17 @@ Fae.form.select = { }) }); - // Add actions to wraper - $deselect_all_action.insertAfter($chosen); + // prevent multiple deselect all actions from being added when nested forms are generated + if ($('.multiselect-action_wrap').length === 0) { + // Add actions to wraper + $deselect_all_action.insertAfter($chosen); + } - // Add special "Select All" option and notify Chosen of new option - addSelectAllOption($element) + // prevent multiple 'SELECT ALL' options from being added when nested forms are generated + if ($element[0].options[0].value != select_all_value) { + // Add special "Select All" option and notify Chosen of new option + addSelectAllOption($element); + } // Mark label wrapper as having multiselect actions for styling $label.addClass('has-multiselect-actions'); diff --git a/spec/dummy/app/views/admin/wines/_form.html.slim b/spec/dummy/app/views/admin/wines/_form.html.slim index a8c9bd7e5..2cde3d553 100644 --- a/spec/dummy/app/views/admin/wines/_form.html.slim +++ b/spec/dummy/app/views/admin/wines/_form.html.slim @@ -11,6 +11,8 @@ = fae_input f, :food_pairing_en = fae_input f, :food_pairing_zh = fae_input f, :food_pairing_ja + // need for testing + = fae_association f, :releases - if params[:action] == 'edit' section.content#oregon_winemakers_section diff --git a/spec/features/form_helpers/fae_multiselect_spec.rb b/spec/features/form_helpers/fae_multiselect_spec.rb index 5e06d5eda..89108354f 100644 --- a/spec/features/form_helpers/fae_multiselect_spec.rb +++ b/spec/features/form_helpers/fae_multiselect_spec.rb @@ -40,4 +40,27 @@ expect(page).to have_content('Bathroom Reader Monthly') end + scenario 'should only display a single "de-select all" option after rendering nested form', js: true do + wine = FactoryBot.create(:wine) + release = FactoryBot.create(:release, name: 'testy mctest') + release = FactoryBot.create(:release, name: 'testy mctest 2') + + admin_login + visit edit_admin_wine_path(wine) + + click_link 'Add Oregon Winemaker' + expect(page).to have_css('form#new_winemaker') + + + group = find 'div.select' + group.click + within(group) do + find('li', text: 'testy mctest 2').click + end + + eventually { + expect(page).to have_selector('.js-multiselect-action-deselect_all', count: 1) + } + end + end