@flowgram.ai/form-materials 0.1.0-alpha.11 → 0.1.0-alpha.13

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.
Files changed (133) hide show
  1. package/bin/index.ts +14 -22
  2. package/bin/materials.ts +41 -90
  3. package/bin/project.ts +4 -0
  4. package/bin/utils/import.ts +127 -0
  5. package/bin/utils/traverse-file.ts +60 -0
  6. package/dist/esm/index.js +2073 -1601
  7. package/dist/esm/index.js.map +1 -1
  8. package/dist/index.d.mts +392 -131
  9. package/dist/index.d.ts +392 -131
  10. package/dist/index.js +2175 -1710
  11. package/dist/index.js.map +1 -1
  12. package/package.json +5 -4
  13. package/src/components/assign-row/components/blur-input.tsx +27 -0
  14. package/src/components/assign-row/index.tsx +84 -0
  15. package/src/components/assign-row/types.ts +25 -0
  16. package/src/components/assign-rows/index.tsx +59 -0
  17. package/src/components/batch-outputs/index.tsx +7 -14
  18. package/src/components/batch-outputs/types.ts +1 -1
  19. package/src/components/batch-variable-selector/index.tsx +2 -2
  20. package/src/components/code-editor/index.tsx +7 -0
  21. package/src/components/code-editor/language-features.ts +22 -1
  22. package/src/components/code-editor/theme/light.ts +1 -1
  23. package/src/components/code-editor-mini/index.tsx +31 -0
  24. package/src/components/condition-row/constants.ts +8 -10
  25. package/src/components/condition-row/hooks/useOp.tsx +15 -9
  26. package/src/components/condition-row/hooks/useRule.ts +9 -5
  27. package/src/components/condition-row/index.tsx +28 -10
  28. package/src/components/condition-row/types.ts +5 -5
  29. package/src/components/constant-input/index.tsx +20 -61
  30. package/src/components/constant-input/types.ts +6 -9
  31. package/src/components/display-flow-value/index.tsx +59 -0
  32. package/src/components/display-inputs-values/index.tsx +28 -0
  33. package/src/components/display-inputs-values/styles.ts +12 -0
  34. package/src/components/display-outputs/index.tsx +65 -0
  35. package/src/components/display-outputs/styles.ts +12 -0
  36. package/src/components/display-schema-tag/index.tsx +45 -0
  37. package/src/components/display-schema-tag/styles.ts +28 -0
  38. package/src/components/display-schema-tree/index.tsx +75 -0
  39. package/src/components/display-schema-tree/styles.tsx +90 -0
  40. package/src/components/dynamic-value-input/hooks.ts +53 -0
  41. package/src/components/dynamic-value-input/index.tsx +74 -19
  42. package/src/components/dynamic-value-input/styles.tsx +28 -2
  43. package/src/components/index.ts +9 -0
  44. package/src/components/inputs-values/components/blur-input.tsx +27 -0
  45. package/src/components/inputs-values/index.tsx +75 -0
  46. package/src/components/inputs-values/styles.tsx +19 -0
  47. package/src/components/inputs-values/types.ts +22 -0
  48. package/src/components/json-editor-with-variables/extensions/variable-tree.tsx +1 -1
  49. package/src/components/json-editor-with-variables/index.tsx +2 -1
  50. package/src/components/json-schema-editor/default-value.tsx +12 -106
  51. package/src/components/json-schema-editor/hooks.tsx +53 -94
  52. package/src/components/json-schema-editor/index.tsx +32 -13
  53. package/src/components/json-schema-editor/styles.tsx +0 -29
  54. package/src/components/json-schema-editor/types.ts +1 -1
  55. package/src/components/prompt-editor/types.tsx +1 -1
  56. package/src/components/prompt-editor-with-inputs/extensions/inputs-tree.tsx +2 -1
  57. package/src/components/prompt-editor-with-inputs/index.tsx +3 -2
  58. package/src/components/prompt-editor-with-inputs/inputs-picker.tsx +2 -2
  59. package/src/components/prompt-editor-with-variables/extensions/variable-tag.tsx +6 -3
  60. package/src/components/prompt-editor-with-variables/extensions/variable-tree.tsx +1 -1
  61. package/src/components/prompt-editor-with-variables/index.tsx +2 -1
  62. package/src/components/type-selector/index.tsx +58 -13
  63. package/src/components/variable-selector/index.tsx +42 -17
  64. package/src/components/variable-selector/styles.tsx +18 -8
  65. package/src/components/variable-selector/use-variable-tree.tsx +19 -22
  66. package/src/effects/auto-rename-ref/index.ts +1 -1
  67. package/src/effects/index.ts +3 -1
  68. package/src/effects/listen-ref-schema-change/index.ts +56 -0
  69. package/src/effects/listen-ref-value-change/index.ts +53 -0
  70. package/src/effects/provide-batch-input/index.ts +1 -1
  71. package/src/effects/provide-json-schema-outputs/index.ts +1 -3
  72. package/src/effects/sync-variable-title/index.ts +1 -0
  73. package/src/effects/validate-when-variable-sync/index.ts +35 -0
  74. package/src/form-plugins/batch-outputs-plugin/index.ts +1 -1
  75. package/src/form-plugins/index.ts +3 -1
  76. package/src/form-plugins/infer-assign-plugin/index.ts +90 -0
  77. package/src/form-plugins/infer-inputs-plugin/index.ts +108 -0
  78. package/src/hooks/index.tsx +6 -0
  79. package/src/hooks/use-object-list/index.tsx +136 -0
  80. package/src/index.ts +3 -1
  81. package/src/{utils/format-legacy-refs → plugins/disable-declaration-plugin}/config.json +1 -1
  82. package/src/plugins/disable-declaration-plugin/create-disable-declaration-plugin.ts +31 -0
  83. package/src/plugins/disable-declaration-plugin/index.tsx +6 -0
  84. package/src/plugins/index.ts +7 -0
  85. package/src/plugins/json-schema-preset/config.json +9 -0
  86. package/src/plugins/json-schema-preset/create-type-preset-plugin.tsx +28 -0
  87. package/src/plugins/json-schema-preset/index.tsx +41 -0
  88. package/src/plugins/json-schema-preset/manager.ts +18 -0
  89. package/src/plugins/json-schema-preset/type-definition/array.tsx +26 -0
  90. package/src/plugins/json-schema-preset/type-definition/boolean.tsx +33 -0
  91. package/src/plugins/json-schema-preset/type-definition/index.tsx +24 -0
  92. package/src/plugins/json-schema-preset/type-definition/integer.tsx +25 -0
  93. package/src/plugins/json-schema-preset/type-definition/number.tsx +25 -0
  94. package/src/plugins/json-schema-preset/type-definition/object.tsx +26 -0
  95. package/src/plugins/json-schema-preset/type-definition/string.tsx +24 -0
  96. package/src/{utils → shared}/index.ts +1 -1
  97. package/src/shared/inject-material/README.md +170 -0
  98. package/src/shared/inject-material/README.zh.md +174 -0
  99. package/src/shared/inject-material/index.tsx +87 -0
  100. package/src/typings/flow-value/index.ts +11 -0
  101. package/src/typings/index.ts +0 -1
  102. package/src/validate/index.tsx +6 -0
  103. package/src/validate/validate-flow-value/index.tsx +73 -0
  104. package/src/components/batch-outputs/config.json +0 -12
  105. package/src/components/batch-outputs/use-list.ts +0 -86
  106. package/src/components/batch-variable-selector/config.json +0 -5
  107. package/src/components/code-editor/config.json +0 -10
  108. package/src/components/condition-row/config.json +0 -5
  109. package/src/components/constant-input/config.json +0 -6
  110. package/src/components/dynamic-value-input/config.json +0 -5
  111. package/src/components/json-editor-with-variables/config.json +0 -13
  112. package/src/components/json-schema-editor/config.json +0 -13
  113. package/src/components/json-schema-editor/utils.ts +0 -29
  114. package/src/components/prompt-editor/config.json +0 -9
  115. package/src/components/prompt-editor-with-inputs/config.json +0 -13
  116. package/src/components/prompt-editor-with-variables/config.json +0 -13
  117. package/src/components/type-selector/config.json +0 -5
  118. package/src/components/type-selector/constants.tsx +0 -364
  119. package/src/components/variable-selector/config.json +0 -5
  120. package/src/effects/auto-rename-ref/config.json +0 -5
  121. package/src/effects/provide-batch-input/config.json +0 -5
  122. package/src/effects/provide-batch-outputs/config.json +0 -5
  123. package/src/effects/provide-batch-outputs/index.ts +0 -38
  124. package/src/effects/provide-json-schema-outputs/config.json +0 -8
  125. package/src/effects/sync-variable-title/config.json +0 -5
  126. package/src/form-plugins/batch-outputs-plugin/config.json +0 -7
  127. package/src/typings/flow-value/config.json +0 -5
  128. package/src/typings/json-schema/config.json +0 -5
  129. package/src/typings/json-schema/index.ts +0 -36
  130. package/src/utils/json-schema/config.json +0 -5
  131. package/src/utils/json-schema/index.ts +0 -180
  132. /package/src/{utils → shared}/format-legacy-refs/index.ts +0 -0
  133. /package/src/{utils → shared}/format-legacy-refs/readme.md +0 -0
