index.tsx 2.3 KB

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