Skip to content

Commit b49b932

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 b49b932

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

assets/js/field-file-upload.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
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). The dataset flag
18+
// already protects against double-init if `event.detail.newElement` is missing.
19+
initFileUploadFields(event?.detail?.newElement ?? document);
20+
});
1121

1222
class FileUploadField {
1323
#fieldContainerElement;

0 commit comments

Comments
 (0)