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

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 (197) hide show
  1. package/dist/lib/browser/index.mjs +2220 -2058
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +2220 -2058
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/components/ControlSection/ControlSection.d.ts +4 -4
  8. package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +1 -1
  9. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +84 -5
  10. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +1 -1
  11. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +6 -5
  12. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  13. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +82 -5
  14. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
  15. package/dist/types/src/components/Form/Form-old.stories.d.ts +131 -0
  16. package/dist/types/src/components/Form/Form-old.stories.d.ts.map +1 -0
  17. package/dist/types/src/components/Form/Form.d.ts +81 -25
  18. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  19. package/dist/types/src/components/Form/Form.stories.d.ts +127 -55
  20. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  21. package/dist/types/src/components/Form/FormField.d.ts +37 -0
  22. package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
  23. package/dist/types/src/components/Form/FormFieldComponent.d.ts +78 -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 +19 -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 +4 -0
  36. package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
  37. package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
  38. package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
  39. package/dist/types/src/components/Form/fields/RefField.d.ts +14 -0
  40. package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
  41. package/dist/types/src/components/Form/fields/SelectField.d.ts +10 -0
  42. package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
  43. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
  44. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
  45. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +82 -0
  46. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
  47. package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
  48. package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
  49. package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
  50. package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
  51. package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
  52. package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
  53. package/dist/types/src/components/Form/fields/index.d.ts +12 -0
  54. package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
  55. package/dist/types/src/components/Form/index.d.ts +2 -4
  56. package/dist/types/src/components/Form/index.d.ts.map +1 -1
  57. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +46 -0
  58. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
  59. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +85 -0
  60. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -0
  61. package/dist/types/src/components/ObjectPicker/index.d.ts +2 -0
  62. package/dist/types/src/components/ObjectPicker/index.d.ts.map +1 -0
  63. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +21 -9
  64. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  65. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +83 -8
  66. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  67. package/dist/types/src/components/index.d.ts +1 -1
  68. package/dist/types/src/components/index.d.ts.map +1 -1
  69. package/dist/types/src/components/testing.d.ts +4 -3
  70. package/dist/types/src/components/testing.d.ts.map +1 -1
  71. package/dist/types/src/hooks/index.d.ts +2 -3
  72. package/dist/types/src/hooks/index.d.ts.map +1 -1
  73. package/dist/types/src/hooks/useFormHandler.d.ts +82 -0
  74. package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -0
  75. package/dist/types/src/hooks/useKeyHandler.d.ts +6 -0
  76. package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
  77. package/dist/types/src/translations.d.ts +19 -10
  78. package/dist/types/src/translations.d.ts.map +1 -1
  79. package/dist/types/src/util/echo.d.ts +6 -0
  80. package/dist/types/src/util/echo.d.ts.map +1 -0
  81. package/dist/types/src/util/field.d.ts +3 -3
  82. package/dist/types/src/util/field.d.ts.map +1 -1
  83. package/dist/types/src/util/formatting.d.ts +2 -2
  84. package/dist/types/src/util/formatting.d.ts.map +1 -1
  85. package/dist/types/src/util/index.d.ts +2 -1
  86. package/dist/types/src/util/index.d.ts.map +1 -1
  87. package/dist/types/src/util/properties.d.ts +7 -0
  88. package/dist/types/src/util/properties.d.ts.map +1 -0
  89. package/dist/types/src/util/refs.d.ts +2 -3
  90. package/dist/types/src/util/refs.d.ts.map +1 -1
  91. package/dist/types/tsconfig.tsbuildinfo +1 -1
  92. package/package.json +51 -40
  93. package/src/components/ControlSection/ControlSection.stories.tsx +11 -14
  94. package/src/components/ControlSection/ControlSection.tsx +30 -16
  95. package/src/components/FieldEditor/FieldEditor.stories.tsx +52 -31
  96. package/src/components/FieldEditor/FieldEditor.test.tsx +3 -2
  97. package/src/components/FieldEditor/FieldEditor.tsx +86 -81
  98. package/src/components/Form/Form-old.stories.tsx +314 -0
  99. package/src/components/Form/Form.stories.tsx +155 -219
  100. package/src/components/Form/Form.tsx +320 -96
  101. package/src/components/Form/FormField.tsx +286 -0
  102. package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
  103. package/src/components/Form/FormFieldComponent.tsx +174 -0
  104. package/src/components/Form/FormFieldSet.tsx +125 -0
  105. package/src/components/Form/README.md +3 -0
  106. package/src/components/Form/fields/ArrayField.tsx +151 -0
  107. package/src/components/Form/fields/BooleanField.tsx +22 -0
  108. package/src/components/Form/fields/GeoPointField.tsx +120 -0
  109. package/src/components/Form/fields/MarkdownField.tsx +78 -0
  110. package/src/components/Form/fields/NumberField.tsx +39 -0
  111. package/src/components/Form/fields/RefField.tsx +181 -0
  112. package/src/components/Form/fields/SelectField.tsx +65 -0
  113. package/src/components/Form/{custom/SelectOptionsInput.stories.tsx → fields/SelectOptionField.stories.tsx} +25 -22
  114. package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +47 -42
  115. package/src/components/Form/fields/TextAreaField.tsx +38 -0
  116. package/src/components/Form/fields/TextField.tsx +38 -0
  117. package/src/components/Form/{custom/TupleInput.tsx → fields/TupleField.tsx} +16 -9
  118. package/src/components/Form/fields/index.ts +15 -0
  119. package/src/components/Form/index.ts +3 -4
  120. package/src/components/ObjectPicker/ObjectPicker.stories.tsx +214 -0
  121. package/src/components/ObjectPicker/ObjectPicker.tsx +175 -0
  122. package/src/components/ObjectPicker/index.ts +5 -0
  123. package/src/components/ViewEditor/ViewEditor.stories.tsx +137 -61
  124. package/src/components/ViewEditor/ViewEditor.test.tsx +19 -17
  125. package/src/components/ViewEditor/ViewEditor.tsx +324 -203
  126. package/src/components/index.ts +1 -2
  127. package/src/components/testing.tsx +19 -11
  128. package/src/hooks/index.ts +2 -3
  129. package/src/hooks/useFormHandler.ts +362 -0
  130. package/src/hooks/useKeyHandler.tsx +37 -0
  131. package/src/translations.ts +21 -12
  132. package/src/util/echo.ts +25 -0
  133. package/src/util/field.ts +33 -43
  134. package/src/util/formatting.ts +22 -22
  135. package/src/util/index.ts +2 -1
  136. package/src/util/properties.ts +18 -0
  137. package/src/util/refs.ts +12 -18
  138. package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
  139. package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
  140. package/dist/types/src/components/Form/Defaults.d.ts +0 -14
  141. package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
  142. package/dist/types/src/components/Form/FormActions.d.ts +0 -9
  143. package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
  144. package/dist/types/src/components/Form/FormContent.d.ts +0 -57
  145. package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
  146. package/dist/types/src/components/Form/FormContext.d.ts +0 -23
  147. package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
  148. package/dist/types/src/components/Form/Input.d.ts +0 -26
  149. package/dist/types/src/components/Form/Input.d.ts.map +0 -1
  150. package/dist/types/src/components/Form/RefField.d.ts +0 -12
  151. package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
  152. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
  153. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
  154. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
  155. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
  156. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -7
  157. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
  158. package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
  159. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
  160. package/dist/types/src/components/Form/custom/index.d.ts +0 -4
  161. package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
  162. package/dist/types/src/components/Form/factory.d.ts +0 -8
  163. package/dist/types/src/components/Form/factory.d.ts.map +0 -1
  164. package/dist/types/src/components/deprecated/Form.d.ts +0 -15
  165. package/dist/types/src/components/deprecated/Form.d.ts.map +0 -1
  166. package/dist/types/src/components/deprecated/Form.stories.d.ts +0 -13
  167. package/dist/types/src/components/deprecated/Form.stories.d.ts.map +0 -1
  168. package/dist/types/src/components/deprecated/FormInput.d.ts +0 -16
  169. package/dist/types/src/components/deprecated/FormInput.d.ts.map +0 -1
  170. package/dist/types/src/components/deprecated/index.d.ts +0 -2
  171. package/dist/types/src/components/deprecated/index.d.ts.map +0 -1
  172. package/dist/types/src/hooks/useForm.d.ts +0 -67
  173. package/dist/types/src/hooks/useForm.d.ts.map +0 -1
  174. package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -23
  175. package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
  176. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
  177. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
  178. package/dist/types/src/util/schema.d.ts +0 -3
  179. package/dist/types/src/util/schema.d.ts.map +0 -1
  180. package/src/components/Form/ArrayField.tsx +0 -124
  181. package/src/components/Form/Defaults.tsx +0 -203
  182. package/src/components/Form/FormActions.tsx +0 -53
  183. package/src/components/Form/FormContent.tsx +0 -240
  184. package/src/components/Form/FormContext.tsx +0 -110
  185. package/src/components/Form/Input.tsx +0 -51
  186. package/src/components/Form/RefField.tsx +0 -213
  187. package/src/components/Form/custom/GeoPointInput.tsx +0 -72
  188. package/src/components/Form/custom/index.ts +0 -7
  189. package/src/components/Form/factory.tsx +0 -31
  190. package/src/components/deprecated/Form.stories.tsx +0 -62
  191. package/src/components/deprecated/Form.tsx +0 -119
  192. package/src/components/deprecated/FormInput.tsx +0 -71
  193. package/src/components/deprecated/index.ts +0 -5
  194. package/src/hooks/useForm.ts +0 -291
  195. package/src/hooks/useQueryRefOptions.ts +0 -53
  196. package/src/hooks/useRefQueryLookupHandler.ts +0 -39
  197. package/src/util/schema.ts +0 -14
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
 
3
3
  // src/translations.ts
4
- var translationKey = "react-ui-form";
4
+ var translationKey = "@dxos/react-ui-form";
5
5
  var translations = [
6
6
  {
7
7
  "en-US": {
@@ -10,7 +10,7 @@ var translations = [
10
10
  "hidden fields label": "Hidden Fields",
11
11
  "empty readonly ref field label": "(none)",
12
12
  // TODO(burdon): Standardize field/property.
13
- "button add property": "Add property",
13
+ "add property button label": "Add property",
14
14
  "field property label": "Property",
15
15
  "field property placeholder": "Property name",
16
16
  "field format label": "Type",
@@ -20,9 +20,17 @@ var translations = [
20
20
  "field limit reached": "Maximum number of fields reached",
21
21
  "boolean input true value": "Yes",
22
22
  "boolean input false value": "No",
23
- // TODO(burdon): Factor out?
24
- "button cancel": "Cancel",
25
- "button save": "Save",
23
+ "show field label": "Show field",
24
+ "hide field label": "Hide field",
25
+ "delete field label": "Delete field",
26
+ "ref field combobox input placeholder": "Search\u2026",
27
+ "ref field placeholder_one": "Select\u2026",
28
+ "ref field placeholder_other": "Select items\u2026",
29
+ "example placeholder": "Example",
30
+ "latitude placeholder": "Latitude (e.g., 40.7128)",
31
+ "longitude placeholder": "Longitude (e.g., -74.0060)",
32
+ "cancel button label": "Cancel",
33
+ "save button label": "Save",
26
34
  // FormatEnum
27
35
  "format boolean": "Boolean",
28
36
  "format currency": "Currency",
@@ -36,7 +44,7 @@ var translations = [
36
44
  "format hostname": "Hostname",
37
45
  "format integer": "Integer",
38
46
  "format json": "JSON",
39
- "format latlng": "Geopoint",
47
+ "format lnglat": "Geopoint",
40
48
  "format markdown": "Markdown",
41
49
  "format percent": "Percent",
42
50
  "format ref": "Reference",
@@ -52,756 +60,481 @@ var translations = [
52
60
  "format uri": "URI",
53
61
  "format url": "URL",
54
62
  "format uuid": "UUID",
55
- // Placeholders.
56
- "placeholder latitude": "Latitude",
57
- "placeholder longitude": "Longitude",
58
- // SelectOptionsInput.
63
+ // GeoPointField.
64
+ "latitude label": "Latitude",
65
+ "longitude label": "Longitude",
66
+ // SelectOptionsField.
59
67
  "select option label": "Label",
60
68
  "select option label placeholder": "Option label",
61
69
  "select option color": "Color",
62
70
  "select option delete": "Delete",
63
71
  "select option add": "Add option",
64
72
  // System schema message.
65
- "system schema title": "System Schema",
66
- "system schema description": "This is a system schema and cannot be modified."
73
+ "system schema title": "System type",
74
+ "system schema description": "System type: cannot add or remove fields."
67
75
  }
68
76
  }
69
77
  }
70
78
  ];
71
79
 
72
- // src/components/deprecated/FormInput.tsx
73
- import { useSignals as _useSignals } from "@preact-signals/safe-react/tracking";
74
- import React from "react";
75
- import { Input } from "@dxos/react-ui";
76
- import { mx, textBlockWidth } from "@dxos/react-ui-theme";
77
- var DeprecatedFormContainer = ({ children, classNames }) => {
78
- var _effect = _useSignals();
79
- try {
80
- return /* @__PURE__ */ React.createElement("div", {
81
- role: "none",
82
- className: "container-max-width p-4 justify-center overflow-x-hidden overflow-y-auto"
83
- }, /* @__PURE__ */ React.createElement("div", {
84
- role: "form",
85
- className: mx("flex flex-col", textBlockWidth, classNames)
86
- }, children));
87
- } finally {
88
- _effect.f();
89
- }
90
- };
91
- var DeprecatedFormInput = ({ label, description, secondary, children }) => {
92
- var _effect = _useSignals();
93
- try {
94
- const primary = /* @__PURE__ */ React.createElement("div", {
95
- role: "none",
96
- className: "flex w-full gap-4 py-1"
97
- }, /* @__PURE__ */ React.createElement(Input.Root, null, /* @__PURE__ */ React.createElement("div", {
98
- role: "none",
99
- className: "flex flex-col w-full"
100
- }, /* @__PURE__ */ React.createElement(Input.Label, {
101
- classNames: "flex min-bs-[--rail-action] items-center",
102
- style: {
103
- fontSize: "unset"
104
- }
105
- }, label), description && /* @__PURE__ */ React.createElement(Input.DescriptionAndValidation, {
106
- classNames: "mbs-0.5"
107
- }, /* @__PURE__ */ React.createElement(Input.Description, null, description))), /* @__PURE__ */ React.createElement("div", {
108
- role: "none"
109
- }, /* @__PURE__ */ React.createElement("div", {
110
- role: "none",
111
- className: "flex min-bs-[--rail-action] items-center"
112
- }, children))));
113
- if (secondary) {
114
- return /* @__PURE__ */ React.createElement("div", {
115
- role: "none"
116
- }, primary, secondary);
117
- }
118
- return primary;
119
- } finally {
120
- _effect.f();
121
- }
122
- };
123
-
124
80
  // src/components/ControlSection/ControlSection.tsx
125
- import { useSignals as _useSignals2 } from "@preact-signals/safe-react/tracking";
126
- import React2 from "react";
127
- import { Button, Input as Input2, toLocalizedString, useTranslation } from "@dxos/react-ui";
128
- import { mx as mx2 } from "@dxos/react-ui-theme";
81
+ import React from "react";
82
+ import { Button, Input, toLocalizedString, useTranslation } from "@dxos/react-ui";
83
+ import { Layout } from "@dxos/react-ui-mosaic";
84
+ import { mx } from "@dxos/ui-theme";
129
85
  var ControlPage = ({ children, classNames, ...props }) => {
130
- var _effect = _useSignals2();
131
- try {
132
- return /* @__PURE__ */ React2.createElement("div", {
133
- role: "none",
134
- className: mx2("pli-cardSpacingInline", classNames),
135
- ...props
136
- }, children);
137
- } finally {
138
- _effect.f();
139
- }
86
+ return /* @__PURE__ */ React.createElement(Layout.Container, {
87
+ scrollable: true,
88
+ classNames: "[--control-spacing:var(--dx-trimMd)]"
89
+ }, /* @__PURE__ */ React.createElement("div", {
90
+ role: "none",
91
+ className: mx("pli-cardSpacingInline pbe-trimMd", classNames),
92
+ ...props
93
+ }, children));
140
94
  };
141
95
  var ControlSection = ({ title, description, children }) => {
142
- var _effect = _useSignals2();
143
- try {
144
- return /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(ControlSectionHeading, {
145
- title,
146
- description
147
- }), children);
148
- } finally {
149
- _effect.f();
150
- }
96
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(ControlSectionHeading, {
97
+ title,
98
+ description
99
+ }), children);
151
100
  };
152
101
  var ControlSectionHeading = ({ title, description }) => {
153
- var _effect = _useSignals2();
154
- try {
155
- const { t } = useTranslation(translationKey);
156
- return /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement("h2", {
157
- className: "pli-trimMd container-max-width text-xl mbs-trimLg mbe-trimMd"
158
- }, toLocalizedString(title, t)), description && /* @__PURE__ */ React2.createElement("p", {
159
- className: "pli-trimMd mlb-trimMd container-max-width text-description"
160
- }, toLocalizedString(description, t)));
161
- } finally {
162
- _effect.f();
163
- }
102
+ const { t } = useTranslation(translationKey);
103
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("h2", {
104
+ className: "pli-trimMd container-max-width text-xl mbs-trimMd mbe-trimMd"
105
+ }, toLocalizedString(title, t)), description && /* @__PURE__ */ React.createElement("p", {
106
+ className: "pli-trimMd mlb-trimMd container-max-width text-description"
107
+ }, toLocalizedString(description, t)));
164
108
  };
165
109
  var ControlGroupButton = ({ classNames, ...props }) => {
166
- var _effect = _useSignals2();
167
- try {
168
- return /* @__PURE__ */ React2.createElement(Button, {
169
- ...props,
170
- classNames: [
171
- "md:col-span-2",
172
- classNames
173
- ]
174
- });
175
- } finally {
176
- _effect.f();
177
- }
178
- };
179
- var ControlGroup = ({ children, classNames }) => {
180
- var _effect = _useSignals2();
181
- try {
182
- return /* @__PURE__ */ React2.createElement("div", {
183
- role: "none",
184
- className: mx2("group container-max-width grid grid-cols-1 md:grid-cols-[1fr_min-content] gap-trimMd", classNames)
185
- }, children);
186
- } finally {
187
- _effect.f();
188
- }
189
- };
190
- var ControlFrame = ({ children }) => {
191
- var _effect = _useSignals2();
192
- try {
193
- return /* @__PURE__ */ React2.createElement("div", {
194
- role: "none",
195
- className: "p-trimMd container-max-width grid grid-cols-1 md:grid-cols-[1fr_1fr] gap-trimSm md:gap-trimMd border border-separator rounded-md"
196
- }, children);
197
- } finally {
198
- _effect.f();
199
- }
110
+ return /* @__PURE__ */ React.createElement(Button, {
111
+ ...props,
112
+ classNames: [
113
+ "md:col-span-2",
114
+ classNames
115
+ ]
116
+ });
200
117
  };
201
- var controlItemClasses = "pli-trimMd plb-trimMd container-max-width grid md:col-span-2 grid-cols-subgrid items-center gap-trimSm border border-separator rounded-md *:first:!mbs-0 *:last:!mbe-0";
202
- var controlItemTitleClasses = "text-lg font-normal";
203
- var controlItemDescriptionClasses = "text-base mlb-trimSm md:mbe-0 text-description";
118
+ var ControlGroup = ({ children, classNames }) => /* @__PURE__ */ React.createElement("div", {
119
+ role: "none",
120
+ className: mx("group container-max-width grid grid-cols-1 md:grid-cols-[1fr_min-content] gap-trimMd", "[--control-spacing:0px] [&_input]:justify-self-end [&_button]:justify-self-end", classNames)
121
+ }, children);
122
+ var ControlFrame = ({ children }) => /* @__PURE__ */ React.createElement("div", {
123
+ role: "none",
124
+ className: mx("container-max-width grid grid-cols-1 md:grid-cols-[1fr_1fr] gap-trimSm md:gap-trimMd p-trimMd", "border border-separator rounded-md")
125
+ }, children);
126
+ var controlItemClasses = mx([
127
+ "container-max-width grid md:col-span-2 grid-cols-subgrid gap-trimSm items-center",
128
+ // TODO(burdon): Use grid gap consistently or apply margins consistently?
129
+ "mlb-[--control-spacing] *:first:!mbs-0 *:last:!mbe-0 pli-trimMd plb-trimMd",
130
+ "border border-separator rounded-md"
131
+ ]);
132
+ var controlItemTitleClasses = "mbe-0 text-lg text-baseText font-normal";
133
+ var controlItemDescriptionClasses = "mlb-trimSm md:mbe-0 text-base text-description";
204
134
  var ControlItem = ({ title, description, children }) => {
205
- var _effect = _useSignals2();
206
- try {
207
- const { t } = useTranslation(translationKey);
208
- return /* @__PURE__ */ React2.createElement("div", {
209
- className: controlItemClasses
210
- }, /* @__PURE__ */ React2.createElement("div", {
211
- role: "none"
212
- }, /* @__PURE__ */ React2.createElement("h3", {
213
- className: controlItemTitleClasses
214
- }, toLocalizedString(title, t)), description && /* @__PURE__ */ React2.createElement("p", {
215
- className: controlItemDescriptionClasses
216
- }, toLocalizedString(description, t))), children);
217
- } finally {
218
- _effect.f();
219
- }
135
+ const { t } = useTranslation(translationKey);
136
+ return /* @__PURE__ */ React.createElement("div", {
137
+ className: controlItemClasses
138
+ }, /* @__PURE__ */ React.createElement("div", {
139
+ role: "none"
140
+ }, /* @__PURE__ */ React.createElement("h3", {
141
+ className: controlItemTitleClasses
142
+ }, toLocalizedString(title, t)), description && /* @__PURE__ */ React.createElement("p", {
143
+ className: controlItemDescriptionClasses
144
+ }, toLocalizedString(description, t))), children);
220
145
  };
221
146
  var ControlItemInput = ({ title, description, children }) => {
222
- var _effect = _useSignals2();
223
- try {
224
- const { t } = useTranslation(translationKey);
225
- return /* @__PURE__ */ React2.createElement(Input2.Root, null, /* @__PURE__ */ React2.createElement("div", {
226
- className: controlItemClasses
227
- }, /* @__PURE__ */ React2.createElement("div", {
228
- role: "none"
229
- }, /* @__PURE__ */ React2.createElement(Input2.Label, {
230
- classNames: controlItemTitleClasses
231
- }, toLocalizedString(title, t)), description && /* @__PURE__ */ React2.createElement(Input2.DescriptionAndValidation, null, /* @__PURE__ */ React2.createElement(Input2.Description, {
232
- classNames: controlItemDescriptionClasses
233
- }, toLocalizedString(description, t)))), children));
234
- } finally {
235
- _effect.f();
236
- }
147
+ const { t } = useTranslation(translationKey);
148
+ return /* @__PURE__ */ React.createElement(Input.Root, null, /* @__PURE__ */ React.createElement("div", {
149
+ className: controlItemClasses
150
+ }, /* @__PURE__ */ React.createElement("div", {
151
+ role: "none"
152
+ }, /* @__PURE__ */ React.createElement(Input.Label, {
153
+ classNames: controlItemTitleClasses
154
+ }, toLocalizedString(title, t)), description && /* @__PURE__ */ React.createElement(Input.DescriptionAndValidation, null, /* @__PURE__ */ React.createElement(Input.Description, {
155
+ classNames: controlItemDescriptionClasses
156
+ }, toLocalizedString(description, t)))), children));
237
157
  };
238
158
  var ControlFrameItem = ({ title, description, children }) => {
239
- var _effect = _useSignals2();
240
- try {
241
- const { t } = useTranslation(translationKey);
242
- return /* @__PURE__ */ React2.createElement("div", {
243
- role: "group",
244
- className: "min-is-0"
245
- }, /* @__PURE__ */ React2.createElement("h3", {
246
- className: "text-lg mbe-2"
247
- }, toLocalizedString(title, t)), description && /* @__PURE__ */ React2.createElement("p", {
248
- className: "mlb-trimSm md:mbe-0 text-description"
249
- }, toLocalizedString(description, t)), children);
250
- } finally {
251
- _effect.f();
252
- }
159
+ const { t } = useTranslation(translationKey);
160
+ return /* @__PURE__ */ React.createElement("div", {
161
+ role: "group",
162
+ className: "min-is-0"
163
+ }, /* @__PURE__ */ React.createElement("h3", {
164
+ className: "text-lg mbe-2"
165
+ }, toLocalizedString(title, t)), description && /* @__PURE__ */ React.createElement("p", {
166
+ className: "mlb-trimSm md:mbe-0 text-description"
167
+ }, toLocalizedString(description, t)), children);
253
168
  };
254
169
 
255
170
  // src/components/FieldEditor/FieldEditor.tsx
256
- import { useSignals as _useSignals14 } from "@preact-signals/safe-react/tracking";
257
- import React14, { useCallback as useCallback9, useEffect as useEffect8, useMemo as useMemo7, useState as useState5 } from "react";
258
- import { FormatEnum as FormatEnum5, FormatEnums, formatToType } from "@dxos/echo-schema";
259
- import { log as log3 } from "@dxos/log";
260
- import { useTranslation as useTranslation8 } from "@dxos/react-ui";
261
- import { getFormatSchema, getSchemaProperties as getSchemaProperties3, sortProperties } from "@dxos/schema";
262
-
263
- // src/components/Form/custom/GeoPointInput.tsx
264
- import { useSignals as _useSignals4 } from "@preact-signals/safe-react/tracking";
265
- import React4, { useCallback, useState, useEffect, useMemo } from "react";
266
- import { GeoLocation } from "@dxos/echo-schema";
267
- import { Input as Input4, useTranslation as useTranslation2 } from "@dxos/react-ui";
268
- import { safeParseFloat } from "@dxos/util";
171
+ import React18, { useCallback as useCallback14, useEffect as useEffect7, useMemo as useMemo7, useState as useState5 } from "react";
172
+ import { Obj as Obj3 } from "@dxos/echo";
173
+ import { Format as Format4, FormatEnums, formatToType } from "@dxos/echo/internal";
174
+ import { log as log2 } from "@dxos/log";
175
+ import { useAsyncEffect, useTranslation as useTranslation9 } from "@dxos/react-ui";
176
+ import { formatToAdditionalPropertyAttributes, getFormatSchema } from "@dxos/schema";
269
177
 
