Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file assignment for drag&drop in Dropzone #239

Merged
merged 3 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/assets/javascripts/polaris_view_components.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,11 @@ class Dropzone extends Controller {
size: String
};
files=[];
acceptedFiles=[];
rejectedFiles=[];
_dragging=false;
dragTargets=[];
previewRendered=false;
_acceptedFiles=[];
_size="large";
connect() {
document.body.addEventListener("click", this.onExternalTriggerClick);
Expand Down Expand Up @@ -686,6 +686,15 @@ class Dropzone extends Controller {
sizeClassesToRemove.forEach((className => this.element.classList.remove(className)));
this.element.classList.add(this.getSizeClass(val));
}
get acceptedFiles() {
return this._acceptedFiles;
}
set acceptedFiles(val) {
this._acceptedFiles = val;
const list = new DataTransfer;
val.forEach((file => list.items.add(file)));
this.inputTarget.files = list.files;
}
}

function fileAccepted(file, accept) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ export default class extends Controller {
}

files = []
acceptedFiles = []
rejectedFiles = []
_dragging = false
dragTargets = []
previewRendered = false

_acceptedFiles = []
_size = 'large'

connect () {
Expand Down Expand Up @@ -452,6 +453,20 @@ export default class extends Controller {

this.element.classList.add(this.getSizeClass(val))
}

get acceptedFiles () {
return this._acceptedFiles
}

set acceptedFiles (val) {
this._acceptedFiles = val

const list = new DataTransfer()

val.forEach(file => list.items.add(file))

this.inputTarget.files = list.files
}
}

export function fileAccepted (file, accept) {
Expand Down
13 changes: 13 additions & 0 deletions test/system/dropzone_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ def test_file_upload
end
end

def test_submiting_file
with_preview("rails/form_builder_component/dropzone")

within first("form") do
find(".Polaris-DropZone").drop(fixture_file("file.txt"))
assert_selector ".Polaris-DropZone__Preview > .Polaris-Stack > .Polaris-Stack__Item", count: 1

click_on "Submit"
end

assert_text "File: file.txt"
end

def test_image_upload
with_preview("forms/dropzone_component/with_image_upload")

Expand Down