I wanted to implement resizable ponents 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 ponents don't even have the class react-grid-item.
I am using data-grid to pass the grid props to my child ponent. I can't see what I am doing wrongly.
import React, {Component} from 'react';
import ChartHolder from '../ponents/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 ponent 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-ponents';
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"
>
×
</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 ponent 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.
I wanted to implement resizable ponents 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 ponents don't even have the class react-grid-item.
I am using data-grid to pass the grid props to my child ponent. I can't see what I am doing wrongly.
import React, {Component} from 'react';
import ChartHolder from '../ponents/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 ponent 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-ponents';
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"
>
×
</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 ponent 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.
Share Improve this question edited Feb 6, 2019 at 9:55 J.Ewa asked Feb 6, 2019 at 9:41 J.EwaJ.Ewa 2351 gold badge3 silver badges14 bronze badges2 Answers
Reset to default 4I 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 ponents. You have to wrap it around a div.
https://github./STRML/react-grid-layout/issues/576 the above link helped me discover this issue.
You actually can use a customized ponent, but you have to pass some required props to it. This is addressed in issue #299
Inside of GridLayout use your custom ponent like so...
<GridLayout>
<div key="1">...</div>
<div key="2">...</div>
<CustomComponent key="3" />
</GridLayout>
Inside of your customComponent
you need to render the following props.
<div {...this.props} className={this.props.className}>
content goes here...
</div>
Code Sample