Retrieve Prop Values from Imported Custom Component - reactjs

So I have a custom component (TextButton) and I have packaged it inside another component (ContainedButton). I'm currently attempting to style ContainedButton. However, I want to access the value of a prop of TextButton (theme) and use the value when styling ContainedButton.
ContainedButton:
import React, { Component } from 'react';
import TextButton from './TextButton';
class ContainedButton extends Component {
render() {
const { style } = this.props;
return (
<TextButton {...this.props} style={[styles.containedButtonStyle, style]} />
);
}
}
const styles = {
containedButtonStyle: {
backgroundColor: (TextButton prop theme)
padding: 2,
borderWidth: 1,
borderRadius: 5
}
};
export default ContainedButton;
In the parentheses next to 'backgroundColor', I want to insert the value of the theme prop located in TextButton. How would I achieve something like this?
TextButton (in case it's needed):
import React, { Component } from 'react';
import { Text, TouchableOpacity } from 'react-native';
import PropTypes from 'prop-types';
class TextButton extends Component {
constructor(props) {
super(props);
this.state = {};
}
componentWillMount() {}
componentWillReceiveProps(newProps) {
if (newProps.theme !== this.props.theme) {
this.determineTheme(newProps.theme);
}
if (newProps.size !== this.props.size) {
this.determineSize(newProps.size);
}
}
// set the theme
determineTheme = function (theme) {
if (theme === 'primary') {
return {
color: '#0098EE'
};
} else if (theme === 'secondary') {
return {
color: '#E70050'
};
} else if (theme === 'default') {
return {
color: '#E0E0E0'
};
}
return {
color: '#E0E0E0'
};
}
// set the size
determineSize = function (size) {
if (size === 'small') {
return {
fontSize: 16
};
} else if (size === 'medium') {
return {
fontSize: 22
};
} else if (size === 'large') {
return {
fontSize: 28
};
}
return {
fontSize: 22
};
}
render() {
const { onPress, children, theme, size, style } = this.props;
return (
<TouchableOpacity onPress={onPress} style={style}>
<Text style={[this.determineTheme(theme), this.determineSize(size)]}>{children}</Text>
</TouchableOpacity>
);
}
}
TextButton.propTypes = {
onPress: PropTypes.func,
title: PropTypes.string,
theme: PropTypes.string,
size: PropTypes.string
};
export default TextButton;

You can get the value of theme in the same way you get the value of style:
const { theme } = this.props;
Or combine them into a single statement:
const { style, theme } = this.props;

Related

I am trying to implement a way to delete swappable items, but my delete method removes all the items instead of just the one that is clicked

I have two components, App.js which holds my "widget" layout and SwappableComponent.js which creates each swappable widget. I am trying to implement a delete function but when I click on the delete button what happens is it deletes all the swappable components instead of just the one that is clicked on. Any help would be appreciated.
import React, { Component } from 'react';
import Swappable from './components/SwappableComponent'
import './App.css';
import DataTable from './components/tableWidget';
import PropTypes from "prop-types";
import { withStyles } from "#material-ui/core/styles";
import Paper from "#material-ui/core/Paper";
import Grid from "#material-ui/core/Grid";
const styles = theme => ({
root: {
flexGrow: 1
},
paper: {
padding: theme.spacing.unit * 2,
textAlign: "center",
color: theme.palette.text.secondary
}
});
class App extends Component {
constructor(props) {
super(props);
this.state={
widgets:[
{id:1, content: <DataTable/>},
{id:2, content: "#2"},
{id:3, content: "#3"},
{id:4, content: "#4"}
]
}
}
deleteEvent=(index)=>{
const copyWidgets=Object.assign([],this.state.widgets);
copyWidgets.splice(index);
this.setState({
widgets:copyWidgets
})
}
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
<Grid container spacing={24}>
{
this.state.widgets.map((widget,index)=>{
return(
<Grid item xs={12} sm={6}>
<Paper className={classes.paper}><Swappable id={widget.id} content={widget.content} delete={this.deleteEvent.bind(this,index)}/></Paper>
</Grid>
)
})
}
</Grid>
</div>
);
}
}
App.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(App);
import React, { Component } from 'react'
class Swappable extends Component {
constructor() {
super()
this.state = {
customFunc: null
}
}
allowDrop(ev) {
ev.preventDefault();
}
drag(ev, customFunc = null) {
ev.dataTransfer.setData("src", ev.target.id);
console.log(ev.target.parentNode, 'TARGET DRAGSTART')
this.setState({
initialParentNode: ev.target.parentNode
})
}
dragEnd(ev, customFunc = null) {
console.log(ev.target.parentNode, 'TARGET DRAGEND')
if (customFunc && (ev.target.parentNode != this.state.initialParentNode)) {
console.log('custom func')
this.props.customFunc()
}
}
drop(ev, dragableId, dropzoneId, customFunc = null, swappable = true) {
ev.preventDefault();
let src = document.getElementById(ev.dataTransfer.getData("src"));
let srcParent = src.parentNode;
let target = document.getElementById(dragableId);
console.log(src, 'dragged element');
console.log(srcParent, 'parent of dragged');
console.log(target, 'element to be swapped')
swappable ? this.swapElements(src, target, srcParent) : this.transferElement(src, dropzoneId)
}
swapElements(src, target, srcParent) {
target.replaceWith(src);
srcParent.appendChild(target);
}
transferElement(src, dropzoneId) {
let dropzone = document.getElementById(dropzoneId)
dropzone.appendChild(src);
}
render() {
const dropZoneStyle = {
width: '450px',
minHeight: '300px',
padding: '10px',
border: ''
};
const draggableStyle = {
width: '400px',
height: '300px',
padding: '10px',
border: ''
};
const { id, content, swappable, customFunc } = this.props
const dropzoneId = 'drop' + id
const dragableId = 'drag' + id
console.log(customFunc, 'customFunc')
return (
<div
id = {dropzoneId}
onDrop={(event) => this.drop(event, dragableId, dropzoneId, customFunc, swappable)}
onDragOver={(event) => this.allowDrop(event)}
style={dropZoneStyle}>
<div id={ dragableId }
draggable="true"
onDragStart={(event) => this.drag(event)}
onDragEnd = {(event) => this.dragEnd(event, customFunc)}
style={draggableStyle}>
{ content }
<button onClick={this.props.delete}>Delete</button>
</div>
</div>
)
}
}
export default Swappable;
The reason why all of the items are removed is that you didn't specify a second argument to the .splice function which is the number of items to delete from the array or the deleteCount.
From MDN docs:
If deleteCount is omitted, or if its value is equal to or larger than
array.length - start (that is, if it is equal to or greater than the
number of elements left in the array, starting at start), then all of
the elements from start through the end of the array will be deleted.
To fix it, modify your deleteEvent function to the following:
deleteEvent = (index) => {
const copyWidgets = Object.assign([], this.state.widgets);
copyWidgets.splice(index, 1); // delete one item only
this.setState({
widgets: copyWidgets
});
};
A simple example to show the different behaviour:
console.log('without using second argument with splice');
const letters = ['a', 'b', 'c'];
console.log('before', letters);
letters.splice(0);
console.log('after', letters);
console.log('using second argument with splice');
const numbers = [1, 2, 3];
console.log('before', numbers);
numbers.splice(1, 1);
console.log('after splice', numbers);

Multi select dropdown in react native

I am new to react native. Can anyone suggest how do i implement multiple select dropdown in react native. I have tried MultiSelect (https://github.com/toystars/react-native-multiple-select) from react-native-multiple-select but it is not working.
This is a source code of implemented multiselect source list
import React from 'react';
import {View, Text, StyleSheet, FlatList, TouchableHighlight, Dimensions} from 'react-native';
var thisObj;
var deviceHeight = Dimensions.get("window").height;
class MyListItem extends React.PureComponent {
render() {
return (
<View style={{flex: 1}}>
<TouchableHighlight onPress={this.props.onPress.bind(this)} underlayColor='#616161'>
<Text style={this.props.style}>{this.props.item.key}</Text>
</TouchableHighlight>
</View>
);
}
}
export default class MultiSelect extends React.Component {
constructor(props) {
super(props);
var selectedItemsObj = {};
if(this.props.selectedItems) {
var items = this.props.selectedItems.split(',');
items.forEach(function(item) {
selectedItemsObj[item] = true;
});
}
this.state = {
selectedItems: selectedItemsObj
};
}
onItemPressed(item) {
var oldSelectedItems = this.state.selectedItems;
var itemState = oldSelectedItems[item.key];
if(!itemState) {
oldSelectedItems[item.key] = true;
}
else {
var newState = itemState? false: true;
oldSelectedItems[item.key] = newState;
}
this.setState({
selectedItems: oldSelectedItems,
});
var arrayOfSelectedItems = [];
var joinedItems = Object.keys(oldSelectedItems);
joinedItems.forEach(function(key) {
if(oldSelectedItems[key])
arrayOfSelectedItems.push(key);
});
var selectedItem = null;
if(arrayOfSelectedItems.length > 0)
selectedItem = arrayOfSelectedItems.join();
this.props.onValueChange(selectedItem);
}
componentWillMount() {
thisObj = this;
}
getStyle(item) {
try {
console.log(thisObj.state.selectedItems[item.key]);
return thisObj.state.selectedItems[item.key]? styles.itemTextSelected : styles.itemText;
} catch(e) {
return styles.itemText;
}
}
_renderItem = ({item}) => {
return (<MyListItem style={this.getStyle(item)} onPress={this.onItemPressed.bind(this, item)} item={item} />);
}
render() {
return (
<View style={styles.rootView}>
<FlatList style={styles.list}
initialNumToRender={10}
extraData={this.state}
data={this.props.data}
renderItem={this._renderItem.bind(this)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
rootView : {
height: deviceHeight / 2
},
itemText: {
padding: 8,
color: "#fff"
},
itemTextSelected: {
padding: 8,
color: "#fff",
backgroundColor: '#757575'
},
list: {
flex: 1,
}
});
This is how the component should be used
this.state = {
selectedItem : null,
data: [{key:"key1", label:"label1"}, {key:"key2", label:"label2"}]
}
...
<MultiSelect
data={this.state.data}
selectedItems={this.state.selectedItem}
onValueChange={ (itemValue) => thisObj.setState({selectedItem: itemValue})}/>
Selected values will be joined and set in this.state.selectedItem field
I have implemented React Native component.
Source code is attached.
It shows how to make list checkable.
It may be a base for your solution.
Please see.
import React from 'react';
import {View, Text, StyleSheet, FlatList, TouchableHighlight} from 'react-native';
var thisObj;
export default class MultiSelect extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedItems: {}
};
}
onItemPressed(item) {
var oldSelectedItems = this.state.selectedItems;
var itemState = oldSelectedItems[item.key];
if(!itemState) {
oldSelectedItems[item.key] = true;
}
else {
var newState = itemState? false: true;
oldSelectedItems[item.key] = newState;
}
this.setState({
selectedItems: oldSelectedItems,
});
}
componentDidMount() {
thisObj = this;
}
getStyle(item) {
try {
console.log(thisObj.state.selectedItems[item.key]);
return thisObj.state.selectedItems[item.key]? styles.itemTextSelected : styles.itemText;
} catch(e) {
return styles.itemText;
}
}
render() {
return (
<View style={styles.rootView}>
<FlatList style={styles.list}
extraData={this.state}
data={this.props.data}
renderItem={({item}) =>
<TouchableHighlight onPress={this.onItemPressed.bind(this, item)} underlayColor='#f00'>
<Text style={this.getStyle(item)}>{item.key}</Text>
</TouchableHighlight>
}
/>
</View>
);
}
}
const styles = StyleSheet.create({
rootView : {
},
itemText: {
padding: 16,
color: "#fff"
},
itemTextSelected: {
padding: 16,
color: "#fff",
backgroundColor: '#f00'
},
list: {
}
});
How to use this
<Multiselect data = { [{"key" : "item1"}, {"key" : "item2"}{"key" : "item3"}]
}\>

React Native Failed to ImportScripts Error

I'm getting an ImportScripts error which means I'm not importing or exporting something correctly I'm guessing. I've narrowed it down to the import { getPath } from '~/redux/modules/camera' line. But I'm not sure why I get an error. I import connect so I have access to dispatch and then I import the getPath function. What else should I be doing? Thanks!
import React, { PropTypes, Component } from 'react';
import {
Dimensions,
StyleSheet,
Text,
TouchableHighlight,
View
} from 'react-native';
import Camera from 'react-native-camera'
import { connect } from 'react-redux'
import { getPath } from '~/redux/modules/camera'
class NimbusCamera extends Component {
static propTypes = {
navigator: PropTypes.object.isRequired,
dispatch: PropTypes.func.isRequired,
}
state = {
camera: {
aspect: Camera.constants.Aspect.fill,
captureTarget: Camera.constants.CaptureTarget.disk,
type: Camera.constants.Type.front,
orientation: Camera.constants.Orientation.auto,
flashMode: Camera.constants.FlashMode.auto,
}
isRecording: false,
timeLeft: 30,
limitReached: false
}
render() {
console.log(this.props)
return (
<View style={styles.container}>
<Camera
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
aspect={this.state.camera.aspect}
type={this.state.camera.type}
captureTarget={this.state.camera.captureTarget}
captureAudio={true}
flashMode={this.state.camera.flashMode}
>
<Text style={styles.capture} onPress={this.startRecording.bind(this)}>[CAPTURE]</Text>
<Text style={styles.capture} onPress={this.stopRecording.bind(this)}>[STOP_RECORDING]</Text>
</Camera>
</View>
);
}
startRecording = () => {
if (this.camera) {
this.camera.capture({mode: Camera.constants.CaptureMode.video})
.then((data) => console.log(data))
.catch(err => console.error(err));
this.setState({
isRecording: true
});
let timerId = setInterval(countdown, 1000);
function countdown() {
if (this.state.timeLeft === 0) {
clearTimeout(timerId);
this.setState({isRecording: false})
} else {
this.setState({timeLeft: this.state.timeLeft--})
}
}
}
}
stopRecording = () => {
if (this.camera) {
this.camera.stopCapture();
this.setState({
isRecording: false
});
}
}
}
export default connect()(NimbusCamera)
const styles = StyleSheet.create({
container: {
flex: 1
},
preview: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center',
height: Dimensions.get('window').height,
width: Dimensions.get('window').width
},
capture: {
flex: 0,
backgroundColor: '#fff',
borderRadius: 5,
color: '#000',
padding: 10,
margin: 40
}
});
Here is my redux camera module.
const GET_PATH = 'GET_PATH'
const CLEAR_PATH = 'CLEAR_PATH'
initialState = {
videoPath: ''
}
export function getPath (path) {
return {
type: GET_PATH,
path
}
}
export function clearPath () {
return {
type: CLEAR_PATH
}
}
export default function camera (state = initialState, action) {
switch (action.type) {
case GET_PATH :
return {
...state,
videoPath: action.path
}
case CLEAR_PATH :
return {
...state,
videoPath: ''
}
default :
return state
}
}
Any log in your packager console? usually it print much more detail error info.
errors like these pops-up also when you make a typo in your code. Then module cannot be properly imported. Check your code for typos first :)

