|
| 1 | +import { FC, Fragment } from "react"; |
| 2 | +import { Dialog as DialogCore, Transition } from "@headlessui/react"; |
| 3 | +import { XMarkIcon } from "@heroicons/react/24/outline"; |
| 4 | + |
| 5 | +type Props = { |
| 6 | + open: boolean; |
| 7 | + title?: string; |
| 8 | + children: React.ReactNode; |
| 9 | + acceptLabel?: string; |
| 10 | + cancelLabel?: string; |
| 11 | + enableClose?: boolean; |
| 12 | + enableAccept?: boolean; |
| 13 | + enableCancel?: boolean; |
| 14 | + renderLeftIcon?: () => React.ReactNode; |
| 15 | + onClose: () => void; |
| 16 | + onAccept?: () => void; |
| 17 | + onCancel?: () => void; |
| 18 | +}; |
| 19 | + |
| 20 | +const Dialog: FC<Props> = ({ |
| 21 | + open, |
| 22 | + children, |
| 23 | + title, |
| 24 | + acceptLabel = "OK", |
| 25 | + cancelLabel = "Cancel", |
| 26 | + enableClose = true, |
| 27 | + enableAccept = true, |
| 28 | + enableCancel = true, |
| 29 | + onClose, |
| 30 | + onAccept, |
| 31 | + onCancel, |
| 32 | + renderLeftIcon, |
| 33 | +}) => { |
| 34 | + const handleOnClose = () => { |
| 35 | + onClose(); |
| 36 | + }; |
| 37 | + |
| 38 | + const handleOnAccept = () => { |
| 39 | + onAccept && onAccept(); |
| 40 | + onClose(); |
| 41 | + }; |
| 42 | + |
| 43 | + const handleOnCancel = () => { |
| 44 | + onCancel && onCancel(); |
| 45 | + onClose(); |
| 46 | + }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <Transition.Root show={open} as={Fragment}> |
| 50 | + <DialogCore as="div" className="relative z-10" onClose={handleOnClose}> |
| 51 | + <Transition.Child |
| 52 | + as={Fragment} |
| 53 | + enter="ease-out duration-300" |
| 54 | + enterFrom="opacity-0" |
| 55 | + enterTo="opacity-100" |
| 56 | + leave="ease-in duration-200" |
| 57 | + leaveFrom="opacity-100" |
| 58 | + leaveTo="opacity-0" |
| 59 | + > |
| 60 | + <div className="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity" /> |
| 61 | + </Transition.Child> |
| 62 | + |
| 63 | + <div className="fixed inset-0 z-10 w-screen overflow-y-auto"> |
| 64 | + <div className="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0"> |
| 65 | + <Transition.Child |
| 66 | + as={Fragment} |
| 67 | + enter="ease-out duration-300" |
| 68 | + enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95" |
| 69 | + enterTo="opacity-100 translate-y-0 sm:scale-100" |
| 70 | + leave="ease-in duration-200" |
| 71 | + leaveFrom="opacity-100 translate-y-0 sm:scale-100" |
| 72 | + leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95" |
| 73 | + > |
| 74 | + <DialogCore.Panel className="relative transform overflow-hidden rounded-lg bg-white px-4 pb-4 pt-5 text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-lg sm:p-6"> |
| 75 | + {enableClose && ( |
| 76 | + <div className="absolute right-0 top-0 hidden pr-4 pt-4 sm:block"> |
| 77 | + <button |
| 78 | + type="button" |
| 79 | + className="rounded-md bg-white text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2" |
| 80 | + onClick={onClose} |
| 81 | + > |
| 82 | + <span className="sr-only">Close</span> |
| 83 | + <XMarkIcon className="h-6 w-6" aria-hidden="true" /> |
| 84 | + </button> |
| 85 | + </div> |
| 86 | + )} |
| 87 | + <div className="sm:flex sm:items-start"> |
| 88 | + {renderLeftIcon && renderLeftIcon()} |
| 89 | + <div className="mt-3 text-center sm:ml-4 sm:mt-0 sm:text-left"> |
| 90 | + {title && ( |
| 91 | + <DialogCore.Title |
| 92 | + as="h3" |
| 93 | + className="text-base font-semibold leading-6 text-gray-900" |
| 94 | + > |
| 95 | + {title} |
| 96 | + </DialogCore.Title> |
| 97 | + )} |
| 98 | + <div className="mt-2">{children}</div> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + <div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse"> |
| 102 | + {enableAccept && ( |
| 103 | + <button |
| 104 | + type="button" |
| 105 | + className="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 sm:ml-3 sm:w-auto" |
| 106 | + onClick={handleOnAccept} |
| 107 | + > |
| 108 | + {acceptLabel} |
| 109 | + </button> |
| 110 | + )} |
| 111 | + {enableCancel && ( |
| 112 | + <button |
| 113 | + type="button" |
| 114 | + className="mt-3 inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50 sm:mt-0 sm:w-auto" |
| 115 | + onClick={handleOnCancel} |
| 116 | + > |
| 117 | + {cancelLabel} |
| 118 | + </button> |
| 119 | + )} |
| 120 | + </div> |
| 121 | + </DialogCore.Panel> |
| 122 | + </Transition.Child> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </DialogCore> |
| 126 | + </Transition.Root> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export default Dialog; |
0 commit comments