@dxos/react-ui-form 0.7.4 → 0.7.5-labs.5f04cf6

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 (65) hide show
  1. package/dist/lib/browser/index.mjs +574 -496
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +629 -565
  5. package/dist/lib/node/index.cjs.map +4 -4
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +574 -496
  8. package/dist/lib/node-esm/index.mjs.map +4 -4
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +2 -2
  11. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  12. package/dist/types/src/components/Form/ArrayField.d.ts +16 -0
  13. package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -0
  14. package/dist/types/src/components/Form/Defaults.d.ts +5 -6
  15. package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/Form.d.ts +13 -17
  17. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  18. package/dist/types/src/components/Form/Form.stories.d.ts +7 -1
  19. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  20. package/dist/types/src/components/Form/FormActions.d.ts +7 -0
  21. package/dist/types/src/components/Form/FormActions.d.ts.map +1 -0
  22. package/dist/types/src/components/Form/FormContent.d.ts +26 -0
  23. package/dist/types/src/components/Form/FormContent.d.ts.map +1 -0
  24. package/dist/types/src/components/Form/FormContext.d.ts +20 -0
  25. package/dist/types/src/components/Form/FormContext.d.ts.map +1 -0
  26. package/dist/types/src/components/Form/Input.d.ts +5 -6
  27. package/dist/types/src/components/Form/Input.d.ts.map +1 -1
  28. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +4 -0
  29. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +1 -0
  30. package/dist/types/src/components/Form/custom/TupleInput.d.ts +6 -0
  31. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +1 -0
  32. package/dist/types/src/components/Form/custom/index.d.ts +3 -0
  33. package/dist/types/src/components/Form/custom/index.d.ts.map +1 -0
  34. package/dist/types/src/components/Form/factory.d.ts +2 -2
  35. package/dist/types/src/components/Form/factory.d.ts.map +1 -1
  36. package/dist/types/src/components/Form/index.d.ts +2 -2
  37. package/dist/types/src/components/Form/index.d.ts.map +1 -1
  38. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +6 -5
  39. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  40. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  41. package/dist/types/src/hooks/useForm.d.ts +13 -15
  42. package/dist/types/src/hooks/useForm.d.ts.map +1 -1
  43. package/dist/types/tsconfig.tsbuildinfo +1 -0
  44. package/package.json +26 -26
  45. package/src/components/FieldEditor/FieldEditor.stories.tsx +2 -2
  46. package/src/components/FieldEditor/FieldEditor.tsx +75 -50
  47. package/src/components/Form/ArrayField.tsx +111 -0
  48. package/src/components/Form/Defaults.tsx +20 -33
  49. package/src/components/Form/Form.stories.tsx +48 -6
  50. package/src/components/Form/Form.tsx +39 -333
  51. package/src/components/Form/FormActions.tsx +41 -0
  52. package/src/components/Form/FormContent.tsx +155 -0
  53. package/src/components/Form/FormContext.tsx +48 -0
  54. package/src/components/Form/Input.tsx +5 -6
  55. package/src/components/Form/custom/GeoPointInput.tsx +44 -0
  56. package/src/components/Form/custom/TupleInput.tsx +49 -0
  57. package/src/components/Form/custom/index.ts +6 -0
  58. package/src/components/Form/factory.tsx +4 -7
  59. package/src/components/Form/index.ts +3 -2
  60. package/src/components/ViewEditor/ViewEditor.stories.tsx +30 -8
  61. package/src/components/ViewEditor/ViewEditor.tsx +13 -8
  62. package/src/hooks/useForm.ts +67 -78
  63. package/dist/types/src/components/Form/Custom.d.ts +0 -5
  64. package/dist/types/src/components/Form/Custom.d.ts.map +0 -1
  65. package/src/components/Form/Custom.tsx +0 -65
@@ -2,34 +2,30 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { pipe } from 'effect';
6
- import { capitalize } from 'effect/String';
7
- import React, { useEffect, useMemo } from 'react';
5
+ import React, { type ReactElement, useMemo } from 'react';
8
6
 
9
- import { AST, type BaseObject, S, type PropertyKey, type FormatEnum } from '@dxos/echo-schema';
10
- import { findNode, getDiscriminatedType, isDiscriminatedUnion, SimpleType } from '@dxos/effect';
11
- import { log } from '@dxos/log';
12
- import { IconButton, type ThemedClassName, useTranslation } from '@dxos/react-ui';
7
+ import { type BaseObject, type S, type PropertyKey } from '@dxos/echo-schema';
8
+ import { type ThemedClassName } from '@dxos/react-ui';
13
9
  import { mx } from '@dxos/react-ui-theme';
14
- import { getSchemaProperties, type SchemaProperty } from '@dxos/schema';
15
- import { isNotFalsy } from '@dxos/util';
10
+ import { type SchemaProperty } from '@dxos/schema';
16
11
 
17
- import { SelectInput } from './Defaults';
18
- import { InputHeader, type InputComponent } from './Input';
19
- import { getInputComponent } from './factory';
20
- import { type FormHandler, type FormOptions, useForm } from '../../hooks';
21
- import { translationKey } from '../../translations';
22
-
23
- // TODO(burdon): Rename package react-ui-form; delete react-ui-card.
24
-
25
- const padding = 'px-2';
12
+ import { FormActions } from './FormActions';
13
+ import { FormFields } from './FormContent';
14
+ import { FormProvider } from './FormContext';
15
+ import { type InputProps, type InputComponent } from './Input';
16
+ import { type FormOptions } from '../../hooks';
26
17
 
27
18
  export type PropsFilter<T extends BaseObject> = (props: SchemaProperty<T>[]) => SchemaProperty<T>[];
28
19
 
20
+ export type ComponentLookup = (args: {
21
+ prop: string;
22
+ schema: S.Schema<any>;
23
+ inputProps: InputProps;
24
+ }) => ReactElement | undefined;
25
+
29
26
  export type FormProps<T extends BaseObject> = ThemedClassName<
30
27
  {
31
- // TODO(burdon): Partial<T>?
32
- values: T;
28
+ values: Partial<T>;
33
29
 
34
30
  /** Path to the current object from the root. Used with nested forms. */
35
31
  path?: string[];
@@ -43,18 +39,15 @@ export type FormProps<T extends BaseObject> = ThemedClassName<
43
39
  autoSave?: boolean;
44
40
  testId?: string;
45
41
  onCancel?: () => void;
46
-
42
+ lookupComponent?: ComponentLookup;
47
43
  /**
48
44
  * Map of custom renderers for specific properties.
45
+ * Prefer lookupComponent for plugin specific input surfaces.
49
46
  */
50
- Custom?: Partial<Record<string, InputComponent<T>>>;
47
+ Custom?: Partial<Record<string, InputComponent>>;
51
48
  } & Pick<FormOptions<T>, 'schema' | 'onValuesChanged' | 'onValidate' | 'onSave'>
52
49
  >;
53
50
 
