@dxos/react-ui-form 0.8.4-main.84f28bd → 0.8.4-main.9735255

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