@dxos/react-ui-form 0.8.4-main.ae835ea → 0.8.4-main.bc2380dfbc

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 (231) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +0 -1
  3. package/dist/lib/browser/index.mjs +2734 -2312
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/translations.mjs +82 -0
  7. package/dist/lib/browser/translations.mjs.map +7 -0
  8. package/dist/lib/node-esm/index.mjs +2734 -2312
  9. package/dist/lib/node-esm/index.mjs.map +4 -4
  10. package/dist/lib/node-esm/meta.json +1 -1
  11. package/dist/lib/node-esm/translations.mjs +84 -0
  12. package/dist/lib/node-esm/translations.mjs.map +7 -0
  13. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +8 -7
  14. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  15. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +68 -60
  16. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
  17. package/dist/types/src/components/Form/Form.d.ts +109 -26
  18. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  19. package/dist/types/src/components/Form/Form.stories.d.ts +114 -137
  20. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  21. package/dist/types/src/components/Form/FormField.d.ts +43 -0
  22. package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
  23. package/dist/types/src/components/Form/FormFieldComponent.d.ts +79 -0
  24. package/dist/types/src/components/Form/FormFieldComponent.d.ts.map +1 -0
  25. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts +9 -0
  26. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts.map +1 -0
  27. package/dist/types/src/components/Form/FormFieldSet.d.ts +15 -0
  28. package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
  29. package/dist/types/src/components/Form/fields/ArrayField.d.ts +18 -0
  30. package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -0
  31. package/dist/types/src/components/Form/fields/BooleanField.d.ts +4 -0
  32. package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -0
  33. package/dist/types/src/components/Form/fields/GeoPointField.d.ts +5 -0
  34. package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -0
  35. package/dist/types/src/components/Form/fields/MarkdownField.d.ts +11 -0
  36. package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
  37. package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts +84 -0
  38. package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts.map +1 -0
  39. package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
  40. package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
  41. package/dist/types/src/components/Form/fields/RefField.d.ts +22 -0
  42. package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
  43. package/dist/types/src/components/Form/fields/SelectField.d.ts +10 -0
  44. package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
  45. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
  46. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
  47. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +84 -0
  48. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
  49. package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
  50. package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
  51. package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
  52. package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
  53. package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
  54. package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
  55. package/dist/types/src/components/Form/fields/index.d.ts +12 -0
  56. package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
  57. package/dist/types/src/components/Form/index.d.ts +2 -4
  58. package/dist/types/src/components/Form/index.d.ts.map +1 -1
  59. package/dist/types/src/components/ObjectForm/ObjectForm.d.ts +9 -0
  60. package/dist/types/src/components/ObjectForm/ObjectForm.d.ts.map +1 -0
  61. package/dist/types/src/components/ObjectForm/index.d.ts +2 -0
  62. package/dist/types/src/components/ObjectForm/index.d.ts.map +1 -0
  63. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +37 -10
  64. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -1
  65. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +64 -59
  66. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -1
  67. package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts +9 -0
  68. package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts.map +1 -0
  69. package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts +127 -0
  70. package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts.map +1 -0
  71. package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts +2 -0
  72. package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts.map +1 -0
  73. package/dist/types/src/components/ObjectProperties/index.d.ts +2 -0
  74. package/dist/types/src/components/ObjectProperties/index.d.ts.map +1 -0
  75. package/dist/types/src/components/Settings/Settings.d.ts +51 -0
  76. package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
  77. package/dist/types/src/components/Settings/Settings.stories.d.ts +84 -0
  78. package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
  79. package/dist/types/src/components/Settings/SettingsFieldSet.d.ts +43 -0
  80. package/dist/types/src/components/Settings/SettingsFieldSet.d.ts.map +1 -0
  81. package/dist/types/src/components/Settings/SettingsItem.d.ts +16 -0
  82. package/dist/types/src/components/Settings/SettingsItem.d.ts.map +1 -0
  83. package/dist/types/src/components/Settings/SettingsSelectField.d.ts +18 -0
  84. package/dist/types/src/components/Settings/SettingsSelectField.d.ts.map +1 -0
  85. package/dist/types/src/components/Settings/index.d.ts +4 -0
  86. package/dist/types/src/components/Settings/index.d.ts.map +1 -0
  87. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +12 -15
  88. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  89. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +70 -64
  90. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  91. package/dist/types/src/components/index.d.ts +3 -1
  92. package/dist/types/src/components/index.d.ts.map +1 -1
  93. package/dist/types/src/components/testing.d.ts +4 -6
  94. package/dist/types/src/components/testing.d.ts.map +1 -1
  95. package/dist/types/src/hooks/index.d.ts +2 -3
  96. package/dist/types/src/hooks/index.d.ts.map +1 -1
  97. package/dist/types/src/hooks/useFormHandler.d.ts +82 -0
  98. package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -0
  99. package/dist/types/src/hooks/useKeyHandler.d.ts +7 -0
  100. package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
  101. package/dist/types/src/index.d.ts +0 -2
  102. package/dist/types/src/index.d.ts.map +1 -1
  103. package/dist/types/src/translations.d.ts +65 -60
  104. package/dist/types/src/translations.d.ts.map +1 -1
  105. package/dist/types/src/util/field-type.d.ts +19 -0
  106. package/dist/types/src/util/field-type.d.ts.map +1 -0
  107. package/dist/types/src/util/field.d.ts +3 -3
  108. package/dist/types/src/util/field.d.ts.map +1 -1
  109. package/dist/types/src/util/formatting.d.ts +2 -2
  110. package/dist/types/src/util/formatting.d.ts.map +1 -1
  111. package/dist/types/src/util/index.d.ts +2 -1
  112. package/dist/types/src/util/index.d.ts.map +1 -1
  113. package/dist/types/src/util/properties.d.ts +7 -0
  114. package/dist/types/src/util/properties.d.ts.map +1 -0
  115. package/dist/types/src/util/refs.d.ts +2 -2
  116. package/dist/types/src/util/refs.d.ts.map +1 -1
  117. package/dist/types/tsconfig.tsbuildinfo +1 -1
  118. package/package.json +57 -51
  119. package/src/components/FieldEditor/FieldEditor.stories.tsx +44 -25
  120. package/src/components/FieldEditor/FieldEditor.test.tsx +0 -1
  121. package/src/components/FieldEditor/FieldEditor.tsx +76 -79
  122. package/src/components/Form/AUDIT.md +17 -0
  123. package/src/components/Form/Form.stories.tsx +139 -251
  124. package/src/components/Form/Form.tsx +375 -107
  125. package/src/components/Form/FormField.tsx +306 -0
  126. package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
  127. package/src/components/Form/FormFieldComponent.tsx +174 -0
  128. package/src/components/Form/FormFieldSet.tsx +144 -0
  129. package/src/components/Form/README.md +3 -0
  130. package/src/components/Form/fields/ArrayField.tsx +155 -0
  131. package/src/components/Form/fields/BooleanField.tsx +22 -0
  132. package/src/components/Form/fields/GeoPointField.tsx +121 -0
  133. package/src/components/Form/fields/MarkdownField.stories.tsx +206 -0
  134. package/src/components/Form/fields/MarkdownField.tsx +168 -0
  135. package/src/components/Form/fields/NumberField.tsx +39 -0
  136. package/src/components/Form/fields/RefField.tsx +229 -0
  137. package/src/components/Form/fields/SelectField.tsx +65 -0
  138. package/src/components/Form/{custom/SelectOptionsInput.stories.tsx → fields/SelectOptionField.stories.tsx} +23 -18
  139. package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +30 -24
  140. package/src/components/Form/fields/TextAreaField.tsx +38 -0
  141. package/src/components/Form/fields/TextField.tsx +38 -0
  142. package/src/components/Form/{custom/TupleInput.tsx → fields/TupleField.tsx} +16 -9
  143. package/src/components/Form/fields/index.ts +15 -0
  144. package/src/components/Form/index.ts +3 -4
  145. package/src/components/ObjectForm/ObjectForm.tsx +114 -0
  146. package/src/components/{ControlSection → ObjectForm}/index.ts +1 -1
  147. package/src/components/ObjectPicker/ObjectPicker.stories.tsx +30 -25
  148. package/src/components/ObjectPicker/ObjectPicker.tsx +122 -81
  149. package/src/components/ObjectProperties/ObjectProperties.stories.tsx +362 -0
  150. package/src/components/ObjectProperties/ObjectProperties.test.tsx +101 -0
  151. package/src/components/ObjectProperties/ObjectProperties.tsx +141 -0
  152. package/src/components/ObjectProperties/index.ts +5 -0
  153. package/src/components/Settings/AUDIT.md +52 -0
  154. package/src/components/Settings/Settings.stories.tsx +104 -0
  155. package/src/components/Settings/Settings.tsx +80 -0
  156. package/src/components/Settings/SettingsFieldSet.tsx +257 -0
  157. package/src/components/Settings/SettingsItem.tsx +58 -0
  158. package/src/components/Settings/SettingsSelectField.tsx +59 -0
  159. package/src/components/Settings/index.ts +7 -0
  160. package/src/components/ViewEditor/ViewEditor.stories.tsx +53 -50
  161. package/src/components/ViewEditor/ViewEditor.test.tsx +6 -6
  162. package/src/components/ViewEditor/ViewEditor.tsx +291 -208
  163. package/src/components/index.ts +3 -1
  164. package/src/components/testing.tsx +24 -11
  165. package/src/hooks/index.ts +2 -3
  166. package/src/hooks/useFormHandler.ts +380 -0
  167. package/src/hooks/useKeyHandler.tsx +37 -0
  168. package/src/index.ts +0 -4
  169. package/src/translations.ts +70 -65
  170. package/src/util/field-type.ts +60 -0
  171. package/src/util/field.ts +25 -25
  172. package/src/util/formatting.ts +22 -22
  173. package/src/util/index.ts +2 -1
  174. package/src/util/properties.ts +18 -0
  175. package/src/util/refs.ts +16 -18
  176. package/dist/types/src/components/ControlSection/ControlSection.d.ts +0 -23
  177. package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +0 -1
  178. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +0 -84
  179. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +0 -1
  180. package/dist/types/src/components/ControlSection/index.d.ts +0 -2
  181. package/dist/types/src/components/ControlSection/index.d.ts.map +0 -1
  182. package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
  183. package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
  184. package/dist/types/src/components/Form/Defaults.d.ts +0 -14
  185. package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
  186. package/dist/types/src/components/Form/FormActions.d.ts +0 -9
  187. package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
  188. package/dist/types/src/components/Form/FormContent.d.ts +0 -63
  189. package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
  190. package/dist/types/src/components/Form/FormContext.d.ts +0 -23
  191. package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
  192. package/dist/types/src/components/Form/Input.d.ts +0 -29
  193. package/dist/types/src/components/Form/Input.d.ts.map +0 -1
  194. package/dist/types/src/components/Form/RefField.d.ts +0 -22
  195. package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
  196. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
  197. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
  198. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
  199. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
  200. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -79
  201. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
  202. package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
  203. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
  204. package/dist/types/src/components/Form/custom/index.d.ts +0 -4
  205. package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
  206. package/dist/types/src/components/Form/factory.d.ts +0 -8
  207. package/dist/types/src/components/Form/factory.d.ts.map +0 -1
  208. package/dist/types/src/hooks/useForm.d.ts +0 -67
  209. package/dist/types/src/hooks/useForm.d.ts.map +0 -1
  210. package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -29
  211. package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
  212. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
  213. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
  214. package/dist/types/src/util/schema.d.ts +0 -3
  215. package/dist/types/src/util/schema.d.ts.map +0 -1
  216. package/src/components/ControlSection/ControlSection.stories.tsx +0 -38
  217. package/src/components/ControlSection/ControlSection.tsx +0 -153
  218. package/src/components/Form/ArrayField.tsx +0 -125
  219. package/src/components/Form/Defaults.tsx +0 -205
  220. package/src/components/Form/FormActions.tsx +0 -54
  221. package/src/components/Form/FormContent.tsx +0 -322
  222. package/src/components/Form/FormContext.tsx +0 -110
  223. package/src/components/Form/Input.tsx +0 -54
  224. package/src/components/Form/RefField.tsx +0 -283
  225. package/src/components/Form/custom/GeoPointInput.tsx +0 -80
  226. package/src/components/Form/custom/index.ts +0 -7
  227. package/src/components/Form/factory.tsx +0 -31
  228. package/src/hooks/useForm.ts +0 -296
  229. package/src/hooks/useQueryRefOptions.ts +0 -62
  230. package/src/hooks/useRefQueryLookupHandler.ts +0 -39
  231. package/src/util/schema.ts +0 -12