54
- /**
55
- * General purpose form component that displays field controls based on the given schema.
56
- */
57
- // TODO(burdon): Area to show general validation errors.
58
51
  export const Form = <T extends BaseObject>({
59
52
  classNames,
60
53
  schema,
@@ -69,319 +62,32 @@ export const Form = <T extends BaseObject>({
69
62
  onValidate,
70
63
  onSave,
71
64
  onCancel,
65
+ lookupComponent,
72
66
  Custom,
73
67
  }: FormProps<T>) => {
74
68
  const onValid = useMemo(() => (autoSave ? onSave : undefined), [autoSave, onSave]);
75
- const { canSave, values, errors, handleSave, ...inputProps } = useForm<T>({
76
- schema,
77
- initialValues,
78
- onValuesChanged,
79
- onValidate,
80
- onValid,
81
- onSave,
82
- });
83
-
84
- // Filter and sort props.
85
- // TODO(burdon): Move into useForm?
86
- const properties = useMemo(() => {
87
- const props = getSchemaProperties<T>(schema.ast, values);
88
- const filtered = filter ? filter(props) : props;
89
- const findIndex = (props: PropertyKey<T>[], prop: PropertyKey<T>) => {
90
- const idx = props.findIndex((p) => p === prop);
91
- return idx === -1 ? Infinity : idx;
92
- };
93
-
94
- return sort ? filtered.sort(({ name: a }, { name: b }) => findIndex(sort, a) - findIndex(sort, b)) : filtered;
95
- }, [schema, filter, values]);
96
-
97
- // TODO(burdon): Highlight in UX.
98
- useEffect(() => {
99
- if (errors && Object.keys(errors).length) {
100
- log.warn('validation', { errors });
101
- }
102
- }, [errors]);
103
-
104
- return (
105
- <div role='form' className={mx('flex flex-col w-full gap-2 py-2', classNames)} data-testid={testId}>
106
- {properties
107
- .map((property) => {
108
- const { ast, name, type, format, title, description, examples, options, array } = property;
109
- const key = [...path, name];
110
- const label = title ?? pipe(name, capitalize);
111
- const placeholder = examples?.length ? `Example: "${examples[0]}"` : description;
112
-
113
- // Get generic input.
114
- // TODO(ZaymonFC): We might need to switch this to using globs since we're now indexing into arrays.
115
- let InputComponent = Custom?.[key.join('.')];
116
- if (!InputComponent) {
117
- // Select.
118
- if (options) {
119
- return (
120
- <div key={name} role='none' className={padding}>
121
- <SelectInput
122
- type={type}
123
- format={format}
124
- property={name}
125
- disabled={readonly}
126
- label={label}
127
- options={options.map((option) => ({ value: option, label: String(option) }))}
128
- placeholder={placeholder}
129
- {...inputProps}
130
- />
131
- </div>
132
- );
133
- }
134
-
135
- InputComponent = getInputComponent<T>(type, format);
136
-
137
- // TODO(ZaymonFC): Array handling is inelegant. We need to push this complexity down into SDK/Schema.
138
- if (array) {
139
- return (
140
- <ArrayField
141
- key={name}
142
- name={name}
143
- type={type}
144
- array={true}
145
- label={label}
146
- ast={ast}
147
- values={values}
148
- format={format}
149
- readonly={readonly}
150
- placeholder={placeholder}
151
- inputProps={inputProps}
152
- Custom={Custom}
153
- />
154
- );
155
- }
156
- }
157
-
158
- if (!InputComponent) {
159
- // Recursively render form.
160
- if (type === 'object') {
161
- const baseNode = findNode(ast, isDiscriminatedUnion);
162
- const typeLiteral = baseNode
163
- ? getDiscriminatedType(baseNode, values[name] as any)
164
- : findNode(ast, AST.isTypeLiteral);
165
-
166
- if (typeLiteral) {
167
- const schema = S.make(typeLiteral);
168
- return (
169
- <div key={name} role='none'>
170
- <div className={padding}>{label}</div>
171
- <Form<any>
172
- schema={schema}
173
- path={key}
174
- values={values[name] ?? {}}
175
- onValuesChanged={(childValues) => {
176
- inputProps.onValueChange(name, 'object', childValues);
177
- }}
178
- Custom={Custom as any}
179
- />
180
- </div>
181
- );
182
- }
183
- }
184
-
185
- log('no renderer for property', { name, type });
186
- return null;
187
- }
188
-
189
- return (
190
- <div key={name} role='none' className={padding}>
191
- <InputComponent
192
- type={type}
193
- format={format}
194
- property={name}
195
- disabled={readonly}
196
- label={label}
197
- placeholder={placeholder}
198
- {...inputProps}
199
- />
200
- </div>
201
- );
202
- })
203
- .filter(isNotFalsy)}
204
- {(onCancel || onSave) && !autoSave && (
205
- <FormActions onCancel={onCancel} onSubmit={handleSave} canSubmit={canSave} readonly={readonly} />
206
- )}
207
- </div>
208
- );
209
- };
210
-
211
- type ArrayFieldProps<T extends BaseObject> = {
212
- name: PropertyKey<T>;
213
- type: SimpleType;
214
- array: true;
215
- label: string;
216
- ast: AST.AST;
217
- values: Record<string, any>;
218
- format?: FormatEnum;
219
- readonly?: boolean;
220
- placeholder?: string;
221
- inputProps: Pick<FormHandler<T>, 'getStatus' | 'getValue' | 'onValueChange' | 'onBlur'>;
222
- Custom?: Partial<Record<string, InputComponent<T>>>;
223
- };
224
-
225
- const ArrayField = <T extends BaseObject>({
226
- name,
227
- type,
228
- label,
229
- ast,
230
- values,
231
- format,
232
- readonly,
233
- placeholder,
234
- inputProps,
235
- Custom,
236
- }: ArrayFieldProps<T>) => {
237
- const { t } = useTranslation(translationKey);
238
- const arrayValues = (values[name] ?? []) as any[];
239
- const key = [name];
240
-
241
- // TODO(ZaymonFC): Should this unwrapping happen at a lower level?
242
- const tupleType = findNode(ast, AST.isTupleType);
243
- const elementType = (tupleType as AST.TupleType | undefined)?.rest[0]?.type;
244
-
245
- if (type === 'object' && elementType) {
246
- const baseNode = findNode(elementType, isDiscriminatedUnion);
247
- const typeLiteral = baseNode
248
- ? getDiscriminatedType(baseNode, values[name] as any)
249
- : findNode(elementType, AST.isTypeLiteral);
250
-
251
- if (typeLiteral) {
252
- const schema = S.make(typeLiteral);
253
-
254
- return (
255
- <div key={name} role='none' className={mx(padding)}>
256
- <InputHeader>{label}</InputHeader>
257
- <div role='none' className='flex flex-col gap-1'>
258
- {arrayValues.map((value, index) => {
259
- return (
260
- <div key={index} role='none' className='flex items-center gap-1'>
261
- <div role='none' className='flex-1'>
262
- <Form<any>
263
- schema={schema}
264
- path={[...key, index.toString()]}
265
- values={value}
266
- onValuesChanged={(childValues) => {
267
- const newValues = [...arrayValues];
268
- newValues[index] = childValues;
269
- inputProps.onValueChange(name, 'object', newValues);
270
- }}
271
- Custom={Custom as any}
272
- />
273
- </div>
274
- <IconButton
275
- icon='ph--trash--regular'
276
- iconOnly
277
- label={t('button remove')}
278
- onClick={() => {
279
- const newValues = arrayValues.filter((_, i) => i !== index);
280
- inputProps.onValueChange(name, type, newValues);
281
- }}
282
- />
283
- </div>
284
- );
285
- })}
286
- </div>
287
- <div role='none' className='flex justify-between items-center plb-1'>
288
- <IconButton
289
- icon='ph--plus--regular'
290
- iconOnly
291
- label={t('button add')}
292
- onClick={() => {
293
- const newValues = [...arrayValues, {}];
294
- inputProps.onValueChange(name, type, newValues);
295
- }}
296
- />
297
- </div>
298
- </div>
299
- );
300
- }
301
- return (
302
- <div key={name} role='none'>
303
- Nested form not supported in arrays, yet.
304
- </div>
305
- );
306
- }
307
-
308
- const InputComponent = getInputComponent<T>(type, format);
309
- if (!InputComponent) {
310
- return null;
311
- }
312
69
 
313
70
  return (
314
- <div role='none' key='meta-input' className={mx(padding)}>
315
- <InputHeader>{label}</InputHeader>
316
- <div role='none' className='flex flex-col gap-1'>
317
- {arrayValues.map((_, index) => (
318
- <div key={index} role='none' className='flex items-start gap-1'>
319
- <div role='none' className='flex-1'>
320
- <InputComponent
321
- type={type}
322
- format={format}
323
- label={label}
324
- inputOnly
325
- property={`${name}.${index}`}
326
- disabled={readonly}
327
- placeholder={placeholder}
328
- {...inputProps}
329
- />
330
- </div>
331
- <IconButton
332
- icon='ph--trash--regular'
333
- iconOnly
334
- label={t('button remove')}
335
- onClick={() => {
336
- const newValues = arrayValues.filter((_, i) => i !== index);
337
- inputProps.onValueChange(name, type, newValues);
338
- }}
339
- />
340
- </div>
341
- ))}
342
- </div>
343
- <div role='none' className='flex justify-between items-center plb-1'>
344
- <IconButton
345
- icon='ph--plus--regular'
346
- iconOnly
347
- label={t('button add')}
348
- onClick={() => {
349
- const newValues = [...arrayValues, SimpleType.getDefaultValue(type)];
350
- inputProps.onValueChange(name, type, newValues);
351
- }}
71
+ <FormProvider
72
+ schema={schema}
73
+ initialValues={initialValues}
74
+ onValuesChanged={onValuesChanged}
75
+ onValidate={onValidate}
76
+ onValid={onValid}
77
+ onSave={onSave}
78
+ >
79
+ <div role='none' className={mx('p-2', classNames)} data-testid={testId}>
80
+ <FormFields
81
+ schema={schema}
82
+ path={path}
83
+ readonly={readonly}
84
+ filter={filter}
85
+ sort={sort}
86
+ lookupComponent={lookupComponent}
87
+ Custom={Custom}
352
88
  />
89
+ {(onCancel || onSave) && !autoSave && <FormActions onCancel={onCancel} readonly={readonly} />}
353
90
  </div>
354
- </div>
355
- );
356
- };
357
-
358
- type FormActionsProps = {
359
- readonly?: boolean;
360
- canSubmit: boolean;
361
- onCancel?: () => void;
362
- onSubmit: () => void;
363
- };
364
-
365
- export const FormActions = ({ onCancel, onSubmit, canSubmit, readonly }: FormActionsProps) => {
366
- const { t } = useTranslation(translationKey);
367
-
368
- return (
369
- <div role='none' className='flex justify-center'>
370
- <div role='none' className={mx(onCancel && !readonly && 'grid grid-cols-2 gap-2')}>
371
- {onCancel && !readonly && (
372
- <IconButton data-testid='cancel-button' icon='ph--x--regular' label={t('button cancel')} onClick={onCancel} />
373
- )}
374
- {onSubmit && (
375
- <IconButton
376
- type='submit'
377
- data-testid='save-button'
378
- disabled={!canSubmit}
379
- icon='ph--check--regular'
380
- label={t('button save')}
381
- onClick={onSubmit}
382
- />
383
- )}
384
- </div>
385
- </div>
91
+ </FormProvider>
386
92
  );
387
93
  };
@@ -0,0 +1,41 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import React from 'react';
6
+
7
+ import { IconButton, useTranslation } from '@dxos/react-ui';
8
+ import { mx } from '@dxos/react-ui-theme';
9
+
10
+ import { useFormContext } from './FormContext';
11
+ import { translationKey } from '../../translations';
12
+
13
+ export type FormActionsProps = {
14
+ readonly?: boolean;
15
+ onCancel?: () => void;
16
+ };
17
+
18
+ export const FormActions = ({ onCancel, readonly }: FormActionsProps) => {
19
+ const { t } = useTranslation(translationKey);
20
+ const { canSave, handleSave } = useFormContext();
21
+
22
+ return (
23
+ <div role='none' className='flex justify-center'>
24
+ <div role='none' className={mx(onCancel && !readonly && 'grid grid-cols-2 gap-2')}>
25
+ {onCancel && !readonly && (
26
+ <IconButton data-testid='cancel-button' icon='ph--x--regular' label={t('button cancel')} onClick={onCancel} />
27
+ )}
28
+ {handleSave && (
29
+ <IconButton
30
+ type='submit'
31
+ data-testid='save-button'
32
+ disabled={!canSave}
33
+ icon='ph--check--regular'
34
+ label={t('button save')}
35
+ onClick={handleSave}
36
+ />
37
+ )}
38
+ </div>
39
+ </div>
40
+ );
41
+ };
@@ -0,0 +1,155 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { pipe } from 'effect';
6
+ import { capitalize } from 'effect/String';
7
+ import React, { useMemo } from 'react';
8
+
9
+ import { AST, S } from '@dxos/echo-schema';
10
+ import { createJsonPath, findNode, getDiscriminatedType, isDiscriminatedUnion } from '@dxos/effect';
11
+ import { mx } from '@dxos/react-ui-theme';
12
+ import { getSchemaProperties, type SchemaProperty } from '@dxos/schema';
13
+ import { isNotFalsy } from '@dxos/util';
14
+
15
+ import { ArrayField } from './ArrayField';
16
+ import { SelectInput } from './Defaults';
17
+ import { type ComponentLookup } from './Form';
18
+ import { useInputProps, useFormValues } from './FormContext';
19
+ import { type InputComponent } from './Input';
20
+ import { getInputComponent } from './factory';
21
+
22
+ export type FormContentProps = {
23
+ schema: S.Schema.All;
24
+ path?: (string | number)[];
25
+ filter?: (props: SchemaProperty<any>[]) => SchemaProperty<any>[];
26
+ sort?: string[];
27
+ readonly?: boolean;
28
+ lookupComponent?: ComponentLookup;
29
+ Custom?: Partial<Record<string, InputComponent>>;
30
+ };
31
+
32
+ export type FormFieldProps = {
33
+ property: SchemaProperty<any>;
34
+ path?: (string | number)[];
35
+ readonly?: boolean;
36
+ /** Used to indicate if input should be presented inline (e.g. for array items). */
37
+ inline?: boolean;
38
+ lookupComponent?: ComponentLookup;
39
+ Custom?: Partial<Record<string, InputComponent>>;
40
+ };
41
+
42
+ export const FormField = ({ property, path, readonly, inline, lookupComponent, Custom }: FormFieldProps) => {
43
+ const inputProps = useInputProps(path);
44
+ const { ast, name, type, format, title, description, options, examples, array } = property;
45
+
46
+ const label = useMemo(() => title ?? pipe(name, capitalize), [title, name]);
47
+ const placeholder = useMemo(
48
+ () => (examples?.length ? `Example: "${examples[0]}"` : description),
49
+ [examples, description],
50
+ );
51
+
52
+ if (array) {
53
+ return <ArrayField property={property} path={path} inputProps={inputProps} readonly={readonly} Custom={Custom} />;
54
+ }
55
+
56
+ const FoundComponent = lookupComponent?.({
57
+ prop: name,
58
+ schema: S.make(ast),
59
+ inputProps: {
60
+ type,
61
+ format,
62
+ label,
63
+ disabled: readonly,
64
+ placeholder,
65
+ ...inputProps,
66
+ },
67
+ });
68
+
69
+ if (FoundComponent) {
70
+ return <div>{FoundComponent}</div>;
71
+ }
72
+
73
+ const jsonPath = createJsonPath(path ?? []);
74
+ const InputComponent = Custom?.[jsonPath] || getInputComponent(type, format);
75
+ if (InputComponent) {
76
+ return (
77
+ <div>
78
+ <InputComponent
79
+ type={type}
80
+ format={format}
81
+ label={label}
82
+ inputOnly={inline}
83
+ placeholder={placeholder}
84
+ disabled={readonly}
85
+ {...inputProps}
86
+ />
87
+ </div>
88
+ );
89
+ }
90
+
91
+ if (options) {
92
+ return (
93
+ <div role='none'>
94
+ <SelectInput
95
+ type={type}
96
+ format={format}
97
+ disabled={readonly}
98
+ inputOnly={inline}
99
+ label={label}
100
+ options={options.map((option) => ({ value: option, label: String(option) }))}
101
+ placeholder={placeholder}
102
+ {...inputProps}
103
+ />
104
+ </div>
105
+ );
106
+ }
107
+
108
+ if (type === 'object') {
109
+ const baseNode = findNode(ast, isDiscriminatedUnion);
110
+ const typeLiteral = baseNode
111
+ ? getDiscriminatedType(baseNode, inputProps.getValue() as any)
112
+ : findNode(ast, AST.isTypeLiteral);
113
+
114
+ if (typeLiteral) {
115
+ return (
116
+ <div>
117
+ {!inline && <div>{label}</div>}
118
+ <FormFields schema={S.make(typeLiteral)} path={path} readonly={readonly} Custom={Custom} />
119
+ </div>
120
+ );
121
+ }
122
+ }
123
+
124
+ return null;
125
+ };
126
+
127
+ export const FormFields = ({ schema, path, filter, sort, readonly, lookupComponent, Custom }: FormContentProps) => {
128
+ const values = useFormValues(path);
129
+
130
+ const properties = useMemo(() => {
131
+ const props = getSchemaProperties(schema.ast, values);
132
+ const filtered = filter ? filter(props) : props;
133
+
134
+ return sort ? filtered.sort((a, b) => sort.indexOf(a.name) - sort.indexOf(b.name)) : filtered;
135
+ }, [schema, values, filter, sort]);
136
+
137
+ return (
138
+ <div role='form' className={mx('flex flex-col w-full gap-2 py-2')}>
139
+ {properties
140
+ .map((property) => {
141
+ return (
142
+ <FormField
143
+ key={property.name}
144
+ property={property}
145
+ path={[...(path ?? []), property.name]}
146
+ readonly={readonly}
147
+ lookupComponent={lookupComponent}
148
+ Custom={Custom}
149
+ />
150
+ );
151
+ })
152
+ .filter(isNotFalsy)}
153
+ </div>
154
+ );
155
+ };
@@ -0,0 +1,48 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import React, { createContext, useContext, type FocusEvent, type PropsWithChildren } from 'react';
6
+
7
+ import { raise } from '@dxos/debug';
8
+ import { type BaseObject, getValue } from '@dxos/echo-schema';
9
+ import { createJsonPath, type SimpleType } from '@dxos/effect';
10
+
11
+ import { type FormHandler, type FormOptions, useForm } from '../../hooks';
12
+
13
+ type FormContextValue<T extends BaseObject> = FormHandler<T>;
14
+
15
+ const FormContext = createContext<FormContextValue<any> | undefined>(undefined);
16
+
17
+ export const useFormContext = <T extends BaseObject>(): FormContextValue<T> => {
18
+ return useContext(FormContext) ?? raise(new Error('Missing FormContext'));
19
+ };
20
+
21
+ export const useFormValues = (path: (string | number)[] = []): any => {
22
+ const { values: formValues } = useFormContext();
23
+ const jsonPath = createJsonPath(path);
24
+ return getValue(formValues, jsonPath) as BaseObject;
25
+ };
26
+
27
+ export type FormInputStateProps = {
28
+ getStatus: () => { status?: 'error'; error?: string };
29
+ getValue: <V>() => V | undefined;
30
+ onValueChange: (type: SimpleType, value: any) => void;
31
+ onBlur: (event: FocusEvent<HTMLElement>) => void;
32
+ };
33
+
34
+ export const useInputProps = (path: (string | number)[] = []): FormInputStateProps => {
35
+ const { getStatus, getValue: getFormValue, onValueChange, onTouched } = useFormContext();
36
+
37
+ return {
38
+ getStatus: () => getStatus(path),
39
+ getValue: () => getFormValue(path),
40
+ onValueChange: (type: SimpleType, value: any) => onValueChange(path, type, value),
41
+ onBlur: () => onTouched(path),
42
+ };
43
+ };
44
+
45
+ export const FormProvider = ({ children, ...formOptions }: PropsWithChildren<FormOptions<any>>) => {
46
+ const formHandler = useForm(formOptions);
47
+ return <FormContext.Provider value={formHandler}>{children}</FormContext.Provider>;
48
+ };
@@ -4,30 +4,29 @@
4
4
 
5
5
  import React, { type FC, type PropsWithChildren } from 'react';
6
6
 
7
- import { type BaseObject, type FormatEnum } from '@dxos/echo-schema';
7
+ import { type FormatEnum } from '@dxos/echo-schema';
8
8
  import { type SimpleType } from '@dxos/effect';
9
9
  import { Icon, Tooltip } from '@dxos/react-ui';
10
10
  import { errorText } from '@dxos/react-ui-theme';
11
11
 
12
- import { type FormPath, type FormHandler } from '../../hooks';
12
+ import { type FormInputStateProps } from './FormContext';
13
13
 
14
14
  /**
15
15
  * Props passed to input components.
16
16
  */
17
- export type InputProps<T extends BaseObject> = {
18
- property: FormPath<T>;
17
+ export type InputProps = {
19
18
  type: SimpleType;
20
19
  format?: FormatEnum;
21
20
  label?: string;
22
21
  disabled?: boolean;
23
22
  placeholder?: string;
24
23
  inputOnly?: boolean;
25
- } & Pick<FormHandler<T>, 'getStatus' | 'getValue' | 'onValueChange' | 'onBlur'>;
24
+ } & FormInputStateProps;
26
25
 
27
26
  /**
28
27
  * Form input component.
29
28
  */
30
- export type InputComponent<T extends BaseObject> = FC<InputProps<T>>;
29
+ export type InputComponent = FC<InputProps>;
31
30
 
32
31
  export type InputHeaderProps = PropsWithChildren<{
33
32
  error?: string;