index.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use client'
  2. import cn from 'classnames'
  3. import { Dialog } from '@headlessui/react'
  4. import { useTranslation } from 'react-i18next'
  5. import { XMarkIcon } from '@heroicons/react/24/outline'
  6. import Button from '../button'
  7. export type IDrawerProps = {
  8. title?: string
  9. description?: string
  10. panelClassname?: string
  11. children: React.ReactNode
  12. footer?: React.ReactNode
  13. mask?: boolean
  14. positionCenter?: boolean
  15. isOpen: boolean
  16. // closable: boolean
  17. showClose?: boolean
  18. clickOutsideNotOpen?: boolean
  19. onClose: () => void
  20. onCancel?: () => void
  21. onOk?: () => void
  22. }
  23. export default function Drawer({
  24. title = '',
  25. description = '',
  26. panelClassname = '',
  27. children,
  28. footer,
  29. mask = true,
  30. positionCenter,
  31. showClose = false,
  32. isOpen,
  33. clickOutsideNotOpen,
  34. onClose,
  35. onCancel,
  36. onOk,
  37. }: IDrawerProps) {
  38. const { t } = useTranslation()
  39. return (
  40. <Dialog
  41. unmount={false}
  42. open={isOpen}
  43. onClose={() => !clickOutsideNotOpen && onClose()}
  44. className="fixed z-30 inset-0 overflow-y-auto"
  45. >
  46. <div className={cn('flex w-screen h-screen justify-end', positionCenter && '!justify-center')}>
  47. {/* mask */}
  48. <Dialog.Overlay
  49. className={cn('z-40 fixed inset-0', mask && 'bg-black bg-opacity-30')}
  50. />
  51. <div className={cn('relative z-50 flex flex-col justify-between bg-white w-full max-w-sm p-6 overflow-hidden text-left align-middle shadow-xl', panelClassname)}>
  52. <>
  53. {title && <Dialog.Title
  54. as="h3"
  55. className="text-lg font-medium leading-6 text-gray-900"
  56. >
  57. {title}
  58. </Dialog.Title>}
  59. {showClose && <Dialog.Title className="flex items-center mb-4" as="div">
  60. <XMarkIcon className='w-4 h-4 text-gray-500' onClick={onClose} />
  61. </Dialog.Title>}
  62. {description && <Dialog.Description className='text-gray-500 text-xs font-normal mt-2'>{description}</Dialog.Description>}
  63. {children}
  64. </>
  65. {footer || (footer === null
  66. ? null
  67. : <div className="mt-10 flex flex-row justify-end">
  68. <Button
  69. className='mr-2'
  70. onClick={() => {
  71. onCancel && onCancel()
  72. }}>{t('common.operation.cancel')}</Button>
  73. <Button
  74. onClick={() => {
  75. onOk && onOk()
  76. }}>{t('common.operation.save')}</Button>
  77. </div>)}
  78. </div>
  79. </div>
  80. </Dialog>
  81. )
  82. }