diff --git a/app/assets/javascripts/polaris_view_components.js b/app/assets/javascripts/polaris_view_components.js index 701cef5e..5f6c95f7 100644 --- a/app/assets/javascripts/polaris_view_components.js +++ b/app/assets/javascripts/polaris_view_components.js @@ -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); @@ -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) { diff --git a/app/assets/javascripts/polaris_view_components/dropzone_controller.js b/app/assets/javascripts/polaris_view_components/dropzone_controller.js index 8b8f02a6..83a0cce1 100644 --- a/app/assets/javascripts/polaris_view_components/dropzone_controller.js +++ b/app/assets/javascripts/polaris_view_components/dropzone_controller.js @@ -34,11 +34,12 @@ export default class extends Controller { } files = [] - acceptedFiles = [] rejectedFiles = [] _dragging = false dragTargets = [] previewRendered = false + + _acceptedFiles = [] _size = 'large' connect () { @@ -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) { diff --git a/test/system/dropzone_component_test.rb b/test/system/dropzone_component_test.rb index 99ee0431..c0671d79 100644 --- a/test/system/dropzone_component_test.rb +++ b/test/system/dropzone_component_test.rb @@ -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")