270
- // src/components/Form/Input.tsx
271
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
272
- import React3 from "react";
273
- import { Icon, Input as Input3, Tooltip } from "@dxos/react-ui";
274
- import { labelSpacing } from "@dxos/react-ui-stack";
275
- import { errorText, mx as mx3 } from "@dxos/react-ui-theme";
276
- var InputHeader = ({ error, label, readonly }) => {
277
- var _effect = _useSignals3();
278
- try {
279
- const Label = readonly ? "span" : Input3.Label;
280
- const labelProps = readonly ? {} : {
281
- classNames: "!mlb-0"
282
- };
283
- return /* @__PURE__ */ React3.createElement("div", {
284
- role: "none",
285
- className: mx3("flex justify-between items-center", labelSpacing)
286
- }, /* @__PURE__ */ React3.createElement(Label, labelProps, label), error && /* @__PURE__ */ React3.createElement(Tooltip.Trigger, {
287
- asChild: true,
288
- content: error,
289
- side: "bottom"
290
- }, /* @__PURE__ */ React3.createElement(Icon, {
291
- icon: "ph--warning--regular",
292
- size: 4,
293
- classNames: errorText
294
- })));
295
- } finally {
296
- _effect.f();
178
+ // src/util/echo.ts
179
+ import { Obj } from "@dxos/echo";
180
+ import { setValue as setValuePlain, splitJsonPath } from "@dxos/effect";
181
+ var setValueEchoAware = (obj, path, value3) => {
182
+ if (Obj.isObject(obj)) {
183
+ Obj.change(obj, (mutableObj) => {
184
+ Obj.setValue(mutableObj, splitJsonPath(path), value3);
185
+ });
186
+ return obj;
297
187
  }
188
+ return setValuePlain(obj, path, value3);
298
189
  };
299
190
 
300
- // src/components/Form/custom/GeoPointInput.tsx
301
- var GeoPointInput = ({ type, label, disabled, getStatus, getValue: getValue3, onValueChange, onBlur }) => {
302
- var _effect = _useSignals4();
303
- try {
304
- const { t } = useTranslation2(translationKey);
305
- const { status, error } = getStatus();
306
- const geoPoint = useMemo(() => getValue3() ?? [
307
- 0,
308
- 0
309
- ], [
310
- getValue3
311
- ]);
312
- const geoLocation = useMemo(() => GeoLocation.fromGeoPoint(geoPoint), [
313
- geoPoint
314
- ]);
315
- const [longitudeText, setLongitudeText] = useState(geoLocation.longitude?.toString());
316
- const [latitudeText, setLatitudeText] = useState(geoLocation.latitude?.toString());
317
- useEffect(() => {
318
- const location = GeoLocation.fromGeoPoint(geoPoint);
319
- setLongitudeText(location.longitude.toString());
320
- setLatitudeText(location.latitude.toString());
321
- }, [
322
- geoPoint
323
- ]);
324
- const handleCoordinateChange = useCallback((coordinateType, setText) => (event) => {
325
- const inputText = event.target.value;
326
- setText(inputText);
327
- if (inputText !== "" && inputText !== "-") {
328
- const coord = safeParseFloat(inputText);
329
- if (coord !== void 0 && !isNaN(coord)) {
330
- const currentLocation = GeoLocation.fromGeoPoint(getValue3() ?? [
331
- 0,
332
- 0
333
- ]);
334
- const newLocation = {
335
- ...currentLocation,
336
- [coordinateType]: coord
337
- };
338
- const newValue = GeoLocation.toGeoPoint(newLocation);
339
- onValueChange(type, newValue);
191
+ // src/util/field.ts
192
+ import { Format, GeoLocation, TypeEnum } from "@dxos/echo/internal";
193
+ var parseValue = ({ type, format, value: value3 }) => {
194
+ if (value3 === void 0 || value3 === null || value3 === "") {
195
+ return void 0;
196
+ }
197
+ const parseScalar = (type2) => {
198
+ switch (type2) {
199
+ case TypeEnum.Boolean: {
200
+ if (typeof value3 === "string") {
201
+ const lowercaseValue = value3.toLowerCase();
202
+ if (lowercaseValue === "0" || lowercaseValue === "false") {
203
+ return false;
204
+ } else if (lowercaseValue === "1" || lowercaseValue === "true") {
205
+ return true;
206
+ }
340
207
  }
208
+ return Boolean(value3);
341
209
  }
342
- }, [
343
- getValue3,
344
- onValueChange,
345
- type
346
- ]);
347
- return /* @__PURE__ */ React4.createElement(Input4.Root, {
348
- validationValence: status
349
- }, /* @__PURE__ */ React4.createElement(InputHeader, {
350
- error,
351
- label
352
- }), /* @__PURE__ */ React4.createElement("div", {
353
- role: "none",
354
- className: "grid grid-cols-2 gap-2"
355
- }, /* @__PURE__ */ React4.createElement(Input4.TextInput, {
356
- type: "text",
357
- pattern: "^-?[0-9]*\\.?[0-9]*$",
358
- disabled,
359
- placeholder: t("placeholder latitude"),
360
- value: latitudeText,
361
- onChange: handleCoordinateChange("latitude", setLatitudeText),
362
- onBlur
363
- }), /* @__PURE__ */ React4.createElement(Input4.TextInput, {
364
- type: "text",
365
- pattern: "^-?[0-9]*\\.?[0-9]*$",
366
- disabled,
367
- placeholder: t("placeholder longitude"),
368
- value: longitudeText,
369
- onChange: handleCoordinateChange("longitude", setLongitudeText),
370
- onBlur
371
- })));
372
- } finally {
373
- _effect.f();
374
- }
375
- };
376
-
377
- // src/components/Form/custom/SelectOptionsInput.tsx
378
- import { useSignals as _useSignals5 } from "@preact-signals/safe-react/tracking";
379
- import React5, { useCallback as useCallback2, useEffect as useEffect2, useState as useState2, useRef } from "react";
380
- import { PublicKey } from "@dxos/keys";
381
- import { Icon as Icon2, IconButton, Input as Input5, Tag, Toolbar, useTranslation as useTranslation3 } from "@dxos/react-ui";
382
- import { List } from "@dxos/react-ui-list";
383
- import { HuePicker } from "@dxos/react-ui-pickers";
384
- import { hues, mx as mx4 } from "@dxos/react-ui-theme";
385
- var SelectOptionInput = ({ type, label, disabled, getStatus, getValue: getValue3, onValueChange }) => {
386
- var _effect = _useSignals5();
387
- try {
388
- const [selected, setSelectedId] = useState2(null);
389
- const [isNewOption, setIsNewOption] = useState2(false);
390
- const inputRef = useRef(null);
391
- const { t } = useTranslation3(translationKey);
392
- const { status, error } = getStatus();
393
- const options = getValue3();
394
- useEffect2(() => {
395
- if (options === void 0) {
396
- onValueChange(type, []);
210
+ case TypeEnum.Number: {
211
+ const cleaned = typeof value3 === "string" ? value3.replace(/[^0-9.-]+/g, "") : value3;
212
+ const num = Number(cleaned);
213
+ return Number.isNaN(num) ? null : num;
397
214
  }
398
- }, [
399
- options,
400
- onValueChange,
401
- type
402
- ]);
403
- const randomHue = () => {
404
- const usedHues = new Set(options?.map((option) => option.color) ?? []);
405
- const unusedHues = hues.filter((hue) => !usedHues.has(hue));
406
- if (unusedHues.length > 0) {
407
- return unusedHues[Math.floor(Math.random() * unusedHues.length)];
215
+ case TypeEnum.String:
216
+ case TypeEnum.Ref:
217
+ return String(value3);
218
+ default: {
219
+ return value3;
408
220
  }
409
- const lastThree = new Set(options?.slice(-3).map((option) => option.color) ?? []);
410
- const availableHues = hues.filter((hue) => !lastThree.has(hue));
411
- return availableHues[Math.floor(Math.random() * availableHues.length)] ?? hues[Math.floor(Math.random() * hues.length)];
412
- };
413
- const handleAdd = useCallback2(() => {
414
- const newId = PublicKey.random().truncate();
415
- const newOption = {
416
- id: newId,
417
- title: "",
418
- color: randomHue()
419
- };
420
- onValueChange(type, [
421
- ...options ?? [],
422
- newOption
423
- ]);
424
- setSelectedId(newId);
425
- setIsNewOption(true);
426
- }, [
427
- options,
428
- type,
429
- onValueChange
430
- ]);
431
- const handleDelete = useCallback2((id) => {
432
- const newOptions = options?.filter((option) => option.id !== id) ?? [];
433
- onValueChange(type, newOptions);
434
- setSelectedId(null);
435
- }, [
436
- options,
437
- type,
438
- onValueChange
439
- ]);
440
- const handleMove = useCallback2((from, to) => {
441
- if (!options) {
442
- return;
221
+ }
222
+ };
223
+ if (!format) {
224
+ return parseScalar(type);
225
+ }
226
+ switch (format) {
227
+ case Format.TypeFormat.Boolean:
228
+ return parseScalar(TypeEnum.Boolean);
229
+ case Format.TypeFormat.Number:
230
+ case Format.TypeFormat.Percent:
231
+ case Format.TypeFormat.Currency:
232
+ return parseScalar(TypeEnum.Number);
233
+ case Format.TypeFormat.String:
234
+ case Format.TypeFormat.Markdown: {
235
+ return parseScalar(TypeEnum.String);
236
+ }
237
+ case Format.TypeFormat.Ref:
238
+ throw new Error(`unexpected format: ${Format.TypeFormat.Ref}`);
239
+ case Format.TypeFormat.DateTime:
240
+ case Format.TypeFormat.Date:
241
+ case Format.TypeFormat.Time:
242
+ case Format.TypeFormat.Timestamp: {
243
+ const date = new Date(value3);
244
+ return isNaN(date.getTime()) ? null : date;
245
+ }
246
+ case Format.TypeFormat.GeoPoint: {
247
+ if (typeof value3 === "string") {
248
+ const cleaned = value3.replace(/[^0-9.,-]+/g, "");
249
+ const parts = cleaned.split(",");
250
+ if (parts.length === 2) {
251
+ const lat = parseFloat(parts[0]);
252
+ const long = parseFloat(parts[1]);
253
+ if (!isNaN(lat) && !isNaN(long)) {
254
+ return GeoLocation.toGeoPoint({
255
+ latitude: lat,
256
+ longitude: long
257
+ });
258
+ }
259
+ }
443
260
  }
444
- const newOptions = [
445
- ...options
261
+ return [
262
+ 0,
263
+ 0
446
264
  ];
447
- const [removed] = newOptions.splice(from, 1);
448
- newOptions.splice(to, 0, removed);
449
- onValueChange(type, newOptions);
450
- }, [
451
- options,
452
- type,
453
- onValueChange
454
- ]);
455
- const handleClick = useCallback2((id) => {
456
- setSelectedId((current) => current === id ? null : id);
457
- }, []);
458
- const handleTitleChange = useCallback2((id) => (e) => {
459
- const newOptions = options?.map((o) => o.id === id ? {
460
- ...o,
461
- title: e.target.value
462
- } : o);
463
- onValueChange(type, newOptions ?? []);
464
- }, [
465
- options,
466
- type,
467
- onValueChange
468
- ]);
469
- const handleKeyDown = useCallback2((e) => {
470
- if (e.key === "Enter") {
471
- setSelectedId(null);
472
- }
473
- }, []);
474
- const handleColorChange = useCallback2((id) => (hue) => {
475
- const newOptions = options?.map((o) => o.id === id ? {
476
- ...o,
477
- color: hue
478
- } : o);
479
- onValueChange(type, newOptions ?? []);
480
- }, [
481
- options,
482
- type,
483
- onValueChange
484
- ]);
485
- useEffect2(() => {
486
- if (selected && isNewOption && inputRef.current) {
487
- inputRef.current.focus();
488
- setIsNewOption(false);
489
- }
490
- }, [
491
- selected,
492
- isNewOption
493
- ]);
494
- return /* @__PURE__ */ React5.createElement(Input5.Root, {
495
- validationValence: status
496
- }, /* @__PURE__ */ React5.createElement(InputHeader, {
497
- error,
498
- label
499
- }), /* @__PURE__ */ React5.createElement("div", {
500
- role: "none"
501
- }, options && /* @__PURE__ */ React5.createElement(List.Root, {
502
- items: options,
503
- isItem: (item) => true,
504
- onMove: handleMove
505
- }, ({ items }) => /* @__PURE__ */ React5.createElement("div", {
506
- role: "list",
507
- className: "w-full overflow-auto"
508
- }, items?.map((item) => /* @__PURE__ */ React5.createElement("div", {
509
- key: item.id
510
- }, /* @__PURE__ */ React5.createElement(List.Item, {
511
- role: "button",
512
- item,
513
- classNames: [
514
- "p-1 plb-2",
515
- "flex flex-col",
516
- "cursor-pointer",
517
- "hover:bg-hoverOverlay"
518
- ],
519
- "aria-expanded": selected === item.id
520
- }, /* @__PURE__ */ React5.createElement("div", {
521
- className: "flex items-center"
522
- }, /* @__PURE__ */ React5.createElement(List.ItemDragHandle, null), /* @__PURE__ */ React5.createElement(List.ItemTitle, {
523
- onClick: () => handleClick(item.id),
524
- classNames: "flex-1"
525
- }, /* @__PURE__ */ React5.createElement(Tag, {
526
- palette: item.color
527
- }, item.title || "\u200B")), /* @__PURE__ */ React5.createElement(Icon2, {
528
- icon: selected === item.id ? "ph--caret-down--regular" : "ph--caret-right--regular",
529
- onClick: () => handleClick(item.id)
530
- })), selected === item.id && /* @__PURE__ */ React5.createElement("div", {
531
- className: "mis-4 mbs-2 flex flex-col gap-1"
532
- }, /* @__PURE__ */ React5.createElement(Input5.Label, {
533
- classNames: "text-xs"
534
- }, t("select option label")), /* @__PURE__ */ React5.createElement("div", {
535
- className: "flex flex-row items-center gap-1"
536
- }, /* @__PURE__ */ React5.createElement(Input5.TextInput, {
537
- placeholder: t("select option label placeholder"),
538
- ref: selected === item.id ? inputRef : void 0,
539
- value: item.title,
540
- onChange: handleTitleChange(item.id),
541
- onKeyDown: handleKeyDown,
542
- classNames: "flex-1",
543
- "data-no-submit": true
544
- }), /* @__PURE__ */ React5.createElement(Toolbar.Root, {
545
- classNames: "p-0 m-0 !is-auto"
546
- }, /* @__PURE__ */ React5.createElement(HuePicker, {
547
- value: item.color,
548
- onChange: handleColorChange(item.id),
549
- rootVariant: "toolbar-button"
550
- })), /* @__PURE__ */ React5.createElement(IconButton, {
551
- icon: "ph--trash--fill",
552
- iconOnly: true,
553
- label: t("select option delete"),
554
- onClick: () => handleDelete(item.id)
555
- })))))), /* @__PURE__ */ React5.createElement("div", {
556
- role: "listitem"
557
- }, /* @__PURE__ */ React5.createElement("div", {
558
- role: "button",
559
- className: mx4("p-1 plb-2", "flex flex-col", "cursor-pointer", "hover:bg-hoverOverlay"),
560
- onClick: handleAdd
561
- }, /* @__PURE__ */ React5.createElement("div", {
562
- className: "flex items-center gap-1"
563
- }, /* @__PURE__ */ React5.createElement(Icon2, {
564
- icon: "ph--plus--regular"
565
- }), /* @__PURE__ */ React5.createElement("span", {
566
- className: "text-sm"
567
- }, t("select option add")))))))));
568
- } finally {
569
- _effect.f();
265
+ }
266
+ default: {
267
+ return value3;
268
+ }
570
269
  }
571
270
  };
572
-
573
- // src/components/Form/custom/TupleInput.tsx
574
- import { useSignals as _useSignals6 } from "@preact-signals/safe-react/tracking";
575
- import React6 from "react";
576
- import { Input as Input6 } from "@dxos/react-ui";
577
- import { mx as mx5 } from "@dxos/react-ui-theme";
578
- import { safeParseFloat as safeParseFloat2 } from "@dxos/util";
579
- var gridCols = [
580
- "grid-cols-1",
581
- "grid-cols-2",
582
- "grid-cols-3",
583
- "grid-cols-4"
584
- ];
585
- var TupleInput = ({ binding, type, label, disabled, getStatus, getValue: getValue3, onValueChange, onBlur }) => {
586
- var _effect = _useSignals6();
587
- try {
588
- const { status, error } = getStatus();
589
- const values = getValue3() ?? {};
590
- return /* @__PURE__ */ React6.createElement(Input6.Root, {
591
- validationValence: status
592
- }, /* @__PURE__ */ React6.createElement(InputHeader, {
593
- error,
594
- label
595
- }), /* @__PURE__ */ React6.createElement("div", {
596
- className: mx5("grid gap-2", gridCols[binding.length - 1])
597
- }, binding.map((prop) => /* @__PURE__ */ React6.createElement(Input6.TextInput, {
598
- key: prop,
599
- type: "number",
600
- disabled,
601
- value: values[prop],
602
- onChange: (event) => onValueChange(type, {
603
- ...values,
604
- [prop]: safeParseFloat2(event.target.value, 0)
605
- }),
606
- onBlur
607
- }))));
608
- } finally {
609
- _effect.f();
271
+ var cellClassesForFieldType = ({ type, format }) => {
272
+ switch (format || type) {
273
+ case Format.TypeFormat.Markdown:
274
+ return void 0;
275
+ case Format.TypeFormat.Time:
276
+ case Format.TypeFormat.Timestamp:
277
+ case Format.TypeFormat.DateTime:
278
+ case Format.TypeFormat.Date:
279
+ case Format.TypeFormat.Duration:
280
+ return [
281
+ "font-mono",
282
+ "text-right"
283
+ ];
284
+ case Format.TypeFormat.Currency:
285
+ return [
286
+ "text-right"
287
+ ];
288
+ case Format.TypeFormat.JSON:
289
+ case Format.TypeFormat.DID:
290
+ return [
291
+ "font-mono"
292
+ ];
293
+ case TypeEnum.Number:
294
+ case TypeEnum.Boolean:
295
+ return [
296
+ "text-right",
297
+ "font-mono"
298
+ ];
299
+ case TypeEnum.String:
300
+ return void 0;
301
+ case TypeEnum.Ref:
302
+ return void 0;
303
+ default:
304
+ return void 0;
610
305
  }
611
306
  };
307
+ var pathNotUniqueError = (path) => ({
308
+ path: "path",
309
+ message: `'${path}' is already present`
310
+ });
612
311
 
613
- // src/components/Form/Defaults.tsx
614
- import { useSignals as _useSignals7 } from "@preact-signals/safe-react/tracking";
615
- import React7, { useCallback as useCallback3, useEffect as useEffect3, useRef as useRef2 } from "react";
616
- import { Input as Input7, Select, useTranslation as useTranslation4 } from "@dxos/react-ui";
617
- var TextInput = ({ type, label, inputOnly, disabled, placeholder, getStatus, getValue: getValue3, onValueChange, onBlur }) => {
618
- var _effect = _useSignals7();
619
- try {
620
- const { status, error } = getStatus();
621
- return disabled && !getValue3() ? null : disabled && inputOnly ? /* @__PURE__ */ React7.createElement("p", null, getValue3() ?? "") : /* @__PURE__ */ React7.createElement(Input7.Root, {
622
- validationValence: status
623
- }, !inputOnly && /* @__PURE__ */ React7.createElement(InputHeader, {
624
- error,
625
- label
626
- }), disabled ? /* @__PURE__ */ React7.createElement("p", null, getValue3() ?? "") : /* @__PURE__ */ React7.createElement(Input7.TextInput, {
627
- disabled,
628
- placeholder,
629
- value: getValue3() ?? "",
630
- onChange: (event) => onValueChange(type, event.target.value),
631
- onBlur
632
- }), inputOnly && /* @__PURE__ */ React7.createElement(Input7.Validation, null, error));
633
- } finally {
634
- _effect.f();
635
- }
636
- };
637
- var NumberInput = ({ type, label, inputOnly, disabled, placeholder, getStatus, getValue: getValue3, onValueChange, onBlur }) => {
638
- var _effect = _useSignals7();
639
- try {
640
- const { status, error } = getStatus();
641
- return disabled && !getValue3() ? null : disabled && inputOnly ? /* @__PURE__ */ React7.createElement("p", null, getValue3() ?? "") : /* @__PURE__ */ React7.createElement(Input7.Root, {
642
- validationValence: status
643
- }, !inputOnly && /* @__PURE__ */ React7.createElement(InputHeader, {
644
- error,
645
- label
646
- }), disabled ? /* @__PURE__ */ React7.createElement("p", null, getValue3() ?? "") : /* @__PURE__ */ React7.createElement(Input7.TextInput, {
647
- type: "number",
648
- disabled,
649
- placeholder,
650
- value: getValue3() ?? 0,
651
- onChange: (event) => onValueChange(type, event.target.value),
652
- onBlur
653
- }), inputOnly && /* @__PURE__ */ React7.createElement(Input7.DescriptionAndValidation, null, error));
654
- } finally {
655
- _effect.f();
312
+ // src/util/formatting.ts
313
+ import { format as formatDate } from "date-fns/format";
314
+ import { Format as Format2, GeoLocation as GeoLocation2, TypeEnum as TypeEnum2 } from "@dxos/echo/internal";
315
+ var formatForDisplay = ({ type, format, value: value3, locale = void 0 }) => {
316
+ const formatScalar = (type2) => {
317
+ switch (type2) {
318
+ case TypeEnum2.Boolean:
319
+ return value3.toLocaleString().toUpperCase();
320
+ case TypeEnum2.Number:
321
+ return value3.toLocaleString(locale);
322
+ case TypeEnum2.String:
323
+ case TypeEnum2.Ref:
324
+ return String(value3);
325
+ default: {
326
+ return String(value3);
327
+ }
328
+ }
329
+ };
330
+ if (!format) {
331
+ return formatScalar(type);
656
332
  }
657
- };
658
- var BooleanInput = ({ type, label, inputOnly, getStatus, getValue: getValue3, onValueChange, disabled }) => {
659
- var _effect = _useSignals7();
660
- try {
661
- const { status, error } = getStatus();
662
- const checked = Boolean(getValue3());
663
- const { t } = useTranslation4(translationKey);
664
- return /* @__PURE__ */ React7.createElement(Input7.Root, {
665
- validationValence: status
666
- }, !inputOnly && /* @__PURE__ */ React7.createElement(InputHeader, {
667
- error,
668
- label
669
- }), disabled ? /* @__PURE__ */ React7.createElement("p", null, t(checked ? "boolean input true value" : "boolean input false value")) : /* @__PURE__ */ React7.createElement(Input7.Switch, {
670
- checked,
671
- onCheckedChange: (value) => onValueChange(type, value)
672
- }), inputOnly && /* @__PURE__ */ React7.createElement(Input7.DescriptionAndValidation, null, error));
673
- } finally {
674
- _effect.f();
333
+ switch (format) {
334
+ case Format2.TypeFormat.Boolean: {
335
+ return formatScalar(TypeEnum2.Boolean);
336
+ }
337
+ case Format2.TypeFormat.Number: {
338
+ return formatScalar(TypeEnum2.Number);
339
+ }
340
+ case Format2.TypeFormat.String:
341
+ case Format2.TypeFormat.Ref: {
342
+ return formatScalar(TypeEnum2.String);
343
+ }
344
+ case Format2.TypeFormat.Percent: {
345
+ return `${value3 * 100}%`;
346
+ }
347
+ case Format2.TypeFormat.Currency: {
348
+ return value3.toLocaleString(locale, {
349
+ style: "currency",
350
+ currency: "USD",
351
+ minimumFractionDigits: 2,
352
+ maximumFractionDigits: 2
353
+ });
354
+ }
355
+ case Format2.TypeFormat.Date: {
356
+ try {
357
+ return formatDate(new Date(value3), "yyyy-MM-dd");
358
+ } catch (error) {
359
+ return "Invalid Date";
360
+ }
361
+ }
362
+ case Format2.TypeFormat.Time: {
363
+ try {
364
+ return formatDate(new Date(value3), "HH:mm:ss");
365
+ } catch (error) {
366
+ return "Invalid Time";
367
+ }
368
+ }
369
+ case Format2.TypeFormat.DateTime: {
370
+ try {
371
+ return formatDate(new Date(value3), "yyyy-MM-dd HH:mm:ss");
372
+ } catch (error) {
373
+ return "Invalid DateTime";
374
+ }
375
+ }
376
+ case Format2.TypeFormat.GeoPoint: {
377
+ if (value3 === null || value3 === void 0) {
378
+ return "";
379
+ }
380
+ if (Array.isArray(value3) && value3.length >= 2 && value3.every(Number.isFinite)) {
381
+ const { latitude, longitude } = GeoLocation2.fromGeoPoint(value3);
382
+ return `${latitude},${longitude}`;
383
+ }
384
+ return String(value3);
385
+ }
386
+ default: {
387
+ if (value3 === null || value3 === "undefined") {
388
+ return "";
389
+ }
390
+ return String(value3);
391
+ }
675
392
  }
676
393
  };
677
- var SelectInput = ({ type, label, inputOnly, disabled, placeholder, options, getStatus, getValue: getValue3, onValueChange }) => {
678
- var _effect = _useSignals7();
679
- try {
680
- const { status, error } = getStatus();
681
- const value = getValue3();
682
- const handleValueChange = useCallback3((value2) => onValueChange(type, value2), [
683
- type,
684
- onValueChange
685
- ]);
686
- return disabled && !value ? null : /* @__PURE__ */ React7.createElement(Input7.Root, {
687
- validationValence: status
688
- }, !inputOnly && /* @__PURE__ */ React7.createElement(InputHeader, {
689
- error,
690
- label
691
- }), disabled ? /* @__PURE__ */ React7.createElement("p", null, options?.find(({ value: optionValue }) => optionValue === value)?.label ?? String(value)) : /* @__PURE__ */ React7.createElement(Select.Root, {
692
- value,
693
- onValueChange: handleValueChange
694
- }, /* @__PURE__ */ React7.createElement(Select.TriggerButton, {
695
- classNames: "is-full",
696
- disabled,
697
- placeholder
698
- }), /* @__PURE__ */ React7.createElement(Select.Portal, null, /* @__PURE__ */ React7.createElement(Select.Content, null, /* @__PURE__ */ React7.createElement(Select.Viewport, null, options?.map(({ value: value2, label: label2 }) => (
699
- // NOTE: Numeric values are converted to and from strings.
700
- /* @__PURE__ */ React7.createElement(Select.Option, {
701
- key: String(value2),
702
- value: String(value2)
703
- }, label2 ?? String(value2))
704
- )))))), inputOnly && /* @__PURE__ */ React7.createElement(Input7.DescriptionAndValidation, null, error));
705
- } finally {
706
- _effect.f();
394
+ var formatForEditing = ({ type, format, value: value3, locale = void 0 }) => {
395
+ const formatScalar = (type2) => {
396
+ switch (type2) {
397
+ case TypeEnum2.Boolean:
398
+ case TypeEnum2.Number:
399
+ case TypeEnum2.String:
400
+ case TypeEnum2.Ref:
401
+ return String(value3);
402
+ default:
403
+ return String(value3);
404
+ }
405
+ };
406
+ if (!format) {
407
+ return formatScalar(type);
707
408
  }
708
- };
709
- var MarkdownInput = ({ type, label, inputOnly, disabled, placeholder, getStatus, getValue: getValue3, onValueChange, onBlur }) => {
710
- var _effect = _useSignals7();
711
- try {
712
- const { status, error } = getStatus();
713
- const textareaRef = useRef2(null);
714
- const adjustHeight = useCallback3(() => {
715
- const textarea = textareaRef.current;
716
- if (textarea) {
717
- textarea.style.blockSize = "auto";
718
- textarea.style.blockSize = `${textarea.scrollHeight + 2}px`;
409
+ switch (format) {
410
+ case Format2.TypeFormat.Boolean:
411
+ case Format2.TypeFormat.Number:
412
+ case Format2.TypeFormat.String:
413
+ case Format2.TypeFormat.Ref: {
414
+ return formatScalar(type);
415
+ }
416
+ case Format2.TypeFormat.Percent: {
417
+ return String(value3 * 100);
418
+ }
419
+ case Format2.TypeFormat.Currency: {
420
+ return String(value3);
421
+ }
422
+ case Format2.TypeFormat.DateTime: {
423
+ const date = new Date(value3);
424
+ return date.toISOString();
425
+ }
426
+ case Format2.TypeFormat.Date: {
427
+ const date = new Date(value3);
428
+ return date.toISOString().split("T")[0];
429
+ }
430
+ case Format2.TypeFormat.Time: {
431
+ const date = new Date(value3);
432
+ return date.toISOString().split("T")[1].split(".")[0];
433
+ }
434
+ case Format2.TypeFormat.GeoPoint: {
435
+ if (value3 === null || value3 === void 0) {
436
+ return "";
719
437
  }
720
- }, []);
721
- useEffect3(() => {
722
- adjustHeight();
723
- }, [
724
- adjustHeight
725
- ]);
726
- useEffect3(() => {
727
- adjustHeight();
728
- }, [
729
- getValue3(),
730
- adjustHeight
731
- ]);
732
- return /* @__PURE__ */ React7.createElement(Input7.Root, {
733
- validationValence: status
734
- }, !inputOnly && /* @__PURE__ */ React7.createElement(InputHeader, {
735
- error,
736
- label
737
- }), disabled ? /* @__PURE__ */ React7.createElement("p", null, getValue3() ?? "") : /* @__PURE__ */ React7.createElement(Input7.TextArea, {
738
- ref: textareaRef,
739
- disabled,
740
- placeholder,
741
- value: getValue3() ?? "",
742
- classNames: "min-bs-auto max-h-40 overflow-auto",
743
- onChange: (event) => onValueChange(type, event.target.value),
744
- onBlur,
745
- style: {
746
- resize: "none"
747
- },
748
- spellCheck: false
749
- }), inputOnly && /* @__PURE__ */ React7.createElement(Input7.Validation, null, error));
750
- } finally {
751
- _effect.f();
438
+ if (Array.isArray(value3) && value3.length >= 2 && value3.every(Number.isFinite)) {
439
+ const { latitude, longitude } = GeoLocation2.fromGeoPoint(value3);
440
+ return `${latitude},${longitude}`;
441
+ }
442
+ return String(value3);
443
+ }
444
+ default: {
445
+ return String(value3);
446
+ }
752
447
  }
753
448
  };
754
449
 
755
- // src/components/Form/Form.tsx
756
- import { useSignals as _useSignals13 } from "@preact-signals/safe-react/tracking";
757
- import React13, { useEffect as useEffect7, useMemo as useMemo6, useRef as useRef3 } from "react";
758
- import { cardDialogOverflow, cardSpacing as cardSpacing2 } from "@dxos/react-ui-stack";
759
-
760
- // src/components/Form/FormActions.tsx
761
- import { useSignals as _useSignals9 } from "@preact-signals/safe-react/tracking";
762
- import React9 from "react";
763
- import { IconButton as IconButton2, useTranslation as useTranslation5 } from "@dxos/react-ui";
764
- import { cardSpacing } from "@dxos/react-ui-stack";
765
- import { mx as mx6 } from "@dxos/react-ui-theme";
766
-
767
- // src/components/Form/FormContext.tsx
768
- import { useSignals as _useSignals8 } from "@preact-signals/safe-react/tracking";
769
- import React8, { createContext, useContext, useEffect as useEffect6, useMemo as useMemo3, useCallback as useCallback6 } from "react";
770
- import { raise } from "@dxos/debug";
771
- import { getValue as getValue2 } from "@dxos/echo-schema";
772
- import { createJsonPath as createJsonPath2 } from "@dxos/effect";
450
+ // src/util/properties.ts
451
+ import * as Option from "effect/Option";
452
+ import { Annotation } from "@dxos/echo";
453
+ import { getProperties } from "@dxos/effect";
454
+ var getFormProperties = (ast) => {
455
+ return getProperties(ast).filter((prop) => Annotation.FormInputAnnotation.getFromAst(prop.type).pipe(Option.getOrElse(() => true)));
456
+ };
773
457
 
774
- // src/hooks/useForm.ts
775
- import { SchemaAST } from "effect";
776
- import { useCallback as useCallback4, useEffect as useEffect4, useMemo as useMemo2, useState as useState3 } from "react";
777
- import { getValue, setValue } from "@dxos/echo-schema";
778
- import { createJsonPath, fromEffectValidationPath } from "@dxos/effect";
779
- import { invariant } from "@dxos/invariant";
780
- import { log } from "@dxos/log";
781
- import { validateSchema } from "@dxos/schema";
782
- var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-form/src/hooks/useForm.ts";
783
- var useForm = ({ schema, initialValues, onValuesChanged, onValidate, onValid, onSave }) => {
784
- invariant(SchemaAST.isTypeLiteral(schema.ast), void 0, {
785
- F: __dxlog_file,
786
- L: 93,
787
- S: void 0,
788
- A: [
789
- "SchemaAST.isTypeLiteral(schema.ast)",
790
- ""
791
- ]
792
- });
793
- const [values, setValues] = useState3(initialValues);
794
- useEffect4(() => {
795
- setValues(initialValues);
796
- }, [
797
- initialValues
798
- ]);
799
- const [touched, setTouched] = useState3(createKeySet(initialValues, false));
800
- const [changed, setChanged] = useState3(createKeySet(initialValues, false));
801
- const [errors, setErrors] = useState3({});
802
- const [saving, setSaving] = useState3(false);
803
- const validate = useCallback4((values2) => {
458
+ // src/util/refs.ts
459
+ import { Ref } from "@dxos/echo";
460
+ import { getArrayElementType, isArrayType } from "@dxos/effect";
461
+ var getRefProps = (ast) => {
462
+ if (isArrayType(ast)) {
463
+ const elementType = getArrayElementType(ast);
464
+ if (elementType) {
465
+ if (Ref.isRefType(elementType)) {
466
+ return {
467
+ ast: elementType,
468
+ isArray: true
469
+ };
470
+ }
471
+ }
472
+ }
473
+ if (Ref.isRefType(ast)) {
474
+ return {
475
+ ast,
476
+ isArray: false
477
+ };
478
+ }
479
+ return void 0;
480
+ };
481
+
482
+ // src/components/Form/fields/ArrayField.tsx
483
+ import * as Option3 from "effect/Option";
484
+ import * as SchemaAST2 from "effect/SchemaAST";
485
+ import React6, { useCallback as useCallback3 } from "react";
486
+ import { findNode as findNode2, getArrayElementType as getArrayElementType2, getDiscriminatedType as getDiscriminatedType2, isDiscriminatedUnion as isDiscriminatedUnion2, isNestedType as isNestedType2 } from "@dxos/effect";
487
+ import { IconButton as IconButton2, useTranslation as useTranslation4 } from "@dxos/react-ui";
488
+
489
+ // src/components/Form/Form.tsx
490
+ import { createContext } from "@radix-ui/react-context";
491
+ import * as Schema2 from "effect/Schema";
492
+ import React5, { useEffect as useEffect3, useMemo as useMemo4, useRef } from "react";
493
+ import { createJsonPath as createJsonPath3, getValue as getValue$2 } from "@dxos/effect";
494
+ import { IconButton, ScrollArea, useTranslation as useTranslation3 } from "@dxos/react-ui";
495
+ import { mx as mx4 } from "@dxos/ui-theme";
496
+
497
+ // src/hooks/useFormHandler.ts
498
+ import { useControllableState } from "@radix-ui/react-use-controllable-state";
499
+ import { useCallback, useEffect, useMemo, useState } from "react";
500
+ import { Obj as Obj2 } from "@dxos/echo";
501
+ import { createJsonPath, fromEffectValidationPath, getValue as getValue$, setValue as setValue$ } from "@dxos/effect";
502
+ import { log } from "@dxos/log";
503
+ import { useDefaultValue } from "@dxos/react-ui";
504
+ import { validateSchema } from "@dxos/schema";
505
+ var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-form/src/hooks/useFormHandler.ts";
506
+ var useFormHandler = ({ schema, autoSave, values: valuesProp, defaultValues: defaultValuesProp, onValuesChanged, onValidate, onSave, onCancel, ...props }) => {
507
+ const [changed, setChanged] = useState({});
508
+ const [touched, setTouched] = useState({});
509
+ const [errors, setErrors] = useState({});
510
+ const [saving, setSaving] = useState(false);
511
+ const defaultValues = useDefaultValue(defaultValuesProp, () => ({}));
512
+ const [values$, setValues] = useControllableState({
513
+ prop: valuesProp,
514
+ defaultProp: defaultValues,
515
+ onChange: () => {
516
+ setValues(valuesProp ?? defaultValues);
517
+ setChanged({});
518
+ setTouched({});
519
+ setErrors({});
520
+ setSaving(false);
521
+ }
522
+ });
523
+ const values = values$;
524
+ const validate = useCallback((values2) => {
525
+ if (!schema) {
526
+ return false;
527
+ }
804
528
  let errors2 = validateSchema(schema, values2) ?? [];
529
+ log("validate", {
530
+ values: values2,
531
+ errors: errors2
532
+ }, {
533
+ F: __dxlog_file,
534
+ L: 160,
535
+ S: void 0,
536
+ C: (f, a) => f(...a)
537
+ });
805
538
  if (errors2.length === 0 && onValidate) {
806
539
  const validatedValues = values2;
807
540
  errors2 = onValidate(validatedValues) ?? [];
@@ -813,36 +546,38 @@ var useForm = ({ schema, initialValues, onValuesChanged, onValidate, onValid, on
813
546
  schema,
814
547
  onValidate
815
548
  ]);
816
- const [schemaDirty, setSchemaDirty] = useState3(false);
817
- useEffect4(() => setSchemaDirty(true), [
549
+ const [schemaChanged, setSchemaChanged] = useState(false);
550
+ useEffect(() => setSchemaChanged(true), [
818
551
  schema
819
552
  ]);
820
- useEffect4(() => {
821
- if (schemaDirty) {
553
+ useEffect(() => {
554
+ if (schemaChanged) {
822
555
  validate(values);
823
- setSchemaDirty(false);
556
+ setSchemaChanged(false);
824
557
  }
825
558
  }, [
559
+ validate,
826
560
  values,
827
- schemaDirty
561
+ schemaChanged
828
562
  ]);
829
- const formIsValid = useMemo2(() => {
563
+ const isValid = useMemo(() => {
830
564
  return Object.keys(errors).length === 0;
831
565
  }, [
832
566
  errors
833
567
  ]);
834
- const canSave = useMemo2(() => !saving && // Check if any error paths that are touched have errors.
568
+ const canSave = useMemo(() => !saving && // Check if any error paths that are touched have errors.
835
569
  !Object.entries(touched).some(([path, isTouched]) => isTouched && Object.keys(errors).some((errorPath) => errorPath === path || errorPath.startsWith(`${path}.`) || errorPath.startsWith(`${path}[`))), [
836
570
  touched,
837
571
  errors,
838
572
  saving
839
573
  ]);
840
- const handleSave = useCallback4(async () => {
574
+ const handleSave = useCallback(async () => {
841
575
  if (validate(values)) {
842
576
  setSaving(true);
843
577
  try {
844
578
  await onSave?.(values, {
845
- changed
579
+ changed,
580
+ isValid
846
581
  });
847
582
  } finally {
848
583
  setSaving(false);
@@ -852,11 +587,17 @@ var useForm = ({ schema, initialValues, onValuesChanged, onValidate, onValid, on
852
587
  values,
853
588
  validate,
854
589
  onSave,
855
- changed
590
+ changed,
591
+ isValid
856
592
  ]);
857
- const getStatus = useCallback4((path) => {
593
+ const handleCancel = useCallback(() => {
594
+ onCancel?.();
595
+ }, [
596
+ onCancel
597
+ ]);
598
+ const getStatus = useCallback((path) => {
858
599
  const jsonPath = Array.isArray(path) ? createJsonPath(path) : path;
859
- const matchingError = Object.entries(errors).find(([errorPath]) => errorPath === jsonPath || errorPath.startsWith(`${jsonPath}.`) || errorPath.startsWith(`${jsonPath}[`));
600
+ const [_, error] = Object.entries(errors).find(([errorPath]) => errorPath === jsonPath || errorPath.startsWith(`${jsonPath}.`) || errorPath.startsWith(`${jsonPath}[`)) ?? [];
860
601
  const isTouched = touched[jsonPath];
861
602
  if (!isTouched) {
862
603
  return {
@@ -865,111 +606,120 @@ var useForm = ({ schema, initialValues, onValuesChanged, onValidate, onValid, on
865
606
  };
866
607
  }
867
608
  return {
868
- status: matchingError ? "error" : void 0,
869
- error: matchingError ? matchingError[1] : void 0
609
+ status: error ? "error" : void 0,
610
+ error: error ? error ?? void 0 : void 0
870
611
  };
871
612
  }, [
872
613
  errors,
873
614
  touched
874
615
  ]);
875
- const getFormValue = useCallback4((path) => {
876
- return getValue(values, createJsonPath(path));
616
+ const getValue = useCallback((path) => {
617
+ return getValue$(values, createJsonPath(path));
877
618
  }, [
878
619
  values
879
620
  ]);
880
- const onValueChange = useCallback4((path, type, value) => {
621
+ const onValueChange = useCallback((path, type, value3) => {
622
+ log("onValueChange", {
623
+ path,
624
+ value: value3
625
+ }, {
626
+ F: __dxlog_file,
627
+ L: 259,
628
+ S: void 0,
629
+ C: (f, a) => f(...a)
630
+ });
881
631
  const jsonPath = createJsonPath(path);
882
- let parsedValue = value;
632
+ let parsedValue = value3;
883
633
  try {
884
- if (type === "number") {
885
- parsedValue = parseFloat(value) || 0;
634
+ if (type._tag === "NumberKeyword") {
635
+ parsedValue = parseFloat(value3) || 0;
886
636
  }
887
637
  } catch (err) {
888
638
  log.catch(err, void 0, {
889
639
  F: __dxlog_file,
890
- L: 213,
640
+ L: 268,
891
641
  S: void 0,
892
642
  C: (f, a) => f(...a)
893
643
  });
894
644
  parsedValue = void 0;
895
645
  }
896
- const newValues = {
897
- ...setValue(values, jsonPath, parsedValue)
646
+ const newValues = Obj2.isObject(values) ? (setValueEchoAware(values, jsonPath, parsedValue), values) : {
647
+ ...setValue$(values, jsonPath, parsedValue)
898
648
  };
899
649
  setValues(newValues);
900
- setChanged((prev) => ({
901
- ...prev,
650
+ const newChanged = {
902
651
  [jsonPath]: true
903
- }));
904
- onValuesChanged?.(newValues);
905
- const isValid = validate(newValues);
906
- if (isValid && onValid) {
907
- onValid(newValues, {
908
- changed
909
- });
910
- }
652
+ };
653
+ setChanged({
654
+ ...changed,
655
+ ...newChanged
656
+ });
657
+ const isValid2 = validate(newValues);
658
+ onValuesChanged?.(newValues, {
659
+ isValid: isValid2,
660
+ changed: newChanged
661
+ });
911
662
  }, [
912
663
  values,
913
- onValuesChanged,
664
+ changed,
914
665
  validate,
915
- onValid,
916
- changed
666
+ onValuesChanged
917
667
  ]);
918
- const onTouched = useCallback4((path) => {
668
+ const onBlur = useCallback(async (path) => {
919
669
  const jsonPath = createJsonPath(path);
920
670
  setTouched((touched2) => ({
921
671
  ...touched2,
922
672
  [jsonPath]: true
923
673
  }));
924
- validate(values);
674
+ const isValid2 = validate(values);
675
+ if (Object.keys(changed).length > 0 && isValid2 && autoSave) {
676
+ await onSave?.(values, {
677
+ changed,
678
+ isValid: isValid2
679
+ });
680
+ }
925
681
  }, [
926
682
  validate,
927
- values
683
+ values,
684
+ changed,
685
+ autoSave,
686
+ onSave
928
687
  ]);
929
- return useMemo2(() => ({
688
+ return useMemo(() => ({
930
689
  // State.
690
+ schema,
931
691
  values,
932
692
  errors,
933
693
  touched,
934
694
  changed,
695
+ isValid,
935
696
  canSave,
936
- formIsValid,
697
+ autoSave,
937
698
  // Actions.
938
- handleSave,
699
+ onSave: handleSave,
700
+ onCancel: handleCancel,
939
701
  // Field utils.
940
702
  getStatus,
941
- getValue: getFormValue,
703
+ getValue,
704
+ onBlur,
942
705
  onValueChange,
943
- onTouched
706
+ ...props
944
707
  }), [
708
+ schema,
945
709
  values,
946
710
  errors,
947
711
  touched,
948
712
  changed,
949
713
  canSave,
950
- formIsValid,
951
- handleSave,
714
+ isValid,
952
715
  getStatus,
953
- getFormValue,
716
+ getValue,
717
+ onBlur,
954
718
  onValueChange,
955
- onTouched
719
+ onSave,
720
+ onCancel
956
721
  ]);
957
722
  };
958
- var createKeySet = (obj, value) => {
959
- invariant(obj, void 0, {
960
- F: __dxlog_file,
961
- L: 275,
962
- S: void 0,
963
- A: [
964
- "obj",
965
- ""
966
- ]
967
- });
968
- return Object.keys(obj).reduce((acc, key) => ({
969
- ...acc,
970
- [key]: value
971
- }), {});
972
- };
973
723
  var flatMap = (errors) => {
974
724
  return errors.reduce((result, { path, message }) => {
975
725
  const jsonPath = fromEffectValidationPath(path);
@@ -980,1387 +730,1795 @@ var flatMap = (errors) => {
980
730
  }, {});
981
731
  };
982
732
 
983
- // src/hooks/useRefQueryLookupHandler.ts
984
- import { useCallback as useCallback5 } from "react";
985
- import { getObjectDXN } from "@dxos/echo-schema";
986
- import { Filter } from "@dxos/react-client/echo";
987
- import { isNonNullable } from "@dxos/util";
988
- var useRefQueryLookupHandler = ({ space }) => {
989
- return useCallback5(async (typeInfo) => {
990
- if (!space) {
991
- return [];
992
- }
993
- const query = space.db.query(Filter.typename(typeInfo.typename));
994
- const { objects } = await query.run();
995
- return objects.map((object) => {
996
- const dxn = getObjectDXN(object);
997
- if (!dxn) {
998
- return void 0;
733
+ // src/hooks/useKeyHandler.tsx
734
+ import { useCallback as useCallback2, useEffect as useEffect2 } from "react";
735
+ import { addEventListener } from "@dxos/async";
736
+ var useKeyHandler = (formElement, form) => {
737
+ const handleKeyDown = useCallback2((event) => {
738
+ switch (event.key) {
739
+ case "Enter": {
740
+ if (event.metaKey && form.canSave) {
741
+ form.onSave();
742
+ }
743
+ break;
999
744
  }
1000
- const item = {
1001
- dxn,
1002
- label: object?.name ?? object?.id ?? ""
1003
- };
1004
- return item;
1005
- }).filter(isNonNullable);
745
+ }
1006
746
  }, [
1007
- space
747
+ form.isValid,
748
+ form.canSave,
749
+ form.onSave,
750
+ form.autoSave
1008
751
  ]);
1009
- };
1010
-
1011
- // src/hooks/useQueryRefOptions.ts
1012
- import { useEffect as useEffect5, useState as useState4 } from "react";
1013
- import { log as log2 } from "@dxos/log";
1014
- var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-form/src/hooks/useQueryRefOptions.ts";
1015
- var useQueryRefOptions = ({ refTypeInfo, onQueryRefOptions }) => {
1016
- const [options, setOptions] = useState4([]);
1017
- const [loading, setLoading] = useState4(false);
1018
- useEffect5(() => {
1019
- if (!refTypeInfo || !onQueryRefOptions) {
752
+ useEffect2(() => {
753
+ if (!formElement) {
1020
754
  return;
1021
755
  }
1022
- const fetchOptions = async () => {
1023
- setLoading(true);
1024
- try {
1025
- const fetchedOptions = await onQueryRefOptions(refTypeInfo);
1026
- setOptions(fetchedOptions.map((option) => {
1027
- const dxn = option.dxn.toString();
1028
- return {
1029
- id: dxn,
1030
- label: option.label ?? dxn,
1031
- hue: "neutral"
1032
- };
1033
- }));
1034
- } catch (error) {
1035
- log2.error("Failed to fetch ref options:", error, {
1036
- F: __dxlog_file2,
1037
- L: 42,
1038
- S: void 0,
1039
- C: (f, a) => f(...a)
1040
- });
1041
- setOptions([]);
1042
- } finally {
1043
- setLoading(false);
1044
- }
1045
- };
1046
- void fetchOptions();
756
+ return addEventListener(formElement, "keydown", handleKeyDown);
1047
757
  }, [
1048
- refTypeInfo,
1049
- onQueryRefOptions
758
+ formElement,
759
+ handleKeyDown
1050
760
  ]);
1051
- return {
1052
- options,
1053
- loading
1054
- };
1055
761
  };
1056
762
 
1057
- // src/components/Form/FormContext.tsx
1058
- var FormContext = /* @__PURE__ */ createContext(void 0);
1059
- var useFormContext = () => {
1060
- return useContext(FormContext) ?? raise(new Error("Missing FormContext"));
1061
- };
1062
- var useFormValues = (path = []) => {
1063
- const { values: formValues } = useFormContext();
1064
- const jsonPath = createJsonPath2(path);
1065
- return getValue2(formValues, jsonPath);
1066
- };
1067
- var useInputProps = (path = []) => {
1068
- const { getStatus, getValue: getFormValue, onValueChange, onTouched } = useFormContext();
1069
- const stablePath = useMemo3(() => path, [
1070
- Array.isArray(path) ? path.join(".") : path
1071
- ]);
1072
- return useMemo3(() => ({
1073
- getStatus: () => getStatus(stablePath),
1074
- getValue: () => getFormValue(stablePath),
1075
- onValueChange: (type, value) => onValueChange(stablePath, type, value),
1076
- onBlur: () => onTouched(stablePath)
1077
- }), [
1078
- getStatus,
1079
- getFormValue,
1080
- onValueChange,
1081
- onTouched,
1082
- stablePath
1083
- ]);
1084
- };
1085
- var FormProvider = ({ children, formRef, autoSave, ...formOptions }) => {
1086
- var _effect = _useSignals8();
1087
- try {
1088
- const form = useForm(formOptions);
1089
- const handleKeyDown = useCallback6((event) => {
1090
- const keyIsEnter = event.key === "Enter";
1091
- const modifierUsed = event.ctrlKey || event.altKey || event.metaKey || event.shiftKey;
1092
- const inputIsTextarea = event.target.tagName.toLowerCase() === "textarea";
1093
- const inputOptOut = event.target.hasAttribute("data-no-submit") || event.target.closest("[data-no-submit]") !== null;
1094
- const shouldSubmitRegularInput = !inputIsTextarea && keyIsEnter && !modifierUsed;
1095
- const shouldSubmitTextarea = inputIsTextarea && keyIsEnter && event.metaKey;
1096
- if ((shouldSubmitRegularInput || shouldSubmitTextarea) && !inputOptOut) {
1097
- if (!autoSave && form.canSave) {
1098
- form.handleSave();
1099
- }
1100
- if (autoSave && form.formIsValid) {
1101
- event.target.blur();
1102
- }
1103
- }
1104
- }, [
1105
- form,
1106
- autoSave
1107
- ]);
1108
- useEffect6(() => {
1109
- if (!formRef?.current) {
1110
- return;
1111
- }
1112
- const formElement = formRef.current;
1113
- formElement.addEventListener("keydown", handleKeyDown);
1114
- return () => formElement.removeEventListener("keydown", handleKeyDown);
1115
- }, [
1116
- formRef,
1117
- form.canSave,
1118
- form.formIsValid,
1119
- form.handleSave,
1120
- autoSave
1121
- ]);
1122
- return /* @__PURE__ */ React8.createElement(FormContext.Provider, {
1123
- value: form
1124
- }, children);
1125
- } finally {
1126
- _effect.f();
1127
- }
763
+ // src/components/Form/FormFieldComponent.tsx
764
+ import React2, { Component } from "react";
765
+ import { Icon, Input as Input2, Tooltip } from "@dxos/react-ui";
766
+ import { errorText, mx as mx2 } from "@dxos/ui-theme";
767
+ var labelSpacing = "mbs-inputSpacingBlock mbe-labelSpacingBlock";
768
+ var FormFieldLabel = ({ label, error, readonly, asChild }) => {
769
+ const Label = readonly || asChild ? "span" : Input2.Label;
770
+ const labelProps = readonly || asChild ? {
771
+ className: "text-description text-sm"
772
+ } : {
773
+ classNames: "!mlb-0 text-sm"
774
+ };
775
+ return /* @__PURE__ */ React2.createElement("div", {
776
+ role: "none",
777
+ className: mx2("flex justify-between items-center", labelSpacing)
778
+ }, /* @__PURE__ */ React2.createElement(Label, labelProps, label), error && /* @__PURE__ */ React2.createElement(Tooltip.Trigger, {
779
+ asChild: true,
780
+ content: error,
781
+ side: "bottom"
782
+ }, /* @__PURE__ */ React2.createElement(Icon, {
783
+ icon: "ph--warning--regular",
784
+ size: 4,
785
+ classNames: errorText
786
+ })));
1128
787
  };
1129
-
1130
- // src/components/Form/FormActions.tsx
1131
- var FormActions = ({ readonly, onCancel, outerSpacing = true }) => {
1132
- var _effect = _useSignals9();
1133
- try {
1134
- const { t } = useTranslation5(translationKey);
1135
- const { canSave, handleSave } = useFormContext();
1136
- return /* @__PURE__ */ React9.createElement("div", {
1137
- role: "none",
1138
- className: mx6(outerSpacing === "scroll-fields" ? "pli-cardSpacingInline mbe-cardSpacingBlock" : outerSpacing ? cardSpacing : "mbs-cardSpacingBlock", "flex [&_button]:grow gap-1 first:mbs-0")
1139
- }, onCancel && !readonly && /* @__PURE__ */ React9.createElement(IconButton2, {
1140
- "data-testid": "cancel-button",
1141
- icon: "ph--x--regular",
1142
- label: t("button cancel"),
1143
- onClick: onCancel
1144
- }), handleSave && /* @__PURE__ */ React9.createElement(IconButton2, {
1145
- type: "submit",
1146
- "data-testid": "save-button",
1147
- disabled: !canSave,
1148
- icon: "ph--check--regular",
1149
- label: t("button save"),
1150
- onClick: handleSave
1151
- }));
1152
- } finally {
1153
- _effect.f();
788
+ FormFieldLabel.displayName = "Form.FieldLabel";
789
+ var FormFieldWrapper = (props) => {
790
+ const { children, readonly, layout, label, getStatus, getValue } = props;
791
+ const { status, error } = getStatus();
792
+ const value3 = getValue();
793
+ if (layout === "static" && value3 == null) {
794
+ return null;
1154
795
  }
796
+ const str = String(value3 ?? "");
797
+ return /* @__PURE__ */ React2.createElement(Input2.Root, {
798
+ validationValence: status
799
+ }, layout !== "inline" && /* @__PURE__ */ React2.createElement(FormFieldLabel, {
800
+ error,
801
+ readonly,
802
+ label
803
+ }), layout === "static" ? /* @__PURE__ */ React2.createElement("p", null, str) : children ? children({
804
+ value: value3
805
+ }) : null, layout === "full" && /* @__PURE__ */ React2.createElement(Input2.DescriptionAndValidation, null, /* @__PURE__ */ React2.createElement(Input2.Validation, null, error)));
1155
806
  };
1156
-
1157
- // src/components/Form/FormContent.tsx
1158
- import { useSignals as _useSignals12 } from "@preact-signals/safe-react/tracking";
1159
- import { Schema, SchemaAST as SchemaAST4, pipe as pipe2 } from "effect";
1160
- import { capitalize as capitalize2 } from "effect/String";
1161
- import React12, { forwardRef, useMemo as useMemo5 } from "react";
1162
- import { createJsonPath as createJsonPath3, findNode as findNode3, getDiscriminatedType as getDiscriminatedType2, isDiscriminatedUnion as isDiscriminatedUnion2 } from "@dxos/effect";
1163
- import { mx as mx8 } from "@dxos/react-ui-theme";
1164
- import { getSchemaProperties as getSchemaProperties2 } from "@dxos/schema";
1165
- import { isNotFalsy } from "@dxos/util";
1166
-
1167
- // src/components/Form/ArrayField.tsx
1168
- import { useSignals as _useSignals10 } from "@preact-signals/safe-react/tracking";
1169
- import { SchemaAST as SchemaAST3, pipe } from "effect";
1170
- import { capitalize } from "effect/String";
1171
- import React10, { Fragment, useCallback as useCallback7 } from "react";
1172
- import { findNode as findNode2, getDiscriminatedType, isDiscriminatedUnion, SimpleType } from "@dxos/effect";
1173
- import { invariant as invariant2 } from "@dxos/invariant";
1174
- import { IconButton as IconButton3, useTranslation as useTranslation6 } from "@dxos/react-ui";
1175
- import { getSchemaProperties } from "@dxos/schema";
1176
-
1177
- // src/util/field.ts
1178
- import { FormatEnum, GeoLocation as GeoLocation2, TypeEnum } from "@dxos/echo-schema";
1179
- var parseValue = ({ type, format, value }) => {
1180
- if (value === void 0 || value === null || value === "") {
1181
- return void 0;
807
+ var FormFieldErrorBoundary = class extends Component {
808
+ static getDerivedStateFromError(error) {
809
+ return {
810
+ error
811
+ };
1182
812
  }
1183
- const parseScalar = (type2) => {
1184
- switch (type2) {
1185
- case TypeEnum.Boolean: {
1186
- if (typeof value === "string") {
1187
- const lowercaseValue = value.toLowerCase();
1188
- if (lowercaseValue === "0" || lowercaseValue === "false") {
1189
- return false;
1190
- } else if (lowercaseValue === "1" || lowercaseValue === "true") {
1191
- return true;
1192
- }
1193
- }
1194
- return Boolean(value);
1195
- }
1196
- case TypeEnum.Number: {
1197
- const cleaned = typeof value === "string" ? value.replace(/[^0-9.-]+/g, "") : value;
1198
- const num = Number(cleaned);
1199
- return Number.isNaN(num) ? null : num;
1200
- }
1201
- case TypeEnum.String:
1202
- case TypeEnum.Ref:
1203
- return String(value);
1204
- default: {
1205
- return value;
1206
- }
1207
- }
813
+ state = {
814
+ error: void 0
1208
815
  };
1209
- if (!format) {
1210
- return parseScalar(type);
1211
- }
1212
- switch (format) {
1213
- case FormatEnum.Boolean:
1214
- return parseScalar(TypeEnum.Boolean);
1215
- case FormatEnum.Number:
1216
- case FormatEnum.Percent:
1217
- case FormatEnum.Currency:
1218
- return parseScalar(TypeEnum.Number);
1219
- case FormatEnum.String:
1220
- case FormatEnum.Markdown: {
1221
- return parseScalar(TypeEnum.String);
1222
- }
1223
- case FormatEnum.Ref:
1224
- throw new Error(`unexpected format: ${FormatEnum.Ref}`);
1225
- case FormatEnum.DateTime:
1226
- case FormatEnum.Date:
1227
- case FormatEnum.Time:
1228
- case FormatEnum.Timestamp: {
1229
- const date = new Date(value);
1230
- return isNaN(date.getTime()) ? null : date;
1231
- }
1232
- case FormatEnum.GeoPoint: {
1233
- if (typeof value === "string") {
1234
- const cleaned = value.replace(/[^0-9.,-]+/g, "");
1235
- const parts = cleaned.split(",");
1236
- if (parts.length === 2) {
1237
- const lat = parseFloat(parts[0]);
1238
- const long = parseFloat(parts[1]);
1239
- if (!isNaN(lat) && !isNaN(long)) {
1240
- return GeoLocation2.toGeoPoint({
1241
- latitude: lat,
1242
- longitude: long
1243
- });
1244
- }
1245
- }
1246
- }
1247
- return [
1248
- 0,
1249
- 0
1250
- ];
1251
- }
1252
- default: {
1253
- return value;
816
+ componentDidUpdate(prevProps) {
817
+ if (prevProps.path !== this.props.path) {
818
+ this.resetError();
1254
819
  }
1255
820
  }
1256
- };
1257
- var cellClassesForFieldType = ({ type, format }) => {
1258
- if (!format) {
1259
- switch (type) {
1260
- case TypeEnum.Number:
1261
- return [
1262
- "text-right",
1263
- "font-mono"
1264
- ];
1265
- case TypeEnum.Boolean:
1266
- return [
1267
- "text-right",
1268
- "font-mono"
1269
- ];
1270
- case TypeEnum.String:
1271
- return void 0;
1272
- case TypeEnum.Ref:
1273
- return void 0;
1274
- default: {
1275
- return void 0;
1276
- }
821
+ render() {
822
+ if (this.state.error) {
823
+ return /* @__PURE__ */ React2.createElement("div", {
824
+ className: "flex gap-2 border border-roseFill font-mono text-sm"
825
+ }, /* @__PURE__ */ React2.createElement("span", {
826
+ className: "bg-roseFill text-surfaceText pli-1 font-thin"
827
+ }, "ERROR"), String(this.props.path?.join(".")));
1277
828
  }
829
+ return this.props.children;
1278
830
  }
1279
- switch (format) {
1280
- case FormatEnum.Markdown:
1281
- return void 0;
1282
- case FormatEnum.Time:
1283
- case FormatEnum.Timestamp:
1284
- case FormatEnum.DateTime:
1285
- case FormatEnum.Date:
1286
- case FormatEnum.Duration:
1287
- return [
1288
- "font-mono",
1289
- "text-right"
1290
- ];
1291
- case FormatEnum.Currency:
1292
- return [
1293
- "text-right"
1294
- ];
1295
- case FormatEnum.JSON:
1296
- case FormatEnum.DID:
1297
- return [
1298
- "font-mono"
1299
- ];
1300
- default:
1301
- return void 0;
831
+ resetError() {
832
+ this.setState({
833
+ error: void 0
834
+ });
1302
835
  }
1303
836
  };
1304
- var pathNotUniqueError = (path) => ({
1305
- path: "path",
1306
- message: `'${path}' is already present`
1307
- });
1308
837
 
1309
- // src/util/formatting.ts
1310
- import { format as formatDate } from "date-fns/format";
1311
- import { FormatEnum as FormatEnum2, GeoLocation as GeoLocation3, TypeEnum as TypeEnum2 } from "@dxos/echo-schema";
1312
- var formatForDisplay = ({ type, format, value, locale = void 0 }) => {
1313
- const formatScalar = (type2) => {
1314
- switch (type2) {
1315
- case TypeEnum2.Boolean:
1316
- return value.toLocaleString().toUpperCase();
1317
- case TypeEnum2.Number:
1318
- return value.toLocaleString(locale);
1319
- case TypeEnum2.String:
1320
- case TypeEnum2.Ref:
1321
- return String(value);
1322
- default: {
1323
- return String(value);
1324
- }
1325
- }
838
+ // src/components/Form/FormFieldSet.tsx
839
+ import React4, { forwardRef, useMemo as useMemo3 } from "react";
840
+ import { mx as mx3 } from "@dxos/ui-theme";
841
+ import { isTruthy } from "@dxos/util";
842
+
843
+ // src/components/Form/FormField.tsx
844
+ import * as Match from "effect/Match";
845
+ import * as Option2 from "effect/Option";
846
+ import * as Schema from "effect/Schema";
847
+ import * as SchemaAST from "effect/SchemaAST";
848
+ import * as String2 from "effect/String";
849
+ import React3, { useMemo as useMemo2 } from "react";
850
+ import { Format as Format3 } from "@dxos/echo";
851
+ import { createJsonPath as createJsonPath2, findNode, getAnnotation, getDiscriminatedType, isArrayType as isArrayType2, isDiscriminatedUnion, isLiteralUnion, isNestedType } from "@dxos/effect";
852
+ import { useTranslation as useTranslation2 } from "@dxos/react-ui";
853
+ var FormField = (props) => {
854
+ const {
855
+ type,
856
+ name,
857
+ path,
858
+ projection,
859
+ fieldMap,
860
+ fieldProvider,
861
+ readonly,
862
+ layout,
863
+ // RefFieldProps
864
+ createOptionLabel,
865
+ createOptionIcon,
866
+ createInitialValuePath,
867
+ db,
868
+ schemaHook,
869
+ getOptions: getRefOptions,
870
+ onCreate
871
+ } = props;
872
+ const { t } = useTranslation2(translationKey);
873
+ const title = getAnnotation(SchemaAST.TitleAnnotationId)(type);
874
+ const description = getAnnotation(SchemaAST.DescriptionAnnotationId)(type);
875
+ const examples = getAnnotation(SchemaAST.ExamplesAnnotationId)(type);
876
+ const label = useMemo2(() => title ?? String2.capitalize(name), [
877
+ title,
878
+ name
879
+ ]);
880
+ const placeholder = useMemo2(() => examples?.length ? `${t("example placeholder")}: ${examples[0]}` : description ?? label, [
881
+ examples,
882
+ description,
883
+ label
884
+ ]);
885
+ const fieldState = useFormFieldState(FormField.displayName, path);
886
+ const fieldProps = {
887
+ type,
888
+ format: Format3.FormatAnnotation.getFromAst(type).pipe((annotation) => Option2.getOrUndefined(annotation)),
889
+ readonly,
890
+ label,
891
+ placeholder,
892
+ layout,
893
+ ...fieldState
1326
894
  };
1327
- if (!format) {
1328
- return formatScalar(type);
895
+ const jsonPath = createJsonPath2(path ?? []);
896
+ const CustomField = fieldMap?.[jsonPath];
897
+ if (CustomField) {
898
+ return /* @__PURE__ */ React3.createElement(CustomField, fieldProps);
1329
899
  }
1330
- switch (format) {
1331
- case FormatEnum2.Boolean: {
1332
- return formatScalar(TypeEnum2.Boolean);
1333
- }
1334
- case FormatEnum2.Number: {
1335
- return formatScalar(TypeEnum2.Number);
1336
- }
1337
- case FormatEnum2.String:
1338
- case FormatEnum2.Ref: {
1339
- return formatScalar(TypeEnum2.String);
1340
- }
1341
- case FormatEnum2.Percent: {
1342
- return `${value * 100}%`;
1343
- }
1344
- case FormatEnum2.Currency: {
1345
- return value.toLocaleString(locale, {
1346
- style: "currency",
1347
- currency: "USD",
1348
- minimumFractionDigits: 2,
1349
- maximumFractionDigits: 2
900
+ const component = fieldProvider?.({
901
+ schema: Schema.make(type),
902
+ prop: name,
903
+ fieldProps
904
+ });
905
+ if (component) {
906
+ return component;
907
+ }
908
+ if (isArrayType2(type)) {
909
+ return /* @__PURE__ */ React3.createElement(ArrayField, {
910
+ fieldProps: fieldState,
911
+ label,
912
+ ...props
913
+ });
914
+ }
915
+ const Field = getFormField(fieldProps);
916
+ if (Field) {
917
+ return /* @__PURE__ */ React3.createElement(Field, fieldProps);
918
+ }
919
+ const options = getOptions(type);
920
+ if (options) {
921
+ return /* @__PURE__ */ React3.createElement(SelectField, {
922
+ ...fieldProps,
923
+ options: options.map((option) => ({
924
+ value: option,
925
+ label: option.toString()
926
+ }))
927
+ });
928
+ }
929
+ const refProps = getRefProps(type);
930
+ if (refProps) {
931
+ return /* @__PURE__ */ React3.createElement(RefField, {
932
+ ...fieldProps,
933
+ ...refProps,
934
+ createOptionLabel,
935
+ createOptionIcon,
936
+ createInitialValuePath,
937
+ db,
938
+ schemaHook,
939
+ getOptions: getRefOptions,
940
+ onCreate
941
+ });
942
+ }
943
+ if (isNestedType(type)) {
944
+ const baseNode = findNode(type, isDiscriminatedUnion);
945
+ const typeLiteral = baseNode ? getDiscriminatedType(baseNode, fieldState.getValue()) : findNode(type, SchemaAST.isTypeLiteral);
946
+ if (typeLiteral) {
947
+ const schema = Schema.make(typeLiteral);
948
+ return /* @__PURE__ */ React3.createElement(FormFieldSet, {
949
+ schema,
950
+ path,
951
+ readonly,
952
+ layout,
953
+ label,
954
+ projection,
955
+ fieldMap,
956
+ fieldProvider,
957
+ createOptionLabel,
958
+ createOptionIcon,
959
+ createInitialValuePath,
960
+ db,
961
+ schemaHook,
962
+ getOptions: getRefOptions,
963
+ onCreate
1350
964
  });
1351
965
  }
1352
- case FormatEnum2.Date: {
1353
- try {
1354
- return formatDate(new Date(value), "yyyy-MM-dd");
1355
- } catch (error) {
1356
- return "Invalid Date";
1357
- }
1358
- }
1359
- case FormatEnum2.Time: {
1360
- try {
1361
- return formatDate(new Date(value), "HH:mm:ss");
1362
- } catch (error) {
1363
- return "Invalid Time";
1364
- }
1365
- }
1366
- case FormatEnum2.DateTime: {
1367
- try {
1368
- return formatDate(new Date(value), "yyyy-MM-dd HH:mm:ss");
1369
- } catch (error) {
1370
- return "Invalid DateTime";
1371
- }
966
+ }
967
+ return null;
968
+ };
969
+ FormField.displayName = "Form.FormField";
970
+ var getFormField = ({ type, format }) => {
971
+ const formatField = Match.value(format).pipe(Match.withReturnType(), Match.when(Format3.TypeFormat.GeoPoint, () => GeoPointField), Match.when(Format3.TypeFormat.Markdown, () => MarkdownField), Match.when(Format3.TypeFormat.Text, () => TextAreaField), Match.orElse(() => void 0));
972
+ if (formatField) {
973
+ return formatField;
974
+ }
975
+ switch (type._tag) {
976
+ // TODO(wittjosiah): Schema.Any is currently used to represent template inputs.
977
+ case "AnyKeyword":
978
+ case "StringKeyword":
979
+ return TextField;
980
+ case "NumberKeyword":
981
+ return NumberField;
982
+ case "BooleanKeyword":
983
+ return BooleanField;
984
+ }
985
+ };
986
+ var getOptions = (ast) => {
987
+ if (isLiteralUnion(ast)) {
988
+ return ast.types.map((type) => type.literal).filter((v) => v !== null);
989
+ }
990
+ return Format3.OptionsAnnotation.getFromAst(ast).pipe((annotation) => Option2.getOrUndefined(annotation));
991
+ };
992
+
993
+ // src/components/Form/FormFieldSet.tsx
994
+ var FormFieldSet = /* @__PURE__ */ forwardRef(({ classNames, label, schema, readonly, path, exclude, sort, projection, layout, ...props }, forwardRef4) => {
995
+ const values = useFormValues(FormFieldSet.displayName, path);
996
+ const properties = useMemo3(() => {
997
+ if (!schema) {
998
+ return [];
1372
999
  }
1373
- case FormatEnum2.GeoPoint: {
1374
- if (value === null || value === void 0) {
1375
- return "";
1376
- }
1377
- if (Array.isArray(value) && value.length >= 2 && value.every(Number.isFinite)) {
1378
- const { latitude, longitude } = GeoLocation3.fromGeoPoint(value);
1379
- return `${latitude},${longitude}`;
1000
+ const props2 = getFormProperties(schema.ast);
1001
+ if (projection) {
1002
+ const fieldProjections = projection.getFieldProjections();
1003
+ const hiddenProperties = new Set(projection.getHiddenProperties());
1004
+ const visibleProps = props2.filter((prop) => !hiddenProperties.has(prop.name.toString()));
1005
+ const orderedProps = [];
1006
+ for (const fieldProjection of fieldProjections) {
1007
+ const fieldPath = String(fieldProjection.field.path);
1008
+ const prop = visibleProps.find((p) => p.name === fieldPath);
1009
+ if (prop) {
1010
+ orderedProps.push(prop);
1011
+ }
1380
1012
  }
1381
- return String(value);
1013
+ const projectionPaths = new Set(fieldProjections.map((projection2) => String(projection2.field.path)));
1014
+ const remainingProps = visibleProps.filter((prop) => !projectionPaths.has(prop.name.toString()));
1015
+ orderedProps.push(...remainingProps);
1016
+ return orderedProps;
1382
1017
  }
1383
- default: {
1384
- if (value === null || value === "undefined") {
1385
- return "";
1386
- }
1387
- return String(value);
1018
+ const filtered = exclude ? exclude(props2) : props2;
1019
+ return sort ? filtered.sort(({ name: a }, { name: b }) => sort.indexOf(a.toString()) - sort.indexOf(b.toString())) : filtered;
1020
+ }, [
1021
+ schema,
1022
+ values,
1023
+ exclude,
1024
+ sort,
1025
+ projection?.fields
1026
+ ]);
1027
+ if ((readonly || layout === "static") && values == null) {
1028
+ return null;
1029
+ }
1030
+ return /* @__PURE__ */ React4.createElement("div", {
1031
+ role: "form",
1032
+ className: mx3("is-full", layout === "inline" && "flex flex-col gap-2", classNames),
1033
+ ref: forwardRef4
1034
+ }, layout !== "inline" && label && /* @__PURE__ */ React4.createElement(FormFieldLabel, {
1035
+ label,
1036
+ asChild: true
1037
+ }), properties.map((property) => {
1038
+ const name = property.name.toString();
1039
+ return /* @__PURE__ */ React4.createElement(FormFieldErrorBoundary, {
1040
+ key: name,
1041
+ path: [
1042
+ ...path ?? [],
1043
+ name
1044
+ ]
1045
+ }, /* @__PURE__ */ React4.createElement(FormField, {
1046
+ type: property.type,
1047
+ name,
1048
+ path: [
1049
+ ...path ?? [],
1050
+ name
1051
+ ],
1052
+ readonly,
1053
+ layout,
1054
+ projection,
1055
+ ...props
1056
+ }));
1057
+ }).filter(isTruthy));
1058
+ });
1059
+ FormFieldSet.displayName = "Form.Fields";
1060
+
1061
+ // src/components/Form/Form.tsx
1062
+ var omitId = (schema) => schema.pipe(Schema2.omit("id"));
1063
+ var [FormContextProvider, useFormContext] = createContext("Form");
1064
+ var useFormValues = (componentName, path = [], defaultValue) => {
1065
+ const jsonPath = createJsonPath3(path);
1066
+ const { form: { values } } = useFormContext(componentName);
1067
+ const value3 = getValue$2(values, jsonPath);
1068
+ useEffect3(() => {
1069
+ if (!value3 && defaultValue) {
1070
+ setValueEchoAware(values, jsonPath, defaultValue());
1388
1071
  }
1072
+ }, [
1073
+ value3,
1074
+ defaultValue
1075
+ ]);
1076
+ return value3;
1077
+ };
1078
+ var useFormFieldState = (componentName, path = []) => {
1079
+ const stablePath = useMemo4(() => path, [
1080
+ Array.isArray(path) ? path.join(".") : path
1081
+ ]);
1082
+ const { form: { getStatus, getValue, onBlur, onValueChange } } = useFormContext(componentName);
1083
+ return useMemo4(() => ({
1084
+ getStatus: () => getStatus(stablePath),
1085
+ getValue: () => getValue(stablePath),
1086
+ onBlur: () => onBlur(stablePath),
1087
+ onValueChange: (ast, value3) => onValueChange(stablePath, ast, value3)
1088
+ }), [
1089
+ getStatus,
1090
+ getValue,
1091
+ onBlur,
1092
+ onValueChange,
1093
+ stablePath
1094
+ ]);
1095
+ };
1096
+ var FormRoot = ({ children, schema, values, onSave, onCancel, ...props }) => {
1097
+ const form = useFormHandler({
1098
+ schema,
1099
+ values,
1100
+ onSave,
1101
+ onCancel,
1102
+ ...props
1103
+ });
1104
+ return /* @__PURE__ */ React5.createElement(FormContextProvider, {
1105
+ form,
1106
+ ...props
1107
+ }, children);
1108
+ };
1109
+ FormRoot.displayName = "Form.Root";
1110
+ var FormViewport = ({ classNames, children }) => {
1111
+ return /* @__PURE__ */ React5.createElement(ScrollArea.Root, null, /* @__PURE__ */ React5.createElement(ScrollArea.Viewport, {
1112
+ classNames: [
1113
+ "plb-cardSpacingBlock",
1114
+ classNames
1115
+ ]
1116
+ }, children), /* @__PURE__ */ React5.createElement(ScrollArea.Scrollbar, {
1117
+ orientation: "vertical"
1118
+ }, /* @__PURE__ */ React5.createElement(ScrollArea.Thumb, null)));
1119
+ };
1120
+ FormViewport.displayName = "Form.Viewport";
1121
+ var FormContent = ({ classNames, children }) => {
1122
+ const { form, testId } = useFormContext(FormContent.displayName);
1123
+ const ref = useRef(null);
1124
+ useKeyHandler(ref.current, form);
1125
+ return /* @__PURE__ */ React5.createElement("div", {
1126
+ ref,
1127
+ role: "form",
1128
+ className: mx4("flex flex-col is-full pli-cardSpacingInline density-fine", classNames),
1129
+ "data-testid": testId
1130
+ }, children);
1131
+ };
1132
+ FormContent.displayName = "Form.Content";
1133
+ var FormFieldSet2 = ({ classNames }) => {
1134
+ const { form, ...props } = useFormContext(FormFieldSet2.displayName);
1135
+ return /* @__PURE__ */ React5.createElement(FormFieldSet, {
1136
+ classNames,
1137
+ schema: form.schema,
1138
+ ...props
1139
+ });
1140
+ };
1141
+ FormFieldSet2.displayName = "Form.FieldSet";
1142
+ var FormActions = ({ classNames }) => {
1143
+ const { t } = useTranslation3(translationKey);
1144
+ const { form: { canSave, onSave, onCancel }, readonly, layout } = useFormContext(FormActions.displayName);
1145
+ if (readonly || layout === "static") {
1146
+ return null;
1389
1147
  }
1148
+ return /* @__PURE__ */ React5.createElement("div", {
1149
+ role: "none",
1150
+ className: mx4("grid grid-flow-col auto-cols-fr gap-2 pbs-cardSpacingBlock", classNames)
1151
+ }, onCancel && /* @__PURE__ */ React5.createElement(IconButton, {
1152
+ icon: "ph--x--regular",
1153
+ iconEnd: true,
1154
+ label: t("cancel button label"),
1155
+ onClick: onCancel,
1156
+ "data-testid": "cancel-button"
1157
+ }), onSave && /* @__PURE__ */ React5.createElement(IconButton, {
1158
+ type: "submit",
1159
+ variant: "primary",
1160
+ disabled: !canSave,
1161
+ icon: "ph--check--regular",
1162
+ iconEnd: true,
1163
+ label: t("save button label"),
1164
+ onClick: onSave,
1165
+ "data-testid": "save-button"
1166
+ }));
1390
1167
  };
1391
- var formatForEditing = ({ type, format, value, locale = void 0 }) => {
1392
- const formatScalar = (type2) => {
1393
- switch (type2) {
1394
- case TypeEnum2.Boolean:
1395
- case TypeEnum2.Number:
1396
- case TypeEnum2.String:
1397
- case TypeEnum2.Ref:
1398
- return String(value);
1399
- default:
1400
- return String(value);
1168
+ FormActions.displayName = "Form.Actions";
1169
+ var FormSubmit = ({ classNames, label, icon }) => {
1170
+ const { t } = useTranslation3(translationKey);
1171
+ const { form: { canSave, onSave }, readonly, layout } = useFormContext(FormSubmit.displayName);
1172
+ if (readonly || layout === "static") {
1173
+ return null;
1174
+ }
1175
+ return /* @__PURE__ */ React5.createElement("div", {
1176
+ role: "none",
1177
+ className: mx4("flex is-full pbs-cardSpacingBlock", classNames)
1178
+ }, /* @__PURE__ */ React5.createElement(IconButton, {
1179
+ classNames: "is-full",
1180
+ type: "submit",
1181
+ variant: "primary",
1182
+ disabled: !canSave,
1183
+ icon: icon ?? "ph--check--regular",
1184
+ label: label ?? t("save button label"),
1185
+ onClick: onSave,
1186
+ "data-testid": "save-button"
1187
+ }));
1188
+ };
1189
+ FormSubmit.displayName = "Form.Submit";
1190
+ var Form = {
1191
+ Root: FormRoot,
1192
+ Viewport: FormViewport,
1193
+ Content: FormContent,
1194
+ FieldSet: FormFieldSet2,
1195
+ Actions: FormActions,
1196
+ Submit: FormSubmit,
1197
+ Label: FormFieldLabel
1198
+ };
1199
+
1200
+ // src/components/Form/fields/ArrayField.tsx
1201
+ var ArrayField = ({ type, path, label, readonly, layout, fieldProps: inputProps, ...props }) => {
1202
+ const { t } = useTranslation4(translationKey);
1203
+ const elementType = getArrayElementType2(type);
1204
+ const { onValueChange } = inputProps;
1205
+ const values = useFormValues(ArrayField.displayName, path, () => []);
1206
+ const getDefaultObjectValue = (typeNode) => {
1207
+ const baseNode = findNode2(typeNode, isDiscriminatedUnion2);
1208
+ const typeLiteral = baseNode ? getDiscriminatedType2(baseNode, {}) : findNode2(typeNode, SchemaAST2.isTypeLiteral);
1209
+ if (!typeLiteral) {
1210
+ return {};
1401
1211
  }
1212
+ return Object.fromEntries(getFormProperties(typeLiteral).map((prop) => {
1213
+ const defaultValue = SchemaAST2.getDefaultAnnotation(prop.type).pipe((annotation) => Option3.getOrUndefined(annotation));
1214
+ return [
1215
+ prop.name,
1216
+ defaultValue
1217
+ ];
1218
+ }));
1402
1219
  };
1403
- if (!format) {
1404
- return formatScalar(type);
1220
+ const handleAdd = useCallback3(() => {
1221
+ const defaultValue = isNestedType2(type) && elementType ? getDefaultObjectValue(elementType) : getDefaultValue(elementType);
1222
+ values && onValueChange(type, [
1223
+ ...values,
1224
+ defaultValue
1225
+ ]);
1226
+ }, [
1227
+ onValueChange,
1228
+ type,
1229
+ elementType,
1230
+ values
1231
+ ]);
1232
+ const handleDelete = useCallback3((idx) => {
1233
+ onValueChange(type, values?.filter((_, i) => i !== idx));
1234
+ }, [
1235
+ onValueChange,
1236
+ type,
1237
+ values
1238
+ ]);
1239
+ if (!elementType || (readonly || layout === "static") && values && values.length < 1) {
1240
+ return null;
1405
1241
  }
1406
- switch (format) {
1407
- case FormatEnum2.Boolean:
1408
- case FormatEnum2.Number:
1409
- case FormatEnum2.String:
1410
- case FormatEnum2.Ref: {
1411
- return formatScalar(type);
1412
- }
1413
- case FormatEnum2.Percent: {
1414
- return String(value * 100);
1415
- }
1416
- case FormatEnum2.Currency: {
1417
- return String(value);
1418
- }
1419
- case FormatEnum2.DateTime: {
1420
- const date = new Date(value);
1421
- return date.toISOString();
1422
- }
1423
- case FormatEnum2.Date: {
1424
- const date = new Date(value);
1425
- return date.toISOString().split("T")[0];
1242
+ return /* @__PURE__ */ React6.createElement(React6.Fragment, null, (layout !== "static" || values && values.length > 0) && /* @__PURE__ */ React6.createElement(FormFieldLabel, {
1243
+ readonly,
1244
+ label,
1245
+ asChild: true
1246
+ }), /* @__PURE__ */ React6.createElement("div", {
1247
+ role: "none",
1248
+ className: "flex flex-col gap-2"
1249
+ }, values?.map((_, index) => {
1250
+ return /* @__PURE__ */ React6.createElement("div", {
1251
+ role: "none",
1252
+ key: index,
1253
+ className: "grid grid-cols-[1fr_min-content] gap-2 last:mb-3"
1254
+ }, /* @__PURE__ */ React6.createElement(FormField, {
1255
+ autoFocus: index === values.length - 1,
1256
+ type: elementType,
1257
+ path: [
1258
+ ...path ?? [],
1259
+ index
1260
+ ],
1261
+ readonly: readonly || layout === "static",
1262
+ layout: "inline",
1263
+ ...props
1264
+ }), !readonly && layout !== "static" && /* @__PURE__ */ React6.createElement("div", {
1265
+ role: "none",
1266
+ className: "flex flex-col bs-full justify-end"
1267
+ }, /* @__PURE__ */ React6.createElement("div", {
1268
+ role: "none",
1269
+ className: "flex items-center bs-[var(--line-height)]"
1270
+ }, /* @__PURE__ */ React6.createElement(IconButton2, {
1271
+ icon: "ph--x--regular",
1272
+ iconOnly: true,
1273
+ label: t("button remove"),
1274
+ onClick: () => handleDelete(index),
1275
+ classNames: "self-center"
1276
+ }))));
1277
+ })), !readonly && layout !== "static" && /* @__PURE__ */ React6.createElement(IconButton2, {
1278
+ classNames: "flex is-full _mlb-cardSpacingBlock",
1279
+ icon: "ph--plus--regular",
1280
+ label: t("add field"),
1281
+ onClick: handleAdd
1282
+ }));
1283
+ };
1284
+ ArrayField.displayName = "Form.ArrayField";
1285
+ var getDefaultValue = (ast) => {
1286
+ switch (ast?._tag) {
1287
+ case "StringKeyword": {
1288
+ return "";
1426
1289
  }
1427
- case FormatEnum2.Time: {
1428
- const date = new Date(value);
1429
- return date.toISOString().split("T")[1].split(".")[0];
1290
+ case "NumberKeyword": {
1291
+ return 0;
1430
1292
  }
1431
- case FormatEnum2.GeoPoint: {
1432
- if (value === null || value === void 0) {
1433
- return "";
1434
- }
1435
- if (Array.isArray(value) && value.length >= 2 && value.every(Number.isFinite)) {
1436
- const { latitude, longitude } = GeoLocation3.fromGeoPoint(value);
1437
- return `${latitude},${longitude}`;
1438
- }
1439
- return String(value);
1293
+ case "BooleanKeyword": {
1294
+ return false;
1440
1295
  }
1441
1296
  default: {
1442
- return String(value);
1297
+ if (ast && isNestedType2(ast)) {
1298
+ return {};
1299
+ } else {
1300
+ throw new Error(`Unsupported type: ${ast?._tag}`);
1301
+ }
1443
1302
  }
1444
1303
  }
1445
1304
  };
1446
1305
 
1447
- // src/util/refs.ts
1448
- import { FormatEnum as FormatEnum3, ReferenceAnnotationId } from "@dxos/echo-schema";
1449
- import { findAnnotation } from "@dxos/effect";
1450
-
1451
- // src/util/schema.ts
1452
- import { SchemaAST as SchemaAST2 } from "effect";
1453
- import { findNode } from "@dxos/effect";
1454
- var findArrayElementType = (ast) => {
1455
- const tupleType = findNode(ast, SchemaAST2.isTupleType);
1456
- const elementType = tupleType?.rest[0]?.type;
1457
- return elementType;
1306
+ // src/components/Form/fields/BooleanField.tsx
1307
+ import React7, { useCallback as useCallback4 } from "react";
1308
+ import { Input as Input3 } from "@dxos/react-ui";
1309
+ var BooleanField = ({ type, readonly, onValueChange, ...props }) => {
1310
+ const handleChange = useCallback4((value3) => onValueChange?.(type, value3), [
1311
+ type,
1312
+ onValueChange
1313
+ ]);
1314
+ return /* @__PURE__ */ React7.createElement(FormFieldWrapper, {
1315
+ readonly,
1316
+ ...props
1317
+ }, ({ value: value3 }) => /* @__PURE__ */ React7.createElement(Input3.Switch, {
1318
+ disabled: !!readonly,
1319
+ checked: value3,
1320
+ onCheckedChange: handleChange
1321
+ }));
1458
1322
  };
1459
1323
 
1460
- // src/util/refs.ts
1461
- var getRefProps = (property) => {
1462
- const { ast, format, array } = property;
1463
- if (format === FormatEnum3.Ref) {
1464
- return {
1465
- ast,
1466
- isArray: false
1467
- };
1468
- }
1469
- if (array) {
1470
- const elementType = findArrayElementType(ast);
1471
- if (elementType) {
1472
- const annotation = findAnnotation(elementType, ReferenceAnnotationId);
1473
- if (annotation) {
1474
- return {
1475
- ast: elementType,
1476
- isArray: true
1324
+ // src/components/Form/fields/GeoPointField.tsx
1325
+ import React8, { useCallback as useCallback5, useEffect as useEffect4, useMemo as useMemo5, useState as useState2 } from "react";
1326
+ import { GeoLocation as GeoLocation3 } from "@dxos/echo/internal";
1327
+ import { Input as Input4, useTranslation as useTranslation5 } from "@dxos/react-ui";
1328
+ import { safeParseFloat } from "@dxos/util";
1329
+ var GeoPointField = ({ type, label, readonly, layout, getStatus, getValue, onValueChange, onBlur }) => {
1330
+ const { t } = useTranslation5(translationKey);
1331
+ const { status, error } = getStatus();
1332
+ const geoPoint = useMemo5(() => getValue() ?? [
1333
+ 0,
1334
+ 0
1335
+ ], [
1336
+ getValue
1337
+ ]);
1338
+ const value3 = useMemo5(() => GeoLocation3.fromGeoPoint(geoPoint), [
1339
+ geoPoint
1340
+ ]);
1341
+ const [longitudeText, setLongitudeText] = useState2(value3.longitude?.toString());
1342
+ const [latitudeText, setLatitudeText] = useState2(value3.latitude?.toString());
1343
+ useEffect4(() => {
1344
+ const location = GeoLocation3.fromGeoPoint(geoPoint);
1345
+ setLongitudeText(location.longitude?.toString());
1346
+ setLatitudeText(location.latitude?.toString());
1347
+ }, [
1348
+ geoPoint
1349
+ ]);
1350
+ const handleChange = useCallback5((coordinateType, setText) => (event) => {
1351
+ const inputText = event.target.value;
1352
+ setText(inputText);
1353
+ if (inputText !== "" && inputText !== "-") {
1354
+ const coord = safeParseFloat(inputText);
1355
+ if (coord !== void 0 && !isNaN(coord)) {
1356
+ const currentLocation = GeoLocation3.fromGeoPoint(getValue() ?? [
1357
+ 0,
1358
+ 0
1359
+ ]);
1360
+ const newLocation = {
1361
+ ...currentLocation,
1362
+ [coordinateType]: coord
1477
1363
  };
1364
+ const newValue = GeoLocation3.toGeoPoint(newLocation);
1365
+ onValueChange(type, newValue);
1478
1366
  }
1479
1367
  }
1368
+ }, [
1369
+ type,
1370
+ getValue,
1371
+ onValueChange
1372
+ ]);
1373
+ if ((readonly || layout === "static") && !value3?.latitude && !value3?.longitude) {
1374
+ return null;
1480
1375
  }
1481
- return void 0;
1376
+ return /* @__PURE__ */ React8.createElement(Input4.Root, {
1377
+ validationValence: status
1378
+ }, layout !== "inline" && /* @__PURE__ */ React8.createElement(FormFieldLabel, {
1379
+ error,
1380
+ readonly,
1381
+ label,
1382
+ asChild: true
1383
+ }), layout === "static" ? /* @__PURE__ */ React8.createElement(LatLng, value3) : /* @__PURE__ */ React8.createElement("div", {
1384
+ role: "none",
1385
+ className: "grid grid-cols-2 gap-2"
1386
+ }, /* @__PURE__ */ React8.createElement("div", null, /* @__PURE__ */ React8.createElement(Input4.Root, null, layout !== "inline" && /* @__PURE__ */ React8.createElement(Input4.Label, null, t("latitude label")), /* @__PURE__ */ React8.createElement(Input4.TextInput, {
1387
+ type: "number",
1388
+ step: "0.00001",
1389
+ min: "-90",
1390
+ max: "90",
1391
+ disabled: !!readonly,
1392
+ placeholder: t("latitude placeholder"),
1393
+ value: latitudeText ?? "",
1394
+ onChange: handleChange("latitude", setLatitudeText),
1395
+ onBlur
1396
+ }))), /* @__PURE__ */ React8.createElement("div", null, /* @__PURE__ */ React8.createElement(Input4.Root, null, layout !== "inline" && /* @__PURE__ */ React8.createElement(Input4.Label, null, t("longitude label")), /* @__PURE__ */ React8.createElement(Input4.TextInput, {
1397
+ type: "number",
1398
+ step: "0.00001",
1399
+ min: "-180",
1400
+ max: "180",
1401
+ disabled: !!readonly,
1402
+ placeholder: t("longitude placeholder"),
1403
+ value: longitudeText ?? "",
1404
+ onChange: handleChange("longitude", setLongitudeText),
1405
+ onBlur
1406
+ })))));
1407
+ };
1408
+ var LatLng = ({ latitude = 0, longitude = 0 }) => {
1409
+ const latHem = latitude >= 0 ? "N" : "S";
1410
+ const lngHem = longitude >= 0 ? "E" : "W";
1411
+ return /* @__PURE__ */ React8.createElement("span", {
1412
+ className: "inline-flex items-center gap-1"
1413
+ }, /* @__PURE__ */ React8.createElement("span", null, /* @__PURE__ */ React8.createElement("span", null, Math.abs(latitude).toFixed(5)), /* @__PURE__ */ React8.createElement("span", {
1414
+ className: "text-subdued"
1415
+ }, "\xB0", latHem)), /* @__PURE__ */ React8.createElement("span", null, /* @__PURE__ */ React8.createElement("span", null, Math.abs(longitude).toFixed(5)), /* @__PURE__ */ React8.createElement("span", {
1416
+ className: "text-subdued"
1417
+ }, "\xB0", lngHem)));
1482
1418
  };
1483
1419
 
1484
- // src/components/Form/ArrayField.tsx
1485
- var __dxlog_file3 = "/__w/dxos/dxos/packages/ui/react-ui-form/src/components/Form/ArrayField.tsx";
1486
- var ArrayField = ({ property, readonly, path, inputProps, Custom, lookupComponent }) => {
1487
- var _effect = _useSignals10();
1488
- try {
1489
- const { t } = useTranslation6(translationKey);
1490
- const { ast, name, type, title } = property;
1491
- const values = useFormValues(path ?? []) ?? [];
1492
- invariant2(Array.isArray(values), `Values at path ${path?.join(".")} must be an array.`, {
1493
- F: __dxlog_file3,
1494
- L: 36,
1495
- S: void 0,
1496
- A: [
1497
- "Array.isArray(values)",
1498
- "`Values at path ${path?.join('.')} must be an array.`"
1499
- ]
1500
- });
1501
- const label = title ?? pipe(name, capitalize);
1502
- const elementType = findArrayElementType(ast);
1503
- const getDefaultObjectValue = (typeNode) => {
1504
- const baseNode = findNode2(typeNode, isDiscriminatedUnion);
1505
- const typeLiteral = baseNode ? getDiscriminatedType(baseNode, {}) : findNode2(typeNode, SchemaAST3.isTypeLiteral);
1506
- if (!typeLiteral) {
1507
- return {};
1508
- }
1509
- return Object.fromEntries(getSchemaProperties(typeLiteral, {}).map((prop) => [
1510
- prop.name,
1511
- prop.defaultValue
1512
- ]));
1513
- };
1514
- const getDefaultValue = () => type === "object" && elementType ? getDefaultObjectValue(elementType) : SimpleType.getDefaultValue(type);
1515
- const handleAdd = useCallback7(() => {
1516
- inputProps.onValueChange(type, [
1517
- ...values,
1518
- getDefaultValue()
1519
- ]);
1520
- }, [
1521
- type,
1522
- elementType,
1523
- inputProps,
1524
- values
1525
- ]);
1526
- const handleRemove = useCallback7((index) => {
1527
- const newValues = values.filter((_, i) => i !== index);
1528
- inputProps.onValueChange(type, newValues);
1529
- }, [
1530
- type,
1531
- inputProps,
1532
- values
1533
- ]);
1534
- if (!elementType) {
1535
- return null;
1420
+ // src/components/Form/fields/MarkdownField.tsx
1421
+ import React9, { useCallback as useCallback6, useEffect as useEffect5, useRef as useRef2 } from "react";
1422
+ import { Input as Input5 } from "@dxos/react-ui";
1423
+ var MarkdownField = ({ type, readonly, label, placeholder, layout, getStatus, getValue, onValueChange, onBlur }) => {
1424
+ const { status, error } = getStatus();
1425
+ const textareaRef = useRef2(null);
1426
+ const adjustHeight = useCallback6(() => {
1427
+ const textarea = textareaRef.current;
1428
+ if (textarea) {
1429
+ textarea.style.blockSize = "auto";
1430
+ textarea.style.blockSize = `${textarea.scrollHeight + 2}px`;
1536
1431
  }
1537
- return readonly && values.length < 1 ? null : /* @__PURE__ */ React10.createElement(React10.Fragment, null, /* @__PURE__ */ React10.createElement(InputHeader, {
1538
- readonly: true,
1539
- label
1540
- }), /* @__PURE__ */ React10.createElement("div", {
1541
- role: "none",
1542
- className: readonly ? "flex flex-wrap gap-1 mlb-1" : values.length > 0 ? "grid gap-1 grid-cols-[1fr_min-content] mlb-1" : "hidden"
1543
- }, values.map((_value, index) => {
1544
- const field = /* @__PURE__ */ React10.createElement(FormField, {
1545
- property: {
1546
- ...property,
1547
- array: false,
1548
- ast: elementType
1549
- },
1550
- path: [
1551
- ...path ?? [],
1552
- index
1553
- ],
1554
- readonly,
1555
- inline: true,
1556
- Custom,
1557
- lookupComponent
1558
- });
1559
- return readonly ? field : /* @__PURE__ */ React10.createElement(Fragment, {
1560
- key: index
1561
- }, /* @__PURE__ */ React10.createElement("div", {
1562
- role: "none"
1563
- }, field), /* @__PURE__ */ React10.createElement(IconButton3, {
1564
- icon: "ph--trash--regular",
1565
- iconOnly: true,
1566
- label: t("button remove"),
1567
- onClick: () => handleRemove(index),
1568
- classNames: "self-center"
1569
- }));
1570
- })), !readonly && /* @__PURE__ */ React10.createElement(IconButton3, {
1571
- classNames: "is-full mlb-cardSpacingBlock flex",
1572
- icon: "ph--plus--regular",
1573
- label: t("add field"),
1574
- onClick: handleAdd
1575
- }));
1576
- } finally {
1577
- _effect.f();
1432
+ }, []);
1433
+ useEffect5(() => {
1434
+ adjustHeight();
1435
+ }, [
1436
+ adjustHeight
1437
+ ]);
1438
+ useEffect5(() => {
1439
+ adjustHeight();
1440
+ }, [
1441
+ getValue(),
1442
+ adjustHeight
1443
+ ]);
1444
+ const handleChange = useCallback6((event) => onValueChange(type, event.target.value), [
1445
+ type,
1446
+ onValueChange
1447
+ ]);
1448
+ const value3 = getValue();
1449
+ if ((readonly || layout === "static") && !value3) {
1450
+ return null;
1578
1451
  }
1452
+ return /* @__PURE__ */ React9.createElement(Input5.Root, {
1453
+ validationValence: status
1454
+ }, layout !== "inline" && /* @__PURE__ */ React9.createElement(FormFieldLabel, {
1455
+ error,
1456
+ readonly,
1457
+ label
1458
+ }), layout === "static" ? /* @__PURE__ */ React9.createElement("p", null, value3) : /* @__PURE__ */ React9.createElement(Input5.TextArea, {
1459
+ ref: textareaRef,
1460
+ disabled: !!readonly,
1461
+ placeholder,
1462
+ value: value3 ?? "",
1463
+ classNames: "min-bs-auto max-bs-40 overflow-auto",
1464
+ onChange: handleChange,
1465
+ onBlur,
1466
+ style: {
1467
+ resize: "none"
1468
+ },
1469
+ spellCheck: false
1470
+ }), layout === "full" && /* @__PURE__ */ React9.createElement(Input5.Validation, null, error));
1471
+ };
1472
+
1473
+ // src/components/Form/fields/NumberField.tsx
1474
+ import React10, { useCallback as useCallback7 } from "react";
1475
+ import { Input as Input6 } from "@dxos/react-ui";
1476
+ import { safeParseFloat as safeParseFloat2 } from "@dxos/util";
1477
+ var NumberField = ({ type, readonly, placeholder, onValueChange, onBlur, ...props }) => {
1478
+ const handleChange = useCallback7((event) => onValueChange(type, safeParseFloat2(event.target.value) || 0), [
1479
+ type,
1480
+ onValueChange
1481
+ ]);
1482
+ return /* @__PURE__ */ React10.createElement(FormFieldWrapper, {
1483
+ readonly,
1484
+ ...props
1485
+ }, ({ value: value3 = "" }) => /* @__PURE__ */ React10.createElement(Input6.TextInput, {
1486
+ type: "number",
1487
+ disabled: !!readonly,
1488
+ placeholder,
1489
+ value: value3,
1490
+ onChange: handleChange,
1491
+ onBlur
1492
+ }));
1579
1493
  };
1580
1494
 
1581
- // src/components/Form/RefField.tsx
1582
- import { useSignals as _useSignals11 } from "@preact-signals/safe-react/tracking";
1583
- import React11, { useCallback as useCallback8, useMemo as useMemo4 } from "react";
1584
- import { Expando, getTypeAnnotation, Ref, ReferenceAnnotationId as ReferenceAnnotationId2 } from "@dxos/echo-schema";
1585
- import { findAnnotation as findAnnotation2 } from "@dxos/effect";
1495
+ // src/components/Form/fields/RefField.tsx
1496
+ import "@dxos/lit-ui/dx-tag-picker.pcss";
1497
+ import React12, { useCallback as useCallback9, useMemo as useMemo6 } from "react";
1498
+ import { Entity, Filter, Ref as Ref2 } from "@dxos/echo";
1499
+ import { ReferenceAnnotationId } from "@dxos/echo/internal";
1500
+ import { useQuery, useSchema as useSchema$ } from "@dxos/echo-react";
1501
+ import { findAnnotation } from "@dxos/effect";
1586
1502
  import { DXN } from "@dxos/keys";
1587
- import { DxRefTag } from "@dxos/lit-ui/react";
1588
- import { Input as Input8, useTranslation as useTranslation7 } from "@dxos/react-ui";
1589
- import { TagPicker } from "@dxos/react-ui-tag-picker";
1590
- import { descriptionText, mx as mx7 } from "@dxos/react-ui-theme";
1591
- import { isNonNullable as isNonNullable2 } from "@dxos/util";
1592
- var RefField = ({ type, label, disabled, placeholder, inputOnly, array, ast, getValue: getValue3, onBlur, onQueryRefOptions, onValueChange, ...restInputProps }) => {
1593
- var _effect = _useSignals11();
1594
- try {
1595
- const { t } = useTranslation7(translationKey);
1596
- const refTypeInfo = useMemo4(() => ast ? findAnnotation2(ast, ReferenceAnnotationId2) : void 0, [
1597
- ast
1598
- ]);
1599
- const { options: availableOptions, loading: _loading } = useQueryRefOptions({
1600
- refTypeInfo,
1601
- onQueryRefOptions
1602
- });
1603
- if (refTypeInfo && refTypeInfo?.typename === getTypeAnnotation(Expando)?.typename || !onQueryRefOptions) {
1604
- return /* @__PURE__ */ React11.createElement(RefFieldFallback, {
1605
- type,
1606
- label,
1607
- placeholder,
1608
- disabled,
1609
- inputOnly,
1610
- getValue: getValue3,
1611
- onBlur,
1612
- onValueChange,
1613
- ...restInputProps
1614
- });
1503
+ import { DxAnchor } from "@dxos/lit-ui/react";
1504
+ import { Button as Button2, Icon as Icon2, Input as Input7, useTranslation as useTranslation7 } from "@dxos/react-ui";
1505
+ import { descriptionText, mx as mx5 } from "@dxos/ui-theme";
1506
+
1507
+ // src/components/ObjectPicker/ObjectPicker.tsx
1508
+ import React11, { forwardRef as forwardRef2, useCallback as useCallback8, useState as useState3 } from "react";
1509
+ import { Popover, useTranslation as useTranslation6 } from "@dxos/react-ui";
1510
+ import { Combobox, useSearchListInput, useSearchListResults } from "@dxos/react-ui-searchlist";
1511
+ var CreateItem = ({ createOptionLabel, createOptionIcon, onCreateItemSelect }) => {
1512
+ const { t } = useTranslation6(translationKey);
1513
+ const { query } = useSearchListInput();
1514
+ return /* @__PURE__ */ React11.createElement(Combobox.Item, {
1515
+ value: "__create__",
1516
+ label: t(createOptionLabel[0], {
1517
+ ns: createOptionLabel[1].ns,
1518
+ text: query
1519
+ }),
1520
+ icon: createOptionIcon,
1521
+ classNames: "flex items-center gap-2",
1522
+ closeOnSelect: false,
1523
+ onSelect: () => {
1524
+ onCreateItemSelect(query);
1615
1525
  }
1616
- const handleGetValue = useCallback8(() => {
1617
- const formValue = getValue3();
1618
- const unknownToRefOption = (val) => {
1619
- if (Ref.isRef(val)) {
1620
- const dxnString = val.dxn.toString();
1621
- const matchingOption = availableOptions.find((option2) => option2.id === dxnString);
1622
- if (matchingOption) {
1623
- return matchingOption;
1624
- }
1625
- }
1626
- return void 0;
1627
- };
1628
- if (array && Array.isArray(formValue)) {
1629
- return formValue.map(unknownToRefOption).filter(isNonNullable2) ?? [];
1630
- }
1631
- const option = unknownToRefOption(formValue);
1632
- if (option) {
1633
- return [
1634
- option
1635
- ];
1636
- }
1637
- return [];
1638
- }, [
1639
- getValue3,
1640
- availableOptions,
1641
- array
1642
- ]);
1643
- const handleSearch = useCallback8((text, ids) => availableOptions.filter((option) => !ids.includes(option.id)).filter((option) => option.label.toLowerCase().includes(text.toLowerCase())).map((option) => ({
1644
- id: option.id,
1645
- label: option.label,
1646
- hue: option.hue
1647
- })), [
1648
- availableOptions
1649
- ]);
1650
- const handleUpdate = useCallback8((ids) => {
1651
- if (ids.length === 0) {
1652
- onValueChange("object", void 0);
1653
- return;
1654
- }
1655
- const refs = ids.map((id) => {
1656
- const item = availableOptions.find((option) => option.id === id);
1657
- if (item) {
1658
- const dxn = DXN.parse(item.id);
1659
- return Ref.fromDXN(dxn);
1660
- }
1661
- return null;
1662
- }).filter(isNonNullable2);
1663
- if (array) {
1664
- onValueChange("object", refs);
1665
- } else {
1666
- onValueChange("object", refs[0]);
1667
- }
1668
- }, [
1669
- availableOptions,
1670
- array,
1671
- onValueChange
1672
- ]);
1673
- const tagPickerMode = useMemo4(() => array ? "multi-select" : "single-select", [
1674
- array
1675
- ]);
1676
- if (!refTypeInfo) {
1677
- return null;
1678
- }
1679
- const { status, error } = restInputProps.getStatus();
1680
- const items = handleGetValue();
1681
- return disabled && items.length < 1 ? null : /* @__PURE__ */ React11.createElement(Input8.Root, {
1682
- validationValence: status
1683
- }, !inputOnly && /* @__PURE__ */ React11.createElement(InputHeader, {
1684
- error,
1685
- label
1686
- }), /* @__PURE__ */ React11.createElement("div", {
1687
- "data-no-submit": true
1688
- }, disabled ? items.length < 1 ? /* @__PURE__ */ React11.createElement("p", {
1689
- className: mx7(descriptionText, "mbe-2")
1690
- }, t("empty readonly ref field label")) : items.map((item) => /* @__PURE__ */ React11.createElement(DxRefTag, {
1691
- key: item.id,
1692
- refid: item.id,
1693
- rootclassname: "mie-1",
1694
- style: {
1695
- marginInlineEnd: ".25rem"
1696
- }
1697
- }, item.label)) : /* @__PURE__ */ React11.createElement(TagPicker, {
1698
- readonly: disabled,
1699
- items,
1700
- mode: tagPickerMode,
1701
- onBlur: (event) => onBlur(event),
1702
- onUpdate: handleUpdate,
1703
- onSearch: handleSearch,
1704
- classNames: "bg-inputSurface p-1.5 rounded-sm"
1705
- })), inputOnly && /* @__PURE__ */ React11.createElement(Input8.DescriptionAndValidation, null, error));
1706
- } finally {
1707
- _effect.f();
1708
- }
1526
+ });
1709
1527
  };
1710
- var RefFieldFallback = ({ type, label, disabled, placeholder, inputOnly, getValue: getValue3, onValueChange, ...restInputProps }) => {
1711
- var _effect = _useSignals11();
1712
- try {
1713
- const handleOnValueChange = (_type, dxnString) => {
1714
- const dxn = DXN.tryParse(dxnString);
1715
- if (dxn) {
1716
- onValueChange?.("object", Ref.fromDXN(dxn));
1717
- } else if (dxnString === "") {
1718
- onValueChange?.("object", void 0);
1719
- } else {
1720
- onValueChange?.("string", dxnString);
1721
- }
1722
- };
1723
- const handleGetValue = () => {
1724
- const formValue = getValue3();
1725
- if (typeof formValue === "string") {
1726
- return formValue;
1727
- }
1728
- if (Ref.isRef(formValue)) {
1729
- return formValue.dxn.toString();
1528
+ var ObjectPickerContent = /* @__PURE__ */ forwardRef2(({ options, selectedIds, createSchema, createOptionLabel, createOptionIcon, createInitialValuePath, onSelect, onCreate, ...props }, ref) => {
1529
+ const { t } = useTranslation6(translationKey);
1530
+ const [showForm, setShowForm] = useState3(false);
1531
+ const [formInitialValue, setFormInitialValue] = useState3("");
1532
+ const { results, handleSearch } = useSearchListResults({
1533
+ items: options
1534
+ });
1535
+ const handleFormSave = useCallback8((values) => {
1536
+ onCreate?.(values);
1537
+ setShowForm(false);
1538
+ setFormInitialValue("");
1539
+ }, [
1540
+ onCreate
1541
+ ]);
1542
+ const handleFormCancel = useCallback8(() => {
1543
+ setShowForm(false);
1544
+ setFormInitialValue("");
1545
+ }, []);
1546
+ const handleKeyDown = useCallback8((event) => {
1547
+ if (event.key === "Enter" && createSchema && event.currentTarget.querySelector('[role="option"][aria-selected="true"][data-value="__create__"]')) {
1548
+ event.stopPropagation();
1549
+ event.preventDefault();
1550
+ const input = event.currentTarget.querySelector('input[type="text"]');
1551
+ const currentQuery = input?.value || "";
1552
+ setFormInitialValue(currentQuery);
1553
+ setShowForm(true);
1554
+ }
1555
+ }, [
1556
+ createSchema
1557
+ ]);
1558
+ return /* @__PURE__ */ React11.createElement(Combobox.Content, {
1559
+ ...props,
1560
+ ref,
1561
+ onSearch: handleSearch,
1562
+ onKeyDownCapture: handleKeyDown
1563
+ }, showForm && createSchema ? /* @__PURE__ */ React11.createElement(Popover.Viewport, null, /* @__PURE__ */ React11.createElement(Form.Root, {
1564
+ testId: "create-referenced-object-form",
1565
+ schema: createSchema,
1566
+ values: createInitialValuePath ? {
1567
+ [createInitialValuePath]: formInitialValue
1568
+ } : {},
1569
+ onSave: handleFormSave,
1570
+ onCancel: handleFormCancel
1571
+ }, /* @__PURE__ */ React11.createElement(Form.Viewport, null, /* @__PURE__ */ React11.createElement(Form.Content, null, /* @__PURE__ */ React11.createElement(Form.FieldSet, null), /* @__PURE__ */ React11.createElement(Form.Actions, null))))) : /* @__PURE__ */ React11.createElement(React11.Fragment, null, /* @__PURE__ */ React11.createElement(Combobox.Input, {
1572
+ placeholder: t("ref field combobox input placeholder"),
1573
+ autoFocus: true
1574
+ }), /* @__PURE__ */ React11.createElement(Combobox.List, null, results.map((option) => /* @__PURE__ */ React11.createElement(Combobox.Item, {
1575
+ key: option.id,
1576
+ value: option.id,
1577
+ label: option.label,
1578
+ checked: selectedIds?.includes(option.id),
1579
+ onSelect: () => onSelect(option.id),
1580
+ classNames: "flex items-center gap-2"
1581
+ })), createOptionLabel && createOptionIcon && createSchema && onCreate && /* @__PURE__ */ React11.createElement(CreateItem, {
1582
+ createOptionLabel,
1583
+ createOptionIcon,
1584
+ onCreateItemSelect: (query) => {
1585
+ setFormInitialValue(query);
1586
+ setShowForm(true);
1587
+ }
1588
+ }))), /* @__PURE__ */ React11.createElement(Combobox.Arrow, null));
1589
+ });
1590
+ ObjectPickerContent.displayName = "ObjectPickerContent";
1591
+ var ObjectPicker = {
1592
+ Root: Combobox.Root,
1593
+ Portal: Combobox.Portal,
1594
+ Trigger: Combobox.Trigger,
1595
+ VirtualTrigger: Combobox.VirtualTrigger,
1596
+ Content: ObjectPickerContent
1597
+ };
1598
+
1599
+ // src/components/Form/fields/RefField.tsx
1600
+ var isRefSnapshot = (val) => {
1601
+ return typeof val === "object" && typeof val?.["/"] === "string";
1602
+ };
1603
+ var defaultGetOptions = (results) => results.map((result) => {
1604
+ const id = Entity.getDXN(result).toString();
1605
+ const label = Entity.getLabel(result);
1606
+ return {
1607
+ id,
1608
+ label: label ?? id
1609
+ };
1610
+ });
1611
+ var defaultResultsHook = (db, typename) => useQuery(db, typename ? Filter.typename(typename) : Filter.nothing());
1612
+ var RefField = (props) => {
1613
+ const { type, readonly, label, placeholder, layout, getStatus, getValue, createOptionLabel, createOptionIcon, createInitialValuePath, db, resultsHook: useResults = defaultResultsHook, schemaHook: useSchema = useSchema$, getOptions: getOptions2 = defaultGetOptions, onCreate, onValueChange } = props;
1614
+ const { t } = useTranslation7(translationKey);
1615
+ const { status, error } = getStatus();
1616
+ const typename = useMemo6(() => type ? findAnnotation(type, ReferenceAnnotationId)?.typename : void 0, [
1617
+ type
1618
+ ]);
1619
+ const results = useResults(db, typename);
1620
+ const options = useMemo6(() => getOptions2(results), [
1621
+ results,
1622
+ getOptions2
1623
+ ]);
1624
+ const handleGetValue = useCallback9(() => {
1625
+ const formValue = getValue();
1626
+ const unknownToRefOption = (value3) => {
1627
+ const isRef = Ref2.isRef(value3);
1628
+ if (isRef || isRefSnapshot(value3)) {
1629
+ const dxnString = isRef ? value3.dxn.toString() : value3["/"];
1630
+ const matchingOption = options.find((option) => option.id === dxnString);
1631
+ if (matchingOption) {
1632
+ return matchingOption;
1633
+ }
1730
1634
  }
1731
1635
  return void 0;
1732
1636
  };
1733
- return /* @__PURE__ */ React11.createElement(TextInput, {
1734
- type,
1735
- label,
1736
- disabled,
1737
- placeholder,
1738
- inputOnly,
1739
- getValue: handleGetValue,
1740
- onValueChange: handleOnValueChange,
1741
- ...restInputProps
1742
- });
1743
- } finally {
1744
- _effect.f();
1637
+ return unknownToRefOption(formValue);
1638
+ }, [
1639
+ options,
1640
+ getValue
1641
+ ]);
1642
+ const handleUpdate = useCallback9((id) => {
1643
+ const item2 = options.find((option) => option.id === id);
1644
+ const ref = item2 ? Ref2.fromDXN(DXN.parse(item2.id)) : void 0;
1645
+ onValueChange(type, ref);
1646
+ }, [
1647
+ options,
1648
+ type,
1649
+ onValueChange
1650
+ ]);
1651
+ const item = handleGetValue();
1652
+ const selectedIds = useMemo6(() => item ? [
1653
+ item.id
1654
+ ] : [], [
1655
+ item
1656
+ ]);
1657
+ const createSchema = useSchema(db, typename);
1658
+ const handleCreate = useCallback9((values) => {
1659
+ if (createSchema && onCreate) {
1660
+ onCreate(createSchema, values);
1661
+ }
1662
+ }, [
1663
+ createSchema,
1664
+ onCreate
1665
+ ]);
1666
+ const handleSelect = useCallback9((id) => {
1667
+ if (item?.id === id) {
1668
+ handleUpdate(void 0);
1669
+ } else {
1670
+ handleUpdate(id);
1671
+ }
1672
+ }, [
1673
+ item,
1674
+ handleUpdate
1675
+ ]);
1676
+ if (!typename || (readonly || layout === "static") && !item) {
1677
+ return null;
1745
1678
  }
1679
+ return /* @__PURE__ */ React12.createElement(Input7.Root, {
1680
+ validationValence: status
1681
+ }, layout !== "inline" && /* @__PURE__ */ React12.createElement(FormFieldLabel, {
1682
+ error,
1683
+ readonly,
1684
+ label
1685
+ }), /* @__PURE__ */ React12.createElement("div", null, readonly ? !item ? /* @__PURE__ */ React12.createElement("p", {
1686
+ className: mx5(descriptionText, "mbe-2")
1687
+ }, t("empty readonly ref field label")) : /* @__PURE__ */ React12.createElement(DxAnchor, {
1688
+ key: item.id,
1689
+ refid: item.id,
1690
+ rootclassname: "mie-1"
1691
+ }, item.label) : /* @__PURE__ */ React12.createElement(ObjectPicker.Root, null, /* @__PURE__ */ React12.createElement(ObjectPicker.Trigger, {
1692
+ asChild: true,
1693
+ classNames: "p-0"
1694
+ }, item ? /* @__PURE__ */ React12.createElement("div", {
1695
+ className: "flex gap-2 is-full"
1696
+ }, /* @__PURE__ */ React12.createElement(Input7.Root, {
1697
+ key: item.id
1698
+ }, /* @__PURE__ */ React12.createElement(Input7.TextInput, {
1699
+ value: item.label,
1700
+ readOnly: true,
1701
+ classNames: "is-full"
1702
+ }))) : /* @__PURE__ */ React12.createElement(Button2, {
1703
+ classNames: "is-full text-start gap-2"
1704
+ }, /* @__PURE__ */ React12.createElement("div", {
1705
+ role: "none",
1706
+ className: "grow overflow-hidden"
1707
+ }, /* @__PURE__ */ React12.createElement("span", {
1708
+ className: "flex truncate text-description"
1709
+ }, placeholder ?? t("ref field placeholder"))), /* @__PURE__ */ React12.createElement(Icon2, {
1710
+ size: 3,
1711
+ icon: "ph--caret-down--bold"
1712
+ }))), /* @__PURE__ */ React12.createElement(ObjectPicker.Portal, null, /* @__PURE__ */ React12.createElement(ObjectPicker.Content, {
1713
+ classNames: "popover-card-width",
1714
+ options,
1715
+ selectedIds,
1716
+ createSchema: createSchema && omitId(createSchema),
1717
+ createOptionLabel,
1718
+ createOptionIcon,
1719
+ createInitialValuePath,
1720
+ onCreate: handleCreate,
1721
+ onSelect: handleSelect
1722
+ })))), layout === "full" && /* @__PURE__ */ React12.createElement(Input7.DescriptionAndValidation, null, error));
1746
1723
  };
1747
1724
 
1748
- // src/components/Form/factory.tsx
1749
- import { FormatEnum as FormatEnum4 } from "@dxos/echo-schema";
1750
- var getInputComponent = (type, format) => {
1751
- switch (format) {
1752
- case FormatEnum4.GeoPoint:
1753
- return GeoPointInput;
1754
- case FormatEnum4.Markdown:
1755
- return MarkdownInput;
1756
- }
1757
- switch (type) {
1758
- case "string":
1759
- return TextInput;
1760
- case "number":
1761
- return NumberInput;
1762
- case "boolean":
1763
- return BooleanInput;
1725
+ // src/components/Form/fields/SelectField.tsx
1726
+ import React13, { useCallback as useCallback10 } from "react";
1727
+ import { Input as Input8, Select } from "@dxos/react-ui";
1728
+ var SelectField = ({ type, readonly, label, layout, placeholder, options, getStatus, getValue, onValueChange }) => {
1729
+ const { status, error } = getStatus();
1730
+ const value3 = getValue();
1731
+ const handleValueChange = useCallback10((value4) => onValueChange(type, value4), [
1732
+ type,
1733
+ onValueChange
1734
+ ]);
1735
+ if ((readonly || layout === "static") && value3 == null) {
1736
+ return null;
1764
1737
  }
1738
+ return /* @__PURE__ */ React13.createElement(Input8.Root, {
1739
+ validationValence: status
1740
+ }, layout !== "inline" && /* @__PURE__ */ React13.createElement(FormFieldLabel, {
1741
+ error,
1742
+ readonly,
1743
+ label
1744
+ }), layout === "static" ? /* @__PURE__ */ React13.createElement("p", null, options?.find(({ value: optionValue }) => optionValue === value3)?.label ?? String(value3)) : /* @__PURE__ */ React13.createElement(Select.Root, {
1745
+ value: value3,
1746
+ onValueChange: handleValueChange
1747
+ }, /* @__PURE__ */ React13.createElement(Select.TriggerButton, {
1748
+ classNames: "is-full",
1749
+ disabled: !!readonly,
1750
+ placeholder
1751
+ }), /* @__PURE__ */ React13.createElement(Select.Portal, null, /* @__PURE__ */ React13.createElement(Select.Content, null, /* @__PURE__ */ React13.createElement(Select.Viewport, null, options?.map(({ value: value4, label: label2 }) => (
1752
+ // NOTE: Numeric values are converted to and from strings.
1753
+ /* @__PURE__ */ React13.createElement(Select.Option, {
1754
+ key: String(value4),
1755
+ value: String(value4)
1756
+ }, label2 ?? String(value4))
1757
+ ))), /* @__PURE__ */ React13.createElement(Select.Arrow, null)))), layout === "full" && /* @__PURE__ */ React13.createElement(Input8.DescriptionAndValidation, null, error));
1765
1758
  };
1766
1759
 
1767
- // src/components/Form/FormContent.tsx
1768
- var FormField = ({ property, path, readonly, inline, onQueryRefOptions, lookupComponent, Custom }) => {
1769
- var _effect = _useSignals12();
1770
- try {
1771
- const { ast, name, type, format, title, description, options, examples, array } = property;
1772
- const inputProps = useInputProps(path);
1773
- const label = useMemo5(() => title ?? pipe2(name, capitalize2), [
1774
- title,
1775
- name
1776
- ]);
1777
- const placeholder = useMemo5(() => examples?.length ? `Example: "${examples[0]}"` : description, [
1778
- examples,
1779
- description
1780
- ]);
1781
- const FoundComponent = lookupComponent?.({
1782
- prop: name,
1783
- schema: Schema.make(ast),
1784
- inputProps: {
1785
- type,
1786
- format,
1787
- label,
1788
- disabled: readonly,
1789
- placeholder,
1790
- ...inputProps
1791
- }
1792
- });
1793
- if (FoundComponent) {
1794
- return FoundComponent;
1795
- }
1796
- const jsonPath = createJsonPath3(path ?? []);
1797
- const CustomComponent = Custom?.[jsonPath];
1798
- if (CustomComponent) {
1799
- return /* @__PURE__ */ React12.createElement(CustomComponent, {
1800
- type,
1801
- format,
1802
- label,
1803
- inputOnly: inline,
1804
- placeholder,
1805
- disabled: readonly,
1806
- ...inputProps
1807
- });
1808
- }
1809
- const refProps = getRefProps(property);
1810
- if (refProps) {
1811
- return /* @__PURE__ */ React12.createElement(RefField, {
1812
- ast: refProps.ast,
1813
- array: refProps.isArray,
1814
- type,
1815
- format,
1816
- label,
1817
- placeholder,
1818
- disabled: readonly,
1819
- inputOnly: inline,
1820
- onQueryRefOptions,
1821
- ...inputProps
1822
- });
1760
+ // src/components/Form/fields/SelectOptionField.tsx
1761
+ import React14, { useCallback as useCallback11, useEffect as useEffect6, useRef as useRef3, useState as useState4 } from "react";
1762
+ import { PublicKey } from "@dxos/keys";
1763
+ import { IconButton as IconButton3, Input as Input9, Tag, useTranslation as useTranslation8 } from "@dxos/react-ui";
1764
+ import { List } from "@dxos/react-ui-list";
1765
+ import { HuePicker } from "@dxos/react-ui-pickers";
1766
+ import { hues, osTranslations, subtleHover } from "@dxos/ui-theme";
1767
+ var SelectOptionField = ({ type, readonly, label, getStatus, getValue, onValueChange }) => {
1768
+ const { t } = useTranslation8(translationKey);
1769
+ const { status, error } = getStatus();
1770
+ const [selected, setSelectedId] = useState4(null);
1771
+ const [isNewOption, setIsNewOption] = useState4(false);
1772
+ const inputRef = useRef3(null);
1773
+ const options = getValue();
1774
+ useEffect6(() => {
1775
+ if (options === void 0) {
1776
+ onValueChange(type, []);
1823
1777
  }
1824
- if (array) {
1825
- return /* @__PURE__ */ React12.createElement(ArrayField, {
1826
- property,
1827
- path,
1828
- inputProps,
1829
- readonly,
1830
- Custom
1831
- });
1778
+ }, [
1779
+ options,
1780
+ type,
1781
+ onValueChange
1782
+ ]);
1783
+ const randomHue = () => {
1784
+ const usedHues = new Set(options?.map((option) => option.color) ?? []);
1785
+ const unusedHues = hues.filter((hue) => !usedHues.has(hue));
1786
+ if (unusedHues.length > 0) {
1787
+ return unusedHues[Math.floor(Math.random() * unusedHues.length)];
1832
1788
  }
1833
- const InputComponent = getInputComponent(type, format);
1834
- if (InputComponent) {
1835
- return /* @__PURE__ */ React12.createElement(InputComponent, {
1836
- type,
1837
- format,
1838
- label,
1839
- inputOnly: inline,
1840
- placeholder,
1841
- disabled: readonly,
1842
- ...inputProps
1843
- });
1789
+ const lastThree = new Set(options?.slice(-3).map((option) => option.color) ?? []);
1790
+ const availableHues = hues.filter((hue) => !lastThree.has(hue));
1791
+ return availableHues[Math.floor(Math.random() * availableHues.length)] ?? hues[Math.floor(Math.random() * hues.length)];
1792
+ };
1793
+ const handleAdd = useCallback11(() => {
1794
+ const newId = PublicKey.random().truncate();
1795
+ const newOption = {
1796
+ id: newId,
1797
+ title: "",
1798
+ color: randomHue()
1799
+ };
1800
+ onValueChange(type, [
1801
+ ...options ?? [],
1802
+ newOption
1803
+ ]);
1804
+ setSelectedId(newId);
1805
+ setIsNewOption(true);
1806
+ }, [
1807
+ options,
1808
+ type,
1809
+ onValueChange
1810
+ ]);
1811
+ const handleDelete = useCallback11((id) => {
1812
+ const newOptions = options?.filter((option) => option.id !== id) ?? [];
1813
+ onValueChange(type, newOptions);
1814
+ setSelectedId(null);
1815
+ }, [
1816
+ options,
1817
+ type,
1818
+ onValueChange
1819
+ ]);
1820
+ const handleMove = useCallback11((from, to) => {
1821
+ if (!options) {
1822
+ return;
1844
1823
  }
1845
- if (options) {
1846
- return /* @__PURE__ */ React12.createElement(SelectInput, {
1847
- type,
1848
- format,
1849
- disabled: readonly,
1850
- inputOnly: inline,
1851
- label,
1852
- options: options.map((option) => ({
1853
- value: option,
1854
- label: String(option)
1855
- })),
1856
- placeholder,
1857
- ...inputProps
1858
- });
1824
+ const newOptions = [
1825
+ ...options
1826
+ ];
1827
+ const [removed] = newOptions.splice(from, 1);
1828
+ newOptions.splice(to, 0, removed);
1829
+ onValueChange(type, newOptions);
1830
+ }, [
1831
+ options,
1832
+ type,
1833
+ onValueChange
1834
+ ]);
1835
+ const handleClick = useCallback11((id) => {
1836
+ setSelectedId((current) => current === id ? null : id);
1837
+ }, []);
1838
+ const handleTitleChange = useCallback11((id) => (e) => {
1839
+ const newOptions = options?.map((o) => o.id === id ? {
1840
+ ...o,
1841
+ title: e.target.value
1842
+ } : o);
1843
+ onValueChange(type, newOptions ?? []);
1844
+ }, [
1845
+ options,
1846
+ type,
1847
+ onValueChange
1848
+ ]);
1849
+ const handleKeyDown = useCallback11((ev) => {
1850
+ if (ev.key === "Enter") {
1851
+ setSelectedId(null);
1859
1852
  }
1860
- if (type === "object") {
1861
- const baseNode = findNode3(ast, isDiscriminatedUnion2);
1862
- const typeLiteral = baseNode ? getDiscriminatedType2(baseNode, inputProps.getValue()) : findNode3(ast, SchemaAST4.isTypeLiteral);
1863
- if (typeLiteral) {
1864
- return /* @__PURE__ */ React12.createElement(React12.Fragment, null, !inline && /* @__PURE__ */ React12.createElement("h3", {
1865
- className: mx8("text-lg mlb-inputSpacingBlock first:mbs-0")
1866
- }, label), /* @__PURE__ */ React12.createElement(FormFields, {
1867
- schema: Schema.make(typeLiteral),
1868
- path,
1869
- readonly,
1870
- onQueryRefOptions,
1871
- Custom,
1872
- lookupComponent
1873
- }));
1874
- }
1853
+ }, []);
1854
+ const handleColorChange = useCallback11((id) => (hue) => {
1855
+ const newOptions = options?.map((o) => o.id === id ? {
1856
+ ...o,
1857
+ color: hue
1858
+ } : o);
1859
+ onValueChange(type, newOptions ?? []);
1860
+ }, [
1861
+ options,
1862
+ type,
1863
+ onValueChange
1864
+ ]);
1865
+ useEffect6(() => {
1866
+ if (selected && isNewOption && inputRef.current) {
1867
+ inputRef.current.focus();
1868
+ setIsNewOption(false);
1875
1869
  }
1876
- return null;
1877
- } finally {
1878
- _effect.f();
1879
- }
1870
+ }, [
1871
+ selected,
1872
+ isNewOption
1873
+ ]);
1874
+ return /* @__PURE__ */ React14.createElement(Input9.Root, {
1875
+ validationValence: status
1876
+ }, /* @__PURE__ */ React14.createElement(FormFieldLabel, {
1877
+ error,
1878
+ readonly,
1879
+ label
1880
+ }), /* @__PURE__ */ React14.createElement("div", {
1881
+ role: "none"
1882
+ }, options && /* @__PURE__ */ React14.createElement(List.Root, {
1883
+ items: options,
1884
+ isItem: (_item) => true,
1885
+ onMove: readonly ? void 0 : handleMove,
1886
+ readonly: !!readonly
1887
+ }, ({ items }) => /* @__PURE__ */ React14.createElement("div", {
1888
+ role: "list",
1889
+ className: "is-full overflow-auto"
1890
+ }, items?.map((item) => /* @__PURE__ */ React14.createElement("div", {
1891
+ key: item.id
1892
+ }, /* @__PURE__ */ React14.createElement(List.Item, {
1893
+ role: "button",
1894
+ item,
1895
+ classNames: [
1896
+ subtleHover,
1897
+ "flex flex-col cursor-pointer rounded-sm"
1898
+ ],
1899
+ "aria-expanded": selected === item.id
1900
+ }, /* @__PURE__ */ React14.createElement("div", {
1901
+ className: "flex items-center"
1902
+ }, /* @__PURE__ */ React14.createElement(List.ItemDragHandle, {
1903
+ disabled: !!readonly
1904
+ }), /* @__PURE__ */ React14.createElement(List.ItemTitle, {
1905
+ onClick: () => handleClick(item.id),
1906
+ classNames: "flex-1"
1907
+ }, /* @__PURE__ */ React14.createElement(Tag, {
1908
+ palette: item.color
1909
+ }, item.title || "\u200B")), /* @__PURE__ */ React14.createElement(IconButton3, {
1910
+ iconOnly: true,
1911
+ variant: "ghost",
1912
+ label: t(selected === item.id ? "collapse label" : "expand label", {
1913
+ ns: osTranslations
1914
+ }),
1915
+ icon: selected === item.id ? "ph--caret-down--regular" : "ph--caret-right--regular",
1916
+ onClick: () => handleClick(item.id)
1917
+ })), selected === item.id && /* @__PURE__ */ React14.createElement("div", {
1918
+ className: "pli-cardSpacingInline plb-cardSpacingBlock flex flex-col gap-1 density-fine"
1919
+ }, /* @__PURE__ */ React14.createElement(Input9.Label, {
1920
+ classNames: "text-xs"
1921
+ }, t("select option label")), /* @__PURE__ */ React14.createElement("div", {
1922
+ className: "grid grid-cols-[1fr_min-content_min-content] gap-1"
1923
+ }, /* @__PURE__ */ React14.createElement(Input9.TextInput, {
1924
+ disabled: !!readonly,
1925
+ placeholder: t("select option label placeholder"),
1926
+ ref: selected === item.id ? inputRef : void 0,
1927
+ value: item.title,
1928
+ onChange: handleTitleChange(item.id),
1929
+ onKeyDown: handleKeyDown,
1930
+ classNames: "flex-1"
1931
+ }), /* @__PURE__ */ React14.createElement(HuePicker, {
1932
+ disabled: !!readonly,
1933
+ value: item.color,
1934
+ onChange: handleColorChange(item.id)
1935
+ }), /* @__PURE__ */ React14.createElement(IconButton3, {
1936
+ disabled: !!readonly,
1937
+ icon: "ph--trash--fill",
1938
+ iconOnly: true,
1939
+ label: t("select option delete"),
1940
+ onClick: () => handleDelete(item.id)
1941
+ })))))), /* @__PURE__ */ React14.createElement(IconButton3, {
1942
+ variant: "ghost",
1943
+ icon: "ph--plus--regular",
1944
+ label: t("select option add"),
1945
+ onClick: handleAdd,
1946
+ disabled: !!readonly,
1947
+ classNames: "is-full"
1948
+ })))));
1880
1949
  };
1881
- var FormFields = /* @__PURE__ */ forwardRef(({ classNames, testId, schema, path, filter, sort, readonly, lookupComponent, Custom, onQueryRefOptions }, forwardRef2) => {
1882
- var _effect = _useSignals12();
1883
- try {
1884
- const values = useFormValues(path);
1885
- const properties = useMemo5(() => {
1886
- const props = getSchemaProperties2(schema.ast, values);
1887
- const filtered = filter ? filter(props) : props;
1888
- return sort ? filtered.sort((a, b) => sort.indexOf(a.name) - sort.indexOf(b.name)) : filtered;
1889
- }, [
1890
- schema,
1891
- values,
1892
- filter,
1893
- sort
1894
- ]);
1895
- return /* @__PURE__ */ React12.createElement("div", {
1896
- role: "form",
1897
- className: mx8("is-full", classNames),
1898
- ref: forwardRef2
1899
- }, properties.map((property) => {
1900
- return /* @__PURE__ */ React12.createElement(FormField, {
1901
- key: property.name,
1902
- property,
1903
- path: [
1904
- ...path ?? [],
1905
- property.name
1906
- ],
1907
- readonly,
1908
- onQueryRefOptions,
1909
- lookupComponent,
1910
- Custom
1911
- });
1912
- }).filter(isNotFalsy));
1913
- } finally {
1914
- _effect.f();
1915
- }
1916
- });
1917
1950
 
1918
- // src/components/Form/Form.tsx
1919
- var Form = ({ classNames, testId, values: initialValues, autoFocus, readonly, autoSave, outerSpacing = true, onCancel, schema, onValuesChanged, onValidate, onSave, ...props }) => {
1920
- var _effect = _useSignals13();
1921
- try {
1922
- const formRef = useRef3(null);
1923
- const handleValid = useMemo6(() => autoSave ? onSave : void 0, [
1924
- autoSave,
1925
- onSave
1926
- ]);
1927
- useEffect7(() => {
1928
- if (autoFocus && formRef.current) {
1929
- const input = formRef.current.querySelector("input");
1930
- if (input) {
1931
- input.focus();
1932
- }
1933
- }
1934
- }, [
1935
- autoFocus
1936
- ]);
1937
- return /* @__PURE__ */ React13.createElement(FormProvider, {
1938
- formRef,
1939
- schema,
1940
- autoSave,
1941
- initialValues,
1942
- onValuesChanged,
1943
- onValidate,
1944
- onValid: handleValid,
1945
- onSave
1946
- }, /* @__PURE__ */ React13.createElement("div", {
1947
- role: "none",
1948
- className: "contents",
1949
- "data-testid": testId
1950
- }, /* @__PURE__ */ React13.createElement(FormFields, {
1951
- ...props,
1952
- ref: formRef,
1953
- classNames: [
1954
- outerSpacing === "blockStart-0" ? "pli-cardSpacingInline mbe-cardSpacingBlock [&>.mbs-inputSpacingBlock:first-child]:!mbs-0" : outerSpacing === "scroll-fields" ? "pli-cardSpacingInline pbe-cardSpacingBlock" : outerSpacing ? cardSpacing2 : false,
1955
- outerSpacing === "scroll-fields" && cardDialogOverflow,
1956
- classNames
1957
- ],
1958
- readonly,
1959
- schema
1960
- }), (onCancel || onSave) && !autoSave && /* @__PURE__ */ React13.createElement(FormActions, {
1961
- readonly,
1962
- onCancel,
1963
- outerSpacing
1964
- })));
1965
- } finally {
1966
- _effect.f();
1967
- }
1951
+ // src/components/Form/fields/TextField.tsx
1952
+ import React15, { useCallback as useCallback12 } from "react";
1953
+ import { Input as Input10 } from "@dxos/react-ui";
1954
+ var TextField = ({ type, readonly, placeholder, onValueChange, onBlur, ...props }) => {
1955
+ const handleChange = useCallback12((event) => onValueChange(type, event.target.value), [
1956
+ type,
1957
+ onValueChange
1958
+ ]);
1959
+ return /* @__PURE__ */ React15.createElement(FormFieldWrapper, {
1960
+ readonly,
1961
+ ...props
1962
+ }, ({ value: value3 = "" }) => /* @__PURE__ */ React15.createElement(Input10.TextInput, {
1963
+ noAutoFill: true,
1964
+ disabled: !!readonly,
1965
+ placeholder,
1966
+ value: value3,
1967
+ onChange: handleChange,
1968
+ onBlur
1969
+ }));
1970
+ };
1971
+
1972
+ // src/components/Form/fields/TextAreaField.tsx
1973
+ import React16, { useCallback as useCallback13 } from "react";
1974
+ import { Input as Input11 } from "@dxos/react-ui";
1975
+ var TextAreaField = ({ type, readonly, placeholder, onValueChange, onBlur, ...props }) => {
1976
+ const handleChange = useCallback13((event) => onValueChange(type, event.target.value), [
1977
+ type,
1978
+ onValueChange
1979
+ ]);
1980
+ return /* @__PURE__ */ React16.createElement(FormFieldWrapper, {
1981
+ readonly,
1982
+ ...props
1983
+ }, ({ value: value3 = "" }) => /* @__PURE__ */ React16.createElement(Input11.TextArea, {
1984
+ rows: 3,
1985
+ disabled: !!readonly,
1986
+ placeholder,
1987
+ value: value3,
1988
+ onChange: handleChange,
1989
+ onBlur
1990
+ }));
1991
+ };
1992
+
1993
+ // src/components/Form/fields/TupleField.tsx
1994
+ import React17 from "react";
1995
+ import { Input as Input12 } from "@dxos/react-ui";
1996
+ import { mx as mx6 } from "@dxos/ui-theme";
1997
+ import { safeParseFloat as safeParseFloat3 } from "@dxos/util";
1998
+ var gridCols = [
1999
+ "grid-cols-1",
2000
+ "grid-cols-2",
2001
+ "grid-cols-3",
2002
+ "grid-cols-4"
2003
+ ];
2004
+ var TupleField = ({ binding, type, readonly, label, getStatus, getValue, onValueChange, onBlur }) => {
2005
+ const { status, error } = getStatus();
2006
+ const values = getValue() ?? {};
2007
+ return /* @__PURE__ */ React17.createElement(Input12.Root, {
2008
+ validationValence: status
2009
+ }, /* @__PURE__ */ React17.createElement(FormFieldLabel, {
2010
+ error,
2011
+ readonly,
2012
+ label
2013
+ }), /* @__PURE__ */ React17.createElement("div", {
2014
+ className: mx6("grid gap-2", gridCols[binding.length - 1])
2015
+ }, binding.map((prop) => /* @__PURE__ */ React17.createElement(Input12.TextInput, {
2016
+ key: prop,
2017
+ type: "number",
2018
+ disabled: !!readonly,
2019
+ value: values[prop],
2020
+ onChange: (event) => {
2021
+ onValueChange(type, {
2022
+ ...values,
2023
+ [prop]: safeParseFloat3(event.target.value, 0)
2024
+ });
2025
+ },
2026
+ onBlur
2027
+ }))));
1968
2028
  };
1969
2029
 
1970
2030
  // src/components/FieldEditor/FieldEditor.tsx
1971
- var __dxlog_file4 = "/__w/dxos/dxos/packages/ui/react-ui-form/src/components/FieldEditor/FieldEditor.tsx";
1972
- var FieldEditor = ({ projection, field, registry, onSave, onCancel, outerSpacing }) => {
1973
- var _effect = _useSignals14();
1974
- try {
1975
- const { t } = useTranslation8(translationKey);
1976
- const [props, setProps] = useState5(projection.getFieldProjection(field.id).props);
1977
- useEffect8(() => setProps(projection.getFieldProjection(field.id).props), [
1978
- field,
1979
- projection
1980
- ]);
1981
- const [schemas, setSchemas] = useState5([]);
1982
- useEffect8(() => {
1983
- if (!registry) {
1984
- return;
2031
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-form/src/components/FieldEditor/FieldEditor.tsx";
2032
+ var FieldEditor = ({ readonly, projection, field, registry, view, onSave, onCancel }) => {
2033
+ const { t } = useTranslation9(translationKey);
2034
+ const [props, setProps] = useState5(projection.getFieldProjection(field.id).props);
2035
+ useEffect7(() => setProps(projection.getFieldProjection(field.id).props), [
2036
+ field,
2037
+ projection
2038
+ ]);
2039
+ const [schemas, setSchemas] = useState5([]);
2040
+ useAsyncEffect(async () => {
2041
+ if (!registry) {
2042
+ return;
2043
+ }
2044
+ const subscription = registry.query().subscribe((query) => setSchemas(query.results), {
2045
+ fire: true
2046
+ });
2047
+ const schemas2 = await registry.query().run();
2048
+ setSchemas(schemas2);
2049
+ return () => subscription?.();
2050
+ }, [
2051
+ registry
2052
+ ]);
2053
+ const [referenceSchema, setReferenceSchema] = useState5();
2054
+ useEffect7(() => {
2055
+ setReferenceSchema(schemas.find((schema) => schema.typename === props?.referenceSchema));
2056
+ }, [
2057
+ schemas,
2058
+ props?.referenceSchema
2059
+ ]);
2060
+ const [{ fieldSchema }, setFieldSchema] = useState5({
2061
+ fieldSchema: getFormatSchema(props?.format)
2062
+ });
2063
+ const fieldMap = useMemo7(() => ({
2064
+ ["format"]: (props2) => /* @__PURE__ */ React18.createElement(SelectField, {
2065
+ ...props2,
2066
+ options: FormatEnums.filter((value3) => value3 !== Format4.TypeFormat.None).map((value3) => ({
2067
+ value: value3,
2068
+ label: t(`format ${value3}`)
2069
+ }))
2070
+ }),
2071
+ ["referenceSchema"]: (props2) => /* @__PURE__ */ React18.createElement(SelectField, {
2072
+ ...props2,
2073
+ options: schemas.map((schema) => ({
2074
+ value: schema.typename
2075
+ }))
2076
+ }),
2077
+ ["referencePath"]: (props2) => /* @__PURE__ */ React18.createElement(SelectField, {
2078
+ ...props2,
2079
+ options: referenceSchema ? getFormProperties(referenceSchema.ast).sort((a, b) => a.name.toString().localeCompare(b.name.toString())).map((p) => ({
2080
+ value: p.name.toString()
2081
+ })) : []
2082
+ }),
2083
+ ["options"]: (props2) => /* @__PURE__ */ React18.createElement(SelectOptionField, props2)
2084
+ }), [
2085
+ t,
2086
+ schemas,
2087
+ referenceSchema
2088
+ ]);
2089
+ const propIsNotType = useCallback14((props2) => props2.filter((prop) => prop.name !== "type"), []);
2090
+ const handleValuesChanged = useCallback14((_props) => {
2091
+ setFieldSchema((prev) => {
2092
+ const fieldSchema2 = getFormatSchema(_props.format);
2093
+ if (prev.fieldSchema === fieldSchema2) {
2094
+ return prev;
1985
2095
  }
1986
- const subscription = registry.query().subscribe((query) => setSchemas(query.results), {
1987
- fire: true
1988
- });
1989
- const t2 = setTimeout(async () => {
1990
- const schemas2 = await registry.query().run();
1991
- setSchemas(schemas2);
1992
- });
1993
- return () => {
1994
- clearTimeout(t2);
1995
- subscription?.();
2096
+ return {
2097
+ fieldSchema: fieldSchema2
1996
2098
  };
1997
- }, [
1998
- registry
1999
- ]);
2000
- const [referenceSchema, setReferenceSchema] = useState5();
2001
- useEffect8(() => {
2002
- setReferenceSchema(schemas.find((schema) => schema.typename === props?.referenceSchema));
2003
- }, [
2004
- schemas,
2005
- props?.referenceSchema
2006
- ]);
2007
- const [{ fieldSchema }, setFieldSchema] = useState5({
2008
- fieldSchema: getFormatSchema(props?.format)
2009
2099
  });
2010
- const handleValuesChanged = useCallback9((_props) => {
2011
- setFieldSchema((prev) => {
2012
- const fieldSchema2 = getFormatSchema(_props.format);
2013
- if (prev.fieldSchema === fieldSchema2) {
2014
- return prev;
2100
+ setReferenceSchema((prev) => {
2101
+ if (_props.referenceSchema !== prev?.typename) {
2102
+ const newSchema = schemas.find((schema) => schema.typename === _props.referenceSchema);
2103
+ if (newSchema) {
2104
+ return newSchema;
2015
2105
  }
2106
+ }
2107
+ return prev;
2108
+ });
2109
+ setProps((props2) => {
2110
+ const type = formatToType[_props.format];
2111
+ const additionalProps = formatToAdditionalPropertyAttributes[_props.format];
2112
+ if (props2.type !== type) {
2016
2113
  return {
2017
- fieldSchema: fieldSchema2
2114
+ ...props2,
2115
+ ..._props,
2116
+ ...additionalProps,
2117
+ type
2018
2118
  };
2019
- });
2020
- setReferenceSchema((prev) => {
2021
- if (_props.referenceSchema !== prev?.typename) {
2022
- const newSchema = schemas.find((schema) => schema.typename === _props.referenceSchema);
2023
- if (newSchema) {
2024
- return newSchema;
2025
- }
2026
- }
2027
- return prev;
2028
- });
2029
- setProps((props2) => {
2030
- const type = formatToType[_props.format];
2031
- if (props2.type !== type) {
2032
- return {
2033
- ...props2,
2034
- ..._props,
2035
- type
2036
- };
2119
+ }
2120
+ return _props;
2121
+ });
2122
+ }, [
2123
+ schemas,
2124
+ props.format,
2125
+ props.referenceSchema
2126
+ ]);
2127
+ const handleValidate = useCallback14(({ property }) => {
2128
+ if (property && projection.getFields().find((f) => f.path === property && f.path !== field.path)) {
2129
+ return [
2130
+ {
2131
+ path: "property",
2132
+ message: `property is not unique: '${property}'`
2037
2133
  }
2038
- return _props;
2134
+ ];
2135
+ }
2136
+ }, [
2137
+ projection.getFields(),
2138
+ field
2139
+ ]);
2140
+ const handleSave = useCallback14((props2) => {
2141
+ if (view) {
2142
+ Obj3.change(view, () => {
2143
+ projection.setFieldProjection({
2144
+ field,
2145
+ props: props2
2146
+ });
2039
2147
  });
2040
- }, [
2041
- schemas,
2042
- props.format,
2043
- props.referenceSchema
2044
- ]);
2045
- const handleValidate = useCallback9(({ property }) => {
2046
- if (property && projection.fields.find((f) => f.path === property && f.path !== field.path)) {
2047
- return [
2048
- {
2049
- path: "property",
2050
- message: `property is not unique: '${property}'`
2051
- }
2052
- ];
2053
- }
2054
- }, [
2055
- projection.fields,
2056
- field
2057
- ]);
2058
- const handleSave = useCallback9((props2) => {
2148
+ } else {
2059
2149
  projection.setFieldProjection({
2060
2150
  field,
2061
2151
  props: props2
2062
2152
  });
2063
- onSave();
2064
- }, [
2065
- projection,
2066
- field,
2067
- onSave
2068
- ]);
2069
- const handleCancel = useCallback9(() => {
2070
- requestAnimationFrame(() => onCancel?.());
2071
- onSave();
2072
- }, [
2073
- onSave
2074
- ]);
2075
- const custom = useMemo7(() => ({
2076
- ["format"]: (props2) => /* @__PURE__ */ React14.createElement(SelectInput, {
2077
- ...props2,
2078
- options: FormatEnums.filter((value) => value !== FormatEnum5.None).map((value) => ({
2079
- value,
2080
- label: t(`format ${value}`)
2081
- }))
2082
- }),
2083
- ["referenceSchema"]: (props2) => /* @__PURE__ */ React14.createElement(SelectInput, {
2084
- ...props2,
2085
- options: schemas.map((schema) => ({
2086
- value: schema.typename
2087
- }))
2088
- }),
2089
- ["referencePath"]: (props2) => /* @__PURE__ */ React14.createElement(SelectInput, {
2090
- ...props2,
2091
- options: referenceSchema ? getSchemaProperties3(referenceSchema.ast).sort(sortProperties).map((p) => ({
2092
- value: p.name
2093
- })) : []
2094
- }),
2095
- ["options"]: (props2) => /* @__PURE__ */ React14.createElement(SelectOptionInput, props2)
2096
- }), [
2097
- t,
2098
- schemas,
2099
- referenceSchema
2100
- ]);
2101
- const propIsNotType = useCallback9((props2) => props2.filter((p) => p.name !== "type"), []);
2102
- if (!fieldSchema) {
2103
- log3.warn("invalid format", {
2104
- props
2105
- }, {
2106
- F: __dxlog_file4,
2107
- L: 173,
2108
- S: void 0,
2109
- C: (f, a) => f(...a)
2110
- });
2111
- return null;
2112
2153
  }
2113
- return /* @__PURE__ */ React14.createElement(Form, {
2114
- key: field.id,
2115
- autoFocus: true,
2116
- values: props,
2117
- schema: fieldSchema,
2118
- filter: propIsNotType,
2119
- sort: [
2120
- "property",
2121
- "format"
2122
- ],
2123
- onValuesChanged: handleValuesChanged,
2124
- onValidate: handleValidate,
2125
- onSave: handleSave,
2126
- onCancel: handleCancel,
2127
- Custom: custom,
2128
- outerSpacing
2154
+ onSave();
2155
+ }, [
2156
+ projection,
2157
+ field,
2158
+ view,
2159
+ onSave
2160
+ ]);
2161
+ const handleCancel = useCallback14(() => {
2162
+ onSave();
2163
+ requestAnimationFrame(() => onCancel?.());
2164
+ }, [
2165
+ onSave
2166
+ ]);
2167
+ if (!fieldSchema) {
2168
+ log2.warn("invalid format", {
2169
+ props
2170
+ }, {
2171
+ F: __dxlog_file2,
2172
+ L: 173,
2173
+ S: void 0,
2174
+ C: (f, a) => f(...a)
2129
2175
  });
2130
- } finally {
2131
- _effect.f();
2176
+ return null;
2132
2177
  }
2178
+ return /* @__PURE__ */ React18.createElement(Form.Root, {
2179
+ key: field.id,
2180
+ fieldMap,
2181
+ autoFocus: true,
2182
+ readonly,
2183
+ schema: fieldSchema,
2184
+ values: props,
2185
+ exclude: propIsNotType,
2186
+ sort: [
2187
+ "property",
2188
+ "format"
2189
+ ],
2190
+ onValuesChanged: handleValuesChanged,
2191
+ onValidate: handleValidate,
2192
+ onSave: handleSave,
2193
+ onCancel: handleCancel
2194
+ }, /* @__PURE__ */ React18.createElement(Form.Content, null, /* @__PURE__ */ React18.createElement(Form.FieldSet, null), /* @__PURE__ */ React18.createElement(Form.Actions, null)));
2133
2195
  };
2134
2196
 
2135
2197
  // src/components/ViewEditor/ViewEditor.tsx
2136
- import { useSignals as _useSignals15 } from "@preact-signals/safe-react/tracking";
2137
- import { Schema as Schema2 } from "effect";
2138
- import React15, { useCallback as useCallback10, useMemo as useMemo8, useState as useState6 } from "react";
2139
- import { EchoSchema, Format, isMutable, toJsonSchema } from "@dxos/echo-schema";
2140
- import { invariant as invariant3 } from "@dxos/invariant";
2141
- import { Callout, IconButton as IconButton4, useTranslation as useTranslation9 } from "@dxos/react-ui";
2198
+ import { RegistryContext } from "@effect-atom/atom-react";
2199
+ import * as Array2 from "effect/Array";
2200
+ import * as Match2 from "effect/Match";
2201
+ import * as Option4 from "effect/Option";
2202
+ import * as Schema3 from "effect/Schema";
2203
+ import React19, { forwardRef as forwardRef3, useCallback as useCallback15, useContext, useImperativeHandle, useMemo as useMemo8, useState as useState6 } from "react";
2204
+ import { Filter as Filter2, Format as Format5, Obj as Obj4, Query, QueryAST } from "@dxos/echo";
2205
+ import { EchoSchema, isMutable, toJsonSchema } from "@dxos/echo/internal";
2206
+ import { invariant } from "@dxos/invariant";
2207
+ import { useObject } from "@dxos/react-client/echo";
2208
+ import { Callout, IconButton as IconButton4, Input as Input13, useTranslation as useTranslation10 } from "@dxos/react-ui";
2209
+ import { QueryForm } from "@dxos/react-ui-components";
2142
2210
  import { List as List2 } from "@dxos/react-ui-list";
2143
- import { cardSpacing as cardSpacing3 } from "@dxos/react-ui-stack";
2144
- import { ghostHover, inputTextLabel, mx as mx9 } from "@dxos/react-ui-theme";
2145
- import { FieldSchema, VIEW_FIELD_LIMIT, ProjectionModel } from "@dxos/schema";
2146
- var __dxlog_file5 = "/__w/dxos/dxos/packages/ui/react-ui-form/src/components/ViewEditor/ViewEditor.tsx";
2147
- var grid = "grid grid-cols-[32px_1fr_32px_32px] min-bs-[2.5rem]";
2148
- var ViewMetaSchema = Schema2.Struct({
2149
- typename: Format.URL.annotations({
2150
- title: "Typename"
2151
- })
2152
- }).pipe(Schema2.mutable);
2153
- var ViewEditor = ({ classNames, schema, view, registry, readonly: _readonly, showHeading = false, onTypenameChanged, onDelete, outerSpacing = true }) => {
2154
- var _effect = _useSignals15();
2155
- try {
2156
- const { t } = useTranslation9(translationKey);
2157
- const projection = useMemo8(() => {
2158
- const jsonSchema = schema instanceof EchoSchema ? schema.jsonSchema : toJsonSchema(schema);
2159
- return new ProjectionModel(jsonSchema, view.projection);
2160
- }, [
2161
- schema,
2162
- view.projection
2163
- ]);
2164
- const [field, setField] = useState6();
2165
- const readonly = _readonly || !isMutable(schema);
2166
- const viewValues = useMemo8(() => {
2167
- return {
2168
- // TODO(burdon): Need to warn user of possible consequences of editing.
2169
- // TODO(burdon): Settings should have domain name owned by user.
2170
- typename: view.query.typename
2171
- };
2172
- }, [
2173
- view.query.typename
2174
- ]);
2175
- const handleSelect = useCallback10((field2) => {
2176
- if (readonly) {
2177
- return;
2178
- }
2179
- setField((f) => f === field2 ? void 0 : field2);
2180
- }, [
2181
- readonly
2182
- ]);
2183
- const handleAdd = useCallback10(() => {
2184
- invariant3(!readonly, void 0, {
2185
- F: __dxlog_file5,
2186
- L: 88,
2187
- S: void 0,
2188
- A: [
2189
- "!readonly",
2190
- ""
2191
- ]
2192
- });
2193
- const field2 = projection.createFieldProjection();
2194
- setField(field2);
2195
- }, [
2196
- schema,
2197
- projection,
2198
- readonly
2199
- ]);
2200
- const handleUpdate = useCallback10(({ typename }) => {
2201
- invariant3(!readonly, void 0, {
2202
- F: __dxlog_file5,
2203
- L: 95,
2204
- S: void 0,
2205
- A: [
2206
- "!readonly",
2207
- ""
2208
- ]
2209
- });
2210
- requestAnimationFrame(() => {
2211
- if (view.query.typename !== typename && !readonly) {
2212
- onTypenameChanged?.(typename);
2213
- }
2214
- });
2215
- }, [
2216
- view.query.typename,
2217
- onTypenameChanged,
2218
- readonly
2219
- ]);
2220
- const handleDelete = useCallback10((fieldId) => {
2221
- invariant3(!readonly, void 0, {
2222
- F: __dxlog_file5,
2223
- L: 107,
2224
- S: void 0,
2225
- A: [
2226
- "!readonly",
2227
- ""
2228
- ]
2229
- });
2230
- if (fieldId === field?.id) {
2231
- setField(void 0);
2232
- }
2233
- onDelete?.(fieldId);
2234
- }, [
2235
- field,
2236
- onDelete,
2237
- readonly
2238
- ]);
2239
- const handleMove = useCallback10((fromIndex, toIndex) => {
2240
- invariant3(!readonly, void 0, {
2241
- F: __dxlog_file5,
2242
- L: 119,
2243
- S: void 0,
2244
- A: [
2245
- "!readonly",
2246
- ""
2247
- ]
2248
- });
2211
+ import { FieldSchema, ProjectionModel, VIEW_FIELD_LIMIT, createEchoChangeCallback, getTypenameFromQuery } from "@dxos/schema";
2212
+ import { mx as mx7, osTranslations as osTranslations2, subtleHover as subtleHover2 } from "@dxos/ui-theme";
2213
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/ui/react-ui-form/src/components/ViewEditor/ViewEditor.tsx";
2214
+ var listGrid = "grid grid-cols-[min-content_1fr_min-content_min-content_min-content]";
2215
+ var listItemGrid = "grid grid-cols-subgrid col-span-5";
2216
+ var ViewEditor = /* @__PURE__ */ forwardRef3(({ classNames, schema, view, mode = "schema", registry, readonly, showHeading = false, types, tags, onQueryChanged, onDelete }, forwardedRef) => {
2217
+ const atomRegistry = useContext(RegistryContext);
2218
+ const schemaReadonly = !isMutable(schema);
2219
+ const { t } = useTranslation10(translationKey);
2220
+ const projectionModel = useMemo8(() => {
2221
+ const jsonSchema = schema instanceof EchoSchema ? schema.jsonSchema : toJsonSchema(schema);
2222
+ const change = createEchoChangeCallback(view, schema instanceof EchoSchema ? schema : void 0);
2223
+ const model = new ProjectionModel({
2224
+ registry: atomRegistry,
2225
+ view,
2226
+ baseSchema: jsonSchema,
2227
+ change
2228
+ });
2229
+ return model;
2230
+ }, [
2231
+ atomRegistry,
2232
+ schema,
2233
+ view
2234
+ ]);
2235
+ useImperativeHandle(forwardedRef, () => projectionModel, [
2236
+ projectionModel
2237
+ ]);
2238
+ const [expandedField, setExpandedField] = useState6();
2239
+ const queueTarget = Match2.value(view.query.ast).pipe(Match2.when({
2240
+ type: "options"
2241
+ }, ({ options }) => {
2242
+ return Option4.fromNullable(options.queues).pipe(Option4.flatMap((queues) => Array2.head(queues)), Option4.getOrUndefined);
2243
+ }), Match2.orElse(() => void 0));
2244
+ const viewSchema = useMemo8(() => {
2245
+ const base = Schema3.Struct({
2246
+ query: mode === "schema" ? Format5.URL.annotations({
2247
+ title: "Record type"
2248
+ }) : QueryAST.Query.annotations({
2249
+ title: "Query"
2250
+ })
2251
+ });
2252
+ if (mode === "tag") {
2253
+ return Schema3.Struct({
2254
+ ...base.fields,
2255
+ target: Schema3.optional(Schema3.String.annotations({
2256
+ title: "Target Queue"
2257
+ }))
2258
+ }).pipe(Schema3.mutable);
2259
+ }
2260
+ return base.pipe(Schema3.mutable);
2261
+ }, [
2262
+ mode
2263
+ ]);
2264
+ const viewValues = useMemo8(() => ({
2265
+ query: mode === "schema" ? getTypenameFromQuery(view.query.ast) : view.query.ast,
2266
+ target: queueTarget
2267
+ }), [
2268
+ mode,
2269
+ view.query.ast,
2270
+ queueTarget
2271
+ ]);
2272
+ const fieldMap = useMemo8(() => mode === "tag" ? customFields({
2273
+ types,
2274
+ tags
2275
+ }) : void 0, [
2276
+ mode,
2277
+ types,
2278
+ tags
2279
+ ]);
2280
+ const handleAdd = useCallback15(() => {
2281
+ invariant(!readonly, void 0, {
2282
+ F: __dxlog_file3,
2283
+ L: 146,
2284
+ S: void 0,
2285
+ A: [
2286
+ "!readonly",
2287
+ ""
2288
+ ]
2289
+ });
2290
+ const field = projectionModel.createFieldProjection();
2291
+ setExpandedField(field.id);
2292
+ }, [
2293
+ projectionModel,
2294
+ readonly
2295
+ ]);
2296
+ const handleUpdate = useCallback15((values) => {
2297
+ requestAnimationFrame(() => {
2298
+ const query = mode === "schema" ? Query.select(Filter2.typename(values.query)).ast : values.query;
2299
+ onQueryChanged?.(query, values.target);
2300
+ });
2301
+ }, [
2302
+ onQueryChanged,
2303
+ view,
2304
+ queueTarget,
2305
+ mode
2306
+ ]);
2307
+ const handleDelete = useCallback15((fieldId) => {
2308
+ invariant(!readonly, void 0, {
2309
+ F: __dxlog_file3,
2310
+ L: 163,
2311
+ S: void 0,
2312
+ A: [
2313
+ "!readonly",
2314
+ ""
2315
+ ]
2316
+ });
2317
+ if (fieldId === expandedField) {
2318
+ setExpandedField(void 0);
2319
+ }
2320
+ onDelete?.(fieldId);
2321
+ }, [
2322
+ expandedField,
2323
+ onDelete,
2324
+ readonly
2325
+ ]);
2326
+ return /* @__PURE__ */ React19.createElement("div", {
2327
+ role: "none",
2328
+ className: mx7(classNames)
2329
+ }, schemaReadonly && !readonly && /* @__PURE__ */ React19.createElement(Callout.Root, {
2330
+ valence: "info",
2331
+ classNames: "mlb-cardSpacingBlock"
2332
+ }, /* @__PURE__ */ React19.createElement(Callout.Title, null, t("system schema description"))), /* @__PURE__ */ React19.createElement(Form.Root, {
2333
+ schema: viewSchema,
2334
+ values: viewValues,
2335
+ fieldMap,
2336
+ autoSave: true,
2337
+ onSave: handleUpdate
2338
+ }, /* @__PURE__ */ React19.createElement(Form.FieldSet, null), /* @__PURE__ */ React19.createElement(FormFieldLabel, {
2339
+ label: t("fields label"),
2340
+ asChild: true
2341
+ }), /* @__PURE__ */ React19.createElement(FieldList, {
2342
+ schema,
2343
+ view,
2344
+ registry,
2345
+ readonly,
2346
+ showHeading,
2347
+ onDelete: handleDelete
2348
+ })), !readonly && !expandedField && /* @__PURE__ */ React19.createElement("div", {
2349
+ role: "none",
2350
+ className: "mlb-cardSpacingBlock"
2351
+ }, /* @__PURE__ */ React19.createElement(IconButton4, {
2352
+ icon: "ph--plus--regular",
2353
+ label: t("add property button label"),
2354
+ onClick: readonly ? void 0 : handleAdd,
2355
+ // TODO(burdon): Show field limit in ux (not tooltip).
2356
+ disabled: view.projection.fields.length >= VIEW_FIELD_LIMIT,
2357
+ classNames: "is-full"
2358
+ })));
2359
+ });
2360
+ var FieldList = ({ schema, view, registry, readonly, showHeading = false, onDelete }) => {
2361
+ const atomRegistry = useContext(RegistryContext);
2362
+ const schemaReadonly = !isMutable(schema);
2363
+ const { t } = useTranslation10(translationKey);
2364
+ const [viewSnapshot] = useObject(view);
2365
+ const projectionModel = useMemo8(() => {
2366
+ const jsonSchema = schema instanceof EchoSchema ? schema.jsonSchema : toJsonSchema(schema);
2367
+ const change = createEchoChangeCallback(view, schema instanceof EchoSchema ? schema : void 0);
2368
+ const model = new ProjectionModel({
2369
+ registry: atomRegistry,
2370
+ view,
2371
+ baseSchema: jsonSchema,
2372
+ change
2373
+ });
2374
+ return model;
2375
+ }, [
2376
+ atomRegistry,
2377
+ schema,
2378
+ view
2379
+ ]);
2380
+ const [expandedField, setExpandedField] = useState6();
2381
+ const handleToggleField = useCallback15((field) => {
2382
+ setExpandedField((prevExpandedFieldId) => prevExpandedFieldId === field.id ? void 0 : field.id);
2383
+ }, [
2384
+ readonly
2385
+ ]);
2386
+ const handleDelete = useCallback15((fieldId) => {
2387
+ invariant(!readonly, void 0, {
2388
+ F: __dxlog_file3,
2389
+ L: 253,
2390
+ S: void 0,
2391
+ A: [
2392
+ "!readonly",
2393
+ ""
2394
+ ]
2395
+ });
2396
+ if (fieldId === expandedField) {
2397
+ setExpandedField(void 0);
2398
+ }
2399
+ onDelete?.(fieldId);
2400
+ }, [
2401
+ expandedField,
2402
+ onDelete,
2403
+ readonly
2404
+ ]);
2405
+ const handleMove = useCallback15((fromIndex, toIndex) => {
2406
+ invariant(!readonly, void 0, {
2407
+ F: __dxlog_file3,
2408
+ L: 265,
2409
+ S: void 0,
2410
+ A: [
2411
+ "!readonly",
2412
+ ""
2413
+ ]
2414
+ });
2415
+ Obj4.change(view, (v) => {
2249
2416
  const fields = [
2250
- ...view.projection.fields
2417
+ ...v.projection.fields
2251
2418
  ];
2252
2419
  const [moved] = fields.splice(fromIndex, 1);
2253
2420
  fields.splice(toIndex, 0, moved);
2254
- view.projection.fields = fields;
2255
- }, [
2256
- view.projection.fields,
2257
- readonly
2258
- ]);
2259
- const handleClose = useCallback10(() => setField(void 0), []);
2260
- const hiddenProperties = projection.getHiddenProperties();
2261
- const handleHide = useCallback10((fieldId) => {
2262
- setField(void 0);
2263
- projection.hideFieldProjection(fieldId);
2264
- }, [
2265
- projection
2266
- ]);
2267
- const handleShow = useCallback10((property) => {
2268
- setField(void 0);
2269
- projection.showFieldProjection(property);
2270
- }, [
2271
- projection
2272
- ]);
2273
- return /* @__PURE__ */ React15.createElement("div", {
2274
- role: "none",
2275
- className: mx9(classNames)
2276
- }, readonly && /* @__PURE__ */ React15.createElement("div", {
2421
+ v.projection.fields = fields;
2422
+ });
2423
+ }, [
2424
+ view,
2425
+ readonly
2426
+ ]);
2427
+ const handleClose = useCallback15(() => setExpandedField(void 0), []);
2428
+ const handleHide = useCallback15((fieldId) => {
2429
+ setExpandedField(void 0);
2430
+ projectionModel.hideFieldProjection(fieldId);
2431
+ }, [
2432
+ projectionModel
2433
+ ]);
2434
+ const handleShow = useCallback15((property) => {
2435
+ setExpandedField(void 0);
2436
+ projectionModel.showFieldProjection(property);
2437
+ }, [
2438
+ projectionModel
2439
+ ]);
2440
+ if (!viewSnapshot) {
2441
+ return null;
2442
+ }
2443
+ return /* @__PURE__ */ React19.createElement(List2.Root, {
2444
+ items: viewSnapshot.projection.fields,
2445
+ isItem: Schema3.is(FieldSchema),
2446
+ getId: (field) => field.id,
2447
+ onMove: readonly ? void 0 : handleMove,
2448
+ readonly
2449
+ }, ({ items: fields }) => /* @__PURE__ */ React19.createElement(React19.Fragment, null, showHeading && /* @__PURE__ */ React19.createElement("h3", {
2450
+ className: "text-sm"
2451
+ }, t("field path label")), /* @__PURE__ */ React19.createElement("div", {
2452
+ role: "list",
2453
+ className: listGrid
2454
+ }, fields?.map((field) => {
2455
+ const hidden = field.visible === false;
2456
+ return /* @__PURE__ */ React19.createElement(List2.Item, {
2457
+ key: field.id,
2458
+ item: field,
2459
+ classNames: listItemGrid,
2460
+ "aria-expanded": expandedField === field.id
2461
+ }, /* @__PURE__ */ React19.createElement("div", {
2277
2462
  role: "none",
2278
- className: mx9("is-full", outerSpacing && "plb-cardSpacingBlock pli-cardSpacingInline")
2279
- }, /* @__PURE__ */ React15.createElement(Callout.Root, {
2280
- valence: "info"
2281
- }, /* @__PURE__ */ React15.createElement(Callout.Title, null, t("system schema description")))), /* @__PURE__ */ React15.createElement(Form, {
2282
- autoSave: true,
2283
- schema: ViewMetaSchema,
2284
- values: viewValues,
2285
- readonly,
2286
- onSave: handleUpdate,
2287
- outerSpacing
2288
- }), /* @__PURE__ */ React15.createElement("div", {
2463
+ className: mx7(subtleHover2, listItemGrid, "rounded-sm cursor-pointer min-bs-10")
2464
+ }, /* @__PURE__ */ React19.createElement(List2.ItemDragHandle, {
2465
+ disabled: readonly || schemaReadonly
2466
+ }), /* @__PURE__ */ React19.createElement(List2.ItemTitle, {
2467
+ classNames: hidden && "text-subdued",
2468
+ onClick: () => handleToggleField(field)
2469
+ }, field.path), /* @__PURE__ */ React19.createElement(List2.ItemButton, {
2470
+ label: t(hidden ? "show field label" : "hide field label"),
2471
+ "data-testid": hidden ? "show-field-button" : "hide-field-button",
2472
+ icon: hidden ? "ph--eye-closed--regular" : "ph--eye--regular",
2473
+ autoHide: false,
2474
+ disabled: readonly || !hidden && projectionModel.getFields().length <= 1,
2475
+ onClick: () => hidden ? handleShow(field.path) : handleHide(field.id)
2476
+ }), !readonly && /* @__PURE__ */ React19.createElement(React19.Fragment, null, /* @__PURE__ */ React19.createElement(List2.ItemDeleteButton, {
2477
+ label: t("delete field label"),
2478
+ autoHide: false,
2479
+ disabled: readonly || schemaReadonly || viewSnapshot.projection.fields.length <= 1,
2480
+ onClick: () => handleDelete(field.id),
2481
+ "data-testid": "field.delete"
2482
+ }), /* @__PURE__ */ React19.createElement(IconButton4, {
2483
+ iconOnly: true,
2484
+ variant: "ghost",
2485
+ label: t("toggle expand label", {
2486
+ ns: osTranslations2
2487
+ }),
2488
+ icon: expandedField === field.id ? "ph--caret-down--regular" : "ph--caret-right--regular",
2489
+ onClick: () => handleToggleField(field),
2490
+ "data-testid": "field.toggle"
2491
+ }))), expandedField === field.id && !readonly && /* @__PURE__ */ React19.createElement("div", {
2289
2492
  role: "none",
2290
- className: outerSpacing ? cardSpacing3 : "mlb-cardSpacingBlock"
2291
- }, /* @__PURE__ */ React15.createElement("label", {
2292
- className: mx9(inputTextLabel)
2293
- }, t("fields label")), /* @__PURE__ */ React15.createElement(List2.Root, {
2294
- items: view.projection.fields,
2295
- isItem: Schema2.is(FieldSchema),
2296
- getId: (field2) => field2.id,
2297
- readonly,
2298
- onMove: readonly ? void 0 : handleMove
2299
- }, ({ items: fields }) => /* @__PURE__ */ React15.createElement(React15.Fragment, null, showHeading && /* @__PURE__ */ React15.createElement("div", {
2300
- role: "heading",
2301
- className: grid
2302
- }, /* @__PURE__ */ React15.createElement("div", null), /* @__PURE__ */ React15.createElement("div", {
2303
- className: "flex items-center text-sm"
2304
- }, t("field path label"))), /* @__PURE__ */ React15.createElement("div", {
2305
- role: "list",
2306
- className: "flex flex-col is-full"
2307
- }, fields?.map((field2) => /* @__PURE__ */ React15.createElement(List2.Item, {
2308
- key: field2.id,
2309
- item: field2,
2310
- classNames: mx9(grid, ghostHover, "overflow-hidden", !readonly && "cursor-pointer")
2311
- }, /* @__PURE__ */ React15.createElement(List2.ItemDragHandle, null), /* @__PURE__ */ React15.createElement(List2.ItemTitle, {
2312
- onClick: () => handleSelect(field2)
2313
- }, field2.path), /* @__PURE__ */ React15.createElement(List2.ItemButton, {
2314
- "data-testid": "hide-field-button",
2315
- icon: "ph--eye-slash--regular",
2316
- // TDOO(burdon): Is this the correct test?
2317
- disabled: view.projection.fields.length <= 1,
2318
- onClick: () => handleHide(field2.id)
2319
- }), !readonly && /* @__PURE__ */ React15.createElement(List2.ItemDeleteButton, {
2320
- disabled: view.projection.fields.length <= 1,
2321
- onClick: () => handleDelete(field2.id)
2322
- })))))), hiddenProperties.length > 0 && /* @__PURE__ */ React15.createElement(React15.Fragment, null, /* @__PURE__ */ React15.createElement("label", {
2323
- className: mx9(inputTextLabel)
2324
- }, t("hidden fields label")), /* @__PURE__ */ React15.createElement(List2.Root, {
2325
- items: hiddenProperties,
2326
- isItem: (item) => typeof item === "string",
2327
- getId: (property) => property
2328
- }, ({ items: properties }) => /* @__PURE__ */ React15.createElement("div", {
2329
- role: "list",
2330
- className: "flex flex-col is-full"
2331
- }, properties?.map((property) => /* @__PURE__ */ React15.createElement(List2.Item, {
2332
- key: property,
2333
- item: property,
2334
- classNames: mx9(grid, ghostHover, !readonly && "cursor-pointer")
2335
- }, /* @__PURE__ */ React15.createElement(List2.ItemDragHandle, {
2336
- disabled: true
2337
- }), /* @__PURE__ */ React15.createElement(List2.ItemTitle, null, property), /* @__PURE__ */ React15.createElement(List2.ItemButton, {
2338
- icon: "ph--eye--regular",
2339
- onClick: () => handleShow(property),
2340
- "data-testid": "show-field-button"
2341
- }))))))), field && /* @__PURE__ */ React15.createElement(FieldEditor, {
2342
- key: field.id,
2343
- projection,
2344
- field,
2493
+ className: "col-span-5 mbs-1 mbe-1 border border-separator rounded-md"
2494
+ }, /* @__PURE__ */ React19.createElement(FieldEditor, {
2495
+ readonly: readonly || schemaReadonly,
2345
2496
  registry,
2497
+ projection: projectionModel,
2498
+ field,
2346
2499
  onSave: handleClose
2347
- }), !readonly && !field && /* @__PURE__ */ React15.createElement("div", {
2348
- role: "none",
2349
- className: outerSpacing ? cardSpacing3 : "mlb-cardSpacingBlock"
2350
- }, /* @__PURE__ */ React15.createElement(IconButton4, {
2351
- icon: "ph--plus--regular",
2352
- label: t("button add property"),
2353
- onClick: readonly ? void 0 : handleAdd,
2354
- // TODO(burdon): Show field limit in ux (not tooltip).
2355
- disabled: view.projection.fields.length >= VIEW_FIELD_LIMIT,
2356
- classNames: "flex is-full"
2357
2500
  })));
2358
- } finally {
2359
- _effect.f();
2360
- }
2501
+ }))));
2361
2502
  };
2503
+ var customFields = ({ types, tags }) => ({
2504
+ query: ({ type, readonly, label, getValue, onValueChange }) => {
2505
+ const handleChange = useCallback15((query) => onValueChange(type, query.ast), [
2506
+ onValueChange
2507
+ ]);
2508
+ return /* @__PURE__ */ React19.createElement(Input13.Root, null, /* @__PURE__ */ React19.createElement(FormFieldLabel, {
2509
+ readonly,
2510
+ label
2511
+ }), /* @__PURE__ */ React19.createElement(QueryForm, {
2512
+ initialQuery: getValue(),
2513
+ types,
2514
+ tags,
2515
+ onChange: handleChange
2516
+ }));
2517
+ }
2518
+ });
2362
2519
  export {
2363
- BooleanInput,
2520
+ ArrayField,
2521
+ BooleanField,
2364
2522
  ControlFrame,
2365
2523
  ControlFrameItem,
2366
2524
  ControlGroup,
@@ -2370,34 +2528,38 @@ export {
2370
2528
  ControlPage,
2371
2529
  ControlSection,
2372
2530
  ControlSectionHeading,
2373
- DeprecatedFormContainer,
2374
- DeprecatedFormInput,
2375
2531
  FieldEditor,
2376
2532
  Form,
2377
- FormProvider,
2378
- GeoPointInput,
2379
- InputHeader,
2380
- MarkdownInput,
2381
- NumberInput,
2382
- SelectInput,
2383
- SelectOptionInput,
2384
- TextInput,
2385
- TupleInput,
2533
+ FormFieldErrorBoundary,
2534
+ FormFieldLabel,
2535
+ FormFieldWrapper,
2536
+ GeoPointField,
2537
+ MarkdownField,
2538
+ NumberField,
2539
+ ObjectPicker,
2540
+ RefField,
2541
+ SelectField,
2542
+ SelectOptionField,
2543
+ TextAreaField,
2544
+ TextField,
2545
+ TupleField,
2386
2546
  ViewEditor,
2387
2547
  cellClassesForFieldType,
2388
2548
  controlItemClasses,
2389
- findArrayElementType,
2390
2549
  formatForDisplay,
2391
2550
  formatForEditing,
2551
+ getDefaultValue,
2552
+ getFormProperties,
2392
2553
  getRefProps,
2554
+ omitId,
2393
2555
  parseValue,
2394
2556
  pathNotUniqueError,
2557
+ setValueEchoAware,
2395
2558
  translations,
2396
- useForm,
2397
2559
  useFormContext,
2560
+ useFormFieldState,
2561
+ useFormHandler,
2398
2562
  useFormValues,
2399
- useInputProps,
2400
- useQueryRefOptions,
2401
- useRefQueryLookupHandler
2563
+ useKeyHandler
2402
2564
  };
2403
2565
  //# sourceMappingURL=index.mjs.map