inputs-values.mdx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { SourceCode } from '@theme';
  2. import { BasicStory, WithSchemaStory } from 'components/form-materials/components/inputs-values';
  3. # InputsValues
  4. InputsValues 是一个键值对输入列表组件,用于收集和管理一组输入参数。每个键值对都支持常量和变量两种输入模式,通过 DynamicValueInput 组件实现灵活的输入方式。
  5. ## 案例演示
  6. ### 基本使用
  7. <BasicStory />
  8. ```tsx pure title="form-meta.tsx"
  9. import { InputsValues } from '@flowgram.ai/form-materials';
  10. const formMeta = {
  11. render: () => (
  12. <>
  13. <FormHeader />
  14. <Field<Record<string, any> | undefined> name="inputs_values">
  15. {({ field }) => (
  16. <InputsValues value={field.value} onChange={(value) => field.onChange(value)} />
  17. )}
  18. </Field>
  19. </>
  20. ),
  21. }
  22. ```
  23. ### 带 Schema 约束
  24. <WithSchemaStory />
  25. ```tsx pure title="form-meta.tsx"
  26. import { InputsValues } from '@flowgram.ai/form-materials';
  27. const formMeta = {
  28. render: () => (
  29. <>
  30. <FormHeader />
  31. <Field<Record<string, any> | undefined> name="inputs_values">
  32. {({ field }) => (
  33. <InputsValues
  34. value={field.value}
  35. onChange={(value) => field.onChange(value)}
  36. schema={{
  37. type: 'string',
  38. }}
  39. />
  40. )}
  41. </Field>
  42. </>
  43. ),
  44. }
  45. ```
  46. ### 自定义常量输入策略
  47. 通过 `constantProps` 可以自定义每个值的输入行为:
  48. ```typescript
  49. const customStrategies = [
  50. {
  51. type: 'string',
  52. render: (props) => <CustomStringInput {...props} />
  53. },
  54. {
  55. type: 'number',
  56. render: (props) => <CustomNumberInput {...props} />
  57. }
  58. ];
  59. <InputsValues
  60. constantProps={{
  61. strategies: customStrategies
  62. }}
  63. />
  64. ```
  65. ## API 参考
  66. ### InputsValues Props
  67. | 属性名 | 类型 | 默认值 | 描述 |
  68. |--------|------|--------|------|
  69. | `value` | `Record<string, IFlowValue \| undefined>` | - | 键值对数据 |
  70. | `onChange` | `(value?: Record<string, IFlowValue \| undefined>) => void` | - | 数据变化时的回调函数 |
  71. | `readonly` | `boolean` | `false` | 是否为只读模式 |
  72. | `hasError` | `boolean` | `false` | 是否显示错误状态 |
  73. | `style` | `React.CSSProperties` | - | 自定义样式 |
  74. | `schema` | `IJsonSchema` | - | 约束所有值类型的 JSON Schema |
  75. | `constantProps` | `ConstantInputProps` | - | 传递给 DynamicValueInput 的额外属性 |
  76. ### 数据结构
  77. ```typescript
  78. interface PropsType {
  79. value?: Record<string, IFlowValue | undefined>;
  80. onChange: (value?: Record<string, IFlowValue | undefined>) => void;
  81. // ... 其他属性
  82. }
  83. type IFlowValue =
  84. | IFlowConstantValue // 常量值
  85. | IFlowRefValue; // 变量引用
  86. interface IFlowConstantValue {
  87. type: 'constant';
  88. content: any; // 常量值
  89. schema: IJsonSchema; // 值的类型定义
  90. }
  91. interface IFlowRefValue {
  92. type: 'ref';
  93. content: string; // 变量路径,如 "user.name"
  94. }
  95. ```
  96. ## 源码导读
  97. <SourceCode
  98. href="https://github.com/bytedance/flowgram.ai/tree/main/packages/materials/form-materials/src/components/inputs-values"
  99. />
  100. 使用 CLI 命令可以复制源代码到本地:
  101. ```bash
  102. npx @flowgram.ai/cli@latest materials components/inputs-values
  103. ```
  104. ### 目录结构讲解
  105. ```
  106. inputs-values/
  107. ├── index.tsx # 主组件实现,包含 InputsValues 核心逻辑
  108. ├── types.ts # 类型定义
  109. ├── styles.tsx # 样式定义,使用 styled-components
  110. └── README.md # 组件说明文档
  111. ```
  112. ### 核心实现说明
  113. #### 键值对管理
  114. 使用 `useObjectList` Hook 管理键值对列表:
  115. ```typescript
  116. const { list, updateKey, updateValue, remove, add } = useObjectList<IFlowValue | undefined>({
  117. value,
  118. onChange,
  119. sortIndexKey: 'extra.index',
  120. });
  121. ```
  122. #### 动态值输入集成
  123. 每个值都使用 `InjectDynamicValueInput` 组件实现输入:
  124. ```typescript
  125. <InjectDynamicValueInput
  126. value={item.value as IFlowConstantRefValue}
  127. onChange={(v) => updateValue(item.id, v)}
  128. schema={schema}
  129. constantProps={constantProps}
  130. />
  131. ```
  132. #### 键名输入
  133. 使用 `BlurInput` 组件实现键名的输入和验证:
  134. ```typescript
  135. <BlurInput
  136. value={item.key}
  137. onChange={(v) => updateKey(item.id, v)}
  138. placeholder={I18n.t('Input Key')}
  139. />
  140. ```
  141. ### 使用到的 flowgram API
  142. #### @flowgram.ai/i18n
  143. - `I18n`: 国际化支持
  144. #### 内部组件
  145. - `InjectDynamicValueInput`: 动态值输入组件
  146. - `BlurInput`: 失焦输入组件
  147. - `useObjectList`: 对象列表管理 Hook
  148. ### 整体流程
  149. ```mermaid
  150. graph TD
  151. A[InputsValues 组件] --> B[渲染键值对列表]
  152. B --> C[每个键值对]
  153. C --> D[键名输入框]
  154. C --> E[值输入组件]
  155. C --> F[删除按钮]
  156. D --> G[用户输入键名]
  157. E --> H[用户输入值]
  158. F --> I[用户删除键值对]
  159. G --> J[updateKey 更新]
  160. H --> K[updateValue 更新]
  161. I --> L[remove 删除]
  162. J --> M[触发 onChange]
  163. K --> M
  164. L --> M
  165. O[添加按钮] --> P[add 新增]
  166. P --> M
  167. ```