Code Monkey home page Code Monkey logo

Comments (4)

sikhaman avatar sikhaman commented on June 9, 2024 2

I would say if you want to have more fresh version, of this library wit same functionality try to look at gridstack

from react-grid-layout.

Thebks avatar Thebks commented on June 9, 2024

Yeh I was wondering about the same thing because I would like to implement DnD feature in my project like Grafana

I checked the examples for react-grid-layout but they are implemented using class-based components which React.js itself doesn't recommend. I would like to use functional components and hooks. I tried to convert one of the examples into a functional component but what I'm not able to do is make the items maintain their sizes and positions when I change the screen size from large to small and back to large screen.

Currently, this is how the code looks

"use client";

import React, { useMemo, useState, ReactNode } from "react";
import ReactGridLayout, { WidthProvider, Responsive } from "react-grid-layout";
import MyResponsiveLine from "@/grid-test/Chart";
import CloseOutlinedIcon from "@mui/icons-material/CloseOutlined";

import "../../grid-test/styles.css";
import { Box, Button, IconButton } from "@mui/material";

type Item = {
  i: string;
  x: number;
  y: number;
  w: number;
  h: number;
  component?: ReactNode;
};

type AddRemoveProps = {
  cols: { lg: number; md: number; sm: number; xs: number; xxs: number };
  rowHeight?: number;
};

const AddRemoveLayout = () => {
  const ResponsiveReactGridLayout = useMemo(
    () => WidthProvider(Responsive),
    []
  );

  const defaultAddRemoveValues: AddRemoveProps = {
    cols: { lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 },
    rowHeight: 100,
  };

  const [items, setItems] = useState<Item[]>([]);

  const [newCounter, setNewCounter] = useState(0);


  const cols = defaultAddRemoveValues.cols;
  const rowHeight = defaultAddRemoveValues.rowHeight;
  console.log("cols", cols);
  console.log("rowHeight", rowHeight);

  const onAddItem = () => {
    console.log("adding", "n" + newCounter);
    const newItem = {
      i: "n" + newCounter,
      x: (items.length * 2) % (cols ? cols.lg : 12),
      y: Infinity,
      w: 2,
      h: 2,
      // add: false
      component: <MyResponsiveLine key={"n" + newCounter} />,
    };
    setItems([...items, newItem]);
    setNewCounter(newCounter + 1);
  };

  const onRemoveItem = (i: string) => {
    console.log("removing", i);
    setItems(items.filter((item) => item.i !== i));
  };

  const onLayoutChange = (layout: any) => {
    // setNewLayout(layout);
    console.log("the layout just changed ", layout);
  };

  const onBreakpointChange = (breakpoint: any, cols: any) => {
    console.log("the layout just changed ", breakpoint, cols);
  };

  return (
    <Box>
      <Button variant="outlined" onClick={onAddItem} style={{ color: "black" }}>
        Add Item
      </Button>
      <ResponsiveReactGridLayout
        onLayoutChange={onLayoutChange}
        onBreakpointChange={onBreakpointChange}
        cols={cols}
        rowHeight={rowHeight}
      >
        {items.map((item, index) => (
          <Box key={index} data-grid={item} className="react-grid-item">
            {item.component}
            <IconButton
              onClick={() => onRemoveItem(item.i)}
              sx={{
                position: "absolute",
                top: 0,
                right: "2px",
              }}
            >
              <CloseOutlinedIcon />
            </IconButton>
          </Box>
        ))}
      </ResponsiveReactGridLayout>
    </Box>
  );
};

export default AddRemoveLayout;

I tried to lookin to Grafana's code but I couldnt make any sense of it

from react-grid-layout.

lkarvec avatar lkarvec commented on June 9, 2024

Still doesn't technically fit my needs.

from react-grid-layout.

Thebks avatar Thebks commented on June 9, 2024

@sikhaman I just the website and it looks promising. I will let you know how it works out for me

Thank You.

from react-grid-layout.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.