antd-overlay 0.0.3 → 0.1.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 +92 -131
- package/dist/index.cjs +19 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +41 -18
- package/dist/index.d.ts +41 -18
- package/dist/index.js +20 -5
- package/dist/index.js.map +1 -1
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -7,11 +7,11 @@ Ant Design Modal/Drawer 命令式调用方案。
|
|
|
7
7
|
|
|
8
8
|
## 特性
|
|
9
9
|
|
|
10
|
-
- 🚀 **命令式调用** -
|
|
11
|
-
- 🎨 **动画支持** -
|
|
12
|
-
- 🌍 **全局挂载** -
|
|
10
|
+
- 🚀 **命令式调用** - 通过函数调用打开/关闭覆盖层,无需在业务里维护 `open` 状态
|
|
11
|
+
- 🎨 **动画支持** - 正确处理打开/关闭动画(Modal 使用 `afterClose`,Drawer 使用 `afterOpenChange`),避免动画未完成就卸载
|
|
12
|
+
- 🌍 **全局挂载** - 支持跨组件调用,覆盖层挂载到 `AntdOverlayProvider` 统一容器
|
|
13
13
|
- 📦 **类型安全** - 完整的 TypeScript 类型支持
|
|
14
|
-
- 🔧 **灵活扩展** -
|
|
14
|
+
- 🔧 **灵活扩展** - `useOverlay` + `propsAdapter` 可对接自定义覆盖层组件
|
|
15
15
|
|
|
16
16
|
## 安装
|
|
17
17
|
|
|
@@ -34,24 +34,40 @@ yarn add antd-overlay
|
|
|
34
34
|
}
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
+
## 本地开发与示例
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pnpm install
|
|
41
|
+
pnpm dev:demo # 启动 Vite 演示(demo/:Modal / Drawer / useOverlay)
|
|
42
|
+
pnpm build # 使用 tsup 构建 dist
|
|
43
|
+
pnpm typecheck # TypeScript 检查
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
演示入口将 `ConfigProvider` 与 `AntdOverlayProvider` 组合使用,与线上应用推荐结构一致。
|
|
47
|
+
|
|
37
48
|
## 快速开始
|
|
38
49
|
|
|
39
50
|
### 1. 包裹 Provider(可选,仅全局 Hook 需要)
|
|
40
51
|
|
|
41
52
|
```tsx
|
|
42
53
|
import { AntdOverlayProvider } from 'antd-overlay';
|
|
54
|
+
import { ConfigProvider } from 'antd';
|
|
43
55
|
|
|
44
56
|
function App() {
|
|
45
57
|
return (
|
|
46
|
-
<
|
|
47
|
-
<
|
|
48
|
-
|
|
58
|
+
<ConfigProvider>
|
|
59
|
+
<AntdOverlayProvider>
|
|
60
|
+
<YourApp />
|
|
61
|
+
</AntdOverlayProvider>
|
|
62
|
+
</ConfigProvider>
|
|
49
63
|
);
|
|
50
64
|
}
|
|
51
65
|
```
|
|
52
66
|
|
|
53
67
|
### 2. 创建覆盖层组件
|
|
54
68
|
|
|
69
|
+
自定义 Modal 需实现 `CustomModalProps`(继承 antd `ModalProps` 与 `CustomOverlayProps`)。请将 antd `Modal` 的 `open` 接到 props 上,由 Hook 注入显示状态。
|
|
70
|
+
|
|
55
71
|
```tsx
|
|
56
72
|
import { Modal, Input } from 'antd';
|
|
57
73
|
import { CustomModalProps } from 'antd-overlay';
|
|
@@ -62,6 +78,7 @@ interface MyModalProps extends CustomModalProps<{ result: string }> {
|
|
|
62
78
|
}
|
|
63
79
|
|
|
64
80
|
const MyModal: React.FC<MyModalProps> = ({
|
|
81
|
+
open,
|
|
65
82
|
customClose,
|
|
66
83
|
customOk,
|
|
67
84
|
initialValue,
|
|
@@ -71,6 +88,7 @@ const MyModal: React.FC<MyModalProps> = ({
|
|
|
71
88
|
|
|
72
89
|
return (
|
|
73
90
|
<Modal
|
|
91
|
+
open={open}
|
|
74
92
|
title="输入内容"
|
|
75
93
|
onCancel={customClose}
|
|
76
94
|
onOk={() => customOk?.({ result: value })}
|
|
@@ -91,10 +109,10 @@ import { useModal, useGlobalModal } from 'antd-overlay';
|
|
|
91
109
|
function LocalUsage() {
|
|
92
110
|
const [openModal, holder] = useModal(MyModal);
|
|
93
111
|
|
|
94
|
-
const handleOpen =
|
|
112
|
+
const handleOpen = () => {
|
|
95
113
|
const controller = openModal({ initialValue: 'hello' });
|
|
96
|
-
// controller.update({ initialValue: 'updated' });
|
|
97
|
-
// controller.close();
|
|
114
|
+
// controller.update({ initialValue: 'updated' });
|
|
115
|
+
// controller.close();
|
|
98
116
|
};
|
|
99
117
|
|
|
100
118
|
return (
|
|
@@ -117,25 +135,23 @@ function GlobalUsage() {
|
|
|
117
135
|
}
|
|
118
136
|
```
|
|
119
137
|
|
|
138
|
+
`openModal(...)` 返回 `OverlayController`:可调用 `update` 传入要更新的字段(与当前已保存的 props 及 Hook 的 `defaultProps` **浅合并**,同名键以本次 `update` 入参为准)、`close` 关闭(会尊重动画配置)。
|
|
139
|
+
|
|
120
140
|
## API
|
|
121
141
|
|
|
122
142
|
### Provider
|
|
123
143
|
|
|
124
144
|
#### `AntdOverlayProvider`
|
|
125
145
|
|
|
126
|
-
|
|
146
|
+
全局覆盖层容器;使用 `useGlobalModal`、`useGlobalDrawer`、`useGlobalOverlay` 时需要在应用内包裹。
|
|
127
147
|
|
|
128
148
|
**属性:**
|
|
129
|
-
- `children: React.ReactNode` - 子节点
|
|
130
|
-
- `defaultModalProps?: Partial<ModalProps>` - 默认 Modal 属性,会应用到所有 Modal
|
|
131
|
-
- `defaultDrawerProps?: Partial<DrawerProps>` - 默认 Drawer 属性,会应用到所有 Drawer
|
|
132
149
|
|
|
133
|
-
|
|
134
|
-
<
|
|
135
|
-
|
|
136
|
-
</AntdOverlayProvider>
|
|
150
|
+
- `children: React.ReactNode`
|
|
151
|
+
- `defaultModalProps?: Partial<ModalProps>` — 默认 Modal 属性,与每次 `open` / `update` 传入的 props 合并(传入方优先)
|
|
152
|
+
- `defaultDrawerProps?: Partial<DrawerProps>` — 同上,作用于 Drawer
|
|
137
153
|
|
|
138
|
-
|
|
154
|
+
```tsx
|
|
139
155
|
<AntdOverlayProvider
|
|
140
156
|
defaultModalProps={{ centered: true, maskClosable: false }}
|
|
141
157
|
defaultDrawerProps={{ width: 600 }}
|
|
@@ -144,93 +160,54 @@ function GlobalUsage() {
|
|
|
144
160
|
</AntdOverlayProvider>
|
|
145
161
|
```
|
|
146
162
|
|
|
163
|
+
#### `useAntdOverlayContext()`
|
|
164
|
+
|
|
165
|
+
读取 Context(含 `holders`、`addHolder`、`removeHolder` 及默认 Modal/Drawer 配置)。**必须在 `AntdOverlayProvider` 内使用**;一般供扩展或库内集成,业务侧很少直接使用。
|
|
166
|
+
|
|
147
167
|
### Modal Hooks
|
|
148
168
|
|
|
149
169
|
#### `useModal<T>(Component, options?)`
|
|
150
170
|
|
|
151
|
-
局部 Modal
|
|
171
|
+
局部 Modal Hook。
|
|
152
172
|
|
|
153
|
-
|
|
154
|
-
- `Component: React.FC<T>` - Modal 组件,需实现 `CustomModalProps` 接口
|
|
155
|
-
- `options?: UseModalOptions` - 配置选项
|
|
156
|
-
- `animation?: boolean` - 是否启用动画,默认 `true`
|
|
173
|
+
**`options`(`UseModalOptions<T>`,`T` 由传入的 Modal 组件推断)** 与底层 `useOverlay` 一致(不含 `propsAdapter` / `keyPrefix`,由内部固定):
|
|
157
174
|
|
|
158
|
-
|
|
159
|
-
- `
|
|
175
|
+
- `animation?: boolean` — 是否等待关闭动画后再卸载,默认 `true`
|
|
176
|
+
- `defaultProps?: Partial<Omit<T, 'customClose'>>` — 每次打开/更新时与入参合并的默认属性
|
|
177
|
+
- 另可将 Modal 相关字段及**组件自定义扩展属性**写在 `options` 顶层,与 `defaultProps` 合并时**顶层字段优先**
|
|
160
178
|
|
|
161
|
-
|
|
162
|
-
const [openModal, holder] = useModal(MyModal);
|
|
163
|
-
```
|
|
179
|
+
**返回值:** `[openModal, holder]` — `openModal` 为 `OverlayOpener<T>`,返回 `OverlayController<T>`。
|
|
164
180
|
|
|
165
181
|
#### `useGlobalModal<T>(Component, options?)`
|
|
166
182
|
|
|
167
|
-
全局 Modal
|
|
168
|
-
|
|
169
|
-
**参数:** 同 `useModal`
|
|
170
|
-
|
|
171
|
-
**返回值:**
|
|
172
|
-
- `openModal` - 打开函数
|
|
173
|
-
|
|
174
|
-
```tsx
|
|
175
|
-
const openModal = useGlobalModal(MyModal);
|
|
176
|
-
```
|
|
183
|
+
全局 Modal Hook;无需渲染 `holder`。
|
|
177
184
|
|
|
178
185
|
#### `generateUseModalHook<T>(Component)`
|
|
179
186
|
|
|
180
|
-
|
|
187
|
+
为指定 Modal 组件生成 `{ useModal, useGlobalModal }`,二者均可传入 `options?: UseModalOptions<T>`(`T` 与组件 props 一致)。
|
|
181
188
|
|
|
182
189
|
```tsx
|
|
183
|
-
// modal.tsx
|
|
184
190
|
export const {
|
|
185
191
|
useModal: useMyModal,
|
|
186
192
|
useGlobalModal: useGlobalMyModal,
|
|
187
193
|
} = generateUseModalHook(MyModal);
|
|
188
|
-
|
|
189
|
-
// usage.tsx
|
|
190
|
-
const openModal = useGlobalMyModal();
|
|
191
194
|
```
|
|
192
195
|
|
|
193
196
|
### Drawer Hooks
|
|
194
197
|
|
|
195
|
-
#### `useDrawer<T>(Component, options?)`
|
|
198
|
+
#### `useDrawer<T>(Component, options?)` / `useGlobalDrawer<T>(Component, options?)`
|
|
196
199
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
```tsx
|
|
200
|
-
const [openDrawer, holder] = useDrawer(MyDrawer);
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
#### `useGlobalDrawer<T>(Component, options?)`
|
|
204
|
-
|
|
205
|
-
全局 Drawer 管理 Hook。
|
|
206
|
-
|
|
207
|
-
```tsx
|
|
208
|
-
const openDrawer = useGlobalDrawer(MyDrawer);
|
|
209
|
-
```
|
|
200
|
+
语义与 Modal 侧相同,选项类型为 `UseDrawerOptions<T>`(`T` 由 Drawer 组件推断;同样支持 `animation`、`defaultProps`、顶层 Drawer 属性及组件自定义扩展字段)。
|
|
210
201
|
|
|
211
202
|
#### `generateUseDrawerHook<T>(Component)`
|
|
212
203
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
```tsx
|
|
216
|
-
export const {
|
|
217
|
-
useDrawer: useMyDrawer,
|
|
218
|
-
useGlobalDrawer: useGlobalMyDrawer,
|
|
219
|
-
} = generateUseDrawerHook(MyDrawer);
|
|
220
|
-
```
|
|
204
|
+
生成 `{ useDrawer, useGlobalDrawer }`。
|
|
221
205
|
|
|
222
206
|
### 通用 Overlay Hooks
|
|
223
207
|
|
|
224
208
|
#### `useOverlay<T>(Component, options?)`
|
|
225
209
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
**参数:**
|
|
229
|
-
- `Component: React.FC<T>` - 覆盖层组件
|
|
230
|
-
- `options?: UseOverlayOptions<T>` - 配置选项
|
|
231
|
-
- `animation?: boolean` - 是否启用动画,默认 `true`
|
|
232
|
-
- `keyPrefix?: string` - React key 前缀
|
|
233
|
-
- `propsAdapter?: (props, state) => T` - 属性适配器函数
|
|
210
|
+
通用覆盖层 Hook;需自行提供 `propsAdapter`,把内部 `state.open` / `state.onClose` / `state.onAnimationEnd` 映射到组件 API。
|
|
234
211
|
|
|
235
212
|
```tsx
|
|
236
213
|
const [openOverlay, holder] = useOverlay(MyOverlay, {
|
|
@@ -238,7 +215,7 @@ const [openOverlay, holder] = useOverlay(MyOverlay, {
|
|
|
238
215
|
...props,
|
|
239
216
|
visible: state.open,
|
|
240
217
|
onClose: state.onClose,
|
|
241
|
-
afterVisibleChange: (visible) => {
|
|
218
|
+
afterVisibleChange: (visible: boolean) => {
|
|
242
219
|
if (!visible) state.onAnimationEnd();
|
|
243
220
|
},
|
|
244
221
|
}),
|
|
@@ -247,67 +224,53 @@ const [openOverlay, holder] = useOverlay(MyOverlay, {
|
|
|
247
224
|
|
|
248
225
|
#### `useGlobalOverlay<T>(Component, options?)`
|
|
249
226
|
|
|
250
|
-
|
|
227
|
+
全局版本,依赖 `AntdOverlayProvider`。
|
|
251
228
|
|
|
252
229
|
#### `generateUseOverlayHook<T>(Component, defaultOptions?)`
|
|
253
230
|
|
|
254
|
-
|
|
231
|
+
生成绑定组件的 `useOverlay` / `useGlobalOverlay`;调用时可再传 `options` 与 `defaultOptions` 浅合并。
|
|
255
232
|
|
|
256
233
|
### 类型定义
|
|
257
234
|
|
|
258
|
-
#### `
|
|
259
|
-
|
|
260
|
-
Modal 组件属性接口,继承自 `antd` 的 `ModalProps`。
|
|
235
|
+
#### `CustomOverlayProps<T, R>`
|
|
261
236
|
|
|
262
237
|
```typescript
|
|
263
|
-
interface
|
|
238
|
+
interface CustomOverlayProps<T = any, R = void> {
|
|
264
239
|
open?: boolean;
|
|
265
240
|
customClose: () => void;
|
|
266
241
|
customOk?: (value: T) => R;
|
|
267
242
|
}
|
|
268
243
|
```
|
|
269
244
|
|
|
270
|
-
#### `CustomDrawerProps<T, R>`
|
|
245
|
+
#### `CustomModalProps<T, R>` / `CustomDrawerProps<T, R>`
|
|
271
246
|
|
|
272
|
-
|
|
247
|
+
分别为 `ModalProps` / `DrawerProps` 与 `CustomOverlayProps` 的交叉类型。
|
|
273
248
|
|
|
274
|
-
|
|
275
|
-
interface CustomDrawerProps<T = any, R = void> extends DrawerProps {
|
|
276
|
-
open?: boolean;
|
|
277
|
-
customClose: () => void;
|
|
278
|
-
customOk?: (value: T) => R;
|
|
279
|
-
}
|
|
280
|
-
```
|
|
249
|
+
#### `InternalOverlayProps<T>`
|
|
281
250
|
|
|
282
|
-
|
|
251
|
+
`Omit<T, 'customClose'>`,表示打开/更新时可传入的属性(由 Hook 注入 `customClose`)。
|
|
283
252
|
|
|
284
|
-
|
|
253
|
+
#### `OverlayController<T>`
|
|
285
254
|
|
|
286
255
|
```typescript
|
|
287
|
-
interface
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
256
|
+
interface OverlayController<T extends CustomOverlayProps> {
|
|
257
|
+
/** 与 defaultProps、当前 props 浅合并后更新 */
|
|
258
|
+
readonly update: (props: InternalOverlayProps<T>) => void;
|
|
259
|
+
readonly close: () => void;
|
|
291
260
|
}
|
|
292
261
|
```
|
|
293
262
|
|
|
294
|
-
#### `
|
|
295
|
-
|
|
296
|
-
覆盖层控制器,由 `openModal`/`openDrawer`/`openOverlay` 返回。
|
|
263
|
+
#### `OverlayOpener<T>`
|
|
297
264
|
|
|
298
|
-
|
|
299
|
-
interface OverlayController<T> {
|
|
300
|
-
update: (props: Omit<T, 'customClose'>) => void;
|
|
301
|
-
close: () => void;
|
|
302
|
-
}
|
|
303
|
-
```
|
|
265
|
+
`(initialize?: InternalOverlayProps<T>) => OverlayController<T>`。
|
|
304
266
|
|
|
305
267
|
## 完整示例
|
|
306
268
|
|
|
307
269
|
### 确认删除 Modal
|
|
308
270
|
|
|
309
271
|
```tsx
|
|
310
|
-
import {
|
|
272
|
+
import React, { useState } from 'react';
|
|
273
|
+
import { Modal, message, List, Button } from 'antd';
|
|
311
274
|
import { CustomModalProps, useGlobalModal } from 'antd-overlay';
|
|
312
275
|
|
|
313
276
|
interface ConfirmDeleteModalProps extends CustomModalProps<void> {
|
|
@@ -315,6 +278,7 @@ interface ConfirmDeleteModalProps extends CustomModalProps<void> {
|
|
|
315
278
|
}
|
|
316
279
|
|
|
317
280
|
const ConfirmDeleteModal: React.FC<ConfirmDeleteModalProps> = ({
|
|
281
|
+
open,
|
|
318
282
|
customClose,
|
|
319
283
|
customOk,
|
|
320
284
|
itemName,
|
|
@@ -327,7 +291,7 @@ const ConfirmDeleteModal: React.FC<ConfirmDeleteModalProps> = ({
|
|
|
327
291
|
try {
|
|
328
292
|
message.success('删除成功');
|
|
329
293
|
customOk?.();
|
|
330
|
-
} catch
|
|
294
|
+
} catch {
|
|
331
295
|
message.error('删除失败');
|
|
332
296
|
} finally {
|
|
333
297
|
setLoading(false);
|
|
@@ -336,6 +300,7 @@ const ConfirmDeleteModal: React.FC<ConfirmDeleteModalProps> = ({
|
|
|
336
300
|
|
|
337
301
|
return (
|
|
338
302
|
<Modal
|
|
303
|
+
open={open}
|
|
339
304
|
title="确认删除"
|
|
340
305
|
onCancel={customClose}
|
|
341
306
|
onOk={handleOk}
|
|
@@ -344,29 +309,31 @@ const ConfirmDeleteModal: React.FC<ConfirmDeleteModalProps> = ({
|
|
|
344
309
|
okType="danger"
|
|
345
310
|
{...props}
|
|
346
311
|
>
|
|
347
|
-
确定要删除
|
|
312
|
+
确定要删除 "{itemName}" 吗?此操作不可恢复。
|
|
348
313
|
</Modal>
|
|
349
314
|
);
|
|
350
315
|
};
|
|
351
316
|
|
|
352
|
-
//
|
|
317
|
+
// 使用(示例数据与类型请按项目替换)
|
|
353
318
|
function ItemList() {
|
|
354
319
|
const openConfirm = useGlobalModal(ConfirmDeleteModal);
|
|
355
320
|
|
|
356
|
-
const handleDelete = (item:
|
|
321
|
+
const handleDelete = (item: { id: number; name: string }) => {
|
|
357
322
|
openConfirm({
|
|
358
323
|
itemName: item.name,
|
|
359
|
-
customOk: () =>
|
|
324
|
+
customOk: () => {
|
|
325
|
+
/* deleteItem(item.id) */
|
|
326
|
+
},
|
|
360
327
|
});
|
|
361
328
|
};
|
|
362
329
|
|
|
363
330
|
return (
|
|
364
331
|
<List
|
|
365
|
-
dataSource={
|
|
332
|
+
dataSource={[]}
|
|
366
333
|
renderItem={(item) => (
|
|
367
334
|
<List.Item
|
|
368
335
|
actions={[
|
|
369
|
-
<Button danger onClick={() => handleDelete(item)}>
|
|
336
|
+
<Button key="del" danger onClick={() => handleDelete(item)}>
|
|
370
337
|
删除
|
|
371
338
|
</Button>,
|
|
372
339
|
]}
|
|
@@ -382,6 +349,7 @@ function ItemList() {
|
|
|
382
349
|
### 用户详情 Drawer
|
|
383
350
|
|
|
384
351
|
```tsx
|
|
352
|
+
import React, { useEffect, useState } from 'react';
|
|
385
353
|
import { Drawer, Descriptions, Spin } from 'antd';
|
|
386
354
|
import { CustomDrawerProps, generateUseDrawerHook } from 'antd-overlay';
|
|
387
355
|
|
|
@@ -390,29 +358,25 @@ interface UserDetailDrawerProps extends CustomDrawerProps {
|
|
|
390
358
|
}
|
|
391
359
|
|
|
392
360
|
const UserDetailDrawer: React.FC<UserDetailDrawerProps> = ({
|
|
361
|
+
open,
|
|
393
362
|
customClose,
|
|
394
363
|
userId,
|
|
395
364
|
...props
|
|
396
365
|
}) => {
|
|
397
|
-
const [user, setUser] = useState<
|
|
366
|
+
const [user, setUser] = useState<{ name: string; email: string; phone: string } | null>(null);
|
|
398
367
|
const [loading, setLoading] = useState(false);
|
|
399
368
|
|
|
400
369
|
useEffect(() => {
|
|
401
370
|
if (open && userId) {
|
|
402
371
|
setLoading(true);
|
|
403
|
-
fetchUser(userId)
|
|
372
|
+
Promise.resolve(/* fetchUser(userId) */)
|
|
404
373
|
.then(setUser)
|
|
405
374
|
.finally(() => setLoading(false));
|
|
406
375
|
}
|
|
407
376
|
}, [open, userId]);
|
|
408
377
|
|
|
409
378
|
return (
|
|
410
|
-
<Drawer
|
|
411
|
-
title="用户详情"
|
|
412
|
-
onClose={customClose}
|
|
413
|
-
width={500}
|
|
414
|
-
{...props}
|
|
415
|
-
>
|
|
379
|
+
<Drawer open={open} title="用户详情" onClose={customClose} width={500} {...props}>
|
|
416
380
|
{loading ? (
|
|
417
381
|
<Spin />
|
|
418
382
|
) : user ? (
|
|
@@ -426,27 +390,24 @@ const UserDetailDrawer: React.FC<UserDetailDrawerProps> = ({
|
|
|
426
390
|
);
|
|
427
391
|
};
|
|
428
392
|
|
|
429
|
-
// 导出专用 Hook
|
|
430
393
|
export const {
|
|
431
394
|
useDrawer: useUserDetailDrawer,
|
|
432
395
|
useGlobalDrawer: useGlobalUserDetailDrawer,
|
|
433
396
|
} = generateUseDrawerHook(UserDetailDrawer);
|
|
434
397
|
|
|
435
|
-
// 使用
|
|
436
398
|
function UserCard({ userId }: { userId: number }) {
|
|
437
399
|
const openDetail = useGlobalUserDetailDrawer();
|
|
438
400
|
|
|
439
|
-
return (
|
|
440
|
-
<Card onClick={() => openDetail({ userId })}>
|
|
441
|
-
查看详情
|
|
442
|
-
</Card>
|
|
443
|
-
);
|
|
401
|
+
return <div onClick={() => openDetail({ userId })}>查看详情</div>;
|
|
444
402
|
}
|
|
445
403
|
```
|
|
446
404
|
|
|
447
405
|
### 动态更新 Modal
|
|
448
406
|
|
|
449
407
|
```tsx
|
|
408
|
+
import { Button } from 'antd';
|
|
409
|
+
// UploadModal、delay 由业务自行实现
|
|
410
|
+
|
|
450
411
|
function ProgressModal() {
|
|
451
412
|
const [openModal, holder] = useModal(UploadModal);
|
|
452
413
|
|
|
@@ -476,11 +437,11 @@ function ProgressModal() {
|
|
|
476
437
|
|
|
477
438
|
```
|
|
478
439
|
┌─────────────────────────────────────────────────────────┐
|
|
479
|
-
│ useModal / useDrawer │
|
|
440
|
+
│ useModal / useDrawer │ 业务层封装
|
|
480
441
|
├─────────────────────────────────────────────────────────┤
|
|
481
|
-
│ useOverlay / useGlobalOverlay │
|
|
442
|
+
│ useOverlay / useGlobalOverlay │ 核心逻辑层
|
|
482
443
|
├─────────────────────────────────────────────────────────┤
|
|
483
|
-
│ AntdOverlayProvider │
|
|
444
|
+
│ AntdOverlayProvider │ 全局容器层
|
|
484
445
|
└─────────────────────────────────────────────────────────┘
|
|
485
446
|
```
|
|
486
447
|
|
package/dist/index.cjs
CHANGED
|
@@ -52,8 +52,16 @@ function useOverlay(OverlayComponent, options = {}) {
|
|
|
52
52
|
const {
|
|
53
53
|
animation = true,
|
|
54
54
|
keyPrefix = "use-overlay",
|
|
55
|
-
|
|
55
|
+
defaultProps: defaultPropsFromOption,
|
|
56
|
+
propsAdapter = defaultPropsAdapter,
|
|
57
|
+
...restDefaultFromTopLevel
|
|
56
58
|
} = options;
|
|
59
|
+
const mergedDefaultProps = {
|
|
60
|
+
...defaultPropsFromOption,
|
|
61
|
+
...restDefaultFromTopLevel
|
|
62
|
+
};
|
|
63
|
+
const defaultPropsRef = React2.useRef(mergedDefaultProps);
|
|
64
|
+
defaultPropsRef.current = mergedDefaultProps;
|
|
57
65
|
const id = React2.useId();
|
|
58
66
|
const key = `${keyPrefix}-${id}`;
|
|
59
67
|
const [open, setOpen] = React2.useState(false);
|
|
@@ -95,13 +103,20 @@ function useOverlay(OverlayComponent, options = {}) {
|
|
|
95
103
|
(initializeProps) => {
|
|
96
104
|
setRenderEnable(true);
|
|
97
105
|
setOpen(true);
|
|
98
|
-
setProps(
|
|
106
|
+
setProps({
|
|
107
|
+
...defaultPropsRef.current,
|
|
108
|
+
...initializeProps
|
|
109
|
+
});
|
|
99
110
|
return {
|
|
100
111
|
/**
|
|
101
112
|
* 更新覆盖层属性
|
|
102
|
-
*
|
|
113
|
+
* 与 defaultProps 合并,传入的 newProps 优先
|
|
103
114
|
*/
|
|
104
|
-
update: (newProps) => setProps(
|
|
115
|
+
update: (newProps) => setProps((prev) => ({
|
|
116
|
+
...defaultPropsRef.current,
|
|
117
|
+
...prev,
|
|
118
|
+
...newProps
|
|
119
|
+
})),
|
|
105
120
|
/**
|
|
106
121
|
* 关闭覆盖层
|
|
107
122
|
*/
|