Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make alt drag work for drop sources that can take either copy or move…
… drag operations (#675) * Default dropEffect to 'copy' when altKey is pressed * Pass dropEffect as options to dropResult * Add example showing copy and move drag operations * Better prop naming * Initialize this.altKeyPressed in constructor
- Loading branch information
1 parent
9e062eb
commit 1573fe9
Showing
8 changed files
with
208 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import { DragSource } from 'react-dnd'; | ||
import ItemTypes from '../Single Target/ItemTypes'; | ||
|
||
const style = { | ||
border: '1px dashed gray', | ||
backgroundColor: 'white', | ||
padding: '0.5rem 1rem', | ||
marginRight: '1.5rem', | ||
marginBottom: '1.5rem', | ||
float: 'left', | ||
}; | ||
|
||
const boxSource = { | ||
beginDrag(props) { | ||
return { | ||
name: props.name, | ||
}; | ||
}, | ||
|
||
endDrag(props, monitor) { | ||
const item = monitor.getItem(); | ||
const dropResult = monitor.getDropResult(); | ||
|
||
if (dropResult) { | ||
let alertMessage = ''; | ||
if (dropResult.allowedDropEffect === 'any' || dropResult.allowedDropEffect === dropResult.dropEffect) { | ||
alertMessage = `You ${dropResult.dropEffect === 'copy' ? 'copied' : 'moved'} ${item.name} into ${dropResult.name}!`; | ||
} else { | ||
alertMessage = `You cannot ${dropResult.dropEffect} an item into the ${dropResult.name}`; | ||
} | ||
window.alert( // eslint-disable-line no-alert | ||
alertMessage, | ||
); | ||
} | ||
}, | ||
}; | ||
|
||
@DragSource(ItemTypes.BOX, boxSource, (connect, monitor) => ({ | ||
connectDragSource: connect.dragSource(), | ||
isDragging: monitor.isDragging(), | ||
})) | ||
export default class Box extends Component { | ||
static propTypes = { | ||
connectDragSource: PropTypes.func.isRequired, | ||
isDragging: PropTypes.bool.isRequired, | ||
name: PropTypes.string.isRequired, | ||
}; | ||
|
||
render() { | ||
const { isDragging, connectDragSource } = this.props; | ||
const { name } = this.props; | ||
const opacity = isDragging ? 0.4 : 1; | ||
|
||
return ( | ||
connectDragSource( | ||
<div style={{ ...style, opacity }}> | ||
{name} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React, { Component } from 'react'; | ||
import { DragDropContextProvider } from 'react-dnd'; | ||
import HTML5Backend from 'react-dnd-html5-backend'; | ||
import Dustbin from './Dustbin'; | ||
import Box from './Box'; | ||
|
||
export default class Container extends Component { | ||
render() { | ||
return ( | ||
<DragDropContextProvider backend={HTML5Backend}> | ||
<div> | ||
<div style={{ overflow: 'hidden', clear: 'both' }}> | ||
<Dustbin allowedDropEffect="any" /> | ||
<Dustbin allowedDropEffect="copy" /> | ||
<Dustbin allowedDropEffect="move" /> | ||
</div> | ||
<div style={{ overflow: 'hidden', clear: 'both' }}> | ||
<Box name="Glass" /> | ||
<Box name="Banana" /> | ||
<Box name="Paper" /> | ||
</div> | ||
</div> | ||
</DragDropContextProvider> | ||
); | ||
} | ||
} |
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,61 @@ | ||
import React, { PropTypes, Component } from 'react'; | ||
import { DropTarget } from 'react-dnd'; | ||
import ItemTypes from '../Single Target/ItemTypes'; | ||
|
||
const style = { | ||
height: '12rem', | ||
width: '12rem', | ||
marginRight: '1.5rem', | ||
marginBottom: '1.5rem', | ||
color: 'white', | ||
padding: '1rem', | ||
textAlign: 'center', | ||
fontSize: '1rem', | ||
lineHeight: 'normal', | ||
float: 'left', | ||
}; | ||
|
||
const boxTarget = { | ||
drop({ allowedDropEffect }) { | ||
return { | ||
name: `${allowedDropEffect} Dustbin`, | ||
allowedDropEffect, | ||
}; | ||
}, | ||
}; | ||
|
||
@DropTarget(ItemTypes.BOX, boxTarget, (connect, monitor) => ({ | ||
connectDropTarget: connect.dropTarget(), | ||
isOver: monitor.isOver(), | ||
canDrop: monitor.canDrop(), | ||
})) | ||
export default class Dustbin extends Component { | ||
static propTypes = { | ||
connectDropTarget: PropTypes.func.isRequired, | ||
isOver: PropTypes.bool.isRequired, | ||
canDrop: PropTypes.bool.isRequired, | ||
allowedDropEffect: PropTypes.string.isRequired, | ||
}; | ||
|
||
render() { | ||
const { canDrop, isOver, allowedDropEffect, connectDropTarget } = this.props; | ||
const isActive = canDrop && isOver; | ||
|
||
let backgroundColor = '#222'; | ||
if (isActive) { | ||
backgroundColor = 'darkgreen'; | ||
} else if (canDrop) { | ||
backgroundColor = 'darkkhaki'; | ||
} | ||
|
||
return connectDropTarget( | ||
<div style={{ ...style, backgroundColor }}> | ||
{ `Works with ${allowedDropEffect} drop effect` }<br /><br /> | ||
{isActive ? | ||
'Release to drop' : | ||
'Drag a box here' | ||
} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, { Component } from 'react'; | ||
import Container from './Container'; | ||
|
||
export default class DustbinCopyOrMove extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<p> | ||
<b><a href="https://github.com/react-dnd/react-dnd/tree/master/examples/01%20Dustbin/Copy%20or%20Move">Browse the Source</a></b> | ||
</p> | ||
<p> | ||
This example demonstrates drop targets that can accept copy and move drop effects, which users can switch between by holding down or releasing the alt key as they drag. | ||
</p> | ||
<p> | ||
In a todo list app, for example, the default drag and drop operation could be used to sort the list, while holding down the alt key while dragging and dropping could copy the todo item to the drop target instead of moving it. | ||
</p> | ||
<Container /> | ||
</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
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