ReactDnD排序元素未在Dragging上更新

时间:2017-01-12 17:00:51

标签: reactjs react-dnd

您好我正在关注此code中的example来制作类似的组件。我的组件正如我预期的那样工作(它在拖放时正确排序)但是当我拖动卡片时,不透明度0必须跟随拖动元素到"感觉"这就像在ReactDnD示例中一样。

查看我的work

和我的代码

var React = require('react');
var ReactDOM = require('react-dom');
var ReactDnDBackend = require('react-dnd-html5-backend');
var DragDropContext = require('react-dnd').DragDropContext;
var DragSource = require('react-dnd').DragSource;
var DropTarget = require('react-dnd').DropTarget;

var Card = React.createClass({
    render: function() {
        var connectDragSource = this.props.connectDragSource;
        var isDragging = this.props.isDragging;
        var connectDropTarget = this.props.connectDropTarget;

        return connectDragSource(connectDropTarget(
            <div style={{
                marginBottom: '5px',
                background: '#fff',
                border: '1px dashed #ccc',
                padding: '10px 16px',
                opacity: isDragging ? 0.5 : 1,
            }}>
                {this.props.name}
            </div>
        ));
    }
});

var Source = {
    beginDrag: function (props) {
        return {
            name: props.name,
            index: props.index
        };
    }
};

Card = DragSource('CARD', Source, function (connect, monitor) {
    return {
        connectDragSource: connect.dragSource(),
        isDragging: monitor.isDragging()
    };
})(Card);


var Target = {
    hover: function (props, monitor, component) {
        var dragIndex = monitor.getItem().index,
            hoverIndex = props.index;
        if (dragIndex === hoverIndex) {
            return;
        }

        var hoverBoundingRect = ReactDOM.findDOMNode(component).getBoundingClientRect(),
            hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2,
            clientOffset = monitor.getClientOffset(),
            hoverClientY = clientOffset.y - hoverBoundingRect.top;


        if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
            return;
        }

        if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
            return;
        }

        props.moveItem(dragIndex, hoverIndex);

        monitor.getItem().index = hoverIndex;
    }
};

Card = DropTarget('CARD', Target, function (connect) {
    return {
        connectDropTarget: connect.dropTarget()
    };
})(Card);

var Container = React.createClass({
    getInitialState: function() {
        return {
            items: [
                {id: 1, name: 'Item 1', order: 0, bg: '#b5b5b5'},
                {id: 4, name: 'Item 4', order: 1, bg: 'red'},
                {id: 2, name: 'Item 2', order: 2, bg: 'blue'},
                {id: 3, name: 'Item 3', order: 3, bg: 'green'},

                {id: 5, name: 'Item 5', order: 4, bg: '#b5b5b5'},
                {id: 8, name: 'Item 8', order: 5, bg: 'red'},
                {id: 6, name: 'Item 6', order: 6, bg: 'blue'},
                {id: 7, name: 'Item 7', order: 7, bg: 'green'},
            ]
        };
    },
    moveItem(dragIndex, hoverIndex) {
        var items = this.state.items.slice(0),
            dragItem = items[dragIndex];

        items.splice(dragIndex, 1);
        items.splice(hoverIndex, 0, dragItem);

        this.setState({
            items: items
        });
    },
    render: function() {
        var self = this;

        return (
            <div style={{
                width: '600px'
            }}>
                {this.state.items.map(function (card, i) {
                    return (
                        <Card
                            key={i}
                            index={i}
                            name={card.name}
                            moveItem={self.moveItem}
                        />
                    );
                })}
            </div>
        );
    }
});
Container = DragDropContext(ReactDnDBackend)(Container);

ReactDOM.render(
    <Container />,
    document.getElementById('app')
);

1 个答案:

答案 0 :(得分:2)

好的,您需要对代码进行一些更改:

1.-在这种情况下,key属性必须与&#34; card.id&#34;相关联,因为如果你改变命令,React想知道要更新哪个元素。

render: function() {
    var self = this;

    return (
        <div style={{
          width: '600px'
        }}>
          {this.state.items.map(function (card, i) {
            return (
                <Card
                    key={card.id}
                    index={i}
                    name={card.name}
                    moveItem={self.moveItem}
                />
            );
          })}
        </div>
    );
  }

2.-要以与示例相同的方式工作,不透明度必须为:0或1

var Card = React.createClass({
  render: function() {
    var connectDragSource = this.props.connectDragSource;
    var isDragging = this.props.isDragging;
    var connectDropTarget = this.props.connectDropTarget;

    return connectDragSource(connectDropTarget(
        <div style={{
          marginBottom: '5px',
          background: '#fff',
          border: '1px dashed #ccc',
          padding: '10px 16px',
          opacity: isDragging ? 0 : 1,
        }}>
          {this.props.name}
        </div>
    ));
  }
});