3

I am using react-grid-layout package for my layout design.

I want to use compactType=null configuration for free items moving on the layout. But when I change the sizes or move items in a row, large gaps appear on the screen as in the screen shot.

Are there any way the fixing layout by removing the gaps after moving items?

enter image description here

Mehmet
  • 1,467
  • 1
  • 14
  • 19

1 Answers1

1

Okay I found the solution by modifying the layout array in onLayoutChange function

 <ResponsiveGridLayout
                        layouts={{ lg: layout }}
                       
                        cols={{
                            lg: colCount,
                            md: colCount,
                            sm: colCount,
                            xs: colCount,
                            xxs: colCount
                        }}
                        rowHeight={46}
                        isResizable={isAlignFieldsModeOn}
                        isDraggable={isAlignFieldsModeOn}
                        isBounded={isAlignFieldsModeOn}
                        onLayoutChange={newLayout => {
                            if (isAlignFieldsModeOn) {
                                let _layout = newLayout;

                                if (compactType === null) {
                                    const fixedLayout = newLayout.map(item => {
                                        // the row before the field row
                                        const upperRow = Math.max(item.y - 1, 0);

                                        const upperRowIsEmpty =
                                            newLayout.filter(f => f.y === upperRow).length === 0;

                                        // If there are no fields on the upper row then move up the fields to the upper row.
                                        if (upperRowIsEmpty) {
                                            return {
                                                ...item,
                                                y: upperRow
                                            };
                                        }
                                        return item;
                                    });

                                    _layout = [...fixedLayout];
                                }

                                // Set the new layout for instant effect
                                setLayout(_layout);
                            }
                        }}
                        margin={[3, 7]}
                        compactType={compactType}
                        useCSSTransforms={false}
                        id={tab?.id}
                        key={tab?.id}
                        measureBeforeMount
                        width={width}
                        preventCollision={false}
                >
                    {generateForm}
                </ResponsiveGridLayout>
Mehmet
  • 1,467
  • 1
  • 14
  • 19
  • Hello! Could you tell please, how have you achieved that the layout changes after setting the layout state? I have tried all solution from issue https://github.com/react-grid-layout/react-grid-layout/issues/382 but it's doesn't work for me – malininss Aug 31 '22 at 07:58
  • Hi @malininss. You can see on this online example https://stackblitz.com/edit/react-grid-layout-experiment-6tinuw?file=index.js – Mehmet Sep 01 '22 at 09:00