Skip to content

nested grids with React #2636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jianghai opened this issue Mar 19, 2024 · 7 comments
Open

nested grids with React #2636

jianghai opened this issue Mar 19, 2024 · 7 comments
Labels

Comments

@jianghai
Copy link

How to implement nested grids with React, because the content in the example is all strings, and it is not very understanding how React needs to be done.

import { useEffect, useRef } from 'react';

function Demo() {
  const gridContainerRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    if (gridContainerRef.current) {
      GridStack.init(
        {
          float: false,
          acceptWidgets: true,
          column: 12,
          minRow: 1,
        },
        gridContainerRef.current
      );
    }
  }, []);

  const items =[{ id: 'item-1-1', x: 0, y: 0, w: 2, h: 2 }, { id: 'item-1-2', x: 2, y: 0, w: 2, h: 2 }]

  return (
    <div className="grid-stack" ref={gridContainerRef}>
      {items.map((item, i) => {
        return (
          <div key={item.id} className="grid-stack-item" gs-id={item.id} gs-w={item.w} gs-h={item.h} gs-x={item.x} gs-y={item.y}>
            <div className="grid-stack-item-content">
              {item.id}
            </div>
          </div>
        )
      })}
    </div>
  )
}
@jianghai
Copy link
Author

I'm not sure if this is the best practice

function Grid({ items }: any) {
  const gridContainerRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    if (gridContainerRef.current) {
      GridStack.init(
        {
          float: false,
          acceptWidgets: true,
          column: 12,
          minRow: 1,
        },
        gridContainerRef.current
      );
    }
  }, []);
  return (
    <div className="grid-stack" ref={gridContainerRef}>
      {items.map((item, i) => {
        return (
          <div key={item.id} className="grid-stack-item" gs-id={item.id} gs-w={item.w} gs-h={item.h} gs-x={item.x} gs-y={item.y}>
            <div className="grid-stack-item-content">
              {item.id}
              {item.children && <Grid items={item.children}></Grid>}
            </div>
          </div>
        )
      })}
    </div>
  )
}

function Demo() {
  const items =[{
    id: 'item-1', x: 0, y: 0, w: 2, h: 2,
  }, {
    id: 'item-2', x: 2, y: 0, w: 2, h: 2,
    children: [{
      id: 'item-2-1', x: 0, y: 0, w: 6, h: 6,
    }]
  }]
  return <Grid items={items} />
}

@wangtangsheng
Copy link

when items has minW、minH, how to setAttribute to 'grid-stack-item' element?
like:

const items =[{
id: 'item-1', x:0, y: 0, w: 2, h: 2, minW: 3
}]

@jianghai

@adumesny adumesny added the React label Mar 29, 2024
@kuzurec
Copy link

kuzurec commented Apr 19, 2024

I have the same problem too

@Aysnine
Copy link
Contributor

Aysnine commented Feb 7, 2025

In gridstack, nesting is realized by subGrid + children in the option, and react cannot be used as a container for the subGrid, i.e. the nested container is managed by gridstack and cannot be a react component.

@adumesny
Copy link
Member

adumesny commented Feb 7, 2025

@Aysnine mostly true, but in my Angular app I do have nested grid that are custom component (with a header and summary text, and a grid child to contain the rest). Of course the gridItem that is a subgrid is done by gs using the subgrid options, but I still have my component in there.
Will have to update the examples to show the next level of complexity... #2938

@liyuewen
Copy link

liyuewen commented May 9, 2025

@Aysnine mostly true, but in my Angular app I do have nested grid that are custom component (with a header and summary text, and a grid child to contain the rest). Of course the gridItem that is a subgrid is done by gs using the subgrid options, but I still have my component in there. Will have to update the examples to show the next level of complexity... #2938

I read the information in your link, but I still don't understand how to use nested components in React. How to load nested components in plain HTML? For example, the non-nested one below.
<div className="grid-stack-item " gs-w={width} gs-h={height}> <div className="grid-stack-item-content"> <Card className="stackItemCard">{children}</Card> </div> </div>

@adumesny
Copy link
Member

adumesny commented May 9, 2025

@liyuewen you cannot do nested grid from html today. I only worked on it from JSON (what real app that need to serialize dashboards use anyway). In angular wrapper, GS calls me back for each custom comp to be created, that's the big difference I see with React vs Angular wrapper I see...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants