Skip to content

Commit 48bece7

Browse files
Vikx001Viikaax
andauthored
Resolves #5281 (#5284)
* Hide Other Agency Type field unless Agency Type is Other * Fix partner profile agency type visibility tests and functionality - Add system tests for Other Agency Type field visibility - Enable partner_step_form feature flag in tests - Fix CSS selectors to properly test hidden elements with visible: false - Tests verify Other Agency Type field shows/hides correctly based on selection - Update hide_by_source_val_controller.js for proper field visibility * Add test for nil agency type and fix empty dropdown behavior - Add comprehensive test suite for Other Agency Type field visibility - Update JavaScript controller to hide field when dropdown is empty - All 5 test scenarios now pass including nil agency type case * Fix rubocop trailing whitespace in test file * Fix ERB linting issues in agency information form * Fix agency type field visibility using valuesToShow logic - Update hide_by_source_val_controller to use Bootstrap d-none class - Change from valuesToHide to valuesToShow for cleaner code - Ensure Other Agency Type field shows only when 'other' is selected - Optimize system tests by combining scenarios - Address reviewer feedback on controller logic simplification * Add backward compatibility for valuesToHide parameter --------- Co-authored-by: Viikaax <[email protected]>
1 parent 7e8f9d1 commit 48bece7

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

app/javascript/controllers/hide_by_source_val_controller.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,32 @@ import { Controller } from "@hotwired/stimulus"
22

33
export default class extends Controller {
44
static targets = ["source", "destination"]
5-
static values = {
6-
valuesToHide: Array
5+
static values = {
6+
valuesToHide: Array,
7+
valuesToShow: Array
78
}
9+
10+
connect() {
11+
this.toggleVisibility()
12+
}
13+
814
sourceChanged() {
9-
const val = $(this.sourceTarget).val()
10-
this.destinationTargets.forEach(
11-
destination_target => { $(destination_target).toggleClass("d-none", this.valuesToHideValue.includes(val)); }
12-
)
15+
this.toggleVisibility()
16+
}
17+
18+
toggleVisibility() {
19+
const sourceValue = this.sourceTarget.value
20+
let shouldShow = false
21+
22+
if (this.hasValuesToShowValue && this.valuesToShowValue.length > 0) {
23+
shouldShow = this.valuesToShowValue.includes(sourceValue)
24+
} else if (this.hasValuesToHideValue && this.valuesToHideValue.length > 0) {
25+
shouldShow = !this.valuesToHideValue.includes(sourceValue)
26+
}
27+
if (shouldShow) {
28+
this.destinationTarget.classList.remove("d-none")
29+
} else {
30+
this.destinationTarget.classList.add("d-none")
31+
}
1332
}
1433
}

app/views/partners/profiles/step/_agency_information_form.html.erb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
</div>
44

55
<%= f.fields_for :profile, profile do |pf| %>
6-
<div class="form-group">
7-
<%= pf.input :agency_type,
8-
# Symbolize keys, because simple_form will only do a I18n lookup for
9-
# symbols
10-
collection: Partners::Profile.agency_types_for_selection,
11-
label: "Agency Type",
12-
class: "form-control",
13-
wrapper: :input_group %>
14-
</div>
15-
16-
<div class="form-group">
17-
<%= pf.input :other_agency_type, label: "Other Agency Type", class: "form-control" %>
6+
<div data-controller="hide-by-source-val"
7+
data-hide-by-source-val-values-to-show-value='["other"]'>
8+
<div class="form-group">
9+
<%= pf.input :agency_type,
10+
collection: Partners::Profile.agency_types_for_selection,
11+
label: "Agency Type",
12+
class: "form-control",
13+
wrapper: :input_group,
14+
input_html: { data: { "hide-by-source-val-target": "source", action: "change->hide-by-source-val#sourceChanged" } } %>
15+
</div>
16+
17+
<div class="form-group" data-hide-by-source-val-target="destination">
18+
<%= pf.input :other_agency_type, label: "Other Agency Type", class: "form-control" %>
19+
</div>
1820
</div>
1921

2022
<%= render "shared/custom_file_input",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Partner Profile Agency Type Field Visibility", type: :system, js: true do
4+
let(:partner) { create(:partner) }
5+
6+
before do
7+
Flipper.enable(:partner_step_form)
8+
sign_in(partner.primary_user)
9+
visit edit_partners_profile_path
10+
find("button[data-bs-target='#agency_information']").click
11+
end
12+
13+
after do
14+
Flipper.disable(:partner_step_form)
15+
end
16+
17+
it "shows/hides Other Agency Type field based on selection and initial state" do
18+
select "Food bank/pantry", from: "Agency Type"
19+
expect(page).to have_css('[data-hide-by-source-val-target="destination"].d-none', visible: false, wait: 5)
20+
21+
select "Other", from: "Agency Type"
22+
expect(page).not_to have_css('[data-hide-by-source-val-target="destination"].d-none', visible: false, wait: 5)
23+
end
24+
25+
context "initial state based on partner agency type" do
26+
it "handles different initial agency types correctly" do
27+
partner.profile.update!(agency_type: nil)
28+
visit edit_partners_profile_path
29+
find("button[data-bs-target='#agency_information']").click
30+
expect(page).to have_css('[data-hide-by-source-val-target="destination"].d-none', visible: false, wait: 5)
31+
32+
partner.profile.update!(agency_type: "food")
33+
visit edit_partners_profile_path
34+
find("button[data-bs-target='#agency_information']").click
35+
expect(page).to have_css('[data-hide-by-source-val-target="destination"].d-none', visible: false, wait: 5)
36+
37+
partner.profile.update!(agency_type: "other")
38+
visit edit_partners_profile_path
39+
find("button[data-bs-target='#agency_information']").click
40+
expect(page).not_to have_css('[data-hide-by-source-val-target="destination"].d-none', visible: false, wait: 5)
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)