1.0.0 • Published 3 years ago

react-layout-grid-generator v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

react-layout-grid-generator

npm package npm downloads

this component is created using react-grid-layout to create dashboard by drag & drop. you can access to the demo.

install it with,

yarn add react-layout-grid-generator

or

npm install react-layout-grid-generator

Options

proptypedefaultdescription
isEditableboolfalseallow the end-user to change the position and size of the grid items.
renderEditfunc-grid item to render when edit mode is turned on, use it as a method. see below for usage
renderViewfunc-the render of grid item when edit mode is off, use it as a method
onChange?func-the function will be raised if the layout changes in edit mode
initialLayouts?objectnullthe initial state of layout
renderDraggableItems?func-to enable drag & drop components into, use it as a method. * each draggable item 'onDragStart' should be binded to 'handleDragStart'
children?nodenullchildren will be rendered inside the LayoutGrid
gridItemMargin?number10margin between each grid item
editingGridColor?string#cccccc80background color in edit mode
breakpointRules?object-min width to apply each layout size e.g. lg, sm, lg and etc.
colRules?object-maximum number of column in width for each layout size e.g. lg, sm, lg and etc.

renderEdit and RenderView

in rendering the grid item you can access to the following properties:

proptypedescription
idstringthe id of grid item
typestringit can be used as a key for rendering decision
optionobjectit can be used as a place for storing additional data
xnumberrow position of the grid item
ynumbercolumn position of the grid item
wnumberwidth of each grid item
hnumberheight of each grid item
onUpdateByIdfunc* only in edit mode. it will update the option property. the inputs are (id, key, value)
onDeleteByIdfunc* only in edit mode. it will delete the grid item by id

EXAMPLES

import { LayoutGrid } from "react-layout-grid-generator";

const widgetItems = [
  { option: { data: "b" }, type: "CHART_A", w: 1, h: 1 },
  { option: { res: "b" }, type: "CHART_B", w: 2, h: 2 },
  { option: {}, type: "WIDGET", w: 1, h: 1 },
];
const [isEditable, setIsEditable] = useState(true);

const [initialLayouts, setInitialLayouts] = useState({});
const [tempLayouts, setTempLayouts] = useState({});

const onLayoutChanged = (newLayouts) => {
  setTempLayouts(newLayouts);
};

const onSave = () => {
  localStorage.setItem("layouts", JSON.stringify({ layouts: tempLayouts }));
};

const onLoad = () => {
  const newLayouts = JSON.parse(localStorage.getItem("layouts")).layouts;
  setInitialLayouts(newLayouts);
};

<LayoutGrid
  isEditable={isEditable}
  renderEdit={({ type, onUpdateById, onDeleteById, id, option }) => (
    <div>
      {type}
      {option?.data ?? ""}
      <button onClick={() => onUpdateById(id, "option", { data: "me" })}>
        Edit
      </button>
      <button onClick={() => onDeleteById(id)}>DELETE</button>
    </div>
  )}
  renderView={({ type, option }) => {
    switch (type) {
      case "CHART_A":
        return <div className="chart-a">CHART_A</div>;
      case "CHART_B":
        return <div className="chart-b">CHART_B</div>;
      case "WIDGET":
        return <div className="chart-c">WIDGETTTTT</div>;

      default:
        return <div className="chart-none">NO TYPE PROVIDED</div>;
    }
  }}
  renderDraggableItems={(handleDragStart) => (
    <div className="layout-grid__widget-items">
      {widgetItems.map((item) => (
        <div
          draggable={true}
          className="widget-item"
          key={item.type}
          onDragStart={(e) => handleDragStart(item, e)}
        >
          {type}
        </div>
      ))}
    </div>
  )}
  onChange={onLayoutChanged}
  initialLayouts={initialLayouts}
>
  <span>Will be added here</span>
</LayoutGrid>;