index.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import type { FC, MouseEventHandler } from 'react'
  2. import React from 'react'
  3. import Spinner from '../spinner'
  4. export type IButtonProps = {
  5. /**
  6. * The style of the button
  7. */
  8. type?: 'primary' | 'warning' | (string & {})
  9. className?: string
  10. disabled?: boolean
  11. loading?: boolean
  12. tabIndex?: number
  13. children: React.ReactNode
  14. onClick?: MouseEventHandler<HTMLButtonElement>
  15. }
  16. const Button: FC<IButtonProps> = ({
  17. type,
  18. disabled,
  19. children,
  20. className,
  21. onClick,
  22. loading = false,
  23. tabIndex,
  24. }) => {
  25. let style = 'cursor-pointer'
  26. switch (type) {
  27. case 'primary':
  28. style = (disabled || loading) ? 'btn-primary-disabled' : 'btn-primary'
  29. break
  30. case 'warning':
  31. style = (disabled || loading) ? 'btn-warning-disabled' : 'btn-warning'
  32. break
  33. default:
  34. style = disabled ? 'btn-default-disabled' : 'btn-default'
  35. break
  36. }
  37. return (
  38. <button
  39. className={`btn ${style} ${className && className}`}
  40. tabIndex={tabIndex}
  41. disabled={disabled}
  42. onClick={onClick}
  43. >
  44. {children}
  45. {/* Spinner is hidden when loading is false */}
  46. <Spinner loading={loading} className='!text-white !h-3 !w-3 !border-2 !ml-1' />
  47. </button>
  48. )
  49. }
  50. export default React.memo(Button)