Advertisement
Thenlie

Component

Sep 10th, 2023
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { Button as ButtonMUI, SxProps, Theme } from '@mui/material';
  3. import CircularProgress from '@mui/material/CircularProgress';
  4.  
  5. interface ButtonProps {
  6.     /**
  7.      * Text to be rendered in the button
  8.      */
  9.     title: string;
  10.     /**
  11.      * Type of button, defaults to `button`
  12.      */
  13.     type?: 'button' | 'submit' | 'reset';
  14.     /**
  15.      * MUI color of button, defaults to `secondary`
  16.      */
  17.     color?: 'inherit' | 'primary' | 'secondary' | 'success' | 'error' | 'info' | 'warning';
  18.     /**
  19.      * Displays loading spinner when true, defaults to `false`
  20.      */
  21.     loading?: boolean;
  22.     /**
  23.      * If the button is clickable, defaults to `false`
  24.      */
  25.     disabled?: boolean;
  26.     /**
  27.      * Icon displayed in front of button text
  28.      */
  29.     startIcon?: React.ReactNode;
  30.     /**
  31.      * Function to be run when button is clicked
  32.      */
  33.     onClick?: () => void;
  34.     /**
  35.      * MUI styling props to override default styling
  36.      */
  37.     sx?: SxProps<Theme>;
  38. }
  39.  
  40. /**
  41.  * Generic form submission button
  42.  */
  43. const Button: React.FC<ButtonProps> = ({
  44.     title,
  45.     type = 'button',
  46.     color = 'secondary',
  47.     loading = false,
  48.     disabled = false,
  49.     startIcon,
  50.     onClick,
  51.     sx,
  52. }) => {
  53.     return (
  54.         <>
  55.             <ButtonMUI
  56.                 variant='contained'
  57.                 size='large'
  58.                 type={type}
  59.                 color={color}
  60.                 disabled={disabled || loading}
  61.                 startIcon={!loading && startIcon}
  62.                 onClick={onClick}
  63.                 sx={{ margin: 0.5, minWidth: 210, minHeight: 45, ...sx }}
  64.             >
  65.                 {loading ? <CircularProgress size={24} sx={{ color: 'white' }} /> : title}
  66.             </ButtonMUI>
  67.         </>
  68.     );
  69. };
  70.  
  71. export default Button;
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement