preview.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Field,
  17. FieldRenderProps,
  18. FormMeta,
  19. WorkflowNodeRegistry,
  20. FormRenderProps,
  21. } from '@flowgram.ai/free-layout-editor';
  22. import { FieldWrapper } from '@flowgram.ai/demo-node-form';
  23. import { Input } from '@douyinfe/semi-ui';
  24. import '../index.css';
  25. interface FormData {
  26. country: string;
  27. city: string;
  28. }
  29. const render = ({ form }: FormRenderProps<FormData>) => (
  30. <div className="demo-node-content">
  31. <div className="demo-node-title">Visibility Examples</div>
  32. <Field name="country">
  33. {({ field }: FieldRenderProps<string>) => (
  34. <FieldWrapper title="Country">
  35. <Input size={'small'} {...field} />
  36. </FieldWrapper>
  37. )}
  38. </Field>
  39. <Field name="city" deps={['country']}>
  40. {({ field }: FieldRenderProps<string>) =>
  41. form.getValueIn('country') ? (
  42. <FieldWrapper title="City">
  43. <Input size={'small'} {...field} />
  44. </FieldWrapper>
  45. ) : (
  46. <></>
  47. )
  48. }
  49. </Field>
  50. </div>
  51. );
  52. const formMeta: FormMeta<FormData> = {
  53. render,
  54. };
  55. export const nodeRegistry: WorkflowNodeRegistry = {
  56. type: 'custom',
  57. meta: {},
  58. defaultPorts: [{ type: 'output' }, { type: 'input' }],
  59. formMeta,
  60. };
  61. `,
  62. active: true,
  63. };
  64. export const NodeFormDynamicPreview = () => {
  65. const files = {
  66. 'node-registry.tsx': nodeRegistryFile,
  67. 'initial-data.ts': { code: defaultInitialDataTs, active: true },
  68. 'field-wrapper.tsx': { code: fieldWrapperTs, active: true },
  69. 'field-wrapper.css': { code: fieldWrapperCss, active: true },
  70. };
  71. return (
  72. <PreviewEditor files={files} previewStyle={{ height: 500 }} editorStyle={{ height: 500 }}>
  73. <Editor registries={[nodeRegistry]} initialData={DEFAULT_INITIAL_DATA} />
  74. </PreviewEditor>
  75. );
  76. };