Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass generic type arguments through to DropTargetMonitor #3300

Merged
merged 4 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .yarn/versions/e9a7807a.yml
@@ -0,0 +1,5 @@
releases:
react-dnd: patch
react-dnd-documentation: patch
react-dnd-examples: patch
react-dnd-test-utils: patch
12 changes: 8 additions & 4 deletions packages/examples/src/04-sortable/simple/Card.tsx
@@ -1,7 +1,7 @@
import { FC, useRef } from 'react'
import { useDrag, useDrop, DropTargetMonitor } from 'react-dnd'
import { useDrag, useDrop } from 'react-dnd'
import { ItemTypes } from './ItemTypes'
import { XYCoord } from 'dnd-core'
import { XYCoord, Identifier } from 'dnd-core'

const style = {
border: '1px dashed gray',
Expand All @@ -26,14 +26,18 @@ interface DragItem {

export const Card: FC<CardProps> = ({ id, text, index, moveCard }) => {
const ref = useRef<HTMLDivElement>(null)
const [{ handlerId }, drop] = useDrop({
const [{ handlerId }, drop] = useDrop<
DragItem,
void,
{ handlerId: Identifier | null }
>({
accept: ItemTypes.CARD,
collect(monitor) {
return {
handlerId: monitor.getHandlerId(),
}
},
hover(item: DragItem, monitor: DropTargetMonitor) {
hover(item: DragItem, monitor) {
if (!ref.current) {
return
}
Expand Down
16 changes: 12 additions & 4 deletions packages/react-dnd/src/hooks/types.ts
Expand Up @@ -113,7 +113,7 @@ export interface DropTargetHookSpec<DragObject, DropResult, CollectedProps> {
*/
drop?: (
item: DragObject,
monitor: DropTargetMonitor,
monitor: DropTargetMonitor<DragObject, DropResult>,
) => DropResult | undefined

/**
Expand All @@ -122,17 +122,25 @@ export interface DropTargetHookSpec<DragObject, DropResult, CollectedProps> {
* the hover happens over just the current target, or over a nested one. Unlike drop(), this method will be called even
* if canDrop() is defined and returns false. You can check monitor.canDrop() to test whether this is the case.
*/
hover?: (item: DragObject, monitor: DropTargetMonitor) => void
hover?: (
item: DragObject,
monitor: DropTargetMonitor<DragObject, DropResult>,
) => void

/**
* Optional. Use it to specify whether the drop target is able to accept the item. If you want to always allow it, just
* omit this method. Specifying it is handy if you'd like to disable dropping based on some predicate over props or
* monitor.getItem(). Note: You may not call monitor.canDrop() inside this method.
*/
canDrop?: (item: DragObject, monitor: DropTargetMonitor) => boolean
canDrop?: (
item: DragObject,
monitor: DropTargetMonitor<DragObject, DropResult>,
) => boolean

/**
* A function to collect rendering properties
*/
collect?: (monitor: DropTargetMonitor) => CollectedProps
collect?: (
monitor: DropTargetMonitor<DragObject, DropResult>,
) => CollectedProps
}
6 changes: 5 additions & 1 deletion packages/react-dnd/src/hooks/useDrag/useDrag.ts
Expand Up @@ -13,7 +13,11 @@ import { invariant } from '@react-dnd/invariant'
* @param sourceSpec The drag source specification (object or function, function preferred)
* @param deps The memoization deps array to use when evaluating spec changes
*/
export function useDrag<DragObject, DropResult, CollectedProps>(
export function useDrag<
DragObject = unknown,
DropResult = unknown,
CollectedProps = unknown,
>(
specArg: FactoryOrInstance<
DragSourceHookSpec<DragObject, DropResult, CollectedProps>
>,
Expand Down
6 changes: 5 additions & 1 deletion packages/react-dnd/src/hooks/useDrop/useDrop.ts
Expand Up @@ -12,7 +12,11 @@ import { useConnectDropTarget } from './connectors'
* @param spec The drop target specification (object or function, function preferred)
* @param deps The memoization deps array to use when evaluating spec changes
*/
export function useDrop<DragObject, DropResult, CollectedProps>(
export function useDrop<
DragObject = unknown,
DropResult = unknown,
CollectedProps = unknown,
>(
specArg: FactoryOrInstance<
DropTargetHookSpec<DragObject, DropResult, CollectedProps>
>,
Expand Down