import React from "react";
import _ from "lodash";
import { Responsive, WidthProvider } from "react-grid-layout";
const ResponsiveReactGridLayout = WidthProvider(Responsive);
export default class DragFromOutsideLayout extends React.Component {
static defaultProps = {
className: "layout",
rowHeight: 30,
onLayoutChange: function() {},
cols: { lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 },
};
state = {
currentBreakpoint: "lg",
compactType: "vertical",
mounted: false,
layouts: { lg: generateLayout() }
};
componentDidMount() {
this.setState({ mounted: true });
}
generateDOM() {
return _.map(this.state.layouts.lg, function(l, i) {
return (
{l.static ? (
Static - {i}
) : (
{i}
)}
);
});
}
onBreakpointChange = breakpoint => {
this.setState({
currentBreakpoint: breakpoint
});
};
onCompactTypeChange = () => {
const { compactType: oldCompactType } = this.state;
const compactType =
oldCompactType === "horizontal"
? "vertical"
: oldCompactType === "vertical"
? null
: "horizontal";
this.setState({ compactType });
};
onLayoutChange = (layout, layouts) => {
this.props.onLayoutChange(layout, layouts);
};
onNewLayout = () => {
this.setState({
layouts: { lg: generateLayout() }
});
};
onDrop = (layout, layoutItem, _event) => {
alert(`Dropped element props:\n${JSON.stringify(layoutItem, ['x', 'y', 'w', 'h'], 2)}`);
};
render() {
return (
Current Breakpoint: {this.state.currentBreakpoint} (
{this.props.cols[this.state.currentBreakpoint]} columns)
Compaction type:{" "}
{_.capitalize(this.state.compactType) || "No Compaction"}
e.dataTransfer.setData("text/plain", "")}
>
Droppable Element (Drag me!)
{this.generateDOM()}
);
}
}
function generateLayout() {
return _.map(_.range(0, 25), function(item, i) {
var y = Math.ceil(Math.random() * 4) + 1;
return {
x: Math.round(Math.random() * 5) * 2,
y: Math.floor(i / 6) * y,
w: 2,
h: y,
i: i.toString(),
static: Math.random() < 0.05
};
});
}
if (process.env.STATIC_EXAMPLES === true) {
import("../test-hook.jsx").then(fn => fn.default(DragFromOutsideLayout));
}