admin管理员组文章数量:1356264
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
本文标签: javascriptReact Grid Layout with datagrid passed down to a child component not workingStack Overflow
版权声明:本文标题:javascript - React Grid Layout with data-grid passed down to a child component not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744025275a2577933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论