monitor.getDropResult() return null

monitor.getDropResult() returns null (I look it console.log). It should return object(dragged item) with its position. Why does it return null?
I signature my component with DragSource,DropTarget..but it still returns null
Here is my entire component code:
import React, { PropTypes } from 'react';
import { DragSource } from 'react-dnd';
import { getEmptyImage } from 'react-dnd-html5-backend';
import { StoneConstants, StoneTypes, ItemTypes } from 'constants/AppConstants';
import OkeyStoneBase from 'components/OkeyStoneBase/OkeyStoneBase';
import './_OkeyStone.scss';
function checkForDragAction(props) {
// TODO receive the action via prop
if (props.stoneType === StoneTypes.ON_WITHDRAW_MIDDLE) {
props.onWithdrawMiddle();
} else if (props.stoneType === StoneTypes.ON_DISCARD_WEST) {
props.onWithdrawLeft();
}
}
function applyDropResult(props, result) {
if (props.stoneType === StoneTypes.ON_WITHDRAW_MIDDLE || props.stoneType === StoneTypes.ON_DISCARD_WEST) {
if (result === null) { //taşı tahtaya koymadıysa
}
props.withdrawRequestAtPosition(result.top, result.left);
}
if (props.stoneType === StoneTypes.ON_RAKE) {
if (result && result.stackType === StoneTypes.ON_DISCARD_SOUTH) {
props.onDiscardStone({
id: props.id,
number: props.number,
color: props.color
});
}
}
}
const stoneSource = {
canDrag(props) {
return props.draggable;
},
beginDrag(props) {
if (props.onBeginDrag) {
props.onBeginDrag();
}
checkForDragAction(props);
return {
id: props.id,
stoneType: props.stoneType,
left: props.left,
top: props.top,
color: props.color,
number: props.number
};
},
endDrag(props, monitor) {
if (props.onEndDrag) {
props.onEndDrag();
}
console.log(props.onEndDrag);
console.log(monitor);
***var result = monitor.getDropResult();***
console.log('stoneSource'+result);
applyDropResult(props, result);
}
};
function collect(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging()
};
}
function getStyles(props) {
const scale = StoneConstants.MINI_SCALE;
let { left, top, isDragging, isMini } = props;
const { zIndex } = props;
const { canTransition } = props;
let transform = `translate3d(${left}px, ${top}px, 0)`;
if (isMini) {
transform += ` scale(${scale}, ${scale})`;
}
let result = {
transformOrigin: 'top left',
transform: transform,
WebkitTransform: transform,
zIndex: zIndex,
opacity: isDragging ? 0 : 1,
height: isDragging ? 0 : ''
};
if (isDragging || !canTransition) {
result.transition = 'none';
}
return result;
}
class OkeyStone extends React.Component {
handleStoneClick (e) {
const { id, onClicked } = this.props;
if (onClicked) {
e.stopPropagation();
onClicked(id);
}
}
componentDidMount() {
this.props.connectDragPreview(getEmptyImage(), {
captureDraggingState: true
});
}
render() {
let { connectDragSource } = this.props;
let { number, color } = this.props;
let { isStable, isSelected } = this.props;
let stableStyle = isStable ? 'okey-stone-stable' : '';
return connectDragSource(
<div className={'okey-stone-parent ' + stableStyle}
onClick={this.handleStoneClick}
style={getStyles(this.props)}>
<OkeyStoneBase number={number} color={color} isSelected={isSelected}/>
</div>
);
}
}
OkeyStone.propTypes = {
connectDragSource: PropTypes.func,
connectDragPreview: PropTypes.func,
isDragging: PropTypes.bool,
id: PropTypes.number,
left: PropTypes.number,
top: PropTypes.number,
stoneType: PropTypes.string,
isStable: PropTypes.bool,
isMini: PropTypes.bool
};
export default DragSource(ItemTypes.STONE, stoneSource, collect)(OkeyStone);
You need to create a DropTarget and define a drop() function within its source, and whatever that returns will be what is returned by the monitor.getDropResult() function inside of the DragSource's endDrag() function (per http://gaearon.github.io/react-dnd/docs-drag-source-monitor.html). I'm not sure what you'd like the component itself to look like, but if you created a DropTarget with a drop function resembling:
const stoneDropSource = {
drop(props, monitor) {
return monitor.getItem();
},
}
Then that is what you would receive from calling getDropResult() in the endDrag() function.

