OmniOwl
OmniOwl

Reputation: 5709

React UI Element Placement?

I'm new to React so how to place UI Elements kind of escapes me.

I want the following layout: enter image description here

But I can't quite figure it out. In the black box there is supposed to be a group of checkboxes (a component I've already made) stack vertically and in the blue box the same group of checkboxes, but put up horizontally. In the red box, a Google Scatter Chart (which I also got set up).

Using react Columns I thought that I could maybe set it up, using two Columns. But then I realized that I'd have to parent the checkboxes somehow so that wasn't a solution.

Then I thought "Why not use a grid?" so I found that Google have grids in their Material package (which is fine as I'm using it any way) so I imported that but I still can't quite get it right:

enter image description here

How would I solve this?

My code is below:

return(
    <div>
        <Grid container spacing={0}>
            <Grid item xs={3}>
                <CheckboxContainer checkboxes={checkboxes}/>
            </Grid>
            <Grid item xs={9}>
                <Chart
                    height="400px"
                    chartType="ScatterChart"
                    loader={<div>Loading Chart</div>}
                    data={[
                        ['Level','Y'],
                        [-4,-1],
                        [-3,3],
                        [-2,-2],
                        [-1,1],
                        [0,5],
                        [1,14],
                        [2,5],
                        [3,3.5],
                        [4,7],
                    ]}
                    options={{
                        title: 'Transparency',
                        hAxis: { title: 'Level', minValue: -9, maxValue: 9 },
                        vAxis: { title: 'Y', minValue: -9, maxValue: 9 },
                        legend: 'none',
                    }}
                    rootProps={{ 'data-testid': '1'}}
                />
            </Grid>
            <Grid item xs={12}>
                <CheckboxContainer checkboxes={checkboxes} />
            </Grid>
        </Grid>
    </div>
)

Upvotes: 0

Views: 950

Answers (2)

Guilherme Lemmi
Guilherme Lemmi

Reputation: 3487

It depends on how you're working with CSS in your project, but using the latest version of create-react-app and material-ui I got the result below:

enter image description here

This is the code used:

import React from 'react';
import PropTypes from 'prop-types';
import Grid from '@material-ui/core/Grid';
import Checkbox from '@material-ui/core/Checkbox';
import { Chart } from "react-google-charts";
import { withStyles } from '@material-ui/core/styles';
import withRoot from '../withRoot';

const styles = theme => ({
  root: {
    textAlign: 'center',
    paddingTop: theme.spacing.unit * 20,
  },
  column1: {
    display:'flex',
    flexDirection:'column',
  },
  row2: {
    textAlign:'right',
  }
});

class Index extends React.Component {
  state = {
    open: false,
    checkedA: true,
    checkedB: true,
    checkedF: true
  };

  handleClose = () => {
    this.setState({
      open: false,
    });
  };

  handleClick = () => {
    this.setState({
      open: true,
    });
  };

  handleChange = name => event => {
    this.setState({ [name]: event.target.checked });
  };


  getCheckboxes = () => {
    return (
      <>
        <Checkbox
          checked={this.state.checkedA}
          onChange={this.handleChange('checkedA')}
          value="checkedA"
        />
        <Checkbox
          checked={this.state.checkedB}
          onChange={this.handleChange('checkedB')}
          value="checkedB"
          color="primary"
        />
        <Checkbox value="checkedC" />
        <Checkbox disabled value="checkedD" />
        <Checkbox disabled checked value="checkedE" />
        <Checkbox
          checked={this.state.checkedF}
          onChange={this.handleChange('checkedF')}
          value="checkedF"
          indeterminate
        />
        <Checkbox defaultChecked color="default" value="checkedG" />
      </>
    );
  }

  render() {
    return(
          <div>
              <Grid container spacing={0}>
                  <Grid item xs={3} className={this.props.classes.column1}>
                    {this.getCheckboxes()}
                  </Grid>
                  <Grid item xs={9}>
                      <Chart
                        height="400px"
                        chartType="ScatterChart"
                        loader={<div>Loading Chart</div>}
                        data={[
                            ['Level','Y'],
                            [-4,-1],
                            [-3,3],
                            [-2,-2],
                            [-1,1],
                            [0,5],
                            [1,14],
                            [2,5],
                            [3,3.5],
                            [4,7],
                        ]}
                        options={{
                            title: 'Transparency',
                            hAxis: { title: 'Level', minValue: -9, maxValue: 9 },
                            vAxis: { title: 'Y', minValue: -9, maxValue: 9 },
                            legend: 'none',
                        }}
                        rootProps={{ 'data-testid': '1'}}
                    />
                  </Grid>
                  <Grid item xs={12} className={this.props.classes.row2}>
                    {this.getCheckboxes()}
                  </Grid>
              </Grid>
          </div>
      );
  }
}

Index.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withRoot(withStyles(styles)(Index));

Upvotes: 0

Kristian Salo
Kristian Salo

Reputation: 360

You can create this layout easily with pure CSS using flexbox for the horixontal and vertical checkboxes, and a float for the scatter chart.

  render() {
    return (
    <div className="page">      
      <div className="main">
        <div className="sidebar">
          <div>
            Check this 
            <input type="checkbox"/>
          </div>
           <div>
            Check this 
            <input type="checkbox"/>
          </div>        
        </div>
        <div className="main">
          <div>
            Check this 
            <input type="checkbox"/>
          </div>
           <div>
            Check this 
            <input type="checkbox"/>
          </div>
        </div>
      </div>
      <div className="footer">
        Scatter plot here
      </div>
    </div>
    );
.page {
  width: 100vw;
}

.main {
  display: flex;
}

.sidebar {
  display: flex;
  flex-direction: column;
  width: 20%;
}

.footer {
  float: right;
  width: 80%;
}

Check out this code in JSFiddle.

Upvotes: 1

Related Questions