react-dnd helloworld
核心理解: 2.beginDrag 的返回值,可以理解为原生的(dataTransfer.setData),设置拖动的数据,传递给hover中使用,(monitor.getItem().index) 3.使用了ES7 的decorator “` const style = { // dnd 生命的对象 拖动源 // 拖动目标 事件 hover(props,monitor,component) { // 当前拖动的index const dragIndex = monitor.getItem().index; // 当前hover 的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; // Determine mouse position const clientOffset = monitor.getClientOffset(); // Get pixels to the top 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.moveCard(dragIndex,hoverIndex); // Note: we're mutating the monitor item here! // Generally it's better to avoid mutations,// but it's good here for the sake of performance // to avoid expensive index searches. // 新的位置 monitor.getItem().index = hoverIndex; }, }; // 声明放下 render() { // isDragging,connectDragSource,connectDropTarget 是 注入的 // text 是父亲传递进来的 const { text,isDragging,connectDropTarget } = this.props; // 是否是isDragging const opacity = isDragging ? 0 : 1; return connectDragSource(connectDropTarget( <div style={{ ...style,opacity }}> {text} </div>,)); } } “` (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |