Skip to content

Commit 113b4c5

Browse files
authored
Merge pull request #5090 from danielabar/4821-part2-partner-profile-remove-attached-docs
4821 part2 partner profile remove attached docs
2 parents cf3911d + 771a028 commit 113b4c5

File tree

3 files changed

+104
-9
lines changed

3 files changed

+104
-9
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Controller } from "@hotwired/stimulus";
2+
3+
/**
4+
* Stimulus controller to enhance the file input UI for multiple files.
5+
*
6+
* This controller:
7+
* - Listens for file selection on an `<input type="file" multiple="multiple">`
8+
* - Displays selected file names in a custom list **when multiple files are selected
9+
* - Defaults to the browser’s built-in display for a single file selection
10+
*
11+
* Expected HTML structure should have a placeholder div for the selected file names:
12+
*
13+
* ```erb
14+
* <div data-controller="file-input">
15+
* <input type="file" multiple data-file-input-target="input">
16+
* <div data-file-input-target="list"></div>
17+
* </div>
18+
* ```
19+
*/
20+
export default class extends Controller {
21+
static targets = ["input", "list"];
22+
23+
connect() {
24+
this.inputTarget.addEventListener("change", () => this.updateFileList());
25+
}
26+
27+
updateFileList() {
28+
const files = this.inputTarget.files;
29+
this.listTarget.innerHTML = ""; // Clear previous list
30+
31+
// If no files or only one file is selected, let the native UI handle it
32+
if (files.length <= 1) {
33+
return;
34+
}
35+
36+
const ul = document.createElement("ul");
37+
ul.classList.add("list-unstyled", "mt-2");
38+
39+
Array.from(files).forEach((file) => {
40+
const li = document.createElement("li");
41+
li.classList.add("p-1", "rounded", "mb-1");
42+
li.textContent = file.name;
43+
ul.appendChild(li);
44+
});
45+
46+
this.listTarget.appendChild(ul);
47+
}
48+
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
<%= f.fields_for :profile, profile do |pf| %>
2-
<div class="form-group">
2+
<div class="form-group" data-controller="file-input">
33
<% if profile.documents.attached? %>
44
<strong>Attached files:</strong>
5-
<ul>
5+
<ul class="list-unstyled">
66
<% profile.documents.each do |doc| %>
77
<% if doc.persisted? %>
8-
<li><%= link_to doc.blob['filename'], rails_blob_path(doc), class: "font-weight-bold" %></li>
9-
<%= pf.hidden_field :documents, multiple: true, value: doc.signed_id %>
8+
<li class="attached-document d-flex justify-content-between align-items-center p-2 border rounded mb-2" data-document-id="<%= doc.signed_id %>">
9+
<div class="text-truncate" style="max-width: 75%;">
10+
<%= link_to doc.blob.filename.to_s, rails_blob_path(doc), class: "font-weight-bold" %>
11+
<%= pf.hidden_field :documents, multiple: true, value: doc.signed_id %>
12+
</div>
13+
<%= remove_element_button "Remove", container_selector: "li", class: "btn btn-sm btn-danger" %>
14+
</li>
1015
<% end %>
1116
<% end %>
1217
</ul>
13-
<%= pf.file_field :documents, multiple: true, class: "form-control-file" %>
14-
<% else %>
15-
<%= pf.file_field :documents, multiple: true, class: "form-control-file" %>
1618
<% end %>
19+
20+
<%# Native file input and placeholder for selected file names %>
21+
<%= pf.file_field :documents, multiple: true, class: "form-control-file", data: { file_input_target: "input" } %>
22+
<div data-file-input-target="list" class="mt-2"></div>
1723
</div>
1824
<% end %>

spec/system/partners/profile_edit_system_spec.rb

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@
100100
end
101101

102102
it "preserves previously uploaded documents when adding new attachments" do
103-
# Upload the first document
103+
# Open attached documents section
104104
find("button[data-bs-target='#attached_documents']").click
105105
expect(page).to have_css("#attached_documents.accordion-collapse.collapse.show", visible: true)
106106

107+
# Upload the first document
107108
within "#attached_documents" do
108109
attach_file("partner_profile_documents", Rails.root.join("spec/fixtures/files/document1.md"), make_visible: true)
109110
end
@@ -137,7 +138,47 @@
137138
end
138139
end
139140

140-
it "persists file upload when there are validation errors" do
141+
it "allows removal of attached documents" do
142+
# Open attached documents section
143+
find("button[data-bs-target='#attached_documents']").click
144+
expect(page).to have_css("#attached_documents.accordion-collapse.collapse.show", visible: true)
145+
146+
# Upload two documents - needs to be done individually because Capybara doesn't have attach_files multiple support
147+
# https://github.com/teamcapybara/capybara/issues/37
148+
within "#attached_documents" do
149+
attach_file("partner_profile_documents", Rails.root.join("spec/fixtures/files/document1.md"), make_visible: true)
150+
end
151+
all("input[type='submit'][value='Save Progress']").last.click
152+
visit edit_partners_profile_path
153+
find("button[data-bs-target='#attached_documents']").click
154+
within "#attached_documents" do
155+
attach_file("partner_profile_documents", Rails.root.join("spec/fixtures/files/document2.md"), make_visible: true)
156+
end
157+
all("input[type='submit'][value='Save Progress']").last.click
158+
159+
# Remove the first document
160+
visit edit_partners_profile_path
161+
find("button[data-bs-target='#attached_documents']").click
162+
within "#attached_documents" do
163+
document_name = "document1.md"
164+
document_li = find("li.attached-document", text: document_name)
165+
document_li.find("a.btn-danger", text: "Remove").click
166+
expect(page).not_to have_selector("li.attached-document", text: document_name)
167+
end
168+
169+
# Save Progress
170+
all("input[type='submit'][value='Save Progress']").last.click
171+
expect(page).to have_css(".alert-success", text: "Details were successfully updated.")
172+
173+
# Verify only one document is listed
174+
visit edit_partners_profile_path
175+
find("button[data-bs-target='#attached_documents']").click
176+
within "#attached_documents" do
177+
expect(page).to have_link("document2.md")
178+
end
179+
end
180+
181+
it "persists individual file upload when there are validation errors" do
141182
# Open up Agency Information section and upload proof-of-status letter
142183
find("button[data-bs-target='#agency_information']").click
143184
within "#agency_information" do

0 commit comments

Comments
 (0)