|
| 1 | +import React from 'react'; |
| 2 | +import classNames from 'classnames'; |
| 3 | +import ReactDOM from 'react-dom'; |
| 4 | +import { CSSTransition } from 'react-transition-group'; |
| 5 | +// import Icon from '../icon/icon'; |
| 6 | +// import Button from '../button/button'; |
| 7 | +import Text from '../text'; |
| 8 | +// import { useOnClickOutside } from '../../hooks'; |
| 9 | + |
| 10 | +type TDialog = { |
| 11 | + cancel_button_text?: string; |
| 12 | + className?: string; |
| 13 | + confirm_button_text?: string; |
| 14 | + dismissable?: boolean; |
| 15 | + disableApp?: () => void; |
| 16 | + enableApp?: () => void; |
| 17 | + has_close_icon?: boolean; |
| 18 | + is_closed_on_cancel?: boolean; |
| 19 | + is_closed_on_confirm?: boolean; |
| 20 | + is_content_centered?: boolean; |
| 21 | + is_loading?: boolean; |
| 22 | + is_mobile_full_width?: boolean; |
| 23 | + is_visible: boolean; |
| 24 | + onCancel?: () => void; |
| 25 | + onClose?: () => void; |
| 26 | + onConfirm: () => void; |
| 27 | + onEscapeButtonCancel?: () => void; |
| 28 | + portal_element_id?: string; |
| 29 | + title?: React.ReactNode; |
| 30 | +}; |
| 31 | + |
| 32 | +const Dialog = ({ |
| 33 | + disableApp, |
| 34 | + // dismissable, |
| 35 | + enableApp, |
| 36 | + is_closed_on_cancel = true, |
| 37 | + is_closed_on_confirm = true, |
| 38 | + is_visible, |
| 39 | + onCancel, |
| 40 | + onClose, |
| 41 | + onConfirm, |
| 42 | + onEscapeButtonCancel, |
| 43 | + ...other_props |
| 44 | +}: React.PropsWithChildren<TDialog>) => { |
| 45 | + const { |
| 46 | + cancel_button_text, |
| 47 | + className, |
| 48 | + children, |
| 49 | + confirm_button_text, |
| 50 | + is_loading, |
| 51 | + is_mobile_full_width = true, |
| 52 | + is_content_centered, |
| 53 | + portal_element_id, |
| 54 | + title, |
| 55 | + has_close_icon, |
| 56 | + } = other_props; |
| 57 | + |
| 58 | + const wrapper_ref = React.useRef() as React.MutableRefObject<HTMLInputElement>; |
| 59 | + |
| 60 | + React.useEffect(() => { |
| 61 | + if (is_visible && !!disableApp) { |
| 62 | + disableApp(); |
| 63 | + } |
| 64 | + }, [is_visible, disableApp]); |
| 65 | + |
| 66 | + React.useEffect(() => { |
| 67 | + const close = (e: { key: string }) => { |
| 68 | + if (e.key === 'Escape') { |
| 69 | + onEscapeButtonCancel?.(); |
| 70 | + } |
| 71 | + }; |
| 72 | + window.addEventListener('keydown', close); |
| 73 | + return () => window.removeEventListener('keydown', close); |
| 74 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 75 | + }, []); |
| 76 | + |
| 77 | + const handleCancel = () => { |
| 78 | + if (is_closed_on_cancel && enableApp) { |
| 79 | + enableApp(); |
| 80 | + } |
| 81 | + onCancel?.(); |
| 82 | + }; |
| 83 | + |
| 84 | + const handleConfirm = () => { |
| 85 | + if (is_closed_on_confirm && enableApp) { |
| 86 | + enableApp(); |
| 87 | + } |
| 88 | + onConfirm(); |
| 89 | + }; |
| 90 | + |
| 91 | + const handleClose = () => { |
| 92 | + if (onClose) { |
| 93 | + onClose(); |
| 94 | + } else if (onCancel) { |
| 95 | + handleCancel(); |
| 96 | + } else { |
| 97 | + handleConfirm(); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + // const validateClickOutside = () => !!dismissable || !!(has_close_icon && is_visible && is_closed_on_cancel); |
| 102 | + |
| 103 | + // useOnClickOutside(wrapper_ref, handleClose, validateClickOutside); |
| 104 | + |
| 105 | + const content_classes = classNames('dc-dialog__content', { |
| 106 | + 'dc-dialog__content--centered': is_content_centered, |
| 107 | + }); |
| 108 | + // |
| 109 | + const is_text = |
| 110 | + typeof children === 'string' || |
| 111 | + (React.isValidElement(children) && typeof children?.props?.i18n_default_text === 'string'); |
| 112 | + const dialog = ( |
| 113 | + <CSSTransition |
| 114 | + appear |
| 115 | + in={is_visible && !is_loading} |
| 116 | + timeout={50} |
| 117 | + classNames={{ |
| 118 | + appear: 'dc-dialog__wrapper--enter', |
| 119 | + enter: 'dc-dialog__wrapper--enter', |
| 120 | + enterDone: 'dc-dialog__wrapper--enter-done', |
| 121 | + exit: 'dc-dialog__wrapper--exit', |
| 122 | + }} |
| 123 | + unmountOnExit |
| 124 | + > |
| 125 | + <div |
| 126 | + className={classNames('dc-dialog__wrapper', className, { |
| 127 | + 'dc-dialog__wrapper--has-portal': !!portal_element_id, |
| 128 | + })} |
| 129 | + > |
| 130 | + <div |
| 131 | + className={classNames('dc-dialog__dialog', { |
| 132 | + 'dc-dialog__dialog--has-margin': !is_mobile_full_width, |
| 133 | + })} |
| 134 | + role='dialog' |
| 135 | + ref={wrapper_ref} |
| 136 | + > |
| 137 | + {(title || has_close_icon) && ( |
| 138 | + <div |
| 139 | + className={classNames('dc-dialog__header-wrapper', { |
| 140 | + 'dc-dialog__header-wrapper--end': !title, |
| 141 | + })} |
| 142 | + > |
| 143 | + {!!title && ( |
| 144 | + <Text as='h1' color='prominent' weight='bold' className='dc-dialog__header--title'> |
| 145 | + {title} |
| 146 | + </Text> |
| 147 | + )} |
| 148 | + {has_close_icon && ( |
| 149 | + <div onClick={handleClose} className='dc-dialog__header--close'> |
| 150 | + <img src='/ic-cross.svg'/> |
| 151 | + {/* <Icon icon='IcCross' /> */} |
| 152 | + </div> |
| 153 | + )} |
| 154 | + </div> |
| 155 | + )} |
| 156 | + {is_text ? ( |
| 157 | + <Text as='p' size='xs' styles={{ lineHeight: '1.43' }} className={content_classes}> |
| 158 | + {children} |
| 159 | + </Text> |
| 160 | + ) : ( |
| 161 | + <div className={content_classes}>{children}</div> |
| 162 | + )} |
| 163 | + <div className='dc-dialog__footer'> |
| 164 | + {!!onCancel && ( |
| 165 | + // <Button |
| 166 | + // className='dc-dialog__button' |
| 167 | + // has_effect |
| 168 | + // text={cancel_button_text} |
| 169 | + // onClick={handleCancel} |
| 170 | + // secondary |
| 171 | + // large |
| 172 | + // /> |
| 173 | + <button className='dc-dialog__button' onClick={handleCancel}> |
| 174 | + {cancel_button_text} |
| 175 | + </button> |
| 176 | + )} |
| 177 | + {!!confirm_button_text && ( |
| 178 | + // <Button |
| 179 | + // className='dc-dialog__button' |
| 180 | + // has_effect |
| 181 | + // text={confirm_button_text} |
| 182 | + // onClick={handleConfirm} |
| 183 | + // primary |
| 184 | + // large |
| 185 | + // /> |
| 186 | + <button className='dc-dialog__button' onClick={handleConfirm}> |
| 187 | + {confirm_button_text} |
| 188 | + </button> |
| 189 | + )} |
| 190 | + </div> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + </CSSTransition> |
| 194 | + ); |
| 195 | + |
| 196 | + if (portal_element_id) { |
| 197 | + const target_element = document.getElementById(portal_element_id); |
| 198 | + if (target_element) return ReactDOM.createPortal(dialog, target_element); |
| 199 | + } |
| 200 | + |
| 201 | + return dialog; |
| 202 | +}; |
| 203 | + |
| 204 | +export default Dialog; |
0 commit comments