index.tsx 2.5 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. import cn from '@/utils/classnames'
  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. showClose?: boolean
  17. clickOutsideNotOpen?: boolean
  18. onClose: () => void
  19. onCancel?: () => void
  20. onOk?: () => void
  21. }
  22. export default function Drawer({
  23. title = '',
  24. description = '',
  25. panelClassname = '',
  26. children,
  27. footer,
  28. mask = true,
  29. positionCenter,
  30. showClose = false,
  31. isOpen,
  32. clickOutsideNotOpen,
  33. onClose,
  34. onCancel,
  35. onOk,
  36. }: IDrawerProps) {
  37. const { t } = useTranslation()
  38. return (
  39. <Dialog
  40. unmount={false}
  41. open={isOpen}
  42. onClose={() => !clickOutsideNotOpen && onClose()}
  43. className="fixed z-30 inset-0 overflow-y-auto"
  44. >
  45. <div className={cn('flex w-screen h-screen justify-end', positionCenter && '!justify-center')}>
  46. {/* mask */}
  47. <Dialog.Overlay
  48. className={cn('z-40 fixed inset-0', mask && 'bg-black bg-opacity-30')}
  49. />
  50. <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)}>
  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. }