Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/use layout effects for registration (#1437)
* fix: use useLayoutEffect for item registration * fix: use layout effects when attaching to dnd-core bring in changes from #1429 * docs: add test case based on codesandbox sample in reported issue
- Loading branch information
1 parent
ce58e73
commit a1fbcd2
Showing
17 changed files
with
265 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/documentation/docsite/markdown/examples/other/drag-source-remount.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
path: '/examples/other/remount-with-correct-props' | ||
title: 'Drag Source Remounts with Correct Props' | ||
--- | ||
|
||
Regression example based on [#1429](https://github.com/react-dnd/react-dnd/pull/1429) that verifies that as drag-sources are remounted, they receive their correct props from React-DnD. | ||
|
||
<view-source name="06-other/remount-with-correct-props" component="other-remount-with-correct-props"> | ||
</view-source> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...es/documentation/examples-decorators/src/06-other/remount-with-correct-props/ItemTypes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
BOX: 'box', | ||
} |
45 changes: 45 additions & 0 deletions
45
...s/documentation/examples-decorators/src/06-other/remount-with-correct-props/SourceBox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react' | ||
import { DragSource, ConnectDragSource } from 'react-dnd' | ||
import ItemTypes from './ItemTypes' | ||
|
||
const getStyle = (isDragging: boolean) => ({ | ||
display: 'inline-block', | ||
padding: '0.5rem 1rem', | ||
cursor: 'pointer', | ||
border: '1px dashed gray', | ||
backgroundColor: isDragging ? 'red' : undefined, | ||
opacity: isDragging ? 0.4 : 1, | ||
}) | ||
|
||
export interface SourceBoxProps { | ||
id: string | ||
isDragging: boolean | ||
connectDragSource: ConnectDragSource | ||
onBeginDrag: () => void | ||
onEndDrag: () => void | ||
} | ||
const SourceBox: React.FC<SourceBoxProps> = ({ | ||
isDragging, | ||
connectDragSource, | ||
}) => { | ||
return connectDragSource(<div style={getStyle(isDragging)}>Drag me</div>) | ||
} | ||
|
||
export default DragSource( | ||
ItemTypes.BOX, | ||
{ | ||
beginDrag: (props: SourceBoxProps) => { | ||
props.onBeginDrag() | ||
return { id: props.id } | ||
}, | ||
endDrag: (props: SourceBoxProps) => { | ||
props.onEndDrag() | ||
}, | ||
isDragging: (props: SourceBoxProps, monitor) => | ||
monitor.getItem().id === props.id, | ||
}, | ||
(connect, monitor) => ({ | ||
connectDragSource: connect.dragSource(), | ||
isDragging: monitor.isDragging(), | ||
}), | ||
)(SourceBox) |
31 changes: 31 additions & 0 deletions
31
...s/documentation/examples-decorators/src/06-other/remount-with-correct-props/TargetBox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react' | ||
import { DropTarget, ConnectDropTarget } from 'react-dnd' | ||
import ItemTypes from './ItemTypes' | ||
|
||
const style: React.CSSProperties = { | ||
border: '1px solid gray', | ||
height: '15rem', | ||
width: '15rem', | ||
padding: '2rem', | ||
textAlign: 'center', | ||
} | ||
|
||
export interface TargetBoxProps { | ||
connectDropTarget: ConnectDropTarget | ||
isActive: boolean | ||
} | ||
const TargetBox: React.FC<TargetBoxProps> = ({ | ||
connectDropTarget, | ||
isActive, | ||
}) => { | ||
return connectDropTarget( | ||
<div style={style}>{isActive ? 'Release to drop' : 'Drag item here'}</div>, | ||
) | ||
} | ||
|
||
export default DropTarget(ItemTypes.BOX, {}, (connect, monitor) => { | ||
return { | ||
connectDropTarget: connect.dropTarget(), | ||
isActive: monitor.isOver() && monitor.canDrop(), | ||
} | ||
})(TargetBox) |
43 changes: 43 additions & 0 deletions
43
packages/documentation/examples-decorators/src/06-other/remount-with-correct-props/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { useState, useEffect, useCallback } from 'react' | ||
import SourceBox from './SourceBox' | ||
import TargetBox from './TargetBox' | ||
|
||
export default function Container() { | ||
const [isMounted, setIsMounted] = useState(true) | ||
const [isDragging, setIsDragging] = useState(false) | ||
useEffect( | ||
function subscribeToIntervalTick() { | ||
if (!isDragging) { | ||
setIsMounted(true) | ||
return | ||
} | ||
const interval = setInterval(() => setIsMounted(!isMounted), 500) | ||
return () => clearInterval(interval) | ||
}, | ||
[isMounted, isDragging], | ||
) | ||
const handleBeginDrag = useCallback(() => setIsDragging(true), []) | ||
const handleEndDrag = useCallback(() => setIsDragging(false), []) | ||
|
||
return ( | ||
<> | ||
<div style={{ height: 100 }}> | ||
<div style={{ marginTop: 10 }}> | ||
{isMounted && ( | ||
<SourceBox | ||
id="1" | ||
onBeginDrag={handleBeginDrag} | ||
onEndDrag={handleEndDrag} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
<div> | ||
<div>Target</div> | ||
<div style={{ marginTop: 10 }}> | ||
<TargetBox /> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/documentation/examples-hooks/src/06-other/remount-with-correct-props/ItemTypes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
BOX: 'box', | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/documentation/examples-hooks/src/06-other/remount-with-correct-props/SourceBox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react' | ||
import { useDrag } from 'react-dnd' | ||
import ItemTypes from './ItemTypes' | ||
|
||
const getStyle = (isDragging: boolean) => ({ | ||
display: 'inline-block', | ||
padding: '0.5rem 1rem', | ||
cursor: 'pointer', | ||
border: '1px dashed gray', | ||
backgroundColor: isDragging ? 'red' : undefined, | ||
opacity: isDragging ? 0.4 : 1, | ||
}) | ||
|
||
export interface SourceBoxProps { | ||
id: string | ||
onBeginDrag: () => void | ||
onEndDrag: () => void | ||
} | ||
export const SourceBox: React.FC<SourceBoxProps> = ({ | ||
id, | ||
onBeginDrag, | ||
onEndDrag, | ||
}) => { | ||
const [{ isDragging }, drag] = useDrag({ | ||
item: { type: ItemTypes.BOX, id }, | ||
isDragging: monitor => monitor.getItem().id === id, | ||
collect: monitor => ({ isDragging: monitor.isDragging() }), | ||
begin: onBeginDrag, | ||
end: onEndDrag, | ||
}) | ||
|
||
return ( | ||
<div ref={drag} style={getStyle(isDragging)}> | ||
Drag me | ||
</div> | ||
) | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/documentation/examples-hooks/src/06-other/remount-with-correct-props/TargetBox.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react' | ||
import { useDrop } from 'react-dnd' | ||
import ItemTypes from './ItemTypes' | ||
|
||
const style: React.CSSProperties = { | ||
border: '1px solid gray', | ||
height: '15rem', | ||
width: '15rem', | ||
padding: '2rem', | ||
textAlign: 'center', | ||
} | ||
|
||
const TargetBox: React.FC = () => { | ||
const [{ isActive }, drop] = useDrop({ | ||
accept: ItemTypes.BOX, | ||
collect: monitor => ({ | ||
isActive: monitor.canDrop() && monitor.isOver(), | ||
}), | ||
}) | ||
return ( | ||
<div ref={drop} style={style}> | ||
{isActive ? 'Release to drop' : 'Drag item here'} | ||
</div> | ||
) | ||
} | ||
|
||
export default TargetBox |
43 changes: 43 additions & 0 deletions
43
packages/documentation/examples-hooks/src/06-other/remount-with-correct-props/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { useState, useEffect, useCallback } from 'react' | ||
import { SourceBox } from './SourceBox' | ||
import TargetBox from './TargetBox' | ||
|
||
export default function Container() { | ||
const [isMounted, setIsMounted] = useState(true) | ||
const [isDragging, setIsDragging] = useState(false) | ||
useEffect( | ||
function subscribeToIntervalTick() { | ||
if (!isDragging) { | ||
setIsMounted(true) | ||
return | ||
} | ||
const interval = setInterval(() => setIsMounted(!isMounted), 500) | ||
return () => clearInterval(interval) | ||
}, | ||
[isMounted, isDragging], | ||
) | ||
const handleBeginDrag = useCallback(() => setIsDragging(true), []) | ||
const handleEndDrag = useCallback(() => setIsDragging(false), []) | ||
|
||
return ( | ||
<> | ||
<div style={{ height: 100 }}> | ||
<div style={{ marginTop: 10 }}> | ||
{isMounted && ( | ||
<SourceBox | ||
id="1" | ||
onBeginDrag={handleBeginDrag} | ||
onEndDrag={handleEndDrag} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
<div> | ||
<div>Target</div> | ||
<div style={{ marginTop: 10 }}> | ||
<TargetBox /> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters