index.tsx 2.4 KB

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