React DnD 排序元素不会在拖动时更新
ReactDnD sorting elements not updating on Dragging
你好,我正在关注这个 code from this example 来制作一个类似的组件。我的组件按预期工作(它在拖放时正确排序)但是当我拖动卡片时,不透明度 0 必须跟随拖动元素到 "feel",就像在 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.- 在这种情况下,键 属性 必须与 "card.id" 相关联,因为如果您更改顺序,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>
));
}
});
你好,我正在关注这个 code from this example 来制作一个类似的组件。我的组件按预期工作(它在拖放时正确排序)但是当我拖动卡片时,不透明度 0 必须跟随拖动元素到 "feel",就像在 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.- 在这种情况下,键 属性 必须与 "card.id" 相关联,因为如果您更改顺序,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>
));
}
});