Skip to content

Commit

Permalink
Merge pull request #1293 from streamich/useDrop-improve
Browse files Browse the repository at this point in the history
fix: πŸ› improve how text is dropped in useDrop hook
  • Loading branch information
streamich authored Jun 13, 2020
2 parents 83c5083 + b2f46d1 commit 62a3727
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
21 changes: 6 additions & 15 deletions src/useDrop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable */
import * as React from 'react';
import useMountedState from './useMountedState';

const { useState, useMemo, useCallback, useEffect } = React;

Expand All @@ -23,13 +22,8 @@ export interface DropAreaOptions {
}

const noop = () => {};
/*
const defaultState: DropAreaState = {
over: false,
};
*/

const createProcess = (options: DropAreaOptions, mounted: boolean) => (dataTransfer: DataTransfer, event) => {
const createProcess = (options: DropAreaOptions) => (dataTransfer: DataTransfer, event) => {
const uri = dataTransfer.getData('text/uri-list');

if (uri) {
Expand All @@ -42,21 +36,18 @@ const createProcess = (options: DropAreaOptions, mounted: boolean) => (dataTrans
return;
}

if (dataTransfer.items && dataTransfer.items.length) {
dataTransfer.items[0].getAsString(text => {
if (mounted) {
(options.onText || noop)(text, event);
}
});
if (event.clipboardData) {
const text = event.clipboardData.getData('text');
(options.onText || noop)(text, event);
return;
}
};

const useDrop = (options: DropAreaOptions = {}, args = []): DropAreaState => {
const { onFiles, onText, onUri } = options;
const isMounted = useMountedState();
const [over, setOverRaw] = useState<boolean>(false);
const setOver = useCallback(setOverRaw, []);
const process = useMemo(() => createProcess(options, isMounted()), [onFiles, onText, onUri]);
const process = useMemo(() => createProcess(options), [onFiles, onText, onUri]);

useEffect(() => {
const onDragOver = event => {
Expand Down
7 changes: 3 additions & 4 deletions stories/useDrop.story.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { action } from '@storybook/addon-actions';
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useDrop } from '../src';
import ShowDocs from './util/ShowDocs';

const Demo = () => {
const state = useDrop({
onFiles: action('onFiles'),
onUri: action('onUri'),
onText: action('onText'),
onFiles: (...args) => console.log('onFiles', ...args),
onUri: (...args) => console.log('onUri', ...args),
onText: (...args) => console.log('onText', ...args),
});

const style: React.CSSProperties = {
Expand Down

0 comments on commit 62a3727

Please sign in to comment.