ButtonWithLoader.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {
  2. Button,
  3. CircularProgress,
  4. makeStyles,
  5. Typography,
  6. } from "@material-ui/core";
  7. import { ReactChild } from "react";
  8. const useStyles = makeStyles((theme) => ({
  9. root: {
  10. position: "relative",
  11. },
  12. button: {
  13. marginTop: theme.spacing(2),
  14. textTransform: "none",
  15. width: "100%",
  16. },
  17. loader: {
  18. position: "absolute",
  19. bottom: 0,
  20. left: "50%",
  21. marginLeft: -12,
  22. marginBottom: 6,
  23. },
  24. error: {
  25. marginTop: theme.spacing(1),
  26. textAlign: "center",
  27. },
  28. }));
  29. export default function ButtonWithLoader({
  30. disabled,
  31. onClick,
  32. showLoader,
  33. error,
  34. children,
  35. }: {
  36. disabled?: boolean;
  37. onClick: () => void;
  38. showLoader?: boolean;
  39. error?: string;
  40. children: ReactChild;
  41. }) {
  42. const classes = useStyles();
  43. return (
  44. <>
  45. <div className={classes.root}>
  46. <Button
  47. color="primary"
  48. variant="contained"
  49. className={classes.button}
  50. disabled={disabled}
  51. onClick={onClick}
  52. >
  53. {children}
  54. </Button>
  55. {showLoader ? (
  56. <CircularProgress
  57. size={24}
  58. color="inherit"
  59. className={classes.loader}
  60. />
  61. ) : null}
  62. </div>
  63. {error ? (
  64. <Typography variant="body2" color="error" className={classes.error}>
  65. {error}
  66. </Typography>
  67. ) : null}
  68. </>
  69. );
  70. }