@@ -3,17 +3,20 @@
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
 
6
- import React, { useMemo } from 'react';
6
+ import React from 'react';
7
7
 
8
+ import { JsonSchemaUtils, IJsonSchema } from '@flowgram.ai/json-schema';
8
9
  import { IconButton } from '@douyinfe/semi-ui';
9
10
  import { IconSetting } from '@douyinfe/semi-icons';
10
11
 
11
- import { Strategy } from '../constant-input/types';
12
- import { ConstantInput } from '../constant-input';
13
- import { IFlowConstantRefValue } from '../../typings/flow-value';
14
- import { UIContainer, UIMain, UITrigger } from './styles';
15
- import { VariableSelector } from '../variable-selector';
16
- import { IJsonSchema } from '../../typings';
12
+ import { IFlowConstantRefValue } from '@/typings/flow-value';
13
+ import { createInjectMaterial } from '@/shared';
14
+ import { InjectVariableSelector } from '@/components/variable-selector';
15
+ import { TypeSelector } from '@/components/type-selector';
16
+ import { ConstantInput, ConstantInputStrategy } from '@/components/constant-input';
17
+
18
+ import { UIContainer, UIMain, UITrigger, UIType } from './styles';
19
+ import { useIncludeSchema, useRefVariable, useSelectSchema } from './hooks';
17
20
 
