Search code examples
javascriptreactjsreact-grid-layout

React Grid Layout with data-grid passed down to a child component not working


I wanted to implement resizable components in React with React Grid Layout. I played around with it a little bit to understand how it works and then installed it in my project but I can't seem to get it working as my child components don't even have the class react-grid-item.

I am using data-grid to pass the grid props to my child component. I can't see what I am doing wrongly.

import React, {Component} from 'react';
import ChartHolder from '../components/charts/chart-holder';
import RGL, { WidthProvider } from "react-grid-layout";

const ReactGridLayout = WidthProvider(RGL);

class TabContent extends Component {
    constructor(props){
        super(props);
    }


    handleChartResize = (layout) => {
        console.log('this is the layout', layout);
    }

    render(){
        let tabsChartData = this.props.tabsData.currentActiveTabData[0].data;
        let chartData = tabsChartData.map((data, index)=>{
            return(
            <ChartHolder
                key={`${this.props.tabsData.currentActiveTabData[0].tab.id}_${index}`}
                id={index}
                position={data.position}
            />);
        });
        return (
                <div
                    className="tab-content"
                >
                    <ReactGridLayout
                        cols={2}
                        rowHeight={450}
                        width={1000}
                        onLayoutChange={this.handleChartResize}
                        isDraggable={false}
                        isResizeable={true}
                    >
                        {chartData}
                    </ReactGridLayout>
                </div>
            );
    }
}

This is the chartHolder component where I add the data-grid:

import React, {Component} from 'react';
import {css} from 'react-emotion';
import GridLoader from '../spinners/grid-loader';
import Plot from 'react-plotly.js';
import _ from 'lodash';
import Chart from './Chart';
import styled from 'styled-components';

const ChartChildGrid = styled.div`
    background-color: white;
    padding: 5px;
    margin: 5px;
`;

class ChartHolder extends Component {
    render() {
        return(
            <ChartChildGrid
                index={this.props.id}
                draggable={true}
                onDragStart={this.handleDragStart}
                onDragEnd={this.handleDragEnd}
                onDragOver={this.handleDragOver}
                onDragLeave={this.handleDragLeave}
                onDrop={this.handleDragDrop}
                data-grid={this.props.position}
            >
                <div 
                    className="chart-holder-header"
                >
                    <span
                        onClick={()=>this.props.handleRemoveChart(this.props.id)}
                        className="close-tab"
                    > 
                            &times;
                    </span>
                </div>
                <div className="chart-holder-body" >
                    {_.isEmpty(this.props.data) && <GridLoader/>}
                    {
                        !_.isEmpty(this.props.data) && 
                        <Chart
                            data={this.props.data}
                            width={this.props.width}
                        />
                    }
                </div>
            </ChartChildGrid>
        );
    }
}

export default ChartHolder;

I have removed most proprietary part of the code but this is not working as expected because I can't resize the chartHolder component and it doesn't even have any class like cssTransforms and react-resizable like I see in the examples. It does console the right layout as I expect but doesn't seem to be resizable.


Solution

  • I managed to get it working. In case anyone is facing the same challenge, the problem was because for some reason react-grid-layout does not accept customised components. You have to wrap it around a div.

    https://github.com/STRML/react-grid-layout/issues/576 the above link helped me discover this issue.