ol-toast.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import classNames from 'classnames'
  2. import { Toast as BS5Toast } from 'react-bootstrap'
  3. import { NotificationIcon, NotificationType } from '../notification'
  4. import { useTranslation } from 'react-i18next'
  5. import MaterialIcon from '../material-icon'
  6. import { ReactNode, useCallback, useState } from 'react'
  7. export type OLToastProps = {
  8. type: NotificationType
  9. className?: string
  10. title?: string
  11. content: string | ReactNode
  12. isDismissible?: boolean
  13. onDismiss?: () => void
  14. autoHide?: boolean
  15. delay?: number
  16. }
  17. export const OLToast = ({
  18. type = 'info',
  19. className = '',
  20. content,
  21. title,
  22. isDismissible,
  23. onDismiss,
  24. autoHide,
  25. delay,
  26. }: OLToastProps) => {
  27. const { t } = useTranslation()
  28. const [show, setShow] = useState(true)
  29. const toastClassName = classNames(
  30. 'notification',
  31. `notification-type-${type}`,
  32. className,
  33. 'toast-content'
  34. )
  35. const handleClose = useCallback(() => {
  36. setShow(false)
  37. }, [])
  38. const handleOnHidden = useCallback(() => {
  39. if (onDismiss) onDismiss()
  40. }, [onDismiss])
  41. const toastElement = (
  42. <div className={toastClassName}>
  43. <NotificationIcon notificationType={type} />
  44. <div className="notification-content-and-cta">
  45. <div className="notification-content">
  46. {title && (
  47. <p>
  48. <b>{title}</b>
  49. </p>
  50. )}
  51. {content}
  52. </div>
  53. </div>
  54. {isDismissible && (
  55. <div className="notification-close-btn">
  56. <button
  57. aria-label={t('close')}
  58. data-bs-dismiss="toast"
  59. onClick={handleClose}
  60. >
  61. <MaterialIcon type="close" />
  62. </button>
  63. </div>
  64. )}
  65. </div>
  66. )
  67. return (
  68. <BS5Toast
  69. onClose={handleClose}
  70. autohide={autoHide}
  71. onExited={handleOnHidden}
  72. delay={delay}
  73. show={show}
  74. >
  75. {toastElement}
  76. </BS5Toast>
  77. )
  78. }