18
21
  interface PropsType {
19
22
  value?: IFlowConstantRefValue;
@@ -23,7 +26,7 @@ interface PropsType {
23
26
  style?: React.CSSProperties;
24
27
  schema?: IJsonSchema;
25
28
  constantProps?: {
26
- strategies?: Strategy[];
29
+ strategies?: ConstantInputStrategy[];
27
30
  schema?: IJsonSchema; // set schema of constant input only
28
31
  [key: string]: any;
29
32
  };
@@ -34,22 +37,59 @@ export function DynamicValueInput({
34
37
  onChange,
35
38
  readonly,
36
39
  style,
37
- schema,
40
+ schema: schemaFromProps,
38
41
  constantProps,
39
42
  }: PropsType) {
40
- // When is number type, include integer as well
41
- const includeSchema = useMemo(() => {
42
- if (schema?.type === 'number') {
43
- return [schema, { type: 'integer' }];
43
+ const refVariable = useRefVariable(value);
44
+ const [selectSchema, setSelectSchema] = useSelectSchema(schemaFromProps, constantProps, value);
45
+ const includeSchema = useIncludeSchema(schemaFromProps);
46
+
47
+ const renderTypeSelector = () => {
48
+ if (schemaFromProps) {
49
+ return <TypeSelector value={schemaFromProps} readonly={true} />;
44
50
  }
45
- return schema;
46
- }, [schema]);
51
+
52
+ if (value?.type === 'ref') {
53
+ const schema = refVariable?.type ? JsonSchemaUtils.astToSchema(refVariable?.type) : undefined;
54
+
55
+ return <TypeSelector value={schema} readonly={true} />;
56
+ }
57
+
58
+ return (
59
+ <TypeSelector
60
+ value={selectSchema}
61
+ onChange={(_v) => {
62
+ setSelectSchema(_v || { type: 'string' });
63
+ let content;
64
+
65
+ if (_v?.type === 'object') {
66
+ content = '{}';
67
+ }
68
+
69
+ if (_v?.type === 'array') {
70
+ content = '[]';
71
+ }
72
+
73
+ if (_v?.type === 'boolean') {
74
+ content = false;
75
+ }
76
+
77
+ onChange({
78
+ type: 'constant',
79
+ content,
80
+ schema: _v || { type: 'string' },
81
+ });
82
+ }}
83
+ readonly={readonly}
84
+ />
85
+ );
86
+ };
47
87
 
48
88
  const renderMain = () => {
49
89
  if (value?.type === 'ref') {
50
90
  // Display Variable Or Delete
51
91
  return (
52
- <VariableSelector
92
+ <InjectVariableSelector
53
93
  style={{ width: '100%' }}
54
94
  value={value?.content}
55
95
  onChange={(_v) => onChange(_v ? { type: 'ref', content: _v } : undefined)}
@@ -59,19 +99,30 @@ export function DynamicValueInput({
59
99
  );
60
100
  }
61
101
 
102
+ const constantSchema = schemaFromProps || selectSchema || { type: 'string' };
103
+
62
104
  return (
63
105
  <ConstantInput
64
106
  value={value?.content}
65
- onChange={(_v) => onChange({ type: 'constant', content: _v })}
66
- schema={schema || { type: 'string' }}
107
+ onChange={(_v) => onChange({ type: 'constant', content: _v, schema: constantSchema })}
108
+ schema={constantSchema || { type: 'string' }}
67
109
  readonly={readonly}
110
+ strategies={[...(constantProps?.strategies || [])]}
111
+ fallbackRenderer={() => (
112
+ <InjectVariableSelector
113
+ style={{ width: '100%' }}
114
+ onChange={(_v) => onChange(_v ? { type: 'ref', content: _v } : undefined)}
115
+ includeSchema={includeSchema}
116
+ readonly={readonly}
117
+ />
118
+ )}
68
119
  {...constantProps}
69
120
  />
70
121
  );
71
122
  };
72
123
 
73
124
  const renderTrigger = () => (
74
- <VariableSelector
125
+ <InjectVariableSelector
75
126
  style={{ width: '100%' }}
76
127
  value={value?.type === 'ref' ? value?.content : undefined}
77
128
  onChange={(_v) => onChange({ type: 'ref', content: _v })}
@@ -85,8 +136,12 @@ export function DynamicValueInput({
85
136
 
86
137
  return (
87
138
  <UIContainer style={style}>
139
+ <UIType>{renderTypeSelector()}</UIType>
88
140
  <UIMain>{renderMain()}</UIMain>
89
141
  <UITrigger>{renderTrigger()}</UITrigger>
90
142
  </UIContainer>
91
143
  );
92
144
  }
145
+
146
+ DynamicValueInput.renderKey = 'dynamic-value-input-render-key';
147
+ export const InjectDynamicValueInput = createInjectMaterial(DynamicValueInput);
@@ -8,7 +8,12 @@ import styled from 'styled-components';
8
8
  export const UIContainer = styled.div`
9
9
  display: flex;
10
10
  align-items: center;
11
- gap: 5px;
11
+ border-radius: 4px;
12
+ border: 1px solid var(--semi-color-border);
13
+
14
+ overflow: hidden;
15
+
16
+ background-color: var(--semi-color-fill-0);
12
17
  `;
13
18
 
14
19
  export const UIMain = styled.div`
@@ -20,7 +25,28 @@ export const UIMain = styled.div`
20
25
  & .semi-input-number,
21
26
  & .semi-select {
22
27
  width: 100%;
28
+ border: none;
29
+ border-radius: 0;
30
+ }
31
+
32
+ & .semi-input-wrapper {
33
+ border: none;
34
+ border-radius: 0;
35
+ }
36
+ `;
37
+
38
+ export const UIType = styled.div`
39
+ border-right: 1px solid #e5e5e5;
40
+
41
+ & .semi-button {
42
+ border-radius: 0;
23
43
  }
24
44
  `;
25
45
 
26
- export const UITrigger = styled.div``;
46
+ export const UITrigger = styled.div`
47
+ border-left: 1px solid #e5e5e5;
48
+
49
+ & .semi-button {
50
+ border-radius: 0;
51
+ }
52
+ `;
@@ -15,4 +15,13 @@ export * from './prompt-editor';
15
15
  export * from './prompt-editor-with-variables';
16
16
  export * from './prompt-editor-with-inputs';
17
17
  export * from './code-editor';
18
+ export * from './code-editor-mini';
18
19
  export * from './json-editor-with-variables';
20
+ export * from './inputs-values';
21
+ export * from './display-schema-tree';
22
+ export * from './display-outputs';
23
+ export * from './display-schema-tag';
24
+ export * from './display-flow-value';
25
+ export * from './display-inputs-values';
26
+ export * from './assign-rows';
27
+ export * from './assign-row';
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React, { useEffect, useState } from 'react';
7
+
8
+ import Input, { InputProps } from '@douyinfe/semi-ui/lib/es/input';
9
+
10
+ export function BlurInput(props: InputProps) {
11
+ const [value, setValue] = useState('');
12
+
13
+ useEffect(() => {
14
+ setValue(props.value as string);
15
+ }, [props.value]);
16
+
17
+ return (
18
+ <Input
19
+ {...props}
20
+ value={value}
21
+ onChange={(value) => {
22
+ setValue(value);
23
+ }}
24
+ onBlur={(e) => props.onChange?.(value, e)}
25
+ />
26
+ );
27
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import React from 'react';
7
+
8
+ import { I18n } from '@flowgram.ai/editor';
9
+ import { Button, IconButton } from '@douyinfe/semi-ui';
10
+ import { IconDelete, IconPlus } from '@douyinfe/semi-icons';
11
+
12
+ import { IFlowConstantRefValue, IFlowValue } from '@/typings';
13
+ import { useObjectList } from '@/hooks';
14
+ import { InjectDynamicValueInput } from '@/components/dynamic-value-input';
15
+
16
+ import { PropsType } from './types';
17
+ import { UIRow, UIRows } from './styles';
18
+ import { BlurInput } from './components/blur-input';
19
+
20
+ export function InputsValues({
21
+ value,
22
+ onChange,
23
+ style,
24
+ readonly,
25
+ constantProps,
26
+ schema,
27
+ hasError,
28
+ }: PropsType) {
29
+ const { list, updateKey, updateValue, remove, add } = useObjectList<IFlowValue | undefined>({
30
+ value,
31
+ onChange,
32
+ sortIndexKey: 'extra.index',
33
+ });
34
+
35
+ return (
36
+ <div>
37
+ <UIRows style={style}>
38
+ {list.map((item) => (
39
+ <UIRow key={item.id}>
40
+ <BlurInput
41
+ style={{ width: 100, minWidth: 100, maxWidth: 100 }}
42
+ disabled={readonly}
43
+ size="small"
44
+ value={item.key}
45
+ onChange={(v) => updateKey(item.id, v)}
46
+ placeholder={I18n.t('Input Key')}
47
+ />
48
+ <InjectDynamicValueInput
49
+ style={{ flexGrow: 1 }}
50
+ readonly={readonly}
51
+ value={item.value as IFlowConstantRefValue}
52
+ onChange={(v) => updateValue(item.id, v)}
53
+ schema={schema}
54
+ hasError={hasError}
55
+ constantProps={{
56
+ ...constantProps,
57
+ strategies: [...(constantProps?.strategies || [])],
58
+ }}
59
+ />
60
+ <IconButton
61
+ disabled={readonly}
62
+ theme="borderless"
63
+ icon={<IconDelete size="small" />}
64
+ size="small"
65
+ onClick={() => remove(item.id)}
66
+ />
67
+ </UIRow>
68
+ ))}
69
+ </UIRows>
70
+ <Button disabled={readonly} icon={<IconPlus />} size="small" onClick={add}>
71
+ Add
72
+ </Button>
73
+ </div>
74
+ );
75
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import styled from 'styled-components';
7
+
8
+ export const UIRows = styled.div`
9
+ display: flex;
10
+ flex-direction: column;
11
+ gap: 10px;
12
+ margin-bottom: 10px;
13
+ `;
14
+
15
+ export const UIRow = styled.div`
16
+ display: flex;
17
+ align-items: center;
18
+ gap: 5px;
19
+ `;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { IJsonSchema } from '@flowgram.ai/json-schema';
7
+
8
+ import { IFlowValue } from '@/typings';
9
+ import { ConstantInputStrategy } from '@/components/constant-input';
10
+
11
+ export interface PropsType {
12
+ value?: Record<string, IFlowValue | undefined>;
13
+ onChange: (value?: Record<string, IFlowValue | undefined>) => void;
14
+ readonly?: boolean;
15
+ hasError?: boolean;
16
+ schema?: IJsonSchema;
17
+ style?: React.CSSProperties;
18
+ constantProps?: {
19
+ strategies?: ConstantInputStrategy[];
20
+ [key: string]: any;
21
+ };
22
+ }
@@ -15,7 +15,7 @@ import {
15
15
  } from '@coze-editor/editor/react';
16
16
  import { EditorAPI } from '@coze-editor/editor/preset-prompt';
17
17
 
18
- import { useVariableTree } from '../../variable-selector';
18
+ import { useVariableTree } from '@/components/variable-selector';
19
19
 
20
20
  export function VariableTree() {
21
21
  const [posKey, setPosKey] = useState('');
@@ -8,9 +8,10 @@ import React from 'react';
8
8
  import { transformerCreator } from '@coze-editor/editor/preset-code';
9
9
  import { Text } from '@coze-editor/editor/language-json';
10
10
 
11
+ import { CodeEditor, type CodeEditorPropsType } from '@/components/code-editor';
12
+
11
13
  import { VariableTree } from './extensions/variable-tree';
12
14
  import { VariableTagInject } from './extensions/variable-tag';
13
- import { CodeEditor, type CodeEditorPropsType } from '../code-editor';
14
15
 
15
16
  type Match = { match: string; range: [number, number] };
16
17
  function findAllMatches(inputString: string, regex: RegExp): Match[] {
@@ -3,26 +3,19 @@
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
 
6
- import React, { useRef, useState, useCallback } from 'react';
6
+ import React from 'react';
7
7
 
8
- import { IconButton, JsonViewer, Tooltip } from '@douyinfe/semi-ui';
9
- import { IconBrackets } from '@douyinfe/semi-icons';
8
+ import { IJsonSchema } from '@flowgram.ai/json-schema';
9
+ import { I18n } from '@flowgram.ai/editor';
10
10
 
11
- import { getValueType } from './utils';
12
- import {
13
- ConstantInputWrapper,
14
- JSONHeader,
15
- JSONHeaderLeft,
16
- JSONHeaderRight,
17
- JSONViewerWrapper,
18
- } from './styles';
19
- import { ConstantInput } from '../constant-input';
20
- import { IJsonSchema } from '../../typings';
11
+ import { ConstantInput } from '@/components/constant-input';
12
+
13
+ import { ConstantInputWrapper } from './styles';
21
14
 
22
15
  /**
23
- * 根据不同的数据类型渲染对应的默认值输入组件。
24
- * @param props - 组件属性,包括 value, type, placeholder, onChange
25
- * @returns 返回对应类型的输入组件或 null
16
+ * Renders the corresponding default value input component based on different data types.
17
+ * @param props - Component properties, including value, type, placeholder, onChange.
18
+ * @returns Returns the input component of the corresponding type or null.
26
19
  */
27
20
  export function DefaultValue(props: {
28
21
  value: any;
@@ -33,102 +26,15 @@ export function DefaultValue(props: {
33
26
  jsonFormatText?: string;
34
27
  onChange: (value: any) => void;
35
28
  }) {
36
- const { value, schema, type, onChange, placeholder, jsonFormatText } = props;
37
-
38
- const wrapperRef = useRef<HTMLDivElement>(null);
39
- const JsonViewerRef = useRef<JsonViewer>(null);
40
-
41
- // 为 JsonViewer 添加状态管理
42
- const [internalJsonValue, setInternalJsonValue] = useState<string>(
43
- getValueType(value) === 'string' ? value : ''
44
- );
45
-
46
- // 使用 useCallback 创建稳定的回调函数
47
- const handleJsonChange = useCallback((val: string) => {
48
- // 只在值真正改变时才更新状态
49
- if (val !== internalJsonValue) {
50
- setInternalJsonValue(val);
51
- }
52
- }, []);
53
-
54
- // 处理编辑完成事件
55
- const handleEditComplete = useCallback(() => {
56
- // 只有当存在key,编辑完成时才触发父组件的 onChange
57
- onChange(internalJsonValue);
58
- // 确保在更新后移除焦点
59
- requestAnimationFrame(() => {
60
- // JsonViewerRef.current?.format();
61
- wrapperRef.current?.blur();
62
- });
63
- setJsonReadOnly(true);
64
- }, [internalJsonValue, onChange]);
65
-
66
- const [jsonReadOnly, setJsonReadOnly] = useState<boolean>(true);
67
-
68
- const handleFormatJson = useCallback(() => {
69
- try {
70
- const parsed = JSON.parse(internalJsonValue);
71
- const formatted = JSON.stringify(parsed, null, 4);
72
- setInternalJsonValue(formatted);
73
- onChange(formatted);
74
- } catch (error) {
75
- console.error('Invalid JSON:', error);
76
- }
77
- }, [internalJsonValue, onChange]);
78
-
79
- return type === 'object' ? (
80
- <>
81
- <JSONHeader>
82
- <JSONHeaderLeft>json</JSONHeaderLeft>
83
- <JSONHeaderRight>
84
- <Tooltip content={jsonFormatText ?? 'Format'}>
85
- <IconButton
86
- icon={<IconBrackets style={{ color: 'var(--semi-color-primary)' }} />}
87
- size="small"
88
- type="tertiary"
89
- theme="borderless"
90
- onClick={handleFormatJson}
91
- />
92
- </Tooltip>
93
- </JSONHeaderRight>
94
- </JSONHeader>
29
+ const { value, schema, onChange, placeholder } = props;
95
30
 
96
- <JSONViewerWrapper
97
- ref={wrapperRef}
98
- tabIndex={-1}
99
- onBlur={(e) => {
100
- if (wrapperRef.current && !wrapperRef.current?.contains(e.relatedTarget as Node)) {
101
- handleEditComplete();
102
- }
103
- }}
104
- onClick={(e: React.MouseEvent) => {
105
- setJsonReadOnly(false);
106
- }}
107
- >
108
- <JsonViewer
109
- ref={JsonViewerRef}
110
- value={getValueType(value) === 'string' ? value : ''}
111
- height={120}
112
- width="100%"
113
- showSearch={false}
114
- options={{
115
- readOnly: jsonReadOnly,
116
- formatOptions: { tabSize: 4, insertSpaces: true, eol: '\n' },
117
- }}
118
- style={{
119
- padding: 0,
120
- }}
121
- onChange={handleJsonChange}
122
- />
123
- </JSONViewerWrapper>
124
- </>
125
- ) : (
31
+ return (
126
32
  <ConstantInputWrapper>
127
33
  <ConstantInput
128
34
  value={value}
129
35
  onChange={(_v) => onChange(_v)}
130
36
  schema={schema || { type: 'string' }}
131
- placeholder={placeholder ?? 'Default value if parameter is not provided'}
37
+ placeholder={placeholder ?? I18n.t('Default value if parameter is not provided')}
132
38
  />
133
39
  </ConstantInputWrapper>
134
40
  );