Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

reactjs - Space between two buttons in Material Design

I have two buttons and would like to put a space between them. So, I have tried to do that by putting a box around them and then applying a margin to the box. See code below.

    render() {
    return (
      <>
        <ThemeProvider theme={theme}>
          <CssBaseline />
          <BrowserRouter>
            <Header />
          </BrowserRouter>
        </ThemeProvider>

        <ThemeProvider theme={theme}>
          <Grid container justify="center">
            <Box pt={3}>
              <Paper>
                <Box pl={3} pr={3} pb={3} pt={3}>
                  <Grid container spacing={0}>
                    <Grid item xs={6}>
                      <Typography
                        variant="h6"
                        gutterBottom={false}
                        onClick={this.handleSetDummyText}
                      >
                        Keyword Processor
                      </Typography>
                    </Grid>

                    <Grid item xs={6}>
                      {/* ********* Offending code section ********** */}
                      <Button
                        color={
                          this.state.sorttypeStatus === 'default'
                            ? 'primary'
                            : 'secondary'
                        }
                        onClick={this.handleSortDefault}
                      >
                        DEFAULT
                      </Button>
                      {/* ********* Offending code section ********** */}
                      <Button
                        color={
                          this.state.sorttypeStatus === 'alpha'
                            ? 'primary'
                            : 'secondary'
                        }
                        onClick={this.handleSortAlpha}
                      >
                        A-Z
                      </Button>
                      <Button
                        color={
                          this.state.sorttypeStatus === 'length'
                            ? 'primary'
                            : 'secondary'
                        }
                        onClick={this.handleSortLength}
                      >
                        LENGTH
                      </Button>
                    </Grid>

                    <Grid container spacing={2}>
                      <Grid item xs={6}>
                        <TextField
                          label="Input Keywords"
                          fullWidth
                          multiline
                          rows={10}
                          variant="outlined"
                          margin="normal"
                          onChange={this.handleInputText}
                          // defaultValue={defaultInputText}
                          value={this.state.inputText}
                        />

                        <Box m={1}>
                          <Button
                            variant="contained"
                            color="primary"
                            onClick={this.inputToOutput}
                          >
                            Parse
                          </Button>

                          <Button
                            variant="outlined"
                            color="secondary"
                            onClick={this.clearInput}
                            // default={this.state.inputText}
                          >
                            Clear
                          </Button>
                        </Box>

                        <br />
                        <Checkboxes
                          handleDedupe={this.handleDedupe}
                          handleRemoveNumbers={this.handleRemoveNumbers}
                          handleConvertToLowercase={
                            this.handleConvertToLowercase
                          }
                          handleOneWordPerLine={this.handleOneWordPerLine}
                          handleAddCommas={this.handleAddCommas}
                          handleAddCommasSpace={this.handleAddCommasSpace}
                          handleStartWords={this.handleStartWords}
                          handleEndWords={this.handleEndWords}
                        />
                      </Grid>
                      <Grid item xs={6}>
                        <TextField
                          label="Output Keywords"
                          fullWidth
                          multiline
                          rows={30}
                          variant="outlined"
                          margin="normal"
                          value={this.state.outputText}
                        />
                        <Button
                          variant="contained"
                          color="primary"
                          mt={5}
                          onClick={() => {
                            navigator.clipboard.writeText(
                              this.state.outputText
                            );
                          }}
                        >
                          Copy
                        </Button>
                        <Button
                          variant="outlined"
                          color="secondary"
                          mt={5}
                          onClick={this.clearOutput}
                        >
                          Clear
                        </Button>
                        <Button
                          variant="outlined"
                          color="secondary"
                          mt={5}
                          onClick={this.handleSetDummyText}
                        >
                          Dummy Data
                        </Button>
                      </Grid>
                    </Grid>
                  </Grid>
                </Box>
              </Paper>
            </Box>
          </Grid>
        </ThemeProvider>
      </>
    );
  }
}

But the boxes seem to stick together like glue! I do have a custom theme but I cannot seem to get that to work with my App.js file because I am using a class component, rather than a functional component. I understand that you are supposed to use withStyles with class components, but I have struggled to get that to work. So, as a fix, I am trying the Box method.

Any idea how to get my box method to work?

question from:https://stackoverflow.com/questions/65886199/space-between-two-buttons-in-material-design

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you could use Grid component of material ui and as justify content use space-between

<Grid
  style={{width:'140px'}}
  container
  direction="row"
  justify="space-between"
  alignItems="center">
   <Button/>// here are the buttons 
   <Button/>
   </Grid>

read more about the Grid https://material-ui.com/components/grid/

or you could do it with <div/> i used inline style so that you can read it easier

<div style ={{ width:'200px',
display:'flex',
flexDirection:'row',
justifyContent:'space-between'
}}>
  <button>button 1</button>
  <button>button 2</button>
</div>

see working examples with div https://codesandbox.io/s/youthful-taussig-jyesc?file=/src/App.js:67-263


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...