@dxos/react-ui-form 0.8.4-main.b97322e → 0.8.4-main.bcb3aa67d6

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 (205) hide show
  1. package/README.md +0 -1
  2. package/dist/lib/browser/index.mjs +2315 -2116
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +2315 -2116
  6. package/dist/lib/node-esm/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +8 -7
  9. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  10. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +82 -5
  11. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
  12. package/dist/types/src/components/Form/Form.d.ts +84 -25
  13. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  14. package/dist/types/src/components/Form/Form.stories.d.ts +127 -55
  15. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/FormField.d.ts +43 -0
  17. package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
  18. package/dist/types/src/components/Form/FormFieldComponent.d.ts +77 -0
  19. package/dist/types/src/components/Form/FormFieldComponent.d.ts.map +1 -0
  20. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts +9 -0
  21. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts.map +1 -0
  22. package/dist/types/src/components/Form/FormFieldSet.d.ts +18 -0
  23. package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
  24. package/dist/types/src/components/Form/fields/ArrayField.d.ts +18 -0
  25. package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -0
  26. package/dist/types/src/components/Form/fields/BooleanField.d.ts +4 -0
  27. package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -0
  28. package/dist/types/src/components/Form/fields/GeoPointField.d.ts +5 -0
  29. package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -0
  30. package/dist/types/src/components/Form/fields/MarkdownField.d.ts +4 -0
  31. package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
  32. package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
  33. package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
  34. package/dist/types/src/components/Form/fields/RefField.d.ts +16 -0
  35. package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
  36. package/dist/types/src/components/Form/fields/SelectField.d.ts +10 -0
  37. package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
  38. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
  39. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
  40. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +82 -0
  41. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
  42. package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
  43. package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
  44. package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
  45. package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
  46. package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
  47. package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
  48. package/dist/types/src/components/Form/fields/index.d.ts +12 -0
  49. package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
  50. package/dist/types/src/components/Form/index.d.ts +2 -4
  51. package/dist/types/src/components/Form/index.d.ts.map +1 -1
  52. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +54 -0
  53. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
  54. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +85 -0
  55. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -0
  56. package/dist/types/src/components/ObjectPicker/index.d.ts +2 -0
  57. package/dist/types/src/components/ObjectPicker/index.d.ts.map +1 -0
  58. package/dist/types/src/components/Settings/Settings.d.ts +39 -0
  59. package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
  60. package/dist/types/src/components/Settings/Settings.stories.d.ts +81 -0
  61. package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
  62. package/dist/types/src/components/Settings/index.d.ts +2 -0
  63. package/dist/types/src/components/Settings/index.d.ts.map +1 -0
  64. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +22 -10
  65. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  66. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +84 -8
  67. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  68. package/dist/types/src/components/index.d.ts +2 -2
  69. package/dist/types/src/components/index.d.ts.map +1 -1
  70. package/dist/types/src/components/testing.d.ts +4 -3
  71. package/dist/types/src/components/testing.d.ts.map +1 -1
  72. package/dist/types/src/hooks/index.d.ts +2 -3
  73. package/dist/types/src/hooks/index.d.ts.map +1 -1
  74. package/dist/types/src/hooks/useFormHandler.d.ts +82 -0
  75. package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -0
  76. package/dist/types/src/hooks/useKeyHandler.d.ts +6 -0
  77. package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
  78. package/dist/types/src/translations.d.ts +63 -54
  79. package/dist/types/src/translations.d.ts.map +1 -1
  80. package/dist/types/src/util/field.d.ts +3 -3
  81. package/dist/types/src/util/field.d.ts.map +1 -1
  82. package/dist/types/src/util/formatting.d.ts +2 -2
  83. package/dist/types/src/util/formatting.d.ts.map +1 -1
  84. package/dist/types/src/util/index.d.ts +1 -1
  85. package/dist/types/src/util/index.d.ts.map +1 -1
  86. package/dist/types/src/util/properties.d.ts +7 -0
  87. package/dist/types/src/util/properties.d.ts.map +1 -0
  88. package/dist/types/src/util/refs.d.ts +3 -3
  89. package/dist/types/src/util/refs.d.ts.map +1 -1
  90. package/dist/types/tsconfig.tsbuildinfo +1 -1
  91. package/package.json +52 -43
  92. package/src/components/FieldEditor/FieldEditor.stories.tsx +51 -32
  93. package/src/components/FieldEditor/FieldEditor.test.tsx +3 -2
  94. package/src/components/FieldEditor/FieldEditor.tsx +87 -83
  95. package/src/components/Form/AUDIT.md +17 -0
  96. package/src/components/Form/Form.stories.tsx +154 -219
  97. package/src/components/Form/Form.tsx +314 -97
  98. package/src/components/Form/FormField.tsx +305 -0
  99. package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
  100. package/src/components/Form/FormFieldComponent.tsx +171 -0
  101. package/src/components/Form/FormFieldSet.tsx +129 -0
  102. package/src/components/Form/README.md +3 -0
  103. package/src/components/Form/fields/ArrayField.tsx +154 -0
  104. package/src/components/Form/fields/BooleanField.tsx +22 -0
  105. package/src/components/Form/fields/GeoPointField.tsx +120 -0
  106. package/src/components/Form/fields/MarkdownField.tsx +78 -0
  107. package/src/components/Form/fields/NumberField.tsx +39 -0
  108. package/src/components/Form/fields/RefField.tsx +199 -0
  109. package/src/components/Form/fields/SelectField.tsx +65 -0
  110. package/src/components/Form/fields/SelectOptionField.stories.tsx +64 -0
  111. package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +49 -45
  112. package/src/components/Form/fields/TextAreaField.tsx +38 -0
  113. package/src/components/Form/fields/TextField.tsx +38 -0
  114. package/src/components/Form/{custom/TupleInput.tsx → fields/TupleField.tsx} +17 -10
  115. package/src/components/Form/fields/index.ts +15 -0
  116. package/src/components/Form/index.ts +3 -4
  117. package/src/components/ObjectPicker/ObjectPicker.stories.tsx +210 -0
  118. package/src/components/ObjectPicker/ObjectPicker.tsx +186 -0
  119. package/src/components/{ControlSection → ObjectPicker}/index.ts +1 -1
  120. package/src/components/Settings/Settings.stories.tsx +56 -0
  121. package/src/components/Settings/Settings.tsx +129 -0
  122. package/src/components/Settings/index.ts +5 -0
  123. package/src/components/ViewEditor/ViewEditor.stories.tsx +141 -63
  124. package/src/components/ViewEditor/ViewEditor.test.tsx +17 -18
  125. package/src/components/ViewEditor/ViewEditor.tsx +353 -203
  126. package/src/components/index.ts +2 -3
  127. package/src/components/testing.tsx +33 -13
  128. package/src/hooks/index.ts +2 -3
  129. package/src/hooks/useFormHandler.ts +380 -0
  130. package/src/hooks/useKeyHandler.tsx +37 -0
  131. package/src/translations.ts +66 -58
  132. package/src/util/field.ts +33 -43
  133. package/src/util/formatting.ts +22 -22
  134. package/src/util/index.ts +1 -1
  135. package/src/util/properties.ts +18 -0
  136. package/src/util/refs.ts +17 -19
  137. package/dist/types/src/components/ControlSection/ControlSection.d.ts +0 -23
  138. package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +0 -1
  139. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +0 -8
  140. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +0 -1
  141. package/dist/types/src/components/ControlSection/index.d.ts +0 -2
  142. package/dist/types/src/components/ControlSection/index.d.ts.map +0 -1
  143. package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
  144. package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
  145. package/dist/types/src/components/Form/Defaults.d.ts +0 -14
  146. package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
  147. package/dist/types/src/components/Form/FormActions.d.ts +0 -9
  148. package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
  149. package/dist/types/src/components/Form/FormContent.d.ts +0 -57
  150. package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
  151. package/dist/types/src/components/Form/FormContext.d.ts +0 -23
  152. package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
  153. package/dist/types/src/components/Form/Input.d.ts +0 -26
  154. package/dist/types/src/components/Form/Input.d.ts.map +0 -1
  155. package/dist/types/src/components/Form/RefField.d.ts +0 -12
  156. package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
  157. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
  158. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
  159. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
  160. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
  161. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -7
  162. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
  163. package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
  164. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
  165. package/dist/types/src/components/Form/custom/index.d.ts +0 -4
  166. package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
  167. package/dist/types/src/components/Form/factory.d.ts +0 -8
  168. package/dist/types/src/components/Form/factory.d.ts.map +0 -1
  169. package/dist/types/src/components/deprecated/Form.d.ts +0 -15
  170. package/dist/types/src/components/deprecated/Form.d.ts.map +0 -1
  171. package/dist/types/src/components/deprecated/Form.stories.d.ts +0 -13
  172. package/dist/types/src/components/deprecated/Form.stories.d.ts.map +0 -1
  173. package/dist/types/src/components/deprecated/FormInput.d.ts +0 -16
  174. package/dist/types/src/components/deprecated/FormInput.d.ts.map +0 -1
  175. package/dist/types/src/components/deprecated/index.d.ts +0 -2
  176. package/dist/types/src/components/deprecated/index.d.ts.map +0 -1
  177. package/dist/types/src/hooks/useForm.d.ts +0 -67
  178. package/dist/types/src/hooks/useForm.d.ts.map +0 -1
  179. package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -23
  180. package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
  181. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
  182. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
  183. package/dist/types/src/util/schema.d.ts +0 -3
  184. package/dist/types/src/util/schema.d.ts.map +0 -1
  185. package/src/components/ControlSection/ControlSection.stories.tsx +0 -41
  186. package/src/components/ControlSection/ControlSection.tsx +0 -139
  187. package/src/components/Form/ArrayField.tsx +0 -124
  188. package/src/components/Form/Defaults.tsx +0 -203
  189. package/src/components/Form/FormActions.tsx +0 -53
  190. package/src/components/Form/FormContent.tsx +0 -240
  191. package/src/components/Form/FormContext.tsx +0 -110
  192. package/src/components/Form/Input.tsx +0 -51
  193. package/src/components/Form/RefField.tsx +0 -213
  194. package/src/components/Form/custom/GeoPointInput.tsx +0 -72
  195. package/src/components/Form/custom/SelectOptionsInput.stories.tsx +0 -61
  196. package/src/components/Form/custom/index.ts +0 -7
  197. package/src/components/Form/factory.tsx +0 -31
  198. package/src/components/deprecated/Form.stories.tsx +0 -62
  199. package/src/components/deprecated/Form.tsx +0 -119
  200. package/src/components/deprecated/FormInput.tsx +0 -71
  201. package/src/components/deprecated/index.ts +0 -5
  202. package/src/hooks/useForm.ts +0 -291
  203. package/src/hooks/useQueryRefOptions.ts +0 -53
  204. package/src/hooks/useRefQueryLookupHandler.ts +0 -39
  205. package/src/util/schema.ts +0 -14
