From 6d255fa70bc89dbc9b750b15966c2aace852e9d3 Mon Sep 17 00:00:00 2001 From: Romain Monteil Date: Mon, 26 Oct 2026 03:37:54 +0100 Subject: [PATCH] [Dropzone] Make the clear button, file name and image preview targets optional in single-file mode | Q | A | -------------- | --- | Bug fix? | no | New feature? | yes | Deprecations? | no | Documentation? | yes | Issues | #4142 | License | MIT Fourth step of the RFC in #4142. In single-file mode, `connect()`, `clear()` and the change handler use the `previewClearButton`, `previewFilename` and `previewImage` targets without checking that they exist. A form theme that leaves one of them out, for example an avatar drop zone that shows no file name, makes the controller throw on connect: Stimulus reports `Missing target element`, and the drop zone does nothing. The controller now checks the file name and image preview targets with `has*Target` before using them, as it already does for `previewList` in `multiple` mode: without one of them, that part is skipped and the rest of the preview still works. The clear button is bound with the `previewClearButtonTargetConnected()` and `previewClearButtonTargetDisconnected()` callbacks instead of in `connect()`, so a theme can leave it out, and a button rendered later (by a LiveComponent re-render or a Turbo morph, for example) also clears the drop zone. As before, it is not bound in `multiple` mode. The docs explain how to clear the drop zone from another element with the `clear` action, as the user cannot deselect the file otherwise. | Theme without... | Before | After | ------------------- | ----------------- | --- | `previewClearButton` | throws on connect | works, nothing to click to clear | `previewFilename` | throws on connect | works, no file name shown | `previewImage` | throws on connect | works, no image preview Tests: one unit test per target, which connects, picks a file and checks the preview and `dropzone:change`; a check that the file name still shows without the image preview, and the image preview without the file name; a disconnect without the clear button; a clear button added after connect; and a clear button that must not clear the files in `multiple` mode. The tests that connect without a target and the one with the clear button added later fail on the base branch. The unit tests now stop the Stimulus applications after each test. Before, every application stayed started, and the controllers of earlier tests also connected to the next test's markup: with them, the test without the clear button passed on the base branch. --- src/Dropzone/CHANGELOG.md | 1 + src/Dropzone/assets/dist/controller.d.ts | 4 + src/Dropzone/assets/dist/controller.js | 26 +++-- src/Dropzone/assets/src/controller.ts | 42 +++++-- .../assets/test/unit/controller.test.ts | 109 ++++++++++++++++++ src/Dropzone/doc/index.rst | 13 +++ 6 files changed, 174 insertions(+), 21 deletions(-) diff --git a/src/Dropzone/CHANGELOG.md b/src/Dropzone/CHANGELOG.md index 0f295fef043..a012e7baff8 100644 --- a/src/Dropzone/CHANGELOG.md +++ b/src/Dropzone/CHANGELOG.md @@ -6,6 +6,7 @@ - Add a `data-state` attribute on the container: `empty`, `filled`, `dragging` or `processing` - Show and hide the parts of the Dropzone with the `hidden` attribute instead of inline styles, see [UPGRADE.md](UPGRADE.md) - Add a `dropzone_preview_item_template` form theme block and a `previewItemTemplate` target to render the preview of each file in `multiple` mode from a `