简体   繁体   English

如何在 MUI 中制作加载按钮?

[英]How to make a loading button in MUI?

I am using with MUI framework and I was wondering how can I create an loading button using this framework?我正在使用MUI框架的,我想知道如何使用这个框架创建加载按钮?

I am looking for something similar to this .我正在寻找类似东西。

To the best of my knowledge, there is no single component that accomplishes this out of the box in material-ui.据我所知,在 material-ui 中没有一个组件可以开箱即用地完成此任务。 However, you can implement your own easily using CircularProgress .但是,您可以使用CircularProgress轻松实现自己的。

Assuming you are using material-ui v1, here's a rough example.假设您使用的是 material-ui v1,这是一个粗略的示例。 First, I create a LoadingButton that accepts a loading prop - if that prop is true , I display a CircularProgress indicator.首先,我创建一个接受loading道具的LoadingButton - 如果该道具为true ,我将显示一个CircularProgress指示器。 It also accepts a done prop - if that's true, the button clears the progress indicator and becomes a checkmark to show success.它还接受done道具 - 如果这是真的,按钮会清除进度指示器并成为显示成功的复选标记。

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Button from 'material-ui/Button';
import { CircularProgress } from 'material-ui/Progress';
import Check from 'material-ui-icons/Check';

const styles = theme => ({
  button: {
    margin: theme.spacing.unit,
  },
});

const LoadingButton = (props) => {
  const { classes, loading, done, ...other } = props;

  if (done) {
    return (
      <Button className={classes.button} {...other} disabled>
        <Check />
      </Button>
    );
  }
  else if (loading) {
    return (
      <Button className={classes.button} {...other}>
        <CircularProgress />
      </Button>
    );
  } else {
    return (
      <Button className={classes.button} {...other} />
    );
  }
}

LoadingButton.defaultProps = {
  loading: false,
  done: false,
  };

LoadingButton.propTypes = {
  classes: PropTypes.object.isRequired,
  loading: PropTypes.bool,
  done: PropTypes.bool,
};

export default withStyles(styles)(LoadingButton);

You can use the LoadingButton as shown in the following example, which uses state to set the appropriate prop on the button.您可以使用LoadingButton ,如下例所示,它使用state在按钮上设置适当的 prop。

import React from 'react';
import LoadingButton from './LoadingButton';

class ControlledButton extends React.Component {
  constructor(props) {
    super(props);

    this.state = { loading: false, finished: false };
  }

  render() {
    const { loading, finished } = this.state;

    const setLoading = !finished && loading;

    return (
      <div>
        <LoadingButton
          loading={setLoading}
          done={finished}
          onClick={() => {
            // Clicked, so show the progress dialog
            this.setState({ loading: true });

            // In a 1.5 seconds, end the progress to show that it's done
            setTimeout(() => { this.setState({ finished: true })}, 1500);
          }}
        >
          Click Me
        </LoadingButton>
      </div>
    );
  }
}

export default ControlledButton;

You can of course tweak the styling and functionality to meet your exact needs.您当然可以调整样式和功能以满足您的确切需求。

In the newer versions of MUI, you can use LoadingButton component, it's currently in the lab package.在较新版本的 MUI 中,您可以使用LoadingButton组件,它目前在实验室包中。 This is just a wrapper of the Button with a loading prop.这只是带有loading道具的Button包装器。 You can customize the loadingIndicator component and its position.您可以自定义loadingIndicator组件及其位置。 See the example below:请参见下面的示例:

import LoadingButton from '@mui/lab/LoadingButton';
<LoadingButton loading={loading}>
  Text
</LoadingButton>
<LoadingButton
  endIcon={<SendIcon />}
  loading={loading}
  loadingPosition="end"
  variant="contained"
>
  Send
</LoadingButton>

Codesandbox 演示

检查这个包react-loading-button

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM