Skip to content

Commit

Permalink
添加DraggableTable组件示例
Browse files Browse the repository at this point in the history
  • Loading branch information
NexxLuo committed Sep 29, 2019
1 parent b303a51 commit 560c563
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 334 deletions.
104 changes: 104 additions & 0 deletions packages/siteCN/doc/components/DragLayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import React, { memo, useState, useEffect } from "react";
import { useDragLayer } from "react-dnd";

const styles_Box = {
border: "1px dashed gray",
padding: "0.5rem 1rem",
cursor: "move"
};
const Box = ({ title, yellow }) => {
const backgroundColor = yellow ? "yellow" : "white";
return <div style={{ ...styles_Box, backgroundColor }}>{title}</div>;
};

const styles = {
display: "inline-block",
transform: "rotate(-7deg)",
WebkitTransform: "rotate(-7deg)"
};
const BoxDragPreview = memo(({ title }) => {
return (
<div style={styles}>
<Box title={title} />
</div>
);
});

function snapToGrid(x, y) {
const snappedX = Math.round(x / 32) * 32;
const snappedY = Math.round(y / 32) * 32;
return [snappedX, snappedY];
}

const ItemTypes = {
BOX: "box"
};

const layerStyles = {
position: "fixed",
pointerEvents: "none",
zIndex: 100,
left: 0,
top: 0,
width: "100%",
height: "100%"
};
function getItemStyles(initialOffset, currentOffset, isSnapToGrid) {
if (!initialOffset || !currentOffset) {
return {
display: "none"
};
}
let { x, y } = currentOffset;
if (isSnapToGrid) {
x -= initialOffset.x;
y -= initialOffset.y;
[x, y] = snapToGrid(x, y);
x += initialOffset.x;
y += initialOffset.y;
}
const transform = `translate(${x}px, ${y}px)`;
return {
transform,
WebkitTransform: transform
};
}
const CustomDragLayer = props => {
const {
itemType,
isDragging,
item,
initialOffset,
currentOffset
} = useDragLayer(monitor => ({
item: monitor.getItem(),
itemType: monitor.getItemType(),
initialOffset: monitor.getInitialSourceClientOffset(),
currentOffset: monitor.getSourceClientOffset(),
isDragging: monitor.isDragging()
}));
function renderItem() {
return <BoxDragPreview title={item.title} />;
switch (itemType) {
case ItemTypes.BOX:
return <BoxDragPreview title={item.title} />;
default:
return null;
}
}
if (!isDragging) {
return null;
}

//console.log("render CustomDragLayer:",item);
return (
<div style={layerStyles}>
<div
style={getItemStyles(initialOffset, currentOffset, props.snapToGrid)}
>
{renderItem()}
</div>
</div>
);
};
export default CustomDragLayer;
Loading

0 comments on commit 560c563

Please sign in to comment.