Skip to content

Commit 80e4b7d

Browse files
authored
feat: form node-context add clientContext (bytedance#363)
* feat: form node-context add clientContext * fix: errorRender add clientContext props
1 parent 02e48eb commit 80e4b7d

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

packages/node-engine/form-core/src/client/node-render.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { memo, useCallback, useEffect } from 'react';
22

3-
import { PlaygroundContext, useRefresh, useService } from '@flowgram.ai/core';
3+
import { PlaygroundContext, useRefresh, useService, PluginContext } from '@flowgram.ai/core';
44

55
import { NodeEngineReactContext } from '../node-react/context/node-engine-react-context';
66
import { useNodeEngineContext } from '../node-react';
@@ -18,6 +18,7 @@ const PureNodeRender = ({ node }: NodeRenderProps) => {
1818
const isNodeError = !!getNodeError(node);
1919
const isFormReady = isNodeFormReady(node);
2020
const playgroundContext = useService<PlaygroundContext>(PlaygroundContext);
21+
const clientContext = useService<PluginContext>(PluginContext);
2122
const nodeManager = useService<NodeManager>(NodeManager);
2223
const nodeFormRender = nodeManager.getPluginRender(PLUGIN_KEY.FORM);
2324
const nodeErrorRender = nodeManager.getPluginRender(PLUGIN_KEY.ERROR);
@@ -40,13 +41,13 @@ const PureNodeRender = ({ node }: NodeRenderProps) => {
4041

4142
const renderContent = useCallback(() => {
4243
if (isNodeError) {
43-
return nodeErrorRender!({ node, playgroundContext });
44+
return nodeErrorRender!({ node, playgroundContext, clientContext });
4445
}
4546
if (!formModel.formMeta) {
4647
return null;
4748
}
4849
if (isFormReady) {
49-
return nodeFormRender!({ node, playgroundContext });
50+
return nodeFormRender!({ node, playgroundContext, clientContext });
5051
}
5152
return nodePlaceholderRender?.({ node, playgroundContext }) || null;
5253
}, [

packages/node-engine/form-core/src/error/renders/error-render.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useCallback, useEffect } from 'react';
22

33
import { FlowNodeEntity } from '@flowgram.ai/document';
4-
import { PlaygroundContext, useRefresh, useService } from '@flowgram.ai/core';
4+
import { PlaygroundContext, useRefresh, useService, PluginContext } from '@flowgram.ai/core';
55

66
import { FlowNodeErrorData } from '../flow-node-error-data';
77
import { MATERIAL_KEY, NodeManager, NodePluginRender } from '../../node';
@@ -10,9 +10,10 @@ import { defaultErrorRender } from './default-error-render';
1010
interface NodeRenderProps {
1111
node: FlowNodeEntity;
1212
playgroundContext: PlaygroundContext;
13+
clientContext: PluginContext;
1314
}
1415

15-
export const ErrorRender = ({ node, playgroundContext }: NodeRenderProps) => {
16+
export const ErrorRender = ({ node, playgroundContext, clientContext }: NodeRenderProps) => {
1617
const refresh = useRefresh();
1718
const nodeErrorData = node.getData<FlowNodeErrorData>(FlowNodeErrorData);
1819
const nodeError = nodeErrorData.getError();
@@ -23,14 +24,14 @@ export const ErrorRender = ({ node, playgroundContext }: NodeRenderProps) => {
2324
if (!nodeErrorRender) {
2425
return defaultErrorRender({
2526
error: nodeError,
26-
context: { node, playgroundContext },
27+
context: { node, playgroundContext, clientContext },
2728
});
2829
}
2930
return nodeErrorRender({
3031
error: nodeError,
31-
context: { node, playgroundContext },
32+
context: { node, playgroundContext, clientContext },
3233
});
33-
}, [nodeError, node, playgroundContext]);
34+
}, [nodeError, node, playgroundContext, clientContext]);
3435

3536
useEffect(() => {
3637
const disposable = nodeErrorData.onDataChange(() => {

packages/node-engine/form-core/src/node/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
22

33
import { FlowNodeEntity } from '@flowgram.ai/document';
4-
import { PlaygroundContext } from '@flowgram.ai/core';
4+
import { PlaygroundContext, PluginContext } from '@flowgram.ai/core';
55

66
export interface NodeContext {
77
node: FlowNodeEntity;
88
playgroundContext: PlaygroundContext;
9+
clientContext: PluginContext & Record<string, any>;
910
}
1011

1112
export type Render<T = any> = (props: T) => any;
@@ -19,5 +20,5 @@ export interface NodeRenderProps {
1920
}
2021

2122
export type NodeRenderHoc = (
22-
Component: React.JSXElementConstructor<NodeRenderProps>,
23+
Component: React.JSXElementConstructor<NodeRenderProps>
2324
) => React.JSXElementConstructor<NodeRenderProps>;

packages/node-engine/node/src/form-model-v2.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
toForm,
2525
} from '@flowgram.ai/form';
2626
import { FlowNodeEntity } from '@flowgram.ai/document';
27-
import { PlaygroundContext } from '@flowgram.ai/core';
27+
import { PlaygroundContext, PluginContext } from '@flowgram.ai/core';
2828

2929
import {
3030
convertGlobPath,
@@ -175,6 +175,7 @@ export class FormModelV2 extends FormModel implements Disposable {
175175
return {
176176
node: this.node,
177177
playgroundContext: this.node.getService(PlaygroundContext),
178+
clientContext: this.node.getService(PluginContext),
178179
};
179180
}
180181

0 commit comments

Comments
 (0)