@@ -0,0 +1,305 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Match from 'effect/Match';
6
+ import * as Option from 'effect/Option';
7
+ import * as Schema from 'effect/Schema';
8
+ import * as SchemaAST from 'effect/SchemaAST';
9
+ import * as String from 'effect/String';
10
+ import React, { useMemo } from 'react';
11
+
12
+ import { Format } from '@dxos/echo';
13
+ import {
14
+ createJsonPath,
15
+ findNode,
16
+ getAnnotation,
17
+ getDiscriminatedType,
18
+ isArrayType,
19
+ isDiscriminatedUnion,
20
+ isLiteralUnion,
21
+ isNestedType,
22
+ } from '@dxos/effect';
23
+ import { useTranslation } from '@dxos/react-ui';
24
+ import { type ProjectionModel } from '@dxos/schema';
25
+
26
+ import { translationKey } from '../../translations';
27
+ import { getRefProps } from '../../util';
28
+
29
+ import {
30
+ ArrayField,
31
+ BooleanField,
32
+ GeoPointField,
33
+ MarkdownField,
34
+ NumberField,
35
+ RefField,
36
+ type RefFieldProps,
37
+ SelectField,
38
+ TextAreaField,
39
+ TextField,
40
+ } from './fields';
41
+ import { useFormFieldState } from './Form';
42
+ import {
43
+ type FormFieldComponent,
44
+ type FormFieldComponentProps,
45
+ type FormFieldMap,
46
+ type FormFieldProvider,
47
+ } from './FormFieldComponent';
48
+ import { FormFieldSet } from './FormFieldSet';
49
+
50
+ export type FormFieldProps = {
51
+ /**
52
+ * AST of the property to render.
53
+ */
54
+ type: SchemaAST.AST;
55
+
56
+ /**
57
+ * Name of the property.
58
+ */
59
+ name: string;
60
+
61
+ /**
62
+ * Path to the current object from the root. Used with nested forms.
63
+ */
64
+ path?: (string | number)[];
65
+
66
+ /**
67
+ * Optional projection for projection-based field management.
68
+ */
69
+ projection?: ProjectionModel;
70
+
71
+ /**
72
+ * Map of custom renderers for specific properties.
73
+ * Prefer fieldProvider for plugin specific input surfaces.
74
+ */
75
+ fieldMap?: FormFieldMap;
76
+
77
+ /**
78
+ * Function to lookup custom renderers for specific properties.
79
+ */
80
+ fieldProvider?: FormFieldProvider;
81
+ /**
82
+ * Typename of the ref type that the create props apply to.
83
+ * When set, createOptionLabel/createOptionIcon/createInitialValuePath/createFieldMap/onCreate
84
+ * are only passed to ref fields whose typename matches.
85
+ */
86
+ createTypename?: string;
87
+ } & Pick<FormFieldComponentProps, 'autoFocus' | 'readonly' | 'layout'> &
88
+ Pick<
89
+ RefFieldProps,
90
+ | 'createOptionLabel'
91
+ | 'createOptionIcon'
92
+ | 'createInitialValuePath'
93
+ | 'createFieldMap'
94
+ | 'db'
95
+ | 'schemaHook'
96
+ | 'getOptions'
97
+ | 'onCreate'
98
+ >;
99
+
100
+ export const FormField = (props: FormFieldProps) => {
101
+ const {
102
+ type,
103
+ name,
104
+ path,
105
+ projection,
106
+ fieldMap,
107
+ fieldProvider,
108
+ readonly,
109
+ layout,
110
+
111
+ // RefFieldProps
112
+ createTypename,
113
+ createOptionLabel,
114
+ createOptionIcon,
115
+ createInitialValuePath,
116
+ createFieldMap,
117
+ db,
118
+ schemaHook,
119
+ getOptions: getRefOptions,
120
+ onCreate,
121
+ } = props;
122
+ const { t } = useTranslation(translationKey);
123
+ const title = getAnnotation<string>(SchemaAST.TitleAnnotationId)(type);
124
+ const description = getAnnotation<string>(SchemaAST.DescriptionAnnotationId)(type);
125
+ const examples = getAnnotation<string[]>(SchemaAST.ExamplesAnnotationId)(type);
126
+
127
+ const label = useMemo(() => title ?? String.capitalize(name), [title, name]);
128
+ const placeholder = useMemo(
129
+ () => (examples?.length ? `${t('example.placeholder')}: ${examples[0]}` : (description ?? label)),
130
+ [examples, description, label],
131
+ );
132
+
133
+ const fieldState = useFormFieldState(FormField.displayName, path);
134
+ const fieldProps: FormFieldComponentProps = {
135
+ type,
136
+ format: Format.FormatAnnotation.getFromAst(type).pipe((annotation) => Option.getOrUndefined(annotation)),
137
+ readonly,
138
+ label,
139
+ placeholder,
140
+ layout,
141
+ ...fieldState,
142
+ };
143
+
144
+ //
145
+ // Custom field.
146
+ //
147
+
148
+ const jsonPath = createJsonPath(path ?? []);
149
+ const CustomField = fieldMap?.[jsonPath];
150
+ if (CustomField) {
151
+ return <CustomField {...fieldProps} />;
152
+ }
153
+
154
+ // TODO(burdon): Expensive to create schema each time; pass AST?
155
+ const component = fieldProvider?.({ schema: Schema.make(type), prop: name, fieldProps });
156
+ if (component) {
157
+ return component;
158
+ }
159
+
160
+ //
161
+ // Array field.
162
+ //
163
+
164
+ if (isArrayType(type)) {
165
+ return <ArrayField fieldProps={fieldState} label={label} {...props} />;
166
+ }
167
+
168
+ //
169
+ // Regular field.
170
+ //
171
+
172
+ const Field = getFormField(fieldProps);
173
+ if (Field) {
174
+ return <Field {...fieldProps} />;
175
+ }
176
+
177
+ //
178
+ // Select field.
179
+ //
180
+
181
+ const options = getOptions(type);
182
+ if (options) {
183
+ // Resolve labels from projection metadata when available.
184
+ const fieldProjections = projection?.getFieldProjections();
185
+ const fieldProjection = fieldProjections?.find((fp) => fp.field.path === name);
186
+ const selectOptions = fieldProjection?.props.options;
187
+
188
+ return (
189
+ <SelectField
190
+ {...fieldProps}
191
+ options={options.map((option) => {
192
+ const selectOption = selectOptions?.find((so) => so.id === globalThis.String(option));
193
+ return {
194
+ value: option,
195
+ label: selectOption?.title ?? option.toString(),
196
+ };
197
+ })}
198
+ />
199
+ );
200
+ }
201
+
202
+ //
203
+ // Ref field.
204
+ //
205
+
206
+ const refProps = getRefProps(type);
207
+ if (refProps) {
208
+ const isCreateTarget = !createTypename || refProps.typename === createTypename;
209
+ return (
210
+ <RefField
211
+ {...fieldProps}
212
+ {...refProps}
213
+ createOptionLabel={isCreateTarget ? createOptionLabel : undefined}
214
+ createOptionIcon={isCreateTarget ? createOptionIcon : undefined}
215
+ createInitialValuePath={isCreateTarget ? createInitialValuePath : undefined}
216
+ createFieldMap={isCreateTarget ? createFieldMap : undefined}
217
+ db={db}
218
+ schemaHook={schemaHook}
219
+ getOptions={getRefOptions}
220
+ onCreate={onCreate}
221
+ />
222
+ );
223
+ }
224
+
225
+ //
226
+ // Nested Object field.
227
+ //
228
+
229
+ if (isNestedType(type)) {
230
+ const baseNode = findNode(type, isDiscriminatedUnion);
231
+ const typeLiteral = baseNode
232
+ ? getDiscriminatedType(baseNode, fieldState.getValue() as any)
233
+ : findNode(type, SchemaAST.isTypeLiteral);
234
+
235
+ if (typeLiteral) {
236
+ const schema = Schema.make(typeLiteral);
237
+ return (
238
+ <FormFieldSet
239
+ schema={schema}
240
+ path={path}
241
+ readonly={readonly}
242
+ layout={layout}
243
+ label={label}
244
+ projection={projection}
245
+ fieldMap={fieldMap}
246
+ fieldProvider={fieldProvider}
247
+ createOptionLabel={createOptionLabel}
248
+ createOptionIcon={createOptionIcon}
249
+ createInitialValuePath={createInitialValuePath}
250
+ db={db}
251
+ schemaHook={schemaHook}
252
+ getOptions={getRefOptions}
253
+ onCreate={onCreate}
254
+ />
255
+ );
256
+ }
257
+ }
258
+
259
+ return null;
260
+ };
261
+
262
+ FormField.displayName = 'Form.FormField';
263
+
264
+ /**
265
+ * Get property input component.
266
+ */
267
+ const getFormField = ({ type, format }: FormFieldComponentProps): FormFieldComponent | undefined => {
268
+ //
269
+ // Standard formats.
270
+ //
271
+
272
+ const formatField = Match.value(format).pipe(
273
+ Match.withReturnType<FormFieldComponent | undefined>(),
274
+ Match.when(Format.TypeFormat.GeoPoint, () => GeoPointField),
275
+ Match.when(Format.TypeFormat.Markdown, () => MarkdownField),
276
+ Match.when(Format.TypeFormat.Text, () => TextAreaField),
277
+ Match.orElse(() => undefined),
278
+ );
279
+ if (formatField) {
280
+ return formatField;
281
+ }
282
+
283
+ //
284
+ // Standard types.
285
+ //
286
+
287
+ switch (type._tag) {
288
+ // TODO(wittjosiah): Schema.Any is currently used to represent template inputs.
289
+ case 'AnyKeyword':
290
+ case 'StringKeyword':
291
+ return TextField;
292
+ case 'NumberKeyword':
293
+ return NumberField;
294
+ case 'BooleanKeyword':
295
+ return BooleanField;
296
+ }
297
+ };
298
+
299
+ const getOptions = (ast: SchemaAST.AST): Format.Options[] | undefined => {
300
+ if (isLiteralUnion(ast)) {
301
+ return ast.types.map((type) => type.literal).filter((v): v is string | number => v !== null);
302
+ }
303
+
304
+ return Format.OptionsAnnotation.getFromAst(ast).pipe((annotation) => Option.getOrUndefined(annotation));
305
+ };
@@ -0,0 +1,65 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
+ import React, { type ChangeEvent, useCallback } from 'react';
7
+
8
+ import { Input } from '@dxos/react-ui';
9
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
10
+
11
+ import { type FormFieldComponentProps, FormFieldWrapper } from './FormFieldComponent';
12
+
13
+ const DefaultStory = ({
14
+ type,
15
+ placeholder,
16
+ readonly,
17
+ onBlur,
18
+ onValueChange,
19
+ ...props
20
+ }: FormFieldComponentProps<string>) => {
21
+ const handleChange = useCallback(
22
+ (event: ChangeEvent<HTMLInputElement>) => onValueChange(type, event.target.value),
23
+ [type, onValueChange],
24
+ );
25
+
26
+ return (
27
+ <div className='p-form-padding'>
28
+ <FormFieldWrapper<string> {...props}>
29
+ {({ value = '' }) => (
30
+ <Input.TextInput
31
+ disabled={!!readonly}
32
+ placeholder={placeholder}
33
+ noAutoFill
34
+ value={value}
35
+ onChange={handleChange}
36
+ onBlur={onBlur}
37
+ />
38
+ )}
39
+ </FormFieldWrapper>
40
+ </div>
41
+ );
42
+ };
43
+
44
+ const meta: Meta<typeof DefaultStory> = {
45
+ title: 'ui/react-ui-form/FormFieldComponent',
46
+ component: DefaultStory,
47
+ decorators: [withTheme(), withLayout({ layout: 'column' })],
48
+ parameters: {
49
+ layout: 'fullscreen',
50
+ },
51
+ };
52
+
53
+ export default meta;
54
+
55
+ type Story = StoryObj<typeof meta>;
56
+
57
+ export const Default: Story = {
58
+ args: {
59
+ label: 'Label',
60
+ getStatus: () => ({ status: 'error', error: 'Required field' }),
61
+ getValue: () => 'DXOS',
62
+ onBlur: () => {},
63
+ onValueChange: () => {},
64
+ },
65
+ };
@@ -0,0 +1,171 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import type * as Schema from 'effect/Schema';
6
+ import type * as SchemaAST from 'effect/SchemaAST';
7
+ import React, {
8
+ Component,
9
+ type FC,
10
+ type FocusEvent,
11
+ type PropsWithChildren,
12
+ type ReactElement,
13
+ type ReactNode,
14
+ } from 'react';
15
+
16
+ import { type Format } from '@dxos/echo/internal';
17
+ import { Icon, Input, Tooltip } from '@dxos/react-ui';
18
+ import { inputTextLabel } from '@dxos/ui-theme';
19
+
20
+ import { type FormFieldStatus } from '../../hooks';
21
+
22
+ /**
23
+ * Dynamic props passed to input components.
24
+ */
25
+ export type FormFieldStateProps<T = any> = {
26
+ getStatus: () => FormFieldStatus;
27
+ getValue: () => T | undefined;
28
+ onBlur: (event: FocusEvent<HTMLElement>) => void;
29
+ onValueChange: (type: SchemaAST.AST, value: T) => void;
30
+ };
31
+
32
+ /**
33
+ * Presentation mode.
34
+ * - full: Show label, control, and status.
35
+ * - compact: Show label and control.
36
+ * - inline: Control only.
37
+ * - static: Plain DOM; omit all undefined values.
38
+ */
39
+ // TODO(burdon): 'inline' should be orthogonal to the layout (e.g., array of inline static).
40
+ export type Presentation = 'full' | 'compact' | 'inline' | 'static';
41
+
42
+ /**
43
+ * Props passed to input components.
44
+ */
45
+ export type FormFieldComponentProps<T = any> = {
46
+ type: SchemaAST.AST;
47
+ format?: Format.TypeFormat;
48
+ readonly?: boolean;
49
+ label: string;
50
+ placeholder?: string;
51
+ autoFocus?: boolean;
52
+ layout?: Presentation;
53
+ } & FormFieldStateProps<T>;
54
+
55
+ /**
56
+ * Form field component.
57
+ */
58
+ export type FormFieldComponent = FC<FormFieldComponentProps>;
59
+
60
+ export type FormFieldMap = Record<string, FormFieldComponent>;
61
+
62
+ export type FormFieldProvider = (props: {
63
+ prop: string; // TODO(burdon): Path?
64
+ schema: Schema.Schema<any>;
65
+ fieldProps: FormFieldComponentProps;
66
+ }) => ReactElement | undefined;
67
+
68
+ //
69
+ // FormFieldLabel
70
+ //
71
+
72
+ export type FormFieldLabelProps = {
73
+ asChild?: boolean;
74
+ error?: string;
75
+ } & Pick<FormFieldComponentProps, 'label' | 'readonly'>;
76
+
77
+ export const FormFieldLabel = ({ label, error, readonly, asChild }: FormFieldLabelProps) => {
78
+ const Label = readonly || asChild ? 'span' : Input.Label;
79
+ return (
80
+ <div role='none' className='flex items-center justify-between'>
81
+ <Label className={inputTextLabel}>{label}</Label>
82
+ {error && (
83
+ <Tooltip.Trigger asChild content={error} side='bottom'>
84
+ <Icon icon='ph--warning--regular' size={4} classNames='text-error-text' />
85
+ </Tooltip.Trigger>
86
+ )}
87
+ </div>
88
+ );
89
+ };
90
+
91
+ FormFieldLabel.displayName = 'Form.FieldLabel';
92
+
93
+ //
94
+ // FormFieldWrapper
95
+ //
96
+
97
+ export type FormFieldWrapperProps<T = any> = Pick<
98
+ FormFieldComponentProps,
99
+ 'readonly' | 'label' | 'layout' | 'getStatus' | 'getValue'
100
+ > & {
101
+ children?: (props: { value: T }) => ReactNode;
102
+ };
103
+
104
+ export const FormFieldWrapper = <T,>(props: FormFieldWrapperProps<T>) => {
105
+ const { children, readonly, layout, label, getStatus, getValue } = props;
106
+ const { status, error } = getStatus();
107
+
108
+ const value = getValue();
109
+ if (layout === 'static' && value == null) {
110
+ return null;
111
+ }
112
+
113
+ const str = String(value ?? '');
114
+
115
+ return (
116
+ <div role='none' className='contents'>
117
+ <Input.Root validationValence={status}>
118
+ {layout !== 'inline' && <FormFieldLabel error={error} readonly={readonly} label={label} />}
119
+ {layout === 'static' ? <p>{str}</p> : children ? children({ value }) : null}
120
+ {layout === 'full' && (
121
+ <Input.DescriptionAndValidation>
122
+ <Input.Validation>{error}</Input.Validation>
123
+ </Input.DescriptionAndValidation>
124
+ )}
125
+ </Input.Root>
126
+ </div>
127
+ );
128
+ };
129
+
130
+ //
131
+ // FormFieldErrorBoundary
132
+ //
133
+
134
+ type FormFieldErrorState = {
135
+ error: Error | undefined;
136
+ };
137
+
138
+ type FormFieldErrorBoundaryProps = PropsWithChildren<{
139
+ path?: (string | number)[];
140
+ }>;
141
+
142
+ export class FormFieldErrorBoundary extends Component<FormFieldErrorBoundaryProps, FormFieldErrorState> {
143
+ static getDerivedStateFromError(error: Error): { error: Error } {
144
+ return { error };
145
+ }
146
+
147
+ override state = { error: undefined };
148
+
149
+ override componentDidUpdate(prevProps: FormFieldErrorBoundaryProps) {
150
+ if (prevProps.path !== this.props.path) {
151
+ this.resetError();
152
+ }
153
+ }
154
+
155
+ override render() {
156
+ if (this.state.error) {
157
+ return (
158
+ <div className='flex gap-2 border border-rose-fill font-mono text-sm'>
159
+ <span className='bg-rose-fill text-surface-text px-1 font-thin'>ERROR</span>
160
+ {String(this.props.path?.join('.'))}
161
+ </div>
162
+ );
163
+ }
164
+
165
+ return this.props.children;
166
+ }
167
+
168
+ private resetError() {
169
+ this.setState({ error: undefined });
170
+ }
171
+ }
@@ -0,0 +1,129 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import React, { useMemo } from 'react';
6
+
7
+ import { type AnyProperties } from '@dxos/echo/internal';
8
+ import { type SchemaProperty } from '@dxos/effect';
9
+
10
+ import { type FormHandlerProps } from '../../hooks';
11
+ import { getFormProperties } from '../../util';
12
+
13
+ import { useFormValues } from './Form';
14
+ import { FormField, type FormFieldProps } from './FormField';
15
+ import { FormFieldErrorBoundary, FormFieldLabel } from './FormFieldComponent';
16
+
17
+ const FORM_FIELDSET_NAME = 'Form.FieldSet';
18
+
19
+ export type FormFieldSetProps<T extends AnyProperties> = {
20
+ label?: string;
21
+ sort?: string[];
22
+ exclude?: (props: SchemaProperty[]) => SchemaProperty[];
23
+ } & Pick<FormHandlerProps<T>, 'schema'> &
24
+ Pick<
25
+ FormFieldProps,
26
+ | 'path'
27
+ | 'autoFocus'
28
+ | 'readonly'
29
+ | 'layout'
30
+ | 'projection'
31
+ | 'fieldMap'
32
+ | 'fieldProvider'
33
+ | 'createTypename'
34
+ | 'createOptionLabel'
35
+ | 'createOptionIcon'
36
+ | 'createInitialValuePath'
37
+ | 'createFieldMap'
38
+ | 'db'
39
+ | 'schemaHook'
40
+ | 'getOptions'
41
+ | 'onCreate'
42
+ >;
43
+
44
+ /**
45
+ * Renders a set of form fields derived from a schema object.
46
+ */
47
+ export const FormFieldSet = ({
48
+ label,
49
+ schema,
50
+ readonly,
51
+ path,
52
+ sort,
53
+ exclude,
54
+ projection,
55
+ layout,
56
+ ...props
57
+ }: FormFieldSetProps<any>) => {
58
+ const values = useFormValues(FORM_FIELDSET_NAME, path);
59
+
60
+ // TODO(burdon): Updates on every value change.
61
+ // Remove values dep if can remove from getSchemaProperties.
62
+ const properties = useMemo(() => {
63
+ if (!schema) {
64
+ return [];
65
+ }
66
+
67
+ // TODO(wittjosiah): Reconcile FormInputAnnotation with projection hidden properties & exclude function.
68
+ const schemaProps = getFormProperties(schema.ast);
69
+ const filteredProps = exclude ? exclude(schemaProps) : schemaProps;
70
+
71
+ // Use projection-based field management when view and projection are available.
72
+ if (projection) {
73
+ const fieldProjections = projection.getFieldProjections();
74
+ const hiddenProperties = new Set(projection.getHiddenProperties());
75
+
76
+ // Filter properties to only include visible ones and order by projection.
77
+ const visibleProps = filteredProps.filter((prop) => !hiddenProperties.has(prop.name.toString()));
78
+ const orderedProps: SchemaProperty[] = [];
79
+
80
+ // Add properties in projection field order.
81
+ for (const fieldProjection of fieldProjections) {
82
+ const fieldPath = String(fieldProjection.field.path);
83
+ const prop = visibleProps.find((p) => p.name === fieldPath);
84
+ if (prop) {
85
+ orderedProps.push(prop);
86
+ }
87
+ }
88
+
89
+ // Add any remaining properties not in projection.
90
+ const projectionPaths = new Set(fieldProjections.map((projection) => String(projection.field.path)));
91
+ const remainingProps = visibleProps.filter((prop) => !projectionPaths.has(prop.name.toString()));
92
+ orderedProps.push(...remainingProps);
93
+ return orderedProps;
94
+ }
95
+
96
+ // Fallback to legacy filter/sort behavior.
97
+ return sort
98
+ ? [...filteredProps].sort(({ name: a }, { name: b }) => sort.indexOf(a.toString()) - sort.indexOf(b.toString()))
99
+ : filteredProps;
100
+ }, [schema, values, exclude, sort, projection]);
101
+
102
+ if ((readonly || layout === 'static') && values == null) {
103
+ return null;
104
+ }
105
+
106
+ return (
107
+ <>
108
+ {layout !== 'inline' && label && <FormFieldLabel label={label} asChild />}
109
+ {properties.map((property) => {
110
+ const name = property.name.toString();
111
+ return (
112
+ <FormFieldErrorBoundary key={name} path={[...(path ?? []), name]}>
113
+ <FormField
114
+ type={property.type}
115
+ name={name}
116
+ path={[...(path ?? []), name]}
117
+ readonly={readonly}
118
+ layout={layout}
119
+ projection={projection}
120
+ {...props}
121
+ />
122
+ </FormFieldErrorBoundary>
123
+ );
124
+ })}
125
+ </>
126
+ );
127
+ };
128
+
129
+ FormFieldSet.displayName = FORM_FIELDSET_NAME;
@@ -0,0 +1,3 @@
1
+ # Form
2
+
3
+ - [ ] Layout options (e.g., label on the side.)