@flowgram.ai/node 0.1.0-alpha.10

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.
@@ -0,0 +1,366 @@
1
+ import * as React from 'react';
2
+ import React__default from 'react';
3
+ import { FormModel, OnFormValuesChangePayload, FormModelValid, FormFeedback, FormManager, IFormItem, FormItem, NodeFormContext, IFormMeta } from '@flowgram.ai/form-core';
4
+ import { FieldName as FieldName$1, FieldValue as FieldValue$1, Errors, Warnings } from '@flowgram.ai/form/src/types';
5
+ import { FormValidateReturn, FormControl, FormModel as FormModel$1, FieldValue, IFieldArray, IField, FieldName, Validate as Validate$1, IForm, FormRenderProps, ValidateTrigger, FormState } from '@flowgram.ai/form';
6
+ import * as _flowgram_ai_document from '@flowgram.ai/document';
7
+ import { FlowNodeEntity } from '@flowgram.ai/document';
8
+ import * as _flowgram_ai_utils from '@flowgram.ai/utils';
9
+ import { Disposable, Emitter, DisposableCollection, Event } from '@flowgram.ai/utils';
10
+
11
+ declare class FormModelV2 extends FormModel implements Disposable {
12
+ protected effectMap: Record<string, EffectOptions[]>;
13
+ protected effectReturnMap: Map<DataEvent, Record<string, EffectReturn>>;
14
+ protected plugins: FormPlugin[];
15
+ protected node: FlowNodeEntity;
16
+ protected formFeedbacks: FormValidateReturn | undefined;
17
+ protected onInitializedEmitter: Emitter<FormModel>;
18
+ protected onValidateEmitter: Emitter<FormModel>;
19
+ readonly onValidate: _flowgram_ai_utils.Event<FormModel>;
20
+ readonly onInitialized: _flowgram_ai_utils.Event<FormModel>;
21
+ protected onDisposeEmitter: Emitter<void>;
22
+ readonly onDispose: _flowgram_ai_utils.Event<void>;
23
+ protected toDispose: DisposableCollection;
24
+ protected onFormValuesChangeEmitter: Emitter<OnFormValuesChangePayload>;
25
+ readonly onFormValuesChange: _flowgram_ai_utils.Event<OnFormValuesChangePayload>;
26
+ protected onValidChangeEmitter: Emitter<FormModelValid>;
27
+ readonly onValidChange: _flowgram_ai_utils.Event<FormModelValid>;
28
+ protected onFeedbacksChangeEmitter: Emitter<FormFeedback[]>;
29
+ readonly onFeedbacksChange: _flowgram_ai_utils.Event<FormFeedback[]>;
30
+ constructor(node: FlowNodeEntity);
31
+ protected _valid: FormModelValid;
32
+ get valid(): FormModelValid;
33
+ private set valid(value);
34
+ get flowNodeEntity(): FlowNodeEntity;
35
+ get formManager(): FormManager;
36
+ protected _formControl?: FormControl<any>;
37
+ get formControl(): FormControl<any> | undefined;
38
+ protected _formMeta: FormMeta;
39
+ get formMeta(): FormMeta;
40
+ get values(): any;
41
+ protected _feedbacks: FormFeedback[];
42
+ get feedbacks(): FormFeedback[];
43
+ updateFormValues(value: any): void;
44
+ private set feedbacks(value);
45
+ get formItemPathMap(): Map<string, IFormItem>;
46
+ protected _initialized: boolean;
47
+ get initialized(): boolean;
48
+ get nodeContext(): {
49
+ node: FlowNodeEntity;
50
+ playgroundContext: unknown;
51
+ clientContext: unknown;
52
+ };
53
+ get nativeFormModel(): FormModel$1 | undefined;
54
+ render(): React.JSX.Element;
55
+ initPlugins(plugins: FormPlugin[]): void;
56
+ init(formMeta: FormMeta, rawInitialValues?: any): void;
57
+ toJSON(): any;
58
+ clearValid(): void;
59
+ validate(): Promise<boolean>;
60
+ getValues<T = any>(): T | undefined;
61
+ getField<TValue = FieldValue, TField extends IFieldArray<TValue> | IField<TValue> = IField<TValue>>(name: FieldName): TField | undefined;
62
+ getValueIn<TValue>(name: FieldName): TValue | undefined;
63
+ setValueIn(name: FieldName, value: any): void;
64
+ /**
65
+ * 监听表单某个路径下的值变化
66
+ * @param name 路径
67
+ * @param callback 回调函数
68
+ */
69
+ onFormValueChangeIn<TValue = FieldValue, TFormValue = FieldValue>(name: FieldName, callback: (payload: onFormValueChangeInPayload<TValue, TFormValue>) => void): Disposable;
70
+ /**
71
+ * @deprecated 该方法用于兼容 V1 版本 FormModel接口,如果确定是FormModelV2 请使用 FormModel.getValueIn
72
+ * @param path glob path
73
+ */
74
+ getFormItemValueByPath(globPath: string): any;
75
+ validateWithFeedbacks(): Promise<FormFeedback[]>;
76
+ /**
77
+ * @deprecated 该方法用于兼容 V1 版本 FormModel接口,如果确定是FormModelV2, 请使用FormModel.getValueIn 和 FormModel.setValueIn
78
+ * @param path glob path
79
+ */
80
+ getFormItemByPath(path: string): FormItem | undefined;
81
+ dispose(): void;
82
+ }
83
+
84
+ interface FormPluginConfig<Opts = any> {
85
+ /**
86
+ * form plugin name, for debug use
87
+ */
88
+ name?: string;
89
+ /**
90
+ * setup formMeta
91
+ * @param ctx
92
+ * @returns
93
+ */
94
+ onSetupFormMeta?: (ctx: FormPluginSetupMetaCtx, opts: Opts) => void;
95
+ /**
96
+ * FormModel 初始化时执行
97
+ * @param ctx
98
+ */
99
+ onInit?: (ctx: FormPluginCtx, opts: Opts) => void;
100
+ /**
101
+ * FormModel 销毁时执行
102
+ */
103
+ onDispose?: (ctx: FormPluginCtx, opts: Opts) => void;
104
+ }
105
+ declare class FormPlugin<Opts = any> implements Disposable {
106
+ readonly name: string;
107
+ readonly pluginId: string;
108
+ readonly config: FormPluginConfig;
109
+ readonly opts?: Opts;
110
+ protected _formModel: FormModelV2;
111
+ constructor(config: FormPluginConfig, opts?: Opts);
112
+ get formModel(): FormModelV2;
113
+ get ctx(): {
114
+ formModel: FormModelV2;
115
+ node: _flowgram_ai_document.FlowNodeEntity;
116
+ playgroundContext: unknown;
117
+ };
118
+ setupFormMeta(formMeta: FormMeta, nodeContext: NodeFormContext): FormMeta;
119
+ init(formModel: FormModelV2): void;
120
+ dispose(): void;
121
+ }
122
+ type FormPluginCreator<Opts> = (opts: Opts) => FormPlugin<Opts>;
123
+ declare function defineFormPluginCreator<Opts>(config: FormPluginConfig): FormPluginCreator<Opts>;
124
+
125
+ /**
126
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
127
+ * SPDX-License-Identifier: MIT
128
+ */
129
+
130
+ interface Node {
131
+ }
132
+ interface Flow {
133
+ }
134
+ /**
135
+ * NodeContext contains
136
+ * - node: the Editor's node entity.
137
+ * - playgroundContext: the Editor's playgroundContext injected when initiate the Editor.
138
+ */
139
+ type NodeContext = NodeFormContext;
140
+ type Validate<TFieldValue = any, TFormValues = any> = (props: {
141
+ value: TFieldValue;
142
+ formValues: TFormValues;
143
+ context: NodeContext;
144
+ name: FieldName$1;
145
+ }) => ReturnType<Validate$1<TFieldValue, TFormValues>>;
146
+ declare enum DataEvent {
147
+ onValueChange = "onValueChange",
148
+ /**
149
+ * When value Init,it triggers when
150
+ * - defaultValue is configured in formMeta, it will trigger when form is initializing.
151
+ * - defaultValue is configured in Field, it will trigger when this Field is initializing if no initial value is set to this field.
152
+ */
153
+ onValueInit = "onValueInit",
154
+ /**
155
+ * When Value Init or change
156
+ */
157
+ onValueInitOrChange = "onValueInitOrChange",
158
+ onArrayAppend = "onArrayAppend",
159
+ onArrayDelete = "onArrayDelete"
160
+ }
161
+ type EffectReturn = () => void;
162
+ interface EffectFuncProps<TFieldValue = any, TFormValues = any> {
163
+ name: FieldName$1;
164
+ value: TFieldValue;
165
+ prevValue?: TFieldValue;
166
+ formValues: TFormValues;
167
+ form: IForm;
168
+ context: NodeContext;
169
+ }
170
+ type Effect<TFieldValue = any, TFormValues = any> = (props: EffectFuncProps<TFieldValue, TFormValues>) => void | EffectReturn;
171
+ type ArrayAppendEffect<TFieldValue = any, TFormValues = any> = (props: {
172
+ index: number;
173
+ value: TFieldValue;
174
+ arrayValues: Array<TFieldValue>;
175
+ formValues: TFormValues;
176
+ form: IForm;
177
+ context: NodeContext;
178
+ }) => void | EffectReturn;
179
+ type ArrayDeleteEffect<TFieldValue = any, TFormValues = any> = (props: {
180
+ index: number;
181
+ arrayValue: Array<TFieldValue>;
182
+ formValues: TFormValues;
183
+ form: IForm;
184
+ context: NodeContext;
185
+ }) => void | EffectReturn;
186
+ type EffectOptions = {
187
+ effect: Effect;
188
+ event: DataEvent;
189
+ } | {
190
+ effect: ArrayAppendEffect;
191
+ event: DataEvent;
192
+ } | {
193
+ effect: ArrayDeleteEffect;
194
+ event: DataEvent;
195
+ };
196
+ interface FormMeta<TValues = any> {
197
+ /**
198
+ * The render method of the node form content. <Form /> is already integrated, so you don't need to wrap your components with <Form />
199
+ * @param props
200
+ */
201
+ render: (props: FormRenderProps<any>) => React.ReactElement;
202
+ /**
203
+ * When to trigger the validation.
204
+ */
205
+ validateTrigger?: ValidateTrigger;
206
+ /**
207
+ * Form data's validation rules. It's a key value map, where the key is a pattern of data's path (or field name), the value is a validate function.
208
+ */
209
+ validate?: Record<FieldName$1, Validate>;
210
+ /**
211
+ * Form data's effects. It's a key value map, where the key is a pattern of data's path (or field name), the value is an array of effect configuration.
212
+ */
213
+ effect?: Record<FieldName$1, EffectOptions[]>;
214
+ /**
215
+ * Form data's complete default value. it will not be sent to formatOnInit, but used directly as form's value when needed.
216
+ */
217
+ defaultValues?: TValues | ((context: NodeContext) => TValues);
218
+ /**
219
+ * This function is to format the value when initiate the form, the returned value will be used as the initial value of the form.
220
+ * @param value value input to node as initialValue.
221
+ * @param context
222
+ */
223
+ formatOnInit?: (value: any, context: NodeContext) => any;
224
+ /**
225
+ * This function is to format the value when FormModel.toJSON is called, the returned value will be used as the final value to be saved .
226
+ * @param value value sent by form before format.
227
+ * @param context
228
+ */
229
+ formatOnSubmit?: (value: any, context: NodeContext) => any;
230
+ /**
231
+ * Form's plugins
232
+ */
233
+ plugins?: FormPlugin[];
234
+ }
235
+ declare function isFormModelV2(fm: FormModel | FormModelV2): fm is FormModelV2;
236
+ declare function isFormMetaV2(formMeta: IFormMeta | FormMeta): boolean;
237
+ type FormPluginCtx = {
238
+ formModel: FormModelV2;
239
+ } & NodeContext;
240
+ type FormPluginSetupMetaCtx = {
241
+ mergeEffect: (effect: Record<string, EffectOptions[]>) => void;
242
+ mergeValidate: (validate: Record<FieldName$1, Validate>) => void;
243
+ addFormatOnInit: (formatOnInit: FormMeta['formatOnInit']) => void;
244
+ addFormatOnSubmit: (formatOnSubmit: FormMeta['formatOnSubmit']) => void;
245
+ } & NodeContext;
246
+ interface onFormValueChangeInPayload<TValue = FieldValue$1, TFormValues = FieldValue$1> {
247
+ value: TValue;
248
+ prevValue: TValue;
249
+ formValues: TFormValues;
250
+ prevFormValues: TFormValues;
251
+ }
252
+
253
+ /**
254
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
255
+ * SPDX-License-Identifier: MIT
256
+ */
257
+
258
+ declare function isFormV2(node: FlowNodeEntity): boolean;
259
+ declare function createEffectOptions<T>(event: DataEvent, effect: T): {
260
+ effect: T;
261
+ event: DataEvent;
262
+ };
263
+
264
+ /**
265
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
266
+ * SPDX-License-Identifier: MIT
267
+ */
268
+
269
+ /**
270
+ * Listen to Form's values and refresh the React component.
271
+ * By providing related node, you can use this hook outside the Form Component.
272
+ * @param node
273
+ */
274
+ declare function useWatchFormValues<T = any>(node: FlowNodeEntity): T | undefined;
275
+ /**
276
+ * Listen to Form's value in a certain path and refresh the React component.
277
+ * By providing related node, you can use this hook outside the Form Component.
278
+ * @param node
279
+ */
280
+ declare function useWatchFormValueIn<T = any>(node: FlowNodeEntity, name: string): T | undefined;
281
+ /**
282
+ * Listen to FormModel's initialization and refresh React component.
283
+ * By providing related node, you can use this hook outside the Form Component.
284
+ * @param node
285
+ */
286
+ declare function useInitializedFormModel(node: FlowNodeEntity): FormModelV2;
287
+ /**
288
+ * Get Form's state, Form State is a proxy, it will refresh the React component when the value you accessed changed
289
+ * By providing related node, you can use this hook outside the Form Component.
290
+ * @param node
291
+ */
292
+ declare function useWatchFormState(node: FlowNodeEntity): FormState | undefined;
293
+ /**
294
+ * Get Form's errors, Form errors is a proxy, it will refresh the React component when the value you accessed changed
295
+ * By providing related node, you can use this hook outside the Form Component.
296
+ * @param node
297
+ */
298
+ declare function useWatchFormErrors(node: FlowNodeEntity): Errors | undefined;
299
+ /**
300
+ * Get Form's warnings, Form warnings is a proxy, it will refresh the React component when the value you accessed changed
301
+ * By providing related node, you can use this hook outside the Form Component.
302
+ * @param node
303
+ */
304
+ declare function useWatchFormWarnings(node: FlowNodeEntity): Warnings | undefined;
305
+
306
+ /**
307
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
308
+ * SPDX-License-Identifier: MIT
309
+ */
310
+
311
+ interface NodeFormProps<TValues> {
312
+ /**
313
+ * The initialValues of the form.
314
+ */
315
+ initialValues: TValues;
316
+ /**
317
+ * Form values. Returns a deep copy of the data in the store.
318
+ */
319
+ values: TValues;
320
+ /**
321
+ * Form state
322
+ */
323
+ state: FormState;
324
+ /**
325
+ * Get value in certain path
326
+ * @param name path
327
+ */
328
+ getValueIn<TValue = FieldValue>(name: FieldName): TValue;
329
+ /**
330
+ * Set value in certain path.
331
+ * It will trigger the re-rendering of the Field Component if a Field is related to this path
332
+ * @param name path
333
+ */
334
+ setValueIn<TValue>(name: FieldName, value: TValue): void;
335
+ /**
336
+ * set form values
337
+ */
338
+ updateFormValues(values: any): void;
339
+ /**
340
+ * Render form
341
+ */
342
+ render: () => React__default.ReactNode;
343
+ /**
344
+ * Form value change event
345
+ */
346
+ onFormValuesChange: Event<OnFormValuesChangePayload>;
347
+ /**
348
+ * Trigger form validate
349
+ */
350
+ validate: () => Promise<boolean>;
351
+ /**
352
+ * Form validate event
353
+ */
354
+ onValidate: Event<FormState>;
355
+ /**
356
+ * Form field value change event
357
+ */
358
+ onFormValueChangeIn<TValue = FieldValue, TFormValue = FieldValue>(name: FieldName, callback: (payload: onFormValueChangeInPayload<TValue, TFormValue>) => void): Disposable;
359
+ }
360
+ /**
361
+ * Only support FormModelV2
362
+ * @param node
363
+ */
364
+ declare function getNodeForm<TValues = FieldValue>(node: FlowNodeEntity): NodeFormProps<TValues> | undefined;
365
+
366
+ export { type ArrayAppendEffect, type ArrayDeleteEffect, DataEvent, type Effect, type EffectFuncProps, type EffectOptions, type EffectReturn, type Flow, type FormMeta, FormModelV2, FormPlugin, type FormPluginConfig, type FormPluginCreator, type FormPluginCtx, type FormPluginSetupMetaCtx, type Node, type NodeFormProps, type Validate, createEffectOptions, defineFormPluginCreator, getNodeForm, isFormMetaV2, isFormModelV2, isFormV2, type onFormValueChangeInPayload, useInitializedFormModel, useWatchFormErrors, useWatchFormState, useWatchFormValueIn, useWatchFormValues, useWatchFormWarnings };