1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| import React, { useState, useRef, useEffect } from 'react' import { Button, Modal, ModalProps } from 'antd' import type { DraggableData, DraggableEvent } from 'react-draggable' import Draggable from 'react-draggable' import Title from './title'
interface IFooterProps { readOnly?: boolean customFooter?: boolean }
const ModalComps: React.FC<ModalProps & IFooterProps> = React.memo((props): React.ReactElement => { const [form, setForm] = useState(null) const [disabled, setDisabled] = useState(false) const [full, setFull] = useState(false) const [bounds, setBounds] = useState({ left: 0, top: 0, bottom: 0, right: 0 }) const draggleRef = useRef<HTMLDivElement>(null)
useEffect(() => { if (props?.visible === false) setFull(false) }, [props.visible])
const childrenRender = () => { const prop = { onModalForm: getChildrenForm, } return React.Children.map(props?.children, (child: any) => React.cloneElement(child, prop)) }
const getChildrenForm = (form: any) => setForm(form)
const onSubmit = (e: React.MouseEvent<HTMLElement>) => { if (form) { form ?.validateFields() .then((values: IObjectProps) => { return values }) .catch(() => { return false }) .then((res: any) => { if (res === false) return props?.onOk?.(res || '') }) return } props?.onOk?.(e) }
const onStart = (_event: DraggableEvent, uiData: DraggableData) => { const { clientWidth, clientHeight } = window.document.documentElement const targetRect = draggleRef.current?.getBoundingClientRect() if (!targetRect) return
setBounds({ left: -targetRect.left + uiData.x, right: clientWidth - (targetRect.right - uiData.x), top: -targetRect.top + uiData.y, bottom: clientHeight - (targetRect.bottom - uiData.y), }) }
const modalRender = (modal: React.ReactNode) => { if (full) return modal
return ( <Draggable disabled={disabled} bounds={bounds} onStart={(event, uiData) => onStart(event, uiData)} > <div ref={draggleRef}>{modal}</div> </Draggable> ) }
return ( <Modal width={900} {...props} title={ <Title title={props.title} disabled={disabled} setDisabled={setDisabled} full={full} setFull={setFull} /> } className={`msh-modal ${full ? 'full' : ''}`} centered={!full} modalRender={modalRender} footer={[ props.readOnly ? ( props.customFooter ? ( props.footer ) : ( <Button key="cancel" onClick={props.onCancel}> 关闭 </Button> ) ) : ( <div key="btn"> <Button key="submit" type="primary" onClick={onSubmit}> 确认 </Button> <Button key="back" onClick={props.onCancel}> 取消 </Button> </div> ), ]} getContainer={'#root'} > {childrenRender()} </Modal> ) })
export default ModalComps
|