@@ -1,120 +1,388 @@
1
1
  //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { useFocusFinders } from '@fluentui/react-tabster';
6
- import type * as Schema from 'effect/Schema';
7
- import React, { type ReactElement, useCallback, useEffect, useRef } from 'react';
8
-
9
- import { type BaseObject, type PropertyKey } from '@dxos/echo/internal';
10
- import { type ThemedClassName } from '@dxos/react-ui';
11
- import { cardDialogOverflow, cardSpacing } from '@dxos/react-ui-stack';
12
- import { type ProjectionModel, type SchemaProperty } from '@dxos/schema';
13
-
14
- import { type FormHandler, type FormOptions } from '../../hooks';
15
-
16
- import { FormActions, type FormOuterSpacing } from './FormActions';
17
- import { FormFields, type FormFieldsProps } from './FormContent';
18
- import { FormProvider } from './FormContext';
19
- import { type InputComponent, type InputProps } from './Input';
20
-
21
- export type PropsFilter<T extends BaseObject> = (props: SchemaProperty<T>[]) => SchemaProperty<T>[];
22
-
23
- export type ComponentLookup = (args: {
24
- prop: string;
25
- schema: Schema.Schema<any>;
26
- inputProps: InputProps;
27
- }) => ReactElement | undefined;
28
-
29
- export type CustomInputMap = Partial<Record<string, InputComponent>>;
30
-
31
- export type FormProps<T extends BaseObject> = ThemedClassName<{
32
- values: Partial<T>;
33
- // TODO(burdon): Autofocus first input.
34
- autoFocus?: boolean;
35
- // TODO(burdon): Change to JsonPath includes/excludes.
36
- filter?: PropsFilter<T>;
37
- sort?: PropertyKey<T>[];
38
- projection?: ProjectionModel;
39
- autoSave?: boolean;
40
- outerSpacing?: FormOuterSpacing;
41
- onCancel?: () => void;
42
- }> &
43
- Pick<FormOptions<T>, 'schema' | 'onValuesChanged' | 'onValidate' | 'onSave'> &
44
- // TODO(wittjosiah): This needs to support different ref field options per field.
45
- FormFieldsProps;
46
-
47
- export const Form = <T extends BaseObject>({
48
- classNames,
49
- testId,
50
- values: initialValues,
51
- autoFocus,
52
- readonly,
53
- autoSave,
54
- outerSpacing = true,
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { createContext } from '@radix-ui/react-context';
6
+ import * as Option from 'effect/Option';
7
+ import * as Schema from 'effect/Schema';
8
+ import * as SchemaAST from 'effect/SchemaAST';
9
+ import React, { type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
10
+
11
+ import { Annotation as EchoAnnotation } from '@dxos/echo';
12
+ import { type AnyProperties } from '@dxos/echo/internal';
13
+ import { createJsonPath, getValue as getValue$ } from '@dxos/effect';
14
+ import {
15
+ IconButton,
16
+ type IconButtonProps,
17
+ ScrollArea,
18
+ type ThemedClassName,
19
+ useMergeRefs,
20
+ useTranslation,
21
+ } from '@dxos/react-ui';
22
+ import { composable, composableProps, mx, withColumn } from '@dxos/ui-theme';
23
+
24
+ import { translationKey } from '#translations';
25
+
26
+ import {
27
+ type FormHandler,
28
+ type FormHandlerProps,
29
+ type FormUpdateMeta,
30
+ useFormHandler,
31
+ useKeyHandler,
32
+ } from '../../hooks';
33
+ import { FormFieldLabel, type FormFieldLabelProps, type FormFieldStateProps } from './FormFieldComponent';
34
+ import {
35
+ FormFieldSet as NaturalFormFieldSet,
36
+ type FormFieldSetProps as NaturalFormFieldSetProps,
37
+ } from './FormFieldSet';
38
+
39
+ // TODO(burdon): Move styles to form.ts (as with ui-theme).
40
+
41
+ // TODO(burdon): Reconcile with @dxos/echo.
42
+ export type ExcludeId<S extends Schema.Schema.AnyNoContext> = Omit<Schema.Schema.Type<S>, 'id'>;
43
+
44
+ // TODO(burdon): Move to @dxos/schema (re-export here).
45
+ export const omitId = <S extends Schema.Schema.AnyNoContext>(schema: S): Schema.Schema<ExcludeId<S>, ExcludeId<S>> =>
46
+ schema.pipe(Schema.omit('id')) as any;
47
+
48
+ /**
49
+ * Drop fields annotated with `FormInputAnnotation.set(false)` from a schema so
50
+ * the form's validator doesn't trip on required-but-hidden fields. Used by
51
+ * the picker's inline create form, where a `FactoryAnnotation` typically
52
+ * supplies the hidden values (e.g. a backing-object Ref) outside the form.
53
+ */
54
+ export const omitHiddenFormFields = <S extends Schema.Schema.AnyNoContext>(schema: S): S => {
55
+ const properties = SchemaAST.getPropertySignatures(schema.ast);
56
+ const hidden = properties
57
+ .filter((prop) => Option.getOrElse(EchoAnnotation.FormInputAnnotation.getFromAst(prop.type), () => true) === false)
58
+ .map((prop) => prop.name as string);
59
+ return hidden.length === 0 ? schema : (schema.pipe(Schema.omit(...(hidden as [string, ...string[]]))) as any);
60
+ };
61
+
62
+ //
63
+ // Context
64
+ //
65
+
66
+ type FormContextValue<T extends AnyProperties = any> = {
67
+ /**
68
+ * Form handler.
69
+ */
70
+ form: FormHandler<T>;
71
+
72
+ /**
73
+ * Show debug info.
74
+ */
75
+ debug?: boolean;
76
+
77
+ /**
78
+ * Testing.
79
+ */
80
+ testId?: string;
81
+ } & Pick<
82
+ NaturalFormFieldSetProps<T>,
83
+ 'readonly' | 'layout' | 'fieldMap' | 'fieldProvider' | 'projection' | 'createTypename' | 'createFieldMap' | 'db'
84
+ >;
85
+
86
+ const [FormContextProvider, useFormContext] = createContext<FormContextValue>('Form');
87
+
88
+ /**
89
+ * Get the current form values.
90
+ */
91
+ const useFormValues: {
92
+ <T extends AnyProperties>(componentName: string, path?: (string | number)[]): T;
93
+ <T extends AnyProperties>(
94
+ componentName: string,
95
+ path: (string | number)[] | undefined,
96
+ defaultValue: () => T,
97
+ ): T | undefined;
98
+ } = (componentName: string, path?: (string | number)[], defaultValue?: () => any) => {
99
+ const stablePath = useMemo(() => path ?? [], [path ? path.join('.') : undefined]);
100
+ const jsonPath = createJsonPath(stablePath);
101
+ const {
102
+ form: { values, onValueChange },
103
+ } = useFormContext(componentName);
104
+
105
+ const value = getValue$(values, jsonPath);
106
+
107
+ // Apply default value once when the field has no value. lastAppliedPathRef prevents
108
+ // re-applying on every render (e.g. when defaultValue() returns null) and ensures
109
+ // we apply per path when the hook is used for different fields.
110
+ const lastAppliedPathRef = useRef<string | null>(null);
111
+ useEffect(() => {
112
+ if (value == null && defaultValue && lastAppliedPathRef.current !== jsonPath) {
113
+ lastAppliedPathRef.current = jsonPath;
114
+ onValueChange(stablePath, SchemaAST.stringKeyword, defaultValue());
115
+ }
116
+ }, [value, defaultValue, onValueChange, stablePath, jsonPath]);
117
+
118
+ return value;
119
+ };
120
+
121
+ /**
122
+ * Get the state props for the given field.
123
+ */
124
+ const useFormFieldState = (componentName: string, path: (string | number)[] = []): FormFieldStateProps => {
125
+ const stablePath = useMemo(() => path, [Array.isArray(path) ? path.join('.') : path]);
126
+ const {
127
+ form: { getStatus, getValue, onBlur, onValueChange },
128
+ } = useFormContext(componentName);
129
+
130
+ return useMemo(
131
+ () => ({
132
+ getStatus: () => getStatus(stablePath),
133
+ getValue: () => getValue(stablePath),
134
+ onBlur: () => onBlur(stablePath),
135
+ onValueChange: (ast: SchemaAST.AST, value: any) => onValueChange(stablePath, ast, value),
136
+ }),
137
+ [getStatus, getValue, onBlur, onValueChange, stablePath],
138
+ );
139
+ };
140
+
141
+ //
142
+ // Root
143
+ //
144
+
145
+ type FormRootProps<T extends AnyProperties = AnyProperties> = PropsWithChildren<
146
+ {
147
+ /**
148
+ * Called when the form is submitted and passes validation.
149
+ */
150
+ onSave?: (values: T, meta: FormUpdateMeta<T>) => void;
151
+
152
+ /**
153
+ * Called when the form is canceled to abandon/undo any pending changes.
154
+ */
155
+ onCancel?: () => void;
156
+ } & Omit<FormContextValue<T>, 'form'> &
157
+ Pick<FormHandlerProps<T>, 'schema' | 'autoSave' | 'values' | 'defaultValues' | 'onValidate' | 'onValuesChanged'> &
158
+ Omit<NaturalFormFieldSetProps<T>, 'schema' | 'path'>
159
+ >;
160
+
161
+ const FormRoot = <T extends AnyProperties = AnyProperties>({
162
+ children,
55
163
  schema,
56
- onCancel,
57
- onValuesChanged,
58
- onValidate,
164
+ values,
59
165
  onSave,
166
+ onCancel,
60
167
  ...props
61
- }: FormProps<T>) => {
62
- const formRef = useRef<HTMLDivElement>(null);
168
+ }: FormRootProps<T>) => {
169
+ const form = useFormHandler({ schema, values, onSave, onCancel, ...props });
63
170
 
64
- // Focus the first focusable element within this form.
65
- const { findFirstFocusable } = useFocusFinders();
66
- useEffect(() => {
67
- if (autoFocus && formRef.current) {
68
- const firstFocusable = findFirstFocusable(formRef.current);
69
- if (firstFocusable) {
70
- firstFocusable.focus();
71
- }
72
- }
73
- }, [autoFocus]);
74
-
75
- // TODO(burdon): Name?
76
- const handleValid = useCallback(
77
- async (values: any, meta: { changed: FormHandler<T>['changed'] }) => {
78
- if (autoSave) {
79
- await onSave?.(values, meta);
80
- }
81
- },
82
- [autoSave, onSave],
171
+ return (
172
+ <FormContextProvider form={form} {...props}>
173
+ {children}
174
+ </FormContextProvider>
83
175
  );
176
+ };
177
+
178
+ FormRoot.displayName = 'Form.Root';
84
179
 
180
+ //
181
+ // Viewport
182
+ //
183
+
184
+ const FORM_VIEWPORT_NAME = 'Form.Viewport';
185
+
186
+ type FormViewportProps = {};
187
+
188
+ const FormViewport = composable<HTMLDivElement>(({ children, ...props }, forwardedRef) => {
85
189
  return (
86
- <FormProvider
87
- formRef={formRef}
88
- schema={schema}
89
- autoSave={autoSave}
90
- initialValues={initialValues}
91
- onValuesChanged={onValuesChanged}
92
- onValidate={onValidate}
93
- onValid={handleValid}
94
- onSave={onSave}
190
+ <ScrollArea.Root {...composableProps(props)} orientation='vertical' centered padding thin ref={forwardedRef}>
191
+ <ScrollArea.Viewport>{children}</ScrollArea.Viewport>
192
+ </ScrollArea.Root>
193
+ );
194
+ });
195
+
196
+ FormViewport.displayName = FORM_VIEWPORT_NAME;
197
+
198
+ //
199
+ // Content
200
+ //
201
+
202
+ const FORM_CONTENT_NAME = 'Form.Content';
203
+
204
+ type FormContentProps = ThemedClassName<PropsWithChildren<{}>>;
205
+
206
+ const FormContent = composable<HTMLDivElement, FormContentProps>(({ children, ...props }, forwardedRef) => {
207
+ const { form, testId } = useFormContext(FORM_CONTENT_NAME);
208
+ const localRef = useRef<HTMLDivElement>(null);
209
+ const mergedRef = useMergeRefs([forwardedRef, localRef]);
210
+ useKeyHandler(localRef, form);
211
+
212
+ return (
213
+ <div
214
+ {...composableProps(props, {
215
+ role: 'form',
216
+ classNames: mx(withColumn.center(), 'flex flex-col w-full pb-form-gap'),
217
+ })}
218
+ data-testid={testId}
219
+ ref={mergedRef}
95
220
  >
96
- <div role='none' className='contents' data-testid={testId}>
97
- <FormFields
98
- {...props}
99
- ref={formRef}
100
- classNames={[
101
- outerSpacing === 'blockStart-0'
102
- ? 'pli-cardSpacingInline mbe-cardSpacingBlock [&>.mbs-inputSpacingBlock:first-child]:!mbs-0'
103
- : outerSpacing === 'scroll-fields'
104
- ? 'pli-cardSpacingInline pbe-cardSpacingBlock'
105
- : outerSpacing
106
- ? cardSpacing
107
- : false,
108
- outerSpacing === 'scroll-fields' && cardDialogOverflow,
109
- classNames,
110
- ]}
111
- readonly={readonly}
112
- schema={schema}
221
+ {children}
222
+ </div>
223
+ );
224
+ });
225
+
226
+ FormContent.displayName = FORM_CONTENT_NAME;
227
+
228
+ //
229
+ // FieldSet
230
+ //
231
+
232
+ const FORM_FIELDSET_NAME = 'Form.FieldSet';
233
+
234
+ type FormFieldSetProps = ThemedClassName<NaturalFormFieldSetProps<any>>;
235
+
236
+ const FormFieldSet = (props: FormFieldSetProps) => {
237
+ const { form, ...contextProps } = useFormContext(FORM_FIELDSET_NAME);
238
+
239
+ return <NaturalFormFieldSet schema={form.schema} {...contextProps} {...props} />;
240
+ };
241
+
242
+ FormFieldSet.displayName = FORM_FIELDSET_NAME;
243
+
244
+ //
245
+ // Actions
246
+ //
247
+
248
+ const FORM_ACTIONS_NAME = 'Form.Actions';
249
+
250
+ type FormActionsProps = ThemedClassName<{}>;
251
+
252
+ const FormActions = ({ classNames }: FormActionsProps) => {
253
+ const { t } = useTranslation(translationKey);
254
+ const {
255
+ form: { canSave, onSave, onCancel },
256
+ readonly,
257
+ layout,
258
+ } = useFormContext(FORM_ACTIONS_NAME);
259
+
260
+ if (readonly || layout === 'static') {
261
+ return null;
262
+ }
263
+
264
+ // TODO(burdon): Currently onCancel is a no-op; implement "revert values".
265
+ // Deprecate FormSubmit ans use FormActions without Cancel button if no callback is supplied.
266
+
267
+ return (
268
+ <div
269
+ className={mx(withColumn.center(), 'grid grid-flow-col gap-form-gap auto-cols-fr py-form-padding', classNames)}
270
+ >
271
+ {onCancel && (
272
+ <IconButton
273
+ icon='ph--x--regular'
274
+ iconEnd
275
+ label={t('cancel-button.label')}
276
+ onClick={onCancel}
277
+ data-testid='cancel-button'
113
278
  />
114
- {(onCancel || onSave) && !autoSave && !readonly && (
115
- <FormActions readonly={readonly} onCancel={onCancel} outerSpacing={outerSpacing} />
116
- )}
279
+ )}
280
+ {onSave && (
281
+ <IconButton
282
+ type='submit'
283
+ variant='primary'
284
+ disabled={!canSave}
285
+ icon='ph--check--regular'
286
+ iconEnd
287
+ label={t('save-button.label')}
288
+ onClick={onSave}
289
+ data-testid='save-button'
290
+ />
291
+ )}
292
+ </div>
293
+ );
294
+ };
295
+
296
+ FormActions.displayName = FORM_ACTIONS_NAME;
297
+
298
+ //
299
+ // Section
300
+ //
301
+
302
+ const FORM_SECTION_NAME = 'Form.Section';
303
+
304
+ type FormSectionProps = ThemedClassName<{ label: string; description?: string }>;
305
+
306
+ const FormSection = composable<HTMLDivElement, FormSectionProps>(
307
+ ({ children, label, description, ...props }, forwardedRef) => {
308
+ return (
309
+ <div
310
+ {...composableProps(props, { classNames: 'flex flex-col pt-form-section-gap first:pt-0' })}
311
+ ref={forwardedRef}
312
+ >
313
+ <h2 className='text-lg'>{label}</h2>
314
+ {description && <p className='text-description'>{description}</p>}
315
+ {children}
117
316
  </div>
118
- </FormProvider>
317
+ );
318
+ },
319
+ );
320
+
321
+ FormSection.displayName = FORM_SECTION_NAME;
322
+
323
+ //
324
+ // Submit
325
+ //
326
+
327
+ const FORM_SUBMIT_NAME = 'Form.Submit';
328
+
329
+ type FormSubmitProps = ThemedClassName<Partial<Pick<IconButtonProps, 'icon' | 'label' | 'disabled'>>>;
330
+
331
+ const FormSubmit = ({ classNames, label, icon, disabled }: FormSubmitProps) => {
332
+ const { t } = useTranslation(translationKey);
333
+ const {
334
+ form: { canSave, onSave },
335
+ readonly,
336
+ layout,
337
+ } = useFormContext(FORM_SUBMIT_NAME);
338
+
339
+ if (readonly || layout === 'static') {
340
+ return null;
341
+ }
342
+
343
+ return (
344
+ <div className={mx('flex w-full pt-form-padding', classNames)}>
345
+ <IconButton
346
+ classNames='w-full'
347
+ type='submit'
348
+ variant='primary'
349
+ disabled={disabled ?? !canSave}
350
+ icon={icon ?? 'ph--check--regular'}
351
+ label={label ?? t('save-button.label')}
352
+ onClick={onSave}
353
+ data-testid='save-button'
354
+ />
355
+ </div>
119
356
  );
120
357
  };
358
+
359
+ FormSubmit.displayName = FORM_SUBMIT_NAME;
360
+
361
+ //
362
+ // Form
363
+ // https://www.radix-ui.com/primitives/docs/guides/composition
364
+ //
365
+
366
+ export const Form = {
367
+ Root: FormRoot,
368
+ Viewport: FormViewport,
369
+ Content: FormContent,
370
+ Section: FormSection,
371
+ FieldSet: FormFieldSet,
372
+ Label: FormFieldLabel,
373
+ Actions: FormActions,
374
+ Submit: FormSubmit,
375
+ };
376
+
377
+ export { useFormContext, useFormValues, useFormFieldState };
378
+
379
+ export type {
380
+ FormRootProps,
381
+ FormViewportProps,
382
+ FormContentProps,
383
+ FormSectionProps,
384
+ FormFieldSetProps,
385
+ FormFieldLabelProps,
386
+ FormActionsProps,
387
+ FormSubmitProps,
388
+ };