antd-overlay 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +479 -0
- package/dist/index.cjs +247 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +635 -0
- package/dist/index.d.ts +635 -0
- package/dist/index.js +231 -0
- package/dist/index.js.map +1 -0
- package/package.json +69 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as React$1 from 'react';
|
|
3
|
+
import React__default from 'react';
|
|
4
|
+
import { ModalProps, DrawerProps } from 'antd';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* AntdOverlay Context 的值类型
|
|
8
|
+
*
|
|
9
|
+
* @property holders - 当前所有已注册的覆盖层 holder 节点
|
|
10
|
+
* @property addHolder - 注册一个新的 holder 到全局容器
|
|
11
|
+
* @property removeHolder - 从全局容器移除一个已注册的 holder
|
|
12
|
+
*/
|
|
13
|
+
interface AntdOverlayContextValue {
|
|
14
|
+
/** 所有已注册的 holder 节点列表 */
|
|
15
|
+
holders: React__default.ReactNode[];
|
|
16
|
+
/** 添加 holder 到全局容器 */
|
|
17
|
+
addHolder: (holder: React__default.ReactNode) => void;
|
|
18
|
+
/** 从全局容器移除 holder */
|
|
19
|
+
removeHolder: (holder: React__default.ReactNode) => void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* AntdOverlay 容器提供者组件
|
|
23
|
+
*
|
|
24
|
+
* 用于管理全局覆盖层的挂载点。所有通过 useGlobalOverlay、useGlobalModal、
|
|
25
|
+
* useGlobalDrawer 等 Hook 创建的覆盖层都会被挂载到这个 Provider 下。
|
|
26
|
+
*
|
|
27
|
+
* 渲染结构:
|
|
28
|
+
* ```
|
|
29
|
+
* <AntdOverlayContext.Provider>
|
|
30
|
+
* {children} <- 应用的主要内容
|
|
31
|
+
* {holders[0]} <- 全局覆盖层 1
|
|
32
|
+
* {holders[1]} <- 全局覆盖层 2
|
|
33
|
+
* ...
|
|
34
|
+
* </AntdOverlayContext.Provider>
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param children - 子节点,通常是整个应用
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // 基础用法 - 在应用入口包裹
|
|
41
|
+
* function App() {
|
|
42
|
+
* return (
|
|
43
|
+
* <AntdOverlayProvider>
|
|
44
|
+
* <Router>
|
|
45
|
+
* <Routes />
|
|
46
|
+
* </Router>
|
|
47
|
+
* </AntdOverlayProvider>
|
|
48
|
+
* );
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* // 与其他 Provider 配合使用
|
|
53
|
+
* function App() {
|
|
54
|
+
* return (
|
|
55
|
+
* <ConfigProvider>
|
|
56
|
+
* <AntdOverlayProvider>
|
|
57
|
+
* <App />
|
|
58
|
+
* </AntdOverlayProvider>
|
|
59
|
+
* </ConfigProvider>
|
|
60
|
+
* );
|
|
61
|
+
* }
|
|
62
|
+
*/
|
|
63
|
+
declare function AntdOverlayProvider({ children }: {
|
|
64
|
+
children: React__default.ReactNode;
|
|
65
|
+
}): react_jsx_runtime.JSX.Element;
|
|
66
|
+
/**
|
|
67
|
+
* 获取 AntdOverlay Context 的 Hook
|
|
68
|
+
*
|
|
69
|
+
* 返回全局容器的注册和注销方法,供 useGlobalOverlay 系列 Hook 内部使用。
|
|
70
|
+
*
|
|
71
|
+
* 注意事项:
|
|
72
|
+
* - 必须在 AntdOverlayProvider 内部使用
|
|
73
|
+
* - 如果在 Provider 外部调用,会抛出明确的错误
|
|
74
|
+
* - 主要供库内部使用,一般不需要在业务代码中直接调用
|
|
75
|
+
*
|
|
76
|
+
* @returns AntdOverlayContextValue 包含 holders、addHolder、removeHolder
|
|
77
|
+
* @throws Error 如果不在 AntdOverlayProvider 内部使用
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* // 库内部使用示例(useGlobalOverlay 的实现)
|
|
81
|
+
* function useGlobalOverlay(Component, options) {
|
|
82
|
+
* const { addHolder, removeHolder } = useAntdOverlayContext();
|
|
83
|
+
* const [open, holder] = useOverlay(Component, options);
|
|
84
|
+
*
|
|
85
|
+
* useEffect(() => {
|
|
86
|
+
* addHolder(holder);
|
|
87
|
+
* return () => removeHolder(holder);
|
|
88
|
+
* }, [holder]);
|
|
89
|
+
*
|
|
90
|
+
* return open;
|
|
91
|
+
* }
|
|
92
|
+
*/
|
|
93
|
+
declare function useAntdOverlayContext(): AntdOverlayContextValue;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @file useOverlay - 通用覆盖层管理 Hook
|
|
97
|
+
* @description
|
|
98
|
+
* 提供一个与具体 UI 组件无关的覆盖层(Overlay)管理方案。
|
|
99
|
+
* 支持任意满足 CustomOverlayProps 接口的组件,如 Modal、Drawer、Popover 等。
|
|
100
|
+
*
|
|
101
|
+
* 核心设计理念:
|
|
102
|
+
* 1. 命令式调用 - 通过函数调用打开覆盖层,而非声明式管理 open 状态
|
|
103
|
+
* 2. 动画支持 - 正确处理打开/关闭动画,避免动画未完成就卸载组件
|
|
104
|
+
* 3. 全局挂载 - 支持将覆盖层挂载到全局容器,实现跨组件调用
|
|
105
|
+
* 4. 类型安全 - 完整的 TypeScript 类型支持
|
|
106
|
+
*
|
|
107
|
+
* 架构层次:
|
|
108
|
+
* ┌─────────────────────────────────────────────────────────┐
|
|
109
|
+
* │ useModal / useDrawer │ <- 业务层封装
|
|
110
|
+
* ├─────────────────────────────────────────────────────────┤
|
|
111
|
+
* │ useOverlay / useGlobalOverlay │ <- 核心逻辑层
|
|
112
|
+
* ├─────────────────────────────────────────────────────────┤
|
|
113
|
+
* │ GlobalHolderProvider │ <- 全局容器层
|
|
114
|
+
* └─────────────────────────────────────────────────────────┘
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* // 直接使用 useOverlay(不推荐,建议使用 useModal/useDrawer)
|
|
118
|
+
* const [openOverlay, holder] = useOverlay(MyOverlayComponent, {
|
|
119
|
+
* propsAdapter: (props, state) => ({ ...props, visible: state.open }),
|
|
120
|
+
* });
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 自定义覆盖层组件必须实现的属性接口
|
|
125
|
+
*
|
|
126
|
+
* 这是所有覆盖层组件的基础接口,定义了控制覆盖层所需的最小属性集。
|
|
127
|
+
* 具体的 UI 组件(如 Modal、Drawer)应该扩展此接口。
|
|
128
|
+
*
|
|
129
|
+
* @template T - customOk 回调接收的参数类型,用于传递确认操作的数据
|
|
130
|
+
* @template R - customOk 回调的返回类型,通常为 void
|
|
131
|
+
*
|
|
132
|
+
* @property open - 控制覆盖层的显示/隐藏状态
|
|
133
|
+
* @property customClose - 关闭覆盖层的回调函数,由 useOverlay 注入
|
|
134
|
+
* @property customOk - 确认操作的回调函数,调用后会自动关闭覆盖层
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* interface MyOverlayProps extends CustomOverlayProps<{ id: number }> {
|
|
138
|
+
* title: string;
|
|
139
|
+
* data: SomeData;
|
|
140
|
+
* }
|
|
141
|
+
*
|
|
142
|
+
* const MyOverlay: React.FC<MyOverlayProps> = ({
|
|
143
|
+
* open,
|
|
144
|
+
* customClose,
|
|
145
|
+
* customOk,
|
|
146
|
+
* title,
|
|
147
|
+
* data,
|
|
148
|
+
* }) => {
|
|
149
|
+
* const handleConfirm = () => {
|
|
150
|
+
* // 调用 customOk 会自动关闭覆盖层
|
|
151
|
+
* customOk?.({ id: data.id });
|
|
152
|
+
* };
|
|
153
|
+
* return (
|
|
154
|
+
* <Modal open={open} onCancel={customClose} onOk={handleConfirm}>
|
|
155
|
+
* {title}
|
|
156
|
+
* </Modal>
|
|
157
|
+
* );
|
|
158
|
+
* };
|
|
159
|
+
*/
|
|
160
|
+
interface CustomOverlayProps<T = any, R = void> {
|
|
161
|
+
/** 覆盖层的显示状态 */
|
|
162
|
+
open?: boolean;
|
|
163
|
+
/** 关闭覆盖层的回调,由 useOverlay 自动注入 */
|
|
164
|
+
customClose: () => void;
|
|
165
|
+
/** 确认操作回调,调用后自动关闭覆盖层 */
|
|
166
|
+
customOk?: (value: T) => R;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* 内部使用的属性类型
|
|
170
|
+
* 排除 customClose,因为它由 useOverlay 自动注入,用户无需传递
|
|
171
|
+
*/
|
|
172
|
+
type InternalProps<T extends CustomOverlayProps> = Omit<T, 'customClose'>;
|
|
173
|
+
/**
|
|
174
|
+
* 覆盖层控制器接口
|
|
175
|
+
*
|
|
176
|
+
* 打开覆盖层后返回的控制器对象,用于后续操作(更新属性或关闭)。
|
|
177
|
+
* 使用 readonly 确保方法引用稳定,不会被意外修改。
|
|
178
|
+
*
|
|
179
|
+
* @template T - 覆盖层组件的属性类型
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* const controller = openModal({ title: '初始标题' });
|
|
183
|
+
*
|
|
184
|
+
* // 动态更新属性
|
|
185
|
+
* controller.update({ title: '新标题', loading: true });
|
|
186
|
+
*
|
|
187
|
+
* // 编程式关闭
|
|
188
|
+
* controller.close();
|
|
189
|
+
*/
|
|
190
|
+
interface OverlayController<T extends CustomOverlayProps> {
|
|
191
|
+
/**
|
|
192
|
+
* 更新覆盖层的属性
|
|
193
|
+
* @param props - 新的属性对象,会完全替换之前的属性
|
|
194
|
+
*/
|
|
195
|
+
readonly update: (props: InternalProps<T>) => void;
|
|
196
|
+
/**
|
|
197
|
+
* 关闭覆盖层
|
|
198
|
+
* 如果启用了动画,会等待动画结束后再卸载组件
|
|
199
|
+
*/
|
|
200
|
+
readonly close: () => void;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 覆盖层打开函数的类型定义
|
|
204
|
+
*
|
|
205
|
+
* @template T - 覆盖层组件的属性类型
|
|
206
|
+
* @param initialize - 初始化属性,可选
|
|
207
|
+
* @returns 覆盖层控制器,用于后续的更新和关闭操作
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* const openModal: OverlayOpener<MyModalProps> = (props) => {
|
|
211
|
+
* // 返回控制器
|
|
212
|
+
* };
|
|
213
|
+
*
|
|
214
|
+
* // 无参数调用
|
|
215
|
+
* const ctrl1 = openModal();
|
|
216
|
+
*
|
|
217
|
+
* // 带初始属性调用
|
|
218
|
+
* const ctrl2 = openModal({ title: '标题', data: someData });
|
|
219
|
+
*/
|
|
220
|
+
type OverlayOpener<T extends CustomOverlayProps> = (initialize?: InternalProps<T>) => OverlayController<T>;
|
|
221
|
+
/**
|
|
222
|
+
* useOverlay Hook 的配置选项
|
|
223
|
+
*
|
|
224
|
+
* @template T - 覆盖层组件的属性类型
|
|
225
|
+
*
|
|
226
|
+
* @property animation - 是否启用动画支持,默认 true
|
|
227
|
+
* - true: 关闭时先设置 open=false,等待动画结束后再卸载组件
|
|
228
|
+
* - false: 关闭时直接卸载组件,适用于无动画的覆盖层
|
|
229
|
+
*
|
|
230
|
+
* @property keyPrefix - React key 的前缀,用于区分不同类型的覆盖层
|
|
231
|
+
* - 默认值: 'use-overlay'
|
|
232
|
+
* - useModal 使用 'use-modal'
|
|
233
|
+
* - useDrawer 使用 'use-drawer'
|
|
234
|
+
*
|
|
235
|
+
* @property propsAdapter - 属性适配器函数
|
|
236
|
+
* 用于将内部状态转换为组件实际需要的属性。
|
|
237
|
+
* 不同的 UI 组件(Modal、Drawer)有不同的动画回调机制,
|
|
238
|
+
* 通过适配器实现统一的接口。
|
|
239
|
+
*/
|
|
240
|
+
interface UseOverlayOptions<T extends CustomOverlayProps> {
|
|
241
|
+
/** 是否启用关闭动画,默认 true */
|
|
242
|
+
animation?: boolean;
|
|
243
|
+
/** React key 前缀,用于标识覆盖层类型 */
|
|
244
|
+
keyPrefix?: string;
|
|
245
|
+
/**
|
|
246
|
+
* 属性适配器函数
|
|
247
|
+
* @param props - 用户传入的属性
|
|
248
|
+
* @param state - 内部状态,包含 open、onClose、onAnimationEnd
|
|
249
|
+
* @returns 传递给组件的最终属性
|
|
250
|
+
*/
|
|
251
|
+
propsAdapter?: (props: InternalProps<T> | undefined, state: {
|
|
252
|
+
/** 当前的打开状态 */
|
|
253
|
+
open: boolean;
|
|
254
|
+
/** 触发关闭的回调 */
|
|
255
|
+
onClose: () => void;
|
|
256
|
+
/** 动画结束后的回调,用于卸载组件 */
|
|
257
|
+
onAnimationEnd: () => void;
|
|
258
|
+
}) => T;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 覆盖层管理核心 Hook
|
|
262
|
+
*
|
|
263
|
+
* 这是整个库的核心,提供覆盖层的生命周期管理:
|
|
264
|
+
* - 挂载/卸载控制
|
|
265
|
+
* - 打开/关闭状态管理
|
|
266
|
+
* - 动画支持
|
|
267
|
+
* - 属性更新
|
|
268
|
+
*
|
|
269
|
+
* 状态机说明:
|
|
270
|
+
* ```
|
|
271
|
+
* [未挂载] --open()--> [已挂载, open=true] --close()--> [已挂载, open=false] --动画结束--> [未挂载]
|
|
272
|
+
* │
|
|
273
|
+
* └── (animation=false) --> [未挂载]
|
|
274
|
+
* ```
|
|
275
|
+
*
|
|
276
|
+
* @template T - 覆盖层组件的属性类型,必须继承 CustomOverlayProps
|
|
277
|
+
*
|
|
278
|
+
* @param OverlayComponent - 覆盖层组件
|
|
279
|
+
* @param options - 配置选项
|
|
280
|
+
*
|
|
281
|
+
* @returns 元组 [openOverlay, contextHolder]
|
|
282
|
+
* - openOverlay: 打开覆盖层的函数,返回控制器
|
|
283
|
+
* - contextHolder: 需要渲染到组件树中的 React 节点
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* function MyComponent() {
|
|
287
|
+
* const [openOverlay, holder] = useOverlay(MyOverlay, {
|
|
288
|
+
* animation: true,
|
|
289
|
+
* propsAdapter: (props, state) => ({
|
|
290
|
+
* ...props,
|
|
291
|
+
* visible: state.open,
|
|
292
|
+
* onClose: state.onClose,
|
|
293
|
+
* afterVisibleChange: (visible) => {
|
|
294
|
+
* if (!visible) state.onAnimationEnd();
|
|
295
|
+
* },
|
|
296
|
+
* }),
|
|
297
|
+
* });
|
|
298
|
+
*
|
|
299
|
+
* return (
|
|
300
|
+
* <>
|
|
301
|
+
* <button onClick={() => openOverlay({ title: '标题' })}>打开</button>
|
|
302
|
+
* {holder}
|
|
303
|
+
* </>
|
|
304
|
+
* );
|
|
305
|
+
* }
|
|
306
|
+
*/
|
|
307
|
+
declare function useOverlay<T extends CustomOverlayProps>(OverlayComponent: React__default.FC<T>, options?: UseOverlayOptions<T>): [OverlayOpener<T>, React__default.ReactNode];
|
|
308
|
+
/**
|
|
309
|
+
* 全局覆盖层管理 Hook
|
|
310
|
+
*
|
|
311
|
+
* 与 useOverlay 的区别:
|
|
312
|
+
* - useOverlay: 需要手动渲染 contextHolder
|
|
313
|
+
* - useGlobalOverlay: 自动挂载到 AntdOverlayProvider
|
|
314
|
+
*
|
|
315
|
+
* 实现原理:
|
|
316
|
+
* 1. 内部调用 useOverlay 获取 openOverlay 和 contextHolder
|
|
317
|
+
* 2. 使用 useEffect 将 contextHolder 注册到全局容器
|
|
318
|
+
* 3. 组件卸载时自动从全局容器移除
|
|
319
|
+
*
|
|
320
|
+
* 使用前提:
|
|
321
|
+
* - 必须在组件树的上层包裹 AntdOverlayProvider
|
|
322
|
+
*
|
|
323
|
+
* @template T - 覆盖层组件的属性类型
|
|
324
|
+
*
|
|
325
|
+
* @param OverlayComponent - 覆盖层组件
|
|
326
|
+
* @param options - 配置选项
|
|
327
|
+
*
|
|
328
|
+
* @returns 打开覆盖层的函数(无需再渲染 holder)
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* // 1. 先在应用入口包裹 Provider
|
|
332
|
+
* function App() {
|
|
333
|
+
* return (
|
|
334
|
+
* <AntdOverlayProvider>
|
|
335
|
+
* <YourApp />
|
|
336
|
+
* </AntdOverlayProvider>
|
|
337
|
+
* );
|
|
338
|
+
* }
|
|
339
|
+
*
|
|
340
|
+
* // 2. 在任意组件中使用
|
|
341
|
+
* function AnyComponent() {
|
|
342
|
+
* const openOverlay = useGlobalOverlay(MyOverlay);
|
|
343
|
+
*
|
|
344
|
+
* return (
|
|
345
|
+
* <button onClick={() => openOverlay({ data: someData })}>
|
|
346
|
+
* 打开全局覆盖层
|
|
347
|
+
* </button>
|
|
348
|
+
* );
|
|
349
|
+
* }
|
|
350
|
+
*/
|
|
351
|
+
declare function useGlobalOverlay<T extends CustomOverlayProps>(OverlayComponent: React__default.FC<T>, options?: UseOverlayOptions<T>): OverlayOpener<T>;
|
|
352
|
+
/**
|
|
353
|
+
* 生成绑定了特定组件的 Hook 工厂函数
|
|
354
|
+
*
|
|
355
|
+
* 当某个覆盖层组件在多处使用时,可以使用此函数生成专用 Hook,
|
|
356
|
+
* 避免每次使用都需要传入组件引用。
|
|
357
|
+
*
|
|
358
|
+
* @template T - 覆盖层组件的属性类型
|
|
359
|
+
*
|
|
360
|
+
* @param OverlayComponent - 覆盖层组件
|
|
361
|
+
* @param defaultOptions - 默认配置选项,会与调用时的选项合并
|
|
362
|
+
*
|
|
363
|
+
* @returns 包含 useOverlay 和 useGlobalOverlay 的对象
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* // 创建专用 Hook
|
|
367
|
+
* const {
|
|
368
|
+
* useOverlay: useMyOverlay,
|
|
369
|
+
* useGlobalOverlay: useGlobalMyOverlay,
|
|
370
|
+
* } = generateUseOverlayHook(MyOverlayComponent, {
|
|
371
|
+
* animation: true,
|
|
372
|
+
* propsAdapter: myAdapter,
|
|
373
|
+
* });
|
|
374
|
+
*
|
|
375
|
+
* // 使用专用 Hook(无需再传组件)
|
|
376
|
+
* function SomeComponent() {
|
|
377
|
+
* const openOverlay = useGlobalMyOverlay();
|
|
378
|
+
* return <button onClick={() => openOverlay()}>打开</button>;
|
|
379
|
+
* }
|
|
380
|
+
*/
|
|
381
|
+
declare function generateUseOverlayHook<T extends CustomOverlayProps>(OverlayComponent: React__default.FC<T>, defaultOptions?: UseOverlayOptions<T>): {
|
|
382
|
+
/**
|
|
383
|
+
* 绑定了特定组件的 useOverlay
|
|
384
|
+
* @param options - 配置选项,会与 defaultOptions 合并
|
|
385
|
+
*/
|
|
386
|
+
useOverlay: (options?: UseOverlayOptions<T>) => [OverlayOpener<T>, React__default.ReactNode];
|
|
387
|
+
/**
|
|
388
|
+
* 绑定了特定组件的 useGlobalOverlay
|
|
389
|
+
* @param options - 配置选项,会与 defaultOptions 合并
|
|
390
|
+
*/
|
|
391
|
+
useGlobalOverlay: (options?: UseOverlayOptions<T>) => OverlayOpener<T>;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* 自定义 Modal 组件的属性接口
|
|
396
|
+
* 继承 Ant Design ModalProps 并添加自定义属性
|
|
397
|
+
*
|
|
398
|
+
* @template T - customOk 回调的参数类型
|
|
399
|
+
* @template R - customOk 回调的返回类型
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* const MyModal: React.FC<CustomModalProps<{ name: string }>> = ({
|
|
403
|
+
* open,
|
|
404
|
+
* customClose,
|
|
405
|
+
* customOk,
|
|
406
|
+
* }) => {
|
|
407
|
+
* return (
|
|
408
|
+
* <Modal open={open} onCancel={customClose} onOk={() => customOk?.({ name: 'test' })}>
|
|
409
|
+
* 内容
|
|
410
|
+
* </Modal>
|
|
411
|
+
* );
|
|
412
|
+
* };
|
|
413
|
+
*/
|
|
414
|
+
interface CustomModalProps<T = any, R = void> extends ModalProps, CustomOverlayProps<T, R> {
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* useModal Hook 的配置选项
|
|
418
|
+
* 排除了 propsAdapter 和 keyPrefix,这些由内部自动处理
|
|
419
|
+
*/
|
|
420
|
+
type UseModalOptions = Omit<UseOverlayOptions<CustomModalProps>, 'propsAdapter' | 'keyPrefix'>;
|
|
421
|
+
/**
|
|
422
|
+
* Modal 管理 Hook
|
|
423
|
+
*
|
|
424
|
+
* @template T - Modal 组件的属性类型,必须继承 CustomModalProps
|
|
425
|
+
*
|
|
426
|
+
* @param ModalComponent - Modal 组件
|
|
427
|
+
* @param options - 配置选项
|
|
428
|
+
*
|
|
429
|
+
* @returns 元组 [openModal, contextHolder]
|
|
430
|
+
* - openModal: 打开 Modal 的函数
|
|
431
|
+
* - contextHolder: 需要渲染到组件树中的 React 节点
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* function MyPage() {
|
|
435
|
+
* const [openModal, modalHolder] = useModal(ConfirmModal);
|
|
436
|
+
*
|
|
437
|
+
* const handleDelete = () => {
|
|
438
|
+
* openModal({
|
|
439
|
+
* title: '确认删除',
|
|
440
|
+
* content: '删除后无法恢复',
|
|
441
|
+
* });
|
|
442
|
+
* };
|
|
443
|
+
*
|
|
444
|
+
* return (
|
|
445
|
+
* <>
|
|
446
|
+
* <button onClick={handleDelete}>删除</button>
|
|
447
|
+
* {modalHolder}
|
|
448
|
+
* </>
|
|
449
|
+
* );
|
|
450
|
+
* }
|
|
451
|
+
*/
|
|
452
|
+
declare function useModal<T extends CustomModalProps>(ModalComponent: React.FC<T>, options?: UseModalOptions): [OverlayOpener<T>, React.ReactNode];
|
|
453
|
+
/**
|
|
454
|
+
* 全局 Modal 管理 Hook
|
|
455
|
+
*
|
|
456
|
+
* 与 useModal 的区别:
|
|
457
|
+
* - 无需手动渲染 contextHolder
|
|
458
|
+
* - Modal 会自动挂载到全局容器
|
|
459
|
+
* - 适合需要跨组件调用的场景
|
|
460
|
+
*
|
|
461
|
+
* @template T - Modal 组件的属性类型
|
|
462
|
+
*
|
|
463
|
+
* @param ModalComponent - Modal 组件
|
|
464
|
+
* @param options - 配置选项
|
|
465
|
+
*
|
|
466
|
+
* @returns 打开 Modal 的函数
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* function DeleteButton() {
|
|
470
|
+
* const openConfirm = useGlobalModal(ConfirmModal);
|
|
471
|
+
*
|
|
472
|
+
* return (
|
|
473
|
+
* <button onClick={() => openConfirm({ title: '确认删除?' })}>
|
|
474
|
+
* 删除
|
|
475
|
+
* </button>
|
|
476
|
+
* );
|
|
477
|
+
* }
|
|
478
|
+
*/
|
|
479
|
+
declare function useGlobalModal<T extends CustomModalProps>(ModalComponent: React.FC<T>, options?: UseModalOptions): OverlayOpener<T>;
|
|
480
|
+
/**
|
|
481
|
+
* 生成绑定了特定 Modal 组件的 Hook 工厂函数
|
|
482
|
+
*
|
|
483
|
+
* 适用场景:
|
|
484
|
+
* - 某个 Modal 在多处使用
|
|
485
|
+
* - 希望简化调用代码
|
|
486
|
+
* - 需要统一管理某个 Modal 的默认配置
|
|
487
|
+
*
|
|
488
|
+
* @template T - Modal 组件的属性类型
|
|
489
|
+
*
|
|
490
|
+
* @param ModalComponent - Modal 组件
|
|
491
|
+
*
|
|
492
|
+
* @returns 包含 useModal 和 useGlobalModal 的对象
|
|
493
|
+
*
|
|
494
|
+
* @example
|
|
495
|
+
* // 在 Modal 组件文件中导出专用 Hook
|
|
496
|
+
* const ConfirmModal: React.FC<CustomModalProps> = (props) => {
|
|
497
|
+
* // ...
|
|
498
|
+
* };
|
|
499
|
+
*
|
|
500
|
+
* export const {
|
|
501
|
+
* useModal: useConfirmModal,
|
|
502
|
+
* useGlobalModal: useGlobalConfirmModal,
|
|
503
|
+
* } = generateUseModalHook(ConfirmModal);
|
|
504
|
+
*
|
|
505
|
+
* // 在其他组件中使用
|
|
506
|
+
* function MyPage() {
|
|
507
|
+
* const openConfirm = useGlobalConfirmModal();
|
|
508
|
+
* return <button onClick={() => openConfirm()}>确认</button>;
|
|
509
|
+
* }
|
|
510
|
+
*/
|
|
511
|
+
declare function generateUseModalHook<T extends CustomModalProps>(ModalComponent: React.FC<T>): {
|
|
512
|
+
useModal: (options?: UseModalOptions) => [OverlayOpener<T>, React$1.ReactNode];
|
|
513
|
+
useGlobalModal: (options?: UseModalOptions) => OverlayOpener<T>;
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* 自定义 Drawer 组件的属性接口
|
|
518
|
+
* 继承 Ant Design DrawerProps 并添加自定义属性
|
|
519
|
+
*
|
|
520
|
+
* @template T - customOk 回调的参数类型
|
|
521
|
+
* @template R - customOk 回调的返回类型
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* const MyDrawer: React.FC<CustomDrawerProps<{ id: number }>> = ({
|
|
525
|
+
* open,
|
|
526
|
+
* customClose,
|
|
527
|
+
* customOk,
|
|
528
|
+
* }) => {
|
|
529
|
+
* return (
|
|
530
|
+
* <Drawer open={open} onClose={customClose}>
|
|
531
|
+
* <Button onClick={() => customOk?.({ id: 1 })}>确认</Button>
|
|
532
|
+
* </Drawer>
|
|
533
|
+
* );
|
|
534
|
+
* };
|
|
535
|
+
*/
|
|
536
|
+
interface CustomDrawerProps<T = any, R = void> extends DrawerProps, CustomOverlayProps<T, R> {
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* useDrawer Hook 的配置选项
|
|
540
|
+
* 排除了 propsAdapter 和 keyPrefix,这些由内部自动处理
|
|
541
|
+
*/
|
|
542
|
+
type UseDrawerOptions = Omit<UseOverlayOptions<CustomDrawerProps>, 'propsAdapter' | 'keyPrefix'>;
|
|
543
|
+
/**
|
|
544
|
+
* Drawer 管理 Hook
|
|
545
|
+
*
|
|
546
|
+
* @template T - Drawer 组件的属性类型,必须继承 CustomDrawerProps
|
|
547
|
+
*
|
|
548
|
+
* @param DrawerComponent - Drawer 组件
|
|
549
|
+
* @param options - 配置选项
|
|
550
|
+
*
|
|
551
|
+
* @returns 元组 [openDrawer, contextHolder]
|
|
552
|
+
* - openDrawer: 打开 Drawer 的函数
|
|
553
|
+
* - contextHolder: 需要渲染到组件树中的 React 节点
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* function MyPage() {
|
|
557
|
+
* const [openDrawer, drawerHolder] = useDrawer(UserDetailDrawer);
|
|
558
|
+
*
|
|
559
|
+
* const handleViewUser = (userId: number) => {
|
|
560
|
+
* openDrawer({ userId, title: '用户详情' });
|
|
561
|
+
* };
|
|
562
|
+
*
|
|
563
|
+
* return (
|
|
564
|
+
* <>
|
|
565
|
+
* <button onClick={() => handleViewUser(1)}>查看用户</button>
|
|
566
|
+
* {drawerHolder}
|
|
567
|
+
* </>
|
|
568
|
+
* );
|
|
569
|
+
* }
|
|
570
|
+
*/
|
|
571
|
+
declare function useDrawer<T extends CustomDrawerProps>(DrawerComponent: React.FC<T>, options?: UseDrawerOptions): [OverlayOpener<T>, React.ReactNode];
|
|
572
|
+
/**
|
|
573
|
+
* 全局 Drawer 管理 Hook
|
|
574
|
+
*
|
|
575
|
+
* 与 useDrawer 的区别:
|
|
576
|
+
* - 无需手动渲染 contextHolder
|
|
577
|
+
* - Drawer 会自动挂载到全局容器
|
|
578
|
+
* - 适合需要跨组件调用的场景
|
|
579
|
+
*
|
|
580
|
+
* @template T - Drawer 组件的属性类型
|
|
581
|
+
*
|
|
582
|
+
* @param DrawerComponent - Drawer 组件
|
|
583
|
+
* @param options - 配置选项
|
|
584
|
+
*
|
|
585
|
+
* @returns 打开 Drawer 的函数
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* function UserCard({ userId }: { userId: number }) {
|
|
589
|
+
* const openDetail = useGlobalDrawer(UserDetailDrawer);
|
|
590
|
+
*
|
|
591
|
+
* return (
|
|
592
|
+
* <Card onClick={() => openDetail({ userId })}>
|
|
593
|
+
* 查看详情
|
|
594
|
+
* </Card>
|
|
595
|
+
* );
|
|
596
|
+
* }
|
|
597
|
+
*/
|
|
598
|
+
declare function useGlobalDrawer<T extends CustomDrawerProps>(DrawerComponent: React.FC<T>, options?: UseDrawerOptions): OverlayOpener<T>;
|
|
599
|
+
/**
|
|
600
|
+
* 生成绑定了特定 Drawer 组件的 Hook 工厂函数
|
|
601
|
+
*
|
|
602
|
+
* 适用场景:
|
|
603
|
+
* - 某个 Drawer 在多处使用
|
|
604
|
+
* - 希望简化调用代码
|
|
605
|
+
* - 需要统一管理某个 Drawer 的默认配置
|
|
606
|
+
*
|
|
607
|
+
* @template T - Drawer 组件的属性类型
|
|
608
|
+
*
|
|
609
|
+
* @param DrawerComponent - Drawer 组件
|
|
610
|
+
*
|
|
611
|
+
* @returns 包含 useDrawer 和 useGlobalDrawer 的对象
|
|
612
|
+
*
|
|
613
|
+
* @example
|
|
614
|
+
* // 在 Drawer 组件文件中导出专用 Hook
|
|
615
|
+
* const ProjectMemberDrawer: React.FC<CustomDrawerProps> = (props) => {
|
|
616
|
+
* // ...
|
|
617
|
+
* };
|
|
618
|
+
*
|
|
619
|
+
* export const {
|
|
620
|
+
* useDrawer: useProjectMemberDrawer,
|
|
621
|
+
* useGlobalDrawer: useGlobalProjectMemberDrawer,
|
|
622
|
+
* } = generateUseDrawerHook(ProjectMemberDrawer);
|
|
623
|
+
*
|
|
624
|
+
* // 在其他组件中使用
|
|
625
|
+
* function ProjectPage() {
|
|
626
|
+
* const openMemberDrawer = useGlobalProjectMemberDrawer();
|
|
627
|
+
* return <button onClick={() => openMemberDrawer()}>管理成员</button>;
|
|
628
|
+
* }
|
|
629
|
+
*/
|
|
630
|
+
declare function generateUseDrawerHook<T extends CustomDrawerProps>(DrawerComponent: React.FC<T>): {
|
|
631
|
+
useDrawer: (options?: UseDrawerOptions) => [OverlayOpener<T>, React$1.ReactNode];
|
|
632
|
+
useGlobalDrawer: (options?: UseDrawerOptions) => OverlayOpener<T>;
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
export { AntdOverlayProvider, type CustomDrawerProps, type CustomModalProps, type CustomOverlayProps, type OverlayController, type OverlayOpener, type UseDrawerOptions, type UseModalOptions, type UseOverlayOptions, generateUseDrawerHook, generateUseModalHook, generateUseOverlayHook, useAntdOverlayContext, useDrawer, useGlobalDrawer, useGlobalModal, useGlobalOverlay, useModal, useOverlay };
|