Sha256: 882dd0a48ab6875a50d207a7f46e381dc72346476a7ced55d385e333b5deec33

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  static targets = ["fileInput"];
  connect() {
    this.element.addEventListener("dragover", this.preventDragDefaults);
    this.element.addEventListener("dragenter", this.preventDragDefaults);
  }

  disconnect() {
    this.element.removeEventListener("dragover", this.preventDragDefaults);
    this.element.removeEventListener("dragenter", this.preventDragDefaults);
  }

  preventDragDefaults(e) {
    e.preventDefault();
    e.stopPropagation();
  }

  trigger() {
    this.fileInputTarget.click();
  }

  acceptFiles(event) {
    event.preventDefault();
    const files = event.dataTransfer ? event.dataTransfer.files : event.target.files;
    [...files].forEach((file) => {
      this.uploadFile(file);
    });
  }

  // Implement your own file upload strategy here...
  uploadFile(file) {
    console.log("Received file for upload: ", file);
    console.log("Implement your own file upload strategy here...");
    //   const formData = new FormData();
    //   formData.append("file", file);

    //   fetch("/upload", {
    //     method: "POST",
    //     body: formData,
    //   })
    //     .then((response) => response.json())
    //     .then((data) => {
    //       console.log(data);
    //     });
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shadcn-ui-0.0.15 app/javascript/controllers/ui/dropzone_controller.js
shadcn-ui-0.0.14 app/javascript/controllers/ui/dropzone_controller.js
shadcn-ui-0.0.13 app/javascript/controllers/ui/dropzone_controller.js
shadcn-ui-0.0.12 app/javascript/controllers/ui/dropzone_controller.js