|
| 1 | +import React, { useRef, useState } from 'react'; |
| 2 | +import { ResizeHandle } from 'react-resizable'; |
| 3 | +import { Button } from 'antd'; |
| 4 | +import { Modal, Resize } from 'dt-react-component'; |
| 5 | +import { RectState } from 'dt-react-component/modal'; |
| 6 | + |
| 7 | +export default function Basic() { |
| 8 | + const [visible, setVisible] = useState(false); |
| 9 | + const [size, setSize] = useState({ width: window.innerWidth, height: window.innerHeight }); |
| 10 | + |
| 11 | + const [position, setPosition] = useState({ x: 120, y: 120 }); |
| 12 | + const [rect, setRect] = useState<RectState>({ width: 520, height: 520 }); |
| 13 | + |
| 14 | + const resizeDirection = useRef<ResizeHandle>('e'); |
| 15 | + |
| 16 | + // 限制 resize 超出当前屏幕 |
| 17 | + const getMaxConstraints = (): [number, number] => { |
| 18 | + switch (resizeDirection.current) { |
| 19 | + case 'e': |
| 20 | + return [size.width - position.x, size.height]; |
| 21 | + case 'n': |
| 22 | + return [size.width, position.y + rect.height]; |
| 23 | + case 's': |
| 24 | + return [size.width, size.height - position.y]; |
| 25 | + case 'w': |
| 26 | + return [position.x + rect.width, size.height]; |
| 27 | + case 'ne': |
| 28 | + return [size.width - position.x, position.y + rect.height]; |
| 29 | + case 'nw': |
| 30 | + return [position.x + rect.width, position.y + rect.height]; |
| 31 | + case 'se': |
| 32 | + return [size.width - position.x, size.height - position.y]; |
| 33 | + case 'sw': |
| 34 | + return [position.x + rect.width, size.height - position.y]; |
| 35 | + default: |
| 36 | + return [0, 0]; |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <Resize onResize={() => setSize({ width: window.innerWidth, height: window.innerHeight })}> |
| 42 | + <Modal |
| 43 | + title="Window Modal" |
| 44 | + visible={visible} |
| 45 | + mask={false} |
| 46 | + resizable={{ |
| 47 | + maxConstraints: getMaxConstraints(), |
| 48 | + onResize: (_, data) => { |
| 49 | + resizeDirection.current = data.handle; |
| 50 | + }, |
| 51 | + }} |
| 52 | + rect={rect} |
| 53 | + draggable={{ |
| 54 | + bounds: 'body', |
| 55 | + }} |
| 56 | + position={position} |
| 57 | + onPositionChange={setPosition} |
| 58 | + onRectChange={setRect} |
| 59 | + onCancel={() => setVisible(false)} |
| 60 | + onOk={() => setVisible(false)} |
| 61 | + > |
| 62 | + <>Just Dtstack It</> |
| 63 | + </Modal> |
| 64 | + <Button type="primary" onClick={() => setVisible(true)}> |
| 65 | + Window Modal |
| 66 | + </Button> |
| 67 | + </Resize> |
| 68 | + ); |
| 69 | +} |
0 commit comments