Skip to content

Commit 9498039

Browse files
authored
Merge pull request #12 from easyops-cn/steve/async
feat(): support update brick properties in event handler
2 parents 5a18f3c + a677b3c commit 9498039

File tree

19 files changed

+497
-79
lines changed

19 files changed

+497
-79
lines changed

apps/test/src/Components/MyModal.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { Modal } from "@next-bricks/containers/modal";
2-
import { type Ref, useImperativeHandle, useRef } from "@next-tsx/core";
2+
import { type RefObject, useImperativeHandle, useRef } from "@next-tsx/core";
33

44
export interface MyModalRef {
55
open: () => void;
66
}
77

8-
export default function MyModal({ ref }: { ref?: Ref<MyModalRef> }) {
8+
export default function MyModal({ ref }: { ref?: RefObject<MyModalRef> }) {
99
const modalRef = useRef<Modal | null>(null);
1010
const formRef = useRef<any | null>(null);
1111
useImperativeHandle(ref, () => ({
@@ -17,5 +17,9 @@ export default function MyModal({ ref }: { ref?: Ref<MyModalRef> }) {
1717
},
1818
}));
1919

20-
return null;
20+
return (
21+
<eo-modal ref={modalRef}>
22+
<eo-form ref={formRef} />
23+
</eo-modal>
24+
);
2125
}

apps/test/src/Pages/About.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
22
callApi,
3+
handleHttpError,
34
useContext,
5+
useRef,
46
useResource,
57
useSearchParams,
68
} from "@next-tsx/core";
@@ -9,17 +11,30 @@ import { LayoutContext } from "../Contexts/LayoutContext";
911
export default function About() {
1012
const { handleClick } = useContext(LayoutContext);
1113
const params = useSearchParams();
12-
const [res] = useResource(
14+
const [res, refetch] = useResource(
1315
() => callApi("my.api@getAboutInfo:1.0.0", { id: params.get("id") }),
1416
{ async: true, fallback: null }
1517
);
18+
const modalRef = useRef();
1619

1720
return (
1821
<>
19-
<div onMount={handleClick}>
22+
<div
23+
onMount={handleClick}
24+
onClick={() => {
25+
refetch().then(
26+
() => {
27+
// eslint-disable-next-line no-console
28+
console.log("ok");
29+
},
30+
(err) => handleHttpError(err)
31+
);
32+
}}
33+
>
2034
About {params.get("id")} {res}
2135
</div>
2236
<div>{res ? <span slot="a">1</span> : <span slot="b">2</span>}</div>
37+
<eo-modal ref={modalRef} />
2338
</>
2439
);
2540
}

apps/test/src/Pages/Layout.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
sessionStore,
88
Routes,
99
Route,
10+
useRef,
1011
} from "@next-tsx/core";
1112
import MyModal from "../Components/MyModal";
1213
import { LayoutContext } from "../Contexts/LayoutContext";
@@ -16,6 +17,7 @@ export default function Layout() {
1617
const [a, setA] = useState<string | null>(null);
1718
const b = a ? "value" : "null";
1819
const [c, setC] = useState<number>(0);
20+
const modalRef = useRef();
1921

2022
useEffect(() => {
2123
if (a) {
@@ -36,6 +38,11 @@ export default function Layout() {
3638
const handleClick = () => {
3739
setA("New Value by callback");
3840
setC((prev) => prev + 42);
41+
modalRef.current?.open();
42+
modalRef.current.width = 500;
43+
Object.assign(modalRef.current, {
44+
height: "auto",
45+
});
3946
};
4047

4148
return (
@@ -76,7 +83,7 @@ export default function Layout() {
7683
<Route path="/about" component={About} />
7784
</Routes>
7885
</div>
79-
{createPortal(<MyModal />)}
86+
{createPortal(<MyModal ref={modalRef} />)}
8087
</LayoutContext.Provider>
8188
);
8289
}

docs/DESIGN.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
apps/
55
└── YOUR-APP-ID/
66
└── src/
7-
├── Components/
8-
├── Contexts/
9-
├── Pages/
10-
├── Utils/
11-
├── app.json
12-
├── i18n.json
13-
└── index.tsx
14-
```
15-
16-
### 组件
17-
18-
```tsx
19-
// ./src/Components/
7+
├── Components/ # 组件(tpl-*)
8+
├── Contexts/ # 跨组件共享上下文
9+
├── Pages/ # 页面(路由)
10+
├── Utils/ # 辅助函数
11+
├── app.json # 应用基本信息
12+
├── i18n.json # 国际化配置
13+
└── index.tsx # 入口文件,定义第一层路由页面
2014
```

packages/converter/src/__snapshots__/convertTsx.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ exports[`convertTsx should work 1`] = `
195195
"args": [],
196196
"key": undefined,
197197
"method": "open",
198-
"target": "[data-root-id="test-root"] [data-ref="modalRef"]",
198+
"target": "[data-root-id="test-root"] [data-ref="modalRef-0"]",
199199
},
200200
],
201201
"toggle": [
@@ -311,7 +311,7 @@ exports[`convertTsx should work 1`] = `
311311
"cancelText": undefined,
312312
"confirmText": undefined,
313313
"dataset": {
314-
"ref": "modalRef",
314+
"ref": "modalRef-0",
315315
},
316316
"hideCancelButton": true,
317317
"keyboard": true,

packages/converter/src/convertEvents.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { BrickEventHandler, BrickEventsMap } from "@next-core/types";
2-
import type { Component, EventHandler } from "@next-tsx/parser";
2+
import type {
3+
Component,
4+
EventHandler,
5+
TypeEventHandlerCallback,
6+
} from "@next-tsx/parser";
37
import type { ConvertOptions } from "./interfaces.js";
48

59
export function convertEvents(component: Component, options: ConvertOptions) {
@@ -99,18 +103,11 @@ function convertEventHandler(
99103
? "state.refresh"
100104
: "context.refresh",
101105
args: [handler.payload.name],
106+
callback: convertCallback(handler.callback, options),
102107
};
103108
case "call_api": {
104109
const { api, http, tool, params, isRawProvider } = handler.payload;
105110

106-
const success = handler.callback?.success
107-
? convertEventHandlers(handler.callback.success, options)
108-
: undefined;
109-
110-
const error = handler.callback?.error
111-
? convertEventHandlers(handler.callback.error, options)
112-
: undefined;
113-
114111
return {
115112
key: handler.key,
116113
...(isRawProvider
@@ -132,12 +129,21 @@ function convertEventHandler(
132129
useProvider: `${api}${api.includes(":") ? "" : ":*"}`,
133130
params,
134131
}),
135-
callback: {
136-
...(success && { success }),
137-
...(error && { error }),
138-
},
132+
callback: convertCallback(handler.callback, options),
139133
};
140134
}
135+
case "update_ref":
136+
return {
137+
key: handler.key,
138+
...(handler.payload.scope === "template"
139+
? {
140+
targetRef: handler.payload.ref,
141+
}
142+
: {
143+
target: `${options.rootId ? `[data-root-id="${options.rootId}"] ` : ""}[data-ref="${handler.payload.ref}"]`,
144+
}),
145+
properties: handler.payload.properties,
146+
};
141147
case "call_ref":
142148
return {
143149
key: handler.key,
@@ -208,3 +214,24 @@ function convertEventHandler(
208214
};
209215
}
210216
}
217+
218+
function convertCallback(
219+
callback: TypeEventHandlerCallback | undefined,
220+
options: ConvertOptions
221+
): {
222+
success?: BrickEventHandler[];
223+
error?: BrickEventHandler[];
224+
} {
225+
const success = callback?.success
226+
? convertEventHandlers(callback.success, options)
227+
: undefined;
228+
229+
const error = callback?.error
230+
? convertEventHandlers(callback.error, options)
231+
: undefined;
232+
233+
return {
234+
...(success && { success }),
235+
...(error && { error }),
236+
};
237+
}

packages/core/index.d.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function useResource<T = any>(
2929
// 设置 async: true 时则不会阻塞。
3030
async?: boolean;
3131
}
32-
): [data: T, refetch: () => void];
32+
): [data: T, refetch: () => Promise<void>];
3333

3434
/**
3535
* 在依赖项数组变化时执行回调函数。
@@ -41,7 +41,7 @@ export function useResource<T = any>(
4141
export function useEffect(callback: () => void, deps: unknown[]): void;
4242

4343
/** 返回一个 ref 对象,其 `.current` 属性初始化为传入的参数 (`initialValue`) */
44-
export function useRef<T>(initialValue: T): RefObject<T>;
44+
export function useRef<T = any>(initialValue?: T): RefObject<T>;
4545

4646
export type Ref<T> = RefObject<T> | null;
4747

@@ -102,21 +102,25 @@ export function usePathName(): string;
102102
export function useHistory(): History;
103103

104104
export interface History {
105-
push: (url: string) => void;
106-
replace: (url: string) => void;
105+
push: (url: string, options?: HistoryOptions) => void;
106+
replace: (url: string, options?: HistoryOptions) => void;
107107
reload: () => void;
108108
pushQuery: UpdateQuery;
109109
replaceQuery: UpdateQuery;
110110
}
111111

112112
export type UpdateQuery = (
113113
newQuery: Record<string, string | null>,
114-
options?: {
114+
options?: HistoryOptions & {
115115
clear?: boolean; // 是否清除已有参数,默认 false
116-
notify?: boolean; // 是否通知路由变化,默认 true
117116
}
118117
) => void;
119118

119+
export interface HistoryOptions {
120+
notify?: boolean; // 是否通知路由变化,默认 true
121+
noIncremental?: boolean; // 是否禁止增量更新,默认 false
122+
}
123+
120124
export function useLocation(): {
121125
href: string;
122126
origin: string;

packages/parser/src/__snapshots__/parseView.spec.ts.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ exports[`parseView should work 1`] = `
349349
"type": "Identifier",
350350
},
351351
"kind": "ref",
352+
"refName": "modalRef-0",
352353
},
353354
Node {
354355
"end": 1039,
@@ -775,7 +776,7 @@ exports[`parseView should work 1`] = `
775776
"payload": {
776777
"args": [],
777778
"method": "open",
778-
"ref": "modalRef",
779+
"ref": "modalRef-0",
779780
"scope": "global",
780781
},
781782
},
@@ -917,7 +918,7 @@ exports[`parseView should work 1`] = `
917918
: "" %>",
918919
"width": "800px",
919920
},
920-
"ref": "modalRef",
921+
"ref": "modalRef-0",
921922
"reference": null,
922923
"slot": undefined,
923924
},

packages/parser/src/interfaces.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface Events {
5151
export type EventHandler =
5252
| TypeEventHandlerOfUpdateVariable
5353
| TypeEventHandlerOfRefreshDataSource
54+
| TypeEventHandlerOfUpdateRef
5455
| TypeEventHandlerOfCallRef
5556
| TypeEventHandlerOfCallSelector
5657
| TypeEventHandlerOfShowMessage
@@ -83,6 +84,16 @@ export interface TypeEventHandlerOfRefreshDataSource extends EventHandlerBase {
8384
name: string;
8485
scope?: "global" | "template";
8586
};
87+
callback?: TypeEventHandlerCallback;
88+
}
89+
90+
export interface TypeEventHandlerOfUpdateRef extends EventHandlerBase {
91+
action: "update_ref";
92+
payload: {
93+
ref: string;
94+
properties: Record<string, any>;
95+
scope?: "global" | "template";
96+
};
8697
}
8798

8899
export interface TypeEventHandlerOfCallRef extends EventHandlerBase {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let counter = 0;
2+
3+
export function getUniqueId(prefix: string) {
4+
return `${prefix}${counter++}`;
5+
}
6+
7+
export function resetUniqueIdCounter() {
8+
counter = 0;
9+
}

0 commit comments

Comments
 (0)