verify-state-modal.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, { useEffect, useRef, useState } from 'react'
  2. import { createPortal } from 'react-dom'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. RiExternalLinkLine,
  6. } from '@remixicon/react'
  7. import Button from '@/app/components/base/button'
  8. export type IConfirm = {
  9. className?: string
  10. isShow: boolean
  11. title: string
  12. content?: React.ReactNode
  13. onConfirm: () => void
  14. onCancel: () => void
  15. maskClosable?: boolean
  16. email?: string
  17. showLink?: boolean
  18. }
  19. function Confirm({
  20. isShow,
  21. title,
  22. content,
  23. onConfirm,
  24. onCancel,
  25. maskClosable = true,
  26. showLink,
  27. email,
  28. }: IConfirm) {
  29. const { t } = useTranslation()
  30. const dialogRef = useRef<HTMLDivElement>(null)
  31. const [isVisible, setIsVisible] = useState(isShow)
  32. useEffect(() => {
  33. const handleKeyDown = (event: KeyboardEvent) => {
  34. if (event.key === 'Escape')
  35. onCancel()
  36. }
  37. document.addEventListener('keydown', handleKeyDown)
  38. return () => {
  39. document.removeEventListener('keydown', handleKeyDown)
  40. }
  41. }, [onCancel])
  42. const handleClickOutside = (event: MouseEvent) => {
  43. if (maskClosable && dialogRef.current && !dialogRef.current.contains(event.target as Node))
  44. onCancel()
  45. }
  46. useEffect(() => {
  47. document.addEventListener('mousedown', handleClickOutside)
  48. return () => {
  49. document.removeEventListener('mousedown', handleClickOutside)
  50. }
  51. }, [maskClosable])
  52. useEffect(() => {
  53. if (isShow) {
  54. setIsVisible(true)
  55. }
  56. else {
  57. const timer = setTimeout(() => setIsVisible(false), 200)
  58. return () => clearTimeout(timer)
  59. }
  60. }, [isShow])
  61. if (!isVisible)
  62. return null
  63. return createPortal(
  64. <div className={'fixed inset-0 z-[10000000] flex items-center justify-center bg-background-overlay'}
  65. onClick={(e) => {
  66. e.preventDefault()
  67. e.stopPropagation()
  68. }}
  69. >
  70. <div ref={dialogRef} className={'relative w-full max-w-[481px] overflow-hidden'}>
  71. <div className='shadows-shadow-lg flex max-w-full flex-col items-start rounded-2xl border-[0.5px] border-solid border-components-panel-border bg-components-panel-bg'>
  72. <div className='flex flex-col items-start gap-2 self-stretch pb-4 pl-6 pr-6 pt-6'>
  73. <div className='title-2xl-semi-bold text-text-primary'>{title}</div>
  74. <div className='system-md-regular w-full text-text-tertiary'>{content}</div>
  75. </div>
  76. {email && (
  77. <div className='w-full space-y-1 px-6 py-3'>
  78. <div className='system-sm-semibold py-1 text-text-secondary'>{t('education.emailLabel')}</div>
  79. <div className='system-sm-regular rounded-lg bg-components-input-bg-disabled px-3 py-2 text-components-input-text-filled-disabled'>{email}</div>
  80. </div>
  81. )}
  82. <div className='flex items-center justify-between gap-2 self-stretch p-6'>
  83. <div className='flex items-center gap-1'>
  84. {showLink && (
  85. <>
  86. <a href='' className='system-xs-regular cursor-pointer text-text-accent'>{t('education.learn')}</a>
  87. <RiExternalLinkLine className='h-3 w-3 text-text-accent' />
  88. </>
  89. )}
  90. </div>
  91. <Button variant='primary' className='!w-20' onClick={onConfirm}>{t('common.operation.ok')}</Button>
  92. </div>
  93. </div>
  94. </div>
  95. </div>, document.body,
  96. )
  97. }
  98. export default React.memo(Confirm)