preview.tsx 2.8 KB

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