import * as React from 'react'; import { useState } from 'react'; import RGL, { ReactGridLayoutProps, WidthProvider } from 'react-grid-layout'; import { ExampleStyle } from './example-style'; import styled from 'styled-components'; import _ from 'lodash'; export default { title: 'grid-layout/Demo', }; const ReactGridLayout = WidthProvider(RGL); function stringifyLayout(layout) { return layout.map(function (l) { const name = l.i === '__dropping-elem__' ? 'drop' : l.i; return (
{name} {`: [${l.x}, ${l.y}, ${l.w}, ${l.h}]`}
); }); } const RightTop = styled.div` position: absolute; right: 0; top: 0; `; const MyItem = ({ children, onClose, ...props }) => { return (
{children}
); }; const Placeholder = styled.div` width: 128px; height: 64px; border: 1px solid lightgrey; `; let ids = 1; export const ControlledLayoutGrid = () => { const [layout, setLayout] = useState(() => [ { i: 'a', x: 0, y: 0, w: 1, h: 2 }, { i: 'b', x: 1, y: 0, w: 3, h: 2 }, { i: 'c', x: 4, y: 0, w: 1, h: 2 }, ]); return (
e.dataTransfer.setData('text/plain', '' + ids)} draggable={true} unselectable="on" > Drag to Drop
Displayed as [x, y, w, h]:
{stringifyLayout(layout)}
{ console.log(`DROP`, event, event.e); const { x, y } = event; setLayout((layout) => { console.log(layout.map((v) => v.i)); return [...layout.filter((v) => !v.i.startsWith('DROPING')), { i: 'DROP' + ids, x, y, w: 2, h: 2 }]; }); ids += 2; }} className="layout" cols={24} rowHeight={30} layout={layout} onLayoutChange={setLayout} > {layout.map(({ i }, ii) => ( setLayout(_.reject(layout, { i }))}> {i}/{ii} ))}
); }; export const DataGridPropLayout = () => { return (
a
b
c
); };