diff --git a/docs/introduction/hooks-and-components.md b/docs/introduction/hooks-and-components.md index 05704c1..5b14d96 100644 --- a/docs/introduction/hooks-and-components.md +++ b/docs/introduction/hooks-and-components.md @@ -23,15 +23,14 @@ export const Introduction: FC = () => { ### PROPS & METHODS -| Prop name | Type | Default | Description | -| --------------------- | ----------------------------------------------------------------------------------------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| accept | string | "" | [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept) states the following: The accept attribute takes as its value a comma-separated list of one or more file types, or [unique file type specifiers](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept#unique_file_type_specifiers), describing which file types to allow. ex: `"audio/*,image/webp,.jpg,.pdf"` | -| disabled | boolean | false | | -| disabledDropOnDocment | boolean | false | | -| multiple | boolean | true | | -| onDrop | (files: File[]) => void | | Callback when Drop event occurs. | -| onSelect | (props: { acceptedFiles: File[], fileRejections: RejectedClassifiedFile[], classifiedFiles: ClassifiedFile[] }) => void | | Callbacks when the Drop event occurs and when a file is selected in a dialog. | -| onError | (error: Error) => void | | | +| Prop name | Type | Default | Description | +| --------- | ----------------------------------------------------------------------------------------------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| accept | string | "" | [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept) states the following: The accept attribute takes as its value a comma-separated list of one or more file types, or [unique file type specifiers](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept#unique_file_type_specifiers), describing which file types to allow. ex: `"audio/*,image/webp,.jpg,.pdf"` | +| disabled | boolean | false | | +| multiple | boolean | true | | +| onDrop | (files: File[]) => void | | Callback when Drop event occurs. | +| onSelect | (props: { acceptedFiles: File[], fileRejections: RejectedClassifiedFile[], classifiedFiles: ClassifiedFile[] }) => void | | Callbacks when the Drop event occurs and when a file is selected in a dialog. | +| onError | (error: Error) => void | | | ### Supplement onSelect diff --git a/lib/DisableOutOfFileDropzoneZone.tsx b/lib/DisableOutOfFileDropzoneZone.tsx new file mode 100644 index 0000000..2c4f258 --- /dev/null +++ b/lib/DisableOutOfFileDropzoneZone.tsx @@ -0,0 +1,26 @@ +import { FC, useEffect } from "react" + +export const DisableOutOfFileDropzoneZone: FC<{ disabled?: boolean }> = ({ + disabled = true, +}) => { + useEffect(() => { + const handleDocumentDrop = (event: DragEvent) => { + event.preventDefault() + event.stopPropagation() + } + + if (disabled) { + document.addEventListener("dragover", handleDocumentDrop) + document.addEventListener("drop", handleDocumentDrop) + } + + return () => { + if (disabled) { + document.removeEventListener("dragover", handleDocumentDrop) + document.removeEventListener("drop", handleDocumentDrop) + } + } + }, [disabled]) + + return <> +} diff --git a/lib/ReactDropzoneVV.tsx b/lib/ReactDropzoneVV.tsx index a8c8e7f..f300531 100644 --- a/lib/ReactDropzoneVV.tsx +++ b/lib/ReactDropzoneVV.tsx @@ -1,5 +1,5 @@ import type { FC, HTMLProps, ReactNode } from "react" -import { useCallback, useEffect } from "react" +import { useCallback } from "react" import type { UseReactDropzoneVV } from "./useReactDropzoneVV" import { classifyByAcceptability, @@ -31,7 +31,6 @@ export const ReactDropzoneVV: FC = ({ reactDropzoneVV: { accept, disabled, - disabledDropOnDocment, multiple, setIsDragging, isDragging, @@ -141,25 +140,6 @@ export const ReactDropzoneVV: FC = ({ [accept, multiple, onSelect, onError] ) - useEffect(() => { - const handleDocumentDrop = (event: DragEvent) => { - event.preventDefault() - event.stopPropagation() - } - - if (disabledDropOnDocment) { - document.addEventListener("dragover", handleDocumentDrop) - document.addEventListener("drop", handleDocumentDrop) - } - - return () => { - if (disabledDropOnDocment) { - document.removeEventListener("dragover", handleDocumentDrop) - document.removeEventListener("drop", handleDocumentDrop) - } - } - }, [disabledDropOnDocment]) - return (
> disabled: boolean setDisabled: React.Dispatch> - disabledDropOnDocment: boolean - setDisabledDropOnDocment: React.Dispatch> multiple: boolean setMultiple: React.Dispatch> isDragging: boolean @@ -25,14 +22,10 @@ export type UseReactDropzoneVV = { export const useReactDropzoneVV = ({ accept: tAccept = "", disabled: tDisabled = false, - disabledDropOnDocment: tDisabledDropOnDocment = false, multiple: tMultiple = true, }: UseReactDropzoneVVProps = {}): UseReactDropzoneVV => { const [accept, setAccept] = useState(tAccept) const [disabled, setDisabled] = useState(tDisabled) - const [disabledDropOnDocment, setDisabledDropOnDocment] = useState( - tDisabledDropOnDocment - ) const [multiple, setMultiple] = useState(tMultiple) const [isDragging, setIsDragging] = useState(false) @@ -48,8 +41,6 @@ export const useReactDropzoneVV = ({ setAccept, disabled, setDisabled, - disabledDropOnDocment, - setDisabledDropOnDocment, multiple, setMultiple, isDragging, diff --git a/src/App.tsx b/src/App.tsx index f9a5081..fe87485 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,7 +4,6 @@ import { useReactDropzoneVV, ReactDropzoneVV, OnSelectProps } from "@lib/index" const App: FC = () => { const reactDropzoneVV = useReactDropzoneVV({ accept: ".png,.jpg,.jpeg,.webp", - disabledDropOnDocment: true, }) const handleSelect = ({ classifiedFiles }: OnSelectProps) => {