Skip to content

Commit 67caa3a

Browse files
committed
Initialise FileUploadField only on the newly added collection item
'ea.collection.item-added' was re-running the document-wide query for file upload inputs, which double-bound the change/click handlers on every already-processed field and could prevent the new field from displaying selected files reliably (Bootstrap markup variations exposed this further). Scope the query to event.detail.newElement, and guard each input with a data-ea-fileupload-initialized flag so re-entry is safe. Fixes #6637
1 parent 9cd95ee commit 67caa3a

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

assets/js/field-file-upload.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { toggleVisibilityClasses } from './helpers';
22

3-
const eaFileUploadHandler = (event) => {
4-
document.querySelectorAll('.ea-fileupload input[type="file"]').forEach((fileUploadElement) => {
3+
const initFileUploadFields = (root) => {
4+
root.querySelectorAll('.ea-fileupload input[type="file"]').forEach((fileUploadElement) => {
5+
if (fileUploadElement.dataset.eaFileUploadInitialized === '1') {
6+
return;
7+
}
8+
fileUploadElement.dataset.eaFileUploadInitialized = '1';
59
new FileUploadField(fileUploadElement);
610
});
711
};
812

9-
window.addEventListener('DOMContentLoaded', eaFileUploadHandler);
10-
document.addEventListener('ea.collection.item-added', eaFileUploadHandler);
13+
window.addEventListener('DOMContentLoaded', () => initFileUploadFields(document));
14+
document.addEventListener('ea.collection.item-added', (event) => {
15+
// only initialise the file upload fields contained in the newly added collection item:
16+
// re-running the query on the whole document would re-attach listeners to already
17+
// processed fields and double-fire their handlers (see #6637).
18+
initFileUploadFields(event.detail.newElement ?? document);
19+
});
1120

1221
class FileUploadField {
1322
#fieldContainerElement;

0 commit comments

Comments
 (0)