CustomButton.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { Button, ButtonProps, makeStyles, Tooltip } from '@material-ui/core';
  2. const buttonStyle = makeStyles(theme => ({
  3. button: {
  4. padding: theme.spacing(1, 3),
  5. textTransform: 'initial',
  6. fontWeight: 'bold',
  7. },
  8. textBtn: {
  9. color: theme.palette.primary.main,
  10. padding: theme.spacing(1),
  11. '&:hover': {
  12. backgroundColor: '#f9f9f9',
  13. },
  14. },
  15. containedBtn: {
  16. color: '#fff',
  17. backgroundColor: theme.palette.primary.main,
  18. boxShadow: 'initial',
  19. fontWeight: 'bold',
  20. lineHeight: '16px',
  21. '&:hover': {
  22. backgroundColor: theme.palette.primary.light,
  23. boxShadow: 'initial',
  24. },
  25. },
  26. containedSecondary: {
  27. backgroundColor: '#fc4c02',
  28. '&:hover': {
  29. backgroundColor: '#fc4c02',
  30. },
  31. },
  32. disabledBtn: {
  33. pointerEvents: 'none',
  34. },
  35. }));
  36. // props types same as Material Button
  37. const CustomButton = (props: ButtonProps & { tooltip?: string }) => {
  38. const classes = buttonStyle();
  39. const { tooltip, ...otherProps } = props;
  40. return (
  41. <>
  42. {/*
  43. add span to let disabled elements show tooltip
  44. see https://material-ui.com/zh/components/tooltips/#disabled-elements
  45. */}
  46. {tooltip ? (
  47. <Tooltip title={tooltip}>
  48. <span>
  49. <Button
  50. classes={{
  51. root: classes.button,
  52. text: classes.textBtn,
  53. contained: classes.containedBtn,
  54. containedSecondary: classes.containedSecondary,
  55. disabled: classes.disabledBtn,
  56. }}
  57. {...otherProps}
  58. >
  59. {props.children}
  60. </Button>
  61. </span>
  62. </Tooltip>
  63. ) : (
  64. <Button
  65. classes={{
  66. root: classes.button,
  67. text: classes.textBtn,
  68. contained: classes.containedBtn,
  69. containedSecondary: classes.containedSecondary,
  70. }}
  71. {...otherProps}
  72. >
  73. {props.children}
  74. </Button>
  75. )}
  76. </>
  77. );
  78. };
  79. export default CustomButton;