Skip to content

Pass dataTransfer through in onDrop #6835

Open
@alecmev

Description

@alecmev

Provide a general summary of the feature here

I use DropZone in a form, and would like to have an easy way to set the associated input's files property. Currently I have to construct my own FileList, but the original dataTransfer is right there, just discarded:

if (typeof options.onDrop === 'function') {
let dropOperation = DROP_EFFECT_TO_DROP_OPERATION[state.dropEffect];
let items = readFromDataTransfer(e.dataTransfer);
let rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
let event: DropEvent = {
type: 'drop',
x: e.clientX - rect.x,
y: e.clientY - rect.y,
items,
dropOperation
};
options.onDrop(event);
}

🤔 Expected Behavior?

I wish I could just do this:

<DropZone
  onDrop={(event) => {
    ok(inputRef.current);
    inputRef.current.files = event.dataTransfer.files;
  }}
>
  <FileTrigger
    ref={inputRef}
    allowsMultiple
  >
    <Button>Select files</Button>
  </FileTrigger>
  <span>Drop files here</span>
</DropZone>

😯 Current Behavior

Currently I have to do this:

<DropZone
  onDrop={async (event) => {
    const dataTransfer = new DataTransfer();
    const files = await Promise.all(
      event.items
        .filter((x) => x.kind === 'file')
        .map(async (x) => x.getFile()),
    );
    for (const file of files) dataTransfer.items.add(file);
    ok(inputRef.current);
    inputRef.current.files = dataTransfer.files;
  }}
>
  <FileTrigger
    ref={inputRef}
    allowsMultiple
  >
    <Button>Select files</Button>
  </FileTrigger>
  <span>Drop files here</span>
</DropZone>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions