-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add (broken) nested drag source example
- Loading branch information
Showing
6 changed files
with
215 additions
and
1 deletion.
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,6 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
YELLOW: 'yellow', | ||
BLUE: 'blue' | ||
}; |
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,35 @@ | ||
'use strict'; | ||
|
||
var React = require('react'), | ||
makeSource = require('./makeSource'), | ||
Colors = require('./Colors'), | ||
Target = require('./Target'); | ||
|
||
var Container = React.createClass({ | ||
render() { | ||
var Yellow = makeSource(Colors.YELLOW), | ||
BlueSource = makeSource(Colors.BLUE); | ||
|
||
return ( | ||
<div style={{ height: 320 }}> | ||
<div style={{ float: 'left' }}> | ||
<BlueSource> | ||
<Yellow> | ||
<Yellow /> | ||
<BlueSource /> | ||
</Yellow> | ||
<BlueSource> | ||
<Yellow /> | ||
</BlueSource> | ||
</BlueSource> | ||
</div> | ||
|
||
<div style={{ float: 'left', marginLeft: 100, marginTop: 50 }}> | ||
<Target /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
module.exports = Container; |
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,75 @@ | ||
'use strict'; | ||
|
||
var React = require('react'), | ||
Colors = require('./Colors'), | ||
{ DragDropMixin } = require('react-dnd'); | ||
|
||
function makeDropTarget(color) { | ||
return { | ||
acceptDrop(component) { | ||
component.setState({ | ||
lastDroppedColor: color | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
var Target = React.createClass({ | ||
mixins: [DragDropMixin], | ||
|
||
statics: { | ||
configureDragDrop(register) { | ||
register(Colors.YELLOW, { | ||
dropTarget: makeDropTarget(Colors.YELLOW) | ||
}); | ||
|
||
register(Colors.BLUE, { | ||
dropTarget: makeDropTarget(Colors.BLUE) | ||
}); | ||
} | ||
}, | ||
|
||
getInitialState() { | ||
return { | ||
lastDroppedColor: null | ||
}; | ||
}, | ||
|
||
render() { | ||
var { lastDroppedColor } = this.state, | ||
blueDropState = this.getDropState(Colors.BLUE), | ||
yellowDropState = this.getDropState(Colors.YELLOW), | ||
isDragging = blueDropState.isDragging || yellowDropState.isDragging, | ||
isHovering = blueDropState.isHovering || yellowDropState.isHovering, | ||
backgroundColor = '#fff', | ||
opacity = isHovering ? 1 : 0.7; | ||
|
||
if (blueDropState.isDragging) { | ||
backgroundColor = 'lightblue'; | ||
} else if (yellowDropState.isDragging) { | ||
backgroundColor = 'lightgoldenrodyellow'; | ||
} | ||
|
||
return ( | ||
<div {...this.dropTargetFor(Colors.YELLOW, Colors.BLUE)} | ||
style={{ | ||
backgroundColor: backgroundColor, | ||
opacity: opacity, | ||
border: '1px dashed gray', | ||
height: '12rem', | ||
width: '12rem', | ||
padding: '2rem', | ||
textAlign: 'center' | ||
}}> | ||
|
||
<p>Drop here.</p> | ||
|
||
{!isDragging && lastDroppedColor && | ||
<p>Last dropped: {lastDroppedColor}</p> | ||
} | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
module.exports = Target; |
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,20 @@ | ||
'use strict'; | ||
|
||
var React = require('react'), | ||
Container = require('./Container'); | ||
|
||
var NestingSources = React.createClass({ | ||
render() { | ||
return ( | ||
<div> | ||
<Container /> | ||
<hr /> | ||
<p> | ||
You can nest drag sources in one another. If a nested drag source returns false in <code>canDrag</code>, its parent will be asked, until a draggable source is found and activated. | ||
</p> | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
module.exports = NestingSources; |
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,75 @@ | ||
'use strict'; | ||
|
||
var React = require('react'), | ||
Colors = require('./Colors'), | ||
LinkedStateMixin = require('react/lib/LinkedStateMixin'), | ||
{ DragDropMixin } = require('react-dnd'); | ||
|
||
function makeSource(sourceColor) { | ||
var dragSource = { | ||
canDrag(component) { | ||
return !component.state.forbidDrag; | ||
}, | ||
|
||
beginDrag() { | ||
return { | ||
item: { } | ||
}; | ||
} | ||
}; | ||
|
||
var Source = React.createClass({ | ||
mixins: [DragDropMixin, LinkedStateMixin], | ||
|
||
statics: { | ||
configureDragDrop(register) { | ||
register(sourceColor, { | ||
dragSource: dragSource | ||
}); | ||
} | ||
}, | ||
|
||
getInitialState() { | ||
return { | ||
forbidDrag: false | ||
}; | ||
}, | ||
|
||
render() { | ||
var { isDragging } = this.getDragState(sourceColor), | ||
backgroundColor; | ||
|
||
switch (sourceColor) { | ||
case Colors.YELLOW: | ||
backgroundColor = 'lightgoldenrodyellow'; | ||
break; | ||
case Colors.BLUE: | ||
backgroundColor = 'lightblue'; | ||
break; | ||
} | ||
|
||
return ( | ||
<div {...this.dragSourceFor(sourceColor)} | ||
style={{ | ||
border: '1px dashed gray', | ||
backgroundColor: backgroundColor, | ||
padding: '0.5rem', | ||
margin: '0.5rem', | ||
opacity: isDragging ? 0.4 : 1 | ||
}}> | ||
|
||
<input type='checkbox' | ||
checkedLink={this.linkState('forbidDrag')}> | ||
Forbid drag | ||
</input> | ||
|
||
{this.props.children} | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
return Source; | ||
} | ||
|
||
module.exports = makeSource; |
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