json-schema-preset.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import React from 'react';
  6. import { Field } from '@flowgram.ai/free-layout-editor';
  7. import {
  8. ConditionRow,
  9. ConditionRowValueType,
  10. createTypePresetPlugin,
  11. DynamicValueInput,
  12. IFlowConstantRefValue,
  13. type IJsonSchema,
  14. } from '@flowgram.ai/form-materials';
  15. import { ColorPicker } from '@douyinfe/semi-ui';
  16. import { IconColorPalette } from '@douyinfe/semi-icons';
  17. import { FreeFormMetaStoryBuilder, FormHeader } from '../../free-form-meta-story-builder';
  18. import './json-schema-preset.css';
  19. const TypeSelector = React.lazy(() =>
  20. import('@flowgram.ai/form-materials').then((module) => ({
  21. default: module.TypeSelector,
  22. }))
  23. );
  24. export const BasicStory = () => (
  25. <FreeFormMetaStoryBuilder
  26. filterEndNode
  27. transformInitialNode={{
  28. start_0: (node) => {
  29. node.data.outputs = {
  30. type: 'object',
  31. properties: {
  32. color_output: {
  33. type: 'color',
  34. },
  35. },
  36. };
  37. return node;
  38. },
  39. }}
  40. plugins={() => [
  41. createTypePresetPlugin({
  42. types: [
  43. {
  44. type: 'color',
  45. icon: <IconColorPalette />,
  46. label: 'Color',
  47. ConstantRenderer: ({ value, onChange }) => (
  48. <div className="json-schema-color-picker-container ">
  49. <ColorPicker
  50. alpha={true}
  51. usePopover={true}
  52. value={value ? ColorPicker.colorStringToValue(value) : undefined}
  53. onChange={(_value) => onChange?.(_value.hex)}
  54. />
  55. </div>
  56. ),
  57. conditionRule: {
  58. eq: { type: 'color' },
  59. },
  60. },
  61. ],
  62. }),
  63. ]}
  64. formMeta={{
  65. render: () => (
  66. <>
  67. <FormHeader />
  68. <b>Type Selector: </b>
  69. <Field<Partial<IJsonSchema> | undefined>
  70. name="type_selector"
  71. defaultValue={{ type: 'color' }}
  72. >
  73. {({ field }) => (
  74. <TypeSelector value={field.value} onChange={(value) => field.onChange(value)} />
  75. )}
  76. </Field>
  77. <br />
  78. <b>DynamicValueInput: </b>
  79. <Field<IFlowConstantRefValue | undefined>
  80. name="dynamic_value_input"
  81. defaultValue={{ type: 'constant', schema: { type: 'color' }, content: '#b5ed0c' }}
  82. >
  83. {({ field }) => (
  84. <DynamicValueInput value={field.value} onChange={(value) => field.onChange(value)} />
  85. )}
  86. </Field>
  87. <br />
  88. <b>Condition: </b>
  89. <Field<ConditionRowValueType | undefined>
  90. name="condition_row"
  91. defaultValue={{
  92. left: { type: 'ref', content: ['start_0', 'color_output'] },
  93. operator: 'eq',
  94. right: { type: 'ref', content: ['start_0', 'color_output'] },
  95. }}
  96. >
  97. {({ field }) => (
  98. <ConditionRow value={field.value} onChange={(value) => field.onChange(value)} />
  99. )}
  100. </Field>
  101. <br />
  102. </>
  103. ),
  104. }}
  105. />
  106. );