index.js
5.04 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* @Author: Sakura
* @LastEditors: Sakura
* @Date: 2024-02-26 10:55:04
* @Description: 可拖动函数组件封装
*/
import { Button, Modal } from "antd";
import React, { useRef, useState } from "react";
import Draggable from "react-draggable";
import { InfoCircleOutlined } from "@ant-design/icons";
import "./index.less";
/**
* 可拖动确认框
* @param {string} title - 确认框头部文字
* @param {boolean} btnShow - 是否需要按钮触发
* @param {string} btnText - 调用按钮文字内容
* @param {string | ReactNode} content - 确认框内容主体
* @param {Function} onOk - 用户 确定 回调函数
* @param {Function} onCancel - 用户 取消 回调函数
*/
export const DraggableConfirmModal = ({
btnShow = true,
btnText = "确定",
title = "温馨提示",
content,
onOk = () => {},
onCancel = () => {}
}) => {
const [open, setOpen] = useState(!btnShow);
const [disabled, setDisabled] = useState(true);
const [bounds, setBounds] = useState({
left: 0,
top: 0,
bottom: 0,
right: 0
});
const draggleRef = useRef(null);
// const FriendlyReminder = commonFunc.showLocalMessage(this.props, 'FriendlyReminder', '温馨提示');
const showModal = () => {
setOpen(true);
};
const handleOk = e => {
setOpen(false);
onOk();
};
const handleCancel = e => {
setOpen(false);
onCancel();
};
const onStart = (_event, uiData) => {
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)
});
};
return (
<>
{btnShow && (
<Button
type="primary"
onClick={showModal}
style={{ width: "100px", height: "40px", borderRadius: "5px" }}
>
{" "}
{btnText}{" "}
</Button>
)}
<Modal
className="mesComfirm mesCommonModal"
title={
<div
style={{
width: "100%",
cursor: "move"
}}
onMouseOver={() => {
if (disabled) {
setDisabled(false);
}
}}
onMouseOut={() => {
setDisabled(true);
}}
>
<InfoCircleOutlined style={{ marginRight: "5px" }} />
{title}
</div>
}
open={open}
onOk={handleOk}
onCancel={handleCancel}
modalRender={modal => (
<Draggable
disabled={disabled}
bounds={bounds}
onStart={(event, uiData) => onStart(event, uiData)}
>
<div ref={draggleRef}>{modal}</div>
</Draggable>
)}
>
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "100%",
height: 60,
fontSize: 20
}}
>
{content}
</div>
</Modal>
</>
);
};
/**
* 可拖动页面
* @param {string} title - 确认框头部文字
* @param {number} width - 拖动框宽度
* @param {Node | string | number} conent - 页面级组件
* @param {Function} onCancel - 用户 取消 回调函数
*/
export const DraggablePage = ({
title,
conent,
width = 520,
onCancel = () => {}
}) => {
const [open, setOpen] = useState(true);
const [disabled, setDisabled] = useState(true);
const [bounds, setBounds] = useState({
left: 0,
top: 0,
bottom: 0,
right: 0
});
const draggleRef = useRef(null);
const handleCancel = e => {
setOpen(false);
onCancel();
};
const onStart = (_event, uiData) => {
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)
});
};
return (
<Modal
className="mesComfirm"
footer={null}
width={width}
title={
<div
style={{
width: "100%",
cursor: "move"
}}
onMouseOver={() => {
if (disabled) {
setDisabled(false);
}
}}
onMouseOut={() => {
setDisabled(true);
}}
>
{title}
</div>
}
open={open}
onCancel={handleCancel}
modalRender={modal => (
<Draggable
disabled={disabled}
bounds={bounds}
onStart={(event, uiData) => onStart(event, uiData)}
>
<div ref={draggleRef}>{modal}</div>
</Draggable>
)}
>
{conent}
</Modal>
);
};