Skip to content

Commit

Permalink
feat: add noClick & noDrag
Browse files Browse the repository at this point in the history
  • Loading branch information
yosipy committed Sep 1, 2024
1 parent a06500d commit 3a2b3d5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
70 changes: 69 additions & 1 deletion lib/ReactDropzoneVV.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Props = {
accept?: string
disabled?: boolean
multiple?: boolean
noClick?: boolean
noDrag?: boolean
}

const TestComponent: FC<Props> = (props) => {
Expand All @@ -31,6 +33,8 @@ const TestComponent: FC<Props> = (props) => {
accept={props.accept}
disabled={props.disabled}
multiple={props.multiple}
noClick={props.noClick}
noDrag={props.noDrag}
onSelect={handleSelect}
>
dropzone
Expand All @@ -39,7 +43,12 @@ const TestComponent: FC<Props> = (props) => {
}

describe("ReactDropzoneVV", () => {
afterEach(cleanup)
afterEach(() => {
cleanup()

acceptedFiles = []
fileRejections = []
})

describe("accept", () => {
test("sets 'accept' props in <input>", async () => {
Expand Down Expand Up @@ -97,4 +106,63 @@ describe("ReactDropzoneVV", () => {
expect(container.querySelector("input")).toHaveProperty("multiple", false)
})
})

describe("noClick", () => {
const example = (noClick: boolean) => {
const { container } = render(<TestComponent noClick={noClick} />)
const inputElem = container.querySelector("input")

if (!inputElem) throw new Error("inputElem is null")
fireEvent.change(inputElem, {
target: { files: [file] },
})
}

describe("noClick is true", () => {
test("acceptedFiles and fileRejections is empty array", async () => {
example(true)

expect(acceptedFiles.length).toBe(0)
expect(fileRejections.length).toBe(0)
})
})

describe("noClick is false", () => {
test("acceptedFiles or fileRejections is non-empty array", async () => {
example(false)

expect(acceptedFiles.length).toBe(1)
expect(fileRejections.length).toBe(0)
})
})
})

describe("noDrag", () => {
const example = (noDrag: boolean) => {
const { getByText } = render(<TestComponent noDrag={noDrag} />)
const dropzone = getByText(/dropzone/i)

fireEvent.drop(dropzone, {
dataTransfer: { files: [file] },
})
}

describe("noDrag is true", () => {
test("acceptedFiles and fileRejections is empty array", async () => {
example(true)

expect(acceptedFiles.length).toBe(0)
expect(fileRejections.length).toBe(0)
})
})

describe("noDrag is false", () => {
test("acceptedFiles or fileRejections is non-empty array", async () => {
example(false)

expect(acceptedFiles.length).toBe(1)
expect(fileRejections.length).toBe(0)
})
})
})
})
34 changes: 27 additions & 7 deletions lib/ReactDropzoneVV.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ export type ReactDropzoneVVProps = Omit<
accept?: string
disabled?: boolean
multiple?: boolean
noClick?: boolean
noDrag?: boolean
onDrop?: (files: File[]) => void
onSelect?: (props: OnSelectProps) => void
onError?: (error: Error) => void
}

export const ReactDropzoneVV: FC<ReactDropzoneVVProps> = ({
children,
reactDropzoneVV: { setIsDragging, isDragging, inputRef, openSelector },
inputProps,
accept = "",
disabled = false,
multiple = true,
reactDropzoneVV: { setIsDragging, isDragging, inputRef, openSelector },
children,
inputProps,
noClick = false,
noDrag = false,
onDrop,
onSelect,
onError,
Expand All @@ -45,38 +49,50 @@ export const ReactDropzoneVV: FC<ReactDropzoneVVProps> = ({
(event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault()
event.stopPropagation()

if (noDrag) return

setIsDragging(true)
},
[setIsDragging]
[noDrag, setIsDragging]
)

const handleDragLeaveDiv = useCallback(
(event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault()
event.stopPropagation()

if (noDrag) return

if (!event.currentTarget.contains(event.relatedTarget as Node)) {
setIsDragging(false)
}
},
[setIsDragging]
[noDrag, setIsDragging]
)

const handleDragOverDiv = useCallback(
(event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault()
event.stopPropagation()

if (noDrag) return

if (!isDragging) {
setIsDragging(true)
}
},
[isDragging, setIsDragging]
[noDrag, isDragging, setIsDragging]
)

const handleDropDiv = useCallback(
(event: React.DragEvent<HTMLDivElement>) => {
try {
event.preventDefault()
event.stopPropagation()

if (noDrag) return

setIsDragging(false)

const files = Array.from(event.dataTransfer.files)
Expand All @@ -102,15 +118,19 @@ export const ReactDropzoneVV: FC<ReactDropzoneVVProps> = ({
}
}
},
[accept, multiple, setIsDragging, onDrop, onSelect, onError]
[accept, multiple, noDrag, setIsDragging, onDrop, onSelect, onError]
)

const handleClickDiv = () => {
if (noClick) return

openSelector()
}

const handleChangeInput = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
if (noClick) return

try {
const files = Array.from(event.target.files || [])
if (files.length > 0) {
Expand Down

0 comments on commit 3a2b3d5

Please sign in to comment.