|
| 1 | +importReact,{useCallback}from'react'; |
| 2 | +importModalfrom'react-modal'; |
| 3 | +import{createSelector}from'reselect'; |
| 4 | +import{getModals,closeModal}from'state/modals'; |
| 5 | +importconnectfrom'utils/connect'; |
| 6 | + |
| 7 | +Modal.setAppElement('#root'); |
| 8 | + |
| 9 | +exportdefaultfunctionwithModal({ name, ...modalProps}){ |
| 10 | +modalProps={ |
| 11 | +className:{ |
| 12 | +base:`modal modal-${name}`, |
| 13 | +afterOpen:'modal-opening', |
| 14 | +beforeClose:'modal-closing' |
| 15 | +}, |
| 16 | +overlayClassName:{ |
| 17 | +base:'modal-overlay', |
| 18 | +afterOpen:'modal-overlay-opening', |
| 19 | +beforeClose:'modal-overlay-closing' |
| 20 | +}, |
| 21 | +closeTimeoutMS:200, |
| 22 | + ...modalProps |
| 23 | +}; |
| 24 | + |
| 25 | +returnWrappedComponent=>{ |
| 26 | +constReduxModal=({ onRequestClose, ...props})=>{ |
| 27 | +consthandleRequestClose=useCallback( |
| 28 | +(dismissed=true)=>{ |
| 29 | +onRequestClose(); |
| 30 | + |
| 31 | +if(dismissed&&props.payload.onDismiss){ |
| 32 | +props.payload.onDismiss(); |
| 33 | +} |
| 34 | +}, |
| 35 | +[props.payload.onDismiss] |
| 36 | +); |
| 37 | + |
| 38 | +return( |
| 39 | +<Modal |
| 40 | +contentLabel={name} |
| 41 | +onRequestClose={handleRequestClose} |
| 42 | +{...modalProps} |
| 43 | +{...props} |
| 44 | +> |
| 45 | +<WrappedComponentonClose={handleRequestClose}{...props}/> |
| 46 | +</Modal> |
| 47 | +); |
| 48 | +}; |
| 49 | + |
| 50 | +constmapState=createSelector( |
| 51 | +getModals, |
| 52 | +modals=>modals[name]||{payload:{}} |
| 53 | +); |
| 54 | + |
| 55 | +constmapDispatch=dispatch=>({ |
| 56 | +onRequestClose:()=>dispatch(closeModal(name)) |
| 57 | +}); |
| 58 | + |
| 59 | +returnconnect( |
| 60 | +mapState, |
| 61 | +mapDispatch |
| 62 | +)(ReduxModal); |
| 63 | +}; |
| 64 | +} |