preview.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import {
  6. DEFAULT_INITIAL_DATA,
  7. defaultInitialDataTs,
  8. fieldWrapperCss,
  9. fieldWrapperTs,
  10. } from '@flowgram.ai/demo-node-form';
  11. import { Editor } from '../editor.tsx';
  12. import { PreviewEditor } from '../../preview-editor.tsx';
  13. import { nodeRegistry } from './node-registry.tsx';
  14. const nodeRegistryFile = {
  15. code: `import {
  16. DataEvent,
  17. EffectFuncProps,
  18. Field,
  19. FieldRenderProps,
  20. FormMeta,
  21. ValidateTrigger,
  22. WorkflowNodeRegistry,
  23. } from '@flowgram.ai/free-layout-editor';
  24. import { FieldWrapper } from '@flowgram.ai/demo-node-form';
  25. import { Input } from '@douyinfe/semi-ui';
  26. import '../index.css';
  27. const render = () => (
  28. <div className="demo-node-content">
  29. <div className="demo-node-title">Effect Examples</div>
  30. <Field name="field1">
  31. {({ field }: FieldRenderProps<string>) => (
  32. <FieldWrapper
  33. title="Basic effect"
  34. note={'The following field will console.log field value on value change'}
  35. >
  36. <Input size={'small'} {...field} />
  37. </FieldWrapper>
  38. )}
  39. </Field>
  40. <Field name="field2">
  41. {({ field }: FieldRenderProps<string>) => (
  42. <FieldWrapper
  43. title="Control other fields"
  44. note={'The following field will change Field 3 value on value change'}
  45. >
  46. <Input size={'small'} {...field} />
  47. </FieldWrapper>
  48. )}
  49. </Field>
  50. <Field name="field3">
  51. {({ field }: FieldRenderProps<string>) => (
  52. <FieldWrapper title="Field 3">
  53. <Input size={'small'} {...field} />
  54. </FieldWrapper>
  55. )}
  56. </Field>
  57. </div>
  58. );
  59. interface FormData {
  60. field1: string;
  61. field2: string;
  62. field3: string;
  63. }
  64. const formMeta: FormMeta<FormData> = {
  65. render,
  66. validateTrigger: ValidateTrigger.onChange,
  67. effect: {
  68. field1: [
  69. {
  70. event: DataEvent.onValueChange,
  71. effect: ({ value }: EffectFuncProps<string, FormData>) => {
  72. console.log('field1 value:', value);
  73. },
  74. },
  75. ],
  76. field2: [
  77. {
  78. event: DataEvent.onValueChange,
  79. effect: ({ value, form }: EffectFuncProps<string, FormData>) => {
  80. form.setValueIn('field3', 'field2 value is ' + value);
  81. },
  82. },
  83. ],
  84. },
  85. };
  86. export const nodeRegistry: WorkflowNodeRegistry = {
  87. type: 'custom',
  88. meta: {},
  89. defaultPorts: [{ type: 'output' }, { type: 'input' }],
  90. formMeta,
  91. };
  92. `,
  93. active: true,
  94. };
  95. export const NodeFormEffectPreview = () => {
  96. const files = {
  97. 'node-registry.tsx': nodeRegistryFile,
  98. 'initial-data.ts': { code: defaultInitialDataTs, active: true },
  99. 'field-wrapper.tsx': { code: fieldWrapperTs, active: true },
  100. 'field-wrapper.css': { code: fieldWrapperCss, active: true },
  101. };
  102. return (
  103. <PreviewEditor files={files} previewStyle={{ height: 500 }} editorStyle={{ height: 500 }}>
  104. <Editor registry={nodeRegistry} initialData={DEFAULT_INITIAL_DATA} />
  105. </PreviewEditor>
  106. );
  107. };