react-dnd uncaught typerrors. Trying to follow the simple sortable example

I've been trying to work off of the simple sortable example in the react-dnd examples but I am having trouble trying to convert the es7 code to es6. I've tried using babel but I don't really understand the code that it spits out.
Here is my code that I've tried to translate from es7 to es6:
import React, {PropTypes} from 'react';
import Router from 'react-router';
import { DragDropContext } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import { DragSource, DropTarget } from 'react-dnd';
const style= {
border: '1px dashed gray',
padding: '0.5rem 1rem',
marginBottom: '.5rem',
backgroundColor: 'white',
cursor: 'move'
}
const ItemTypes = {
Coursepage: 'coursepage'
};
const coursePageSource = {
beginDrag(props) {
return {
id: props.id,
index: props.index
}
}
}
const coursePageTarget = {
hover(props, monitor, component){
const dragIndex = monitor.getItem().index;
const hoverIndex = props.index;
//don't replace items with themselves
if(dragIndex === hoverIndex){
return;
}
//Determine rectangle on screen
const hoverBoundingRect = findDOMNode(component).getBoundingClientRect();
//get vertical middle
const hoverMiddleY = (hoverBoundingRect.Bottom - hoverBoundingRect.Top) /2;
//get top pixels
const hoverClientY = clientOffset.y - hoverBoundingRect.top;
//only perform the move when the mouse has crossed half of the items height
//when dragging downwards, only move when the cursor is below 50%
//when dragging upwards, only move when the cursor is above 50%
//dragging downwards
if(dragIndex < hoverIndex && hoverClientY < hoverMiddleY){
return;
}
//dragging upwards
if(dragIndex > hoverIndex && hoverClientY > hoverMiddleY){
return;
}
//time to actually perform the action
props.moveObject(dragIndex, hoverIndex);
}
}
// const propTypes = {
// connectDragSource: PropTypes.func.isRequired,
// connectDropTarget: PropTypes.func.isRequired,
// index: PropTypes.number.isRequired,
// isDragging: PropTypes.bool.isRequired,
// id: PropTypes.any.isRequired,
// text: PropTypes.string.isRequired,
// moveCard: PropTypes.func.isRequired
// };
function collectDropTarget(connect) {
return {
connectDropTarget: connect.dropTarget(),
};
}
/**
* Specifies which props to inject into your component.
*/
function collectDragSource(connect, monitor) {
return {
// Call this function inside render()
// to let React DnD handle the drag events:
connectDragSource: connect.dragSource(),
// You can ask the monitor about the current drag state:
isDragging: monitor.isDragging()
};
}
class Coursepage extends React.Component{
render(){
console.log(this.props);
const {text, isDragging, connectDragSource, connectDropTarget} = this.props;
const opacity = isDragging ? 0 : 1;
return connectDragSource(connectDropTarget(
<div style={{opacity}}>
{text}
</div>
));
}
}
// Coursepage.propTypes = propTypes;
export default DragSource(ItemTypes.Coursepage, coursePageSource, collectDragSource)(Coursepage);
export default DropTarget(ItemTypes.Coursepage, coursePageTarget, collectDropTarget)(Coursepage);
Now the error I'm getting from this is
"Uncaught TypeError: connectDropTarget is not a function."
I console logged this.props in render and I see that connectDragSource is showing up in the this.props object but not connectDropTarget.
Can anyone tell me what I'm missing?
By the way, this is the example code I was using:
https://github.com/gaearon/react-dnd/blob/master/examples/04%20Sortable/Simple/Card.js
I know this is a little old but I landed up here through google so I figured I would give it a go. First of all, you can't have two default exports as referenced here in section 3.2 http://www.2ality.com/2014/09/es6-modules-final.html
Instead you need to pass the result of one of your current default exports into the second function call - you'll see below.
This took me a couple of hours to get working as I'm also an Es6/7 newbie - so I invite any criticism!
// Container.js;
import React, { Component } from 'react';
import update from 'react/lib/update';
import Card from './Card';
import { DragDropContext } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
const style = {
width: 400
};
class Container extends Component {
constructor(props) {
super(props);
this.moveCard = this.moveCard.bind(this);
this.findCard = this.findCard.bind(this);
this.state = {
cards: [{
id: 1,
text: 'Write a cool JS library'
}, {
id: 2,
text: 'Make it generic enough'
}, {
id: 3,
text: 'Write README'
}, {
id: 4,
text: 'Create some examples'
}, {
id: 5,
text: 'Spam in Twitter and IRC to promote it (note that this element is taller than the others)'
}, {
id: 6,
text: '???'
}, {
id: 7,
text: 'PROFIT'
}]
};
}
findCard(id) {
const { cards } = this.state;
const card = cards.filter(c => c.id === id)[0];
return {
card,
index: cards.indexOf(card)
};
}
moveCard(id, atIndex) {
const { card, index } = this.findCard(id);
this.setState(update(this.state, {
cards: {
$splice: [
[index, 1],
[atIndex, 0, card]
]
}
}));
}
render() {
const { cards } = this.state;
return (
<div style={style}>
{cards.map((card, i) => {
return (
<Card key={card.id}
index={i}
id={card.id}
text={card.text}
moveCard={this.moveCard}
findCard={this.findCard} />
);
})}
</div>
);
}
}
export default DragDropContext(HTML5Backend)(Container)
Then Card.js
// Card.js
import React, { Component, PropTypes } from 'react';
import ItemTypes from './ItemTypes';
import { DragSource, DropTarget } from 'react-dnd';
const style = {
border: '1px dashed gray',
padding: '0.5rem 1rem',
marginBottom: '.5rem',
backgroundColor: 'white',
cursor: 'move'
};
const cardSource = {
beginDrag(props) {
return {
id: props.id,
originalIndex: props.findCard(props.id).index
};
},
endDrag(props, monitor) {
const { id: droppedId, originalIndex } = monitor.getItem();
const didDrop = monitor.didDrop();
if (!didDrop) {
props.moveCard(droppedId, originalIndex);
}
}
};
const cardTarget = {
canDrop() {
return false;
},
hover(props, monitor) {
const { id: draggedId } = monitor.getItem();
const { id: overId } = props;
if (draggedId !== overId) {
const { index: overIndex } = props.findCard(overId);
props.moveCard(draggedId, overIndex);
}
}
};
function collect(connect, monitor) {
console.log( "HERE2", connect );
return {
connectDropTarget: connect.dropTarget(),
isOver: monitor.isOver(),
canDrop: monitor.canDrop()
};
}
function collect2(connect, monitor) {
return {
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging()
};
}
class Card extends Component {
render() {
const { text, isDragging, connectDragSource, connectDropTarget } = this.props;
const opacity = isDragging ? 0 : 1;
return connectDragSource(connectDropTarget(
<div >
{text}
</div>
));
}
}
Card.propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired,
isDragging: PropTypes.bool.isRequired,
id: PropTypes.any.isRequired,
text: PropTypes.string.isRequired,
moveCard: PropTypes.func.isRequired,
findCard: PropTypes.func.isRequired
};
const x = DropTarget(ItemTypes.CARD, cardTarget, collect )(Card)
export default DragSource(ItemTypes.CARD, cardSource, collect2 )( x )
And then the types include
// ItemTypes.js
export default {
CARD: 'card'
};

Resources