代码之家  ›  专栏  ›  技术社区  ›  OmniOwl

反应UI元素的放置?

  •  0
  • OmniOwl  · 技术社区  · 7 年前

    我是个新手,所以我不知道如何放置UI元素。

    enter image description here

    但我不太明白。在黑框中应该有一组复选框(我已经制作的组件)垂直堆叠,在蓝框中是同一组复选框,但水平放置。在红色框中,有一个谷歌散点图(我也设置了它)。

    使用react列,我想我可以设置它,使用两个列。但后来我意识到我必须以某种方式设置复选框的父项,所以这不是一个解决方案。

    然后我想“为什么不使用网格呢?”所以我发现谷歌在他们的材料包中有网格(这很好,因为我一直在使用它),所以我导入了它,但我仍然不能完全正确:

    enter image description here

    我该如何解决这个问题?

    我的代码如下:

    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>
    )
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Kristian Salo    7 年前

    您可以使用纯CSS轻松创建此布局,使用水平和垂直复选框的flexbox,以及散点图的浮点。

      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%;
    }

    请在中查看此代码 JSFiddle .

        2
  •  0
  •   Guilherme Lemmi MjJ    7 年前

    这取决于您在项目中如何使用CSS,但使用的是最新版本的 create-react-app and material-ui 我得到的结果如下:

    enter image description here

    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));