@dxos/react-ui-form 0.8.4-main.406dc2a → 0.8.4-main.422d1c7879

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 (219) hide show
  1. package/README.md +0 -1
  2. package/dist/lib/browser/index.mjs +2856 -2309
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +2856 -2309
  6. package/dist/lib/node-esm/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +8 -7
  9. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  10. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +68 -60
  11. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
  12. package/dist/types/src/components/Form/Form.d.ts +93 -26
  13. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  14. package/dist/types/src/components/Form/Form.stories.d.ts +114 -137
  15. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/FormField.d.ts +43 -0
  17. package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
  18. package/dist/types/src/components/Form/FormFieldComponent.d.ts +80 -0
  19. package/dist/types/src/components/Form/FormFieldComponent.d.ts.map +1 -0
  20. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts +9 -0
  21. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts.map +1 -0
  22. package/dist/types/src/components/Form/FormFieldSet.d.ts +18 -0
  23. package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
  24. package/dist/types/src/components/Form/fields/ArrayField.d.ts +18 -0
  25. package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -0
  26. package/dist/types/src/components/Form/fields/BooleanField.d.ts +4 -0
  27. package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -0
  28. package/dist/types/src/components/Form/fields/GeoPointField.d.ts +5 -0
  29. package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -0
  30. package/dist/types/src/components/Form/fields/MarkdownField.d.ts +11 -0
  31. package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
  32. package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts +83 -0
  33. package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts.map +1 -0
  34. package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
  35. package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
  36. package/dist/types/src/components/Form/fields/RefField.d.ts +22 -0
  37. package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
  38. package/dist/types/src/components/Form/fields/SelectField.d.ts +10 -0
  39. package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
  40. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
  41. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
  42. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +84 -0
  43. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
  44. package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
  45. package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
  46. package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
  47. package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
  48. package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
  49. package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
  50. package/dist/types/src/components/Form/fields/index.d.ts +12 -0
  51. package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
  52. package/dist/types/src/components/Form/index.d.ts +2 -4
  53. package/dist/types/src/components/Form/index.d.ts.map +1 -1
  54. package/dist/types/src/components/ObjectForm/ObjectForm.d.ts +9 -0
  55. package/dist/types/src/components/ObjectForm/ObjectForm.d.ts.map +1 -0
  56. package/dist/types/src/components/ObjectForm/index.d.ts +2 -0
  57. package/dist/types/src/components/ObjectForm/index.d.ts.map +1 -0
  58. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +37 -10
  59. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -1
  60. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +64 -59
  61. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -1
  62. package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts +9 -0
  63. package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts.map +1 -0
  64. package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts +127 -0
  65. package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts.map +1 -0
  66. package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts +2 -0
  67. package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts.map +1 -0
  68. package/dist/types/src/components/ObjectProperties/index.d.ts +2 -0
  69. package/dist/types/src/components/ObjectProperties/index.d.ts.map +1 -0
  70. package/dist/types/src/components/Settings/Settings.d.ts +43 -0
  71. package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
  72. package/dist/types/src/components/Settings/Settings.stories.d.ts +84 -0
  73. package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
  74. package/dist/types/src/components/Settings/SettingsFieldSet.d.ts +38 -0
  75. package/dist/types/src/components/Settings/SettingsFieldSet.d.ts.map +1 -0
  76. package/dist/types/src/components/Settings/SettingsItem.d.ts +17 -0
  77. package/dist/types/src/components/Settings/SettingsItem.d.ts.map +1 -0
  78. package/dist/types/src/components/Settings/index.d.ts +3 -0
  79. package/dist/types/src/components/Settings/index.d.ts.map +1 -0
  80. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +13 -15
  81. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  82. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +70 -64
  83. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  84. package/dist/types/src/components/index.d.ts +3 -1
  85. package/dist/types/src/components/index.d.ts.map +1 -1
  86. package/dist/types/src/components/testing.d.ts +4 -6
  87. package/dist/types/src/components/testing.d.ts.map +1 -1
  88. package/dist/types/src/hooks/index.d.ts +2 -3
  89. package/dist/types/src/hooks/index.d.ts.map +1 -1
  90. package/dist/types/src/hooks/useFormHandler.d.ts +82 -0
  91. package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -0
  92. package/dist/types/src/hooks/useKeyHandler.d.ts +7 -0
  93. package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
  94. package/dist/types/src/translations.d.ts +65 -60
  95. package/dist/types/src/translations.d.ts.map +1 -1
  96. package/dist/types/src/util/field-type.d.ts +19 -0
  97. package/dist/types/src/util/field-type.d.ts.map +1 -0
  98. package/dist/types/src/util/field.d.ts +3 -3
  99. package/dist/types/src/util/field.d.ts.map +1 -1
  100. package/dist/types/src/util/formatting.d.ts +2 -2
  101. package/dist/types/src/util/formatting.d.ts.map +1 -1
  102. package/dist/types/src/util/index.d.ts +2 -1
  103. package/dist/types/src/util/index.d.ts.map +1 -1
  104. package/dist/types/src/util/properties.d.ts +7 -0
  105. package/dist/types/src/util/properties.d.ts.map +1 -0
  106. package/dist/types/src/util/refs.d.ts +2 -2
  107. package/dist/types/src/util/refs.d.ts.map +1 -1
  108. package/dist/types/tsconfig.tsbuildinfo +1 -1
  109. package/package.json +47 -50
  110. package/src/components/FieldEditor/FieldEditor.stories.tsx +43 -25
  111. package/src/components/FieldEditor/FieldEditor.test.tsx +0 -1
  112. package/src/components/FieldEditor/FieldEditor.tsx +74 -78
  113. package/src/components/Form/AUDIT.md +17 -0
  114. package/src/components/Form/Form.stories.tsx +137 -250
  115. package/src/components/Form/Form.tsx +375 -107
  116. package/src/components/Form/FormField.tsx +305 -0
  117. package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
  118. package/src/components/Form/FormFieldComponent.tsx +174 -0
  119. package/src/components/Form/FormFieldSet.tsx +144 -0
  120. package/src/components/Form/README.md +3 -0
  121. package/src/components/Form/fields/ArrayField.tsx +158 -0
  122. package/src/components/Form/fields/BooleanField.tsx +22 -0
  123. package/src/components/Form/fields/GeoPointField.tsx +120 -0
  124. package/src/components/Form/fields/MarkdownField.stories.tsx +151 -0
  125. package/src/components/Form/fields/MarkdownField.tsx +167 -0
  126. package/src/components/Form/fields/NumberField.tsx +39 -0
  127. package/src/components/Form/fields/RefField.tsx +228 -0
  128. package/src/components/Form/fields/SelectField.tsx +65 -0
  129. package/src/components/Form/{custom/SelectOptionsInput.stories.tsx → fields/SelectOptionField.stories.tsx} +21 -17
  130. package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +28 -23
  131. package/src/components/Form/fields/TextAreaField.tsx +38 -0
  132. package/src/components/Form/fields/TextField.tsx +38 -0
  133. package/src/components/Form/{custom/TupleInput.tsx → fields/TupleField.tsx} +16 -9
  134. package/src/components/Form/fields/index.ts +15 -0
  135. package/src/components/Form/index.ts +3 -4
  136. package/src/components/ObjectForm/ObjectForm.tsx +113 -0
  137. package/src/components/{ControlSection → ObjectForm}/index.ts +1 -1
  138. package/src/components/ObjectPicker/ObjectPicker.stories.tsx +23 -21
  139. package/src/components/ObjectPicker/ObjectPicker.tsx +112 -80
  140. package/src/components/ObjectProperties/ObjectProperties.stories.tsx +361 -0
  141. package/src/components/ObjectProperties/ObjectProperties.test.tsx +101 -0
  142. package/src/components/ObjectProperties/ObjectProperties.tsx +140 -0
  143. package/src/components/ObjectProperties/index.ts +5 -0
  144. package/src/components/Settings/Settings.stories.tsx +103 -0
  145. package/src/components/Settings/Settings.tsx +81 -0
  146. package/src/components/Settings/SettingsFieldSet.tsx +211 -0
  147. package/src/components/Settings/SettingsItem.tsx +62 -0
  148. package/src/components/Settings/index.ts +6 -0
  149. package/src/components/ViewEditor/ViewEditor.stories.tsx +53 -50
  150. package/src/components/ViewEditor/ViewEditor.test.tsx +6 -6
  151. package/src/components/ViewEditor/ViewEditor.tsx +296 -206
  152. package/src/components/index.ts +3 -1
  153. package/src/components/testing.tsx +24 -11
  154. package/src/hooks/index.ts +2 -3
  155. package/src/hooks/useFormHandler.ts +380 -0
  156. package/src/hooks/useKeyHandler.tsx +37 -0
  157. package/src/translations.ts +70 -65
  158. package/src/util/field-type.ts +57 -0
  159. package/src/util/field.ts +25 -25
  160. package/src/util/formatting.ts +22 -22
  161. package/src/util/index.ts +2 -1
  162. package/src/util/properties.ts +18 -0
  163. package/src/util/refs.ts +16 -18
  164. package/dist/types/src/components/ControlSection/ControlSection.d.ts +0 -23
  165. package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +0 -1
  166. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +0 -84
  167. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +0 -1
  168. package/dist/types/src/components/ControlSection/index.d.ts +0 -2
  169. package/dist/types/src/components/ControlSection/index.d.ts.map +0 -1
  170. package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
  171. package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
  172. package/dist/types/src/components/Form/Defaults.d.ts +0 -14
  173. package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
  174. package/dist/types/src/components/Form/FormActions.d.ts +0 -9
  175. package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
  176. package/dist/types/src/components/Form/FormContent.d.ts +0 -63
  177. package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
  178. package/dist/types/src/components/Form/FormContext.d.ts +0 -23
  179. package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
  180. package/dist/types/src/components/Form/Input.d.ts +0 -29
  181. package/dist/types/src/components/Form/Input.d.ts.map +0 -1
  182. package/dist/types/src/components/Form/RefField.d.ts +0 -22
  183. package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
  184. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
  185. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
  186. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
  187. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
  188. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -79
  189. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
  190. package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
  191. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
  192. package/dist/types/src/components/Form/custom/index.d.ts +0 -4
  193. package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
  194. package/dist/types/src/components/Form/factory.d.ts +0 -8
  195. package/dist/types/src/components/Form/factory.d.ts.map +0 -1
  196. package/dist/types/src/hooks/useForm.d.ts +0 -67
  197. package/dist/types/src/hooks/useForm.d.ts.map +0 -1
  198. package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -29
  199. package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
  200. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
  201. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
  202. package/dist/types/src/util/schema.d.ts +0 -3
  203. package/dist/types/src/util/schema.d.ts.map +0 -1
  204. package/src/components/ControlSection/ControlSection.stories.tsx +0 -38
  205. package/src/components/ControlSection/ControlSection.tsx +0 -153
  206. package/src/components/Form/ArrayField.tsx +0 -125
  207. package/src/components/Form/Defaults.tsx +0 -205
  208. package/src/components/Form/FormActions.tsx +0 -54
  209. package/src/components/Form/FormContent.tsx +0 -322
  210. package/src/components/Form/FormContext.tsx +0 -110
  211. package/src/components/Form/Input.tsx +0 -54
  212. package/src/components/Form/RefField.tsx +0 -283
  213. package/src/components/Form/custom/GeoPointInput.tsx +0 -80
  214. package/src/components/Form/custom/index.ts +0 -7
  215. package/src/components/Form/factory.tsx +0 -31
  216. package/src/hooks/useForm.ts +0 -296
  217. package/src/hooks/useQueryRefOptions.ts +0 -62
  218. package/src/hooks/useRefQueryLookupHandler.ts +0 -39
  219. package/src/util/schema.ts +0 -12
@@ -0,0 +1,361 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
+ import * as Schema from 'effect/Schema';
7
+ import React, { useEffect } from 'react';
8
+ import { expect, userEvent, within } from 'storybook/test';
9
+
10
+ import { Annotation, Filter, Obj, Ref, Tag, Type } from '@dxos/echo';
11
+ import { FormInputAnnotation, LabelAnnotation } from '@dxos/echo/internal';
12
+ import { useQuery } from '@dxos/react-client/echo';
13
+ import { useClientStory, withClientProvider } from '@dxos/react-client/testing';
14
+ import { Panel } from '@dxos/react-ui';
15
+ import { Loading, withLayout, withTheme } from '@dxos/react-ui/testing';
16
+ import { FactoryAnnotation, type FactoryFn } from '@dxos/schema';
17
+ import { Pipeline } from '@dxos/types';
18
+
19
+ import { translations } from '../../translations';
20
+ import { OBJECT_PROPERTIES_DEBUG_SYMBOL } from '../testing';
21
+ import { ObjectProperties } from './ObjectProperties';
22
+
23
+ //
24
+ // Test-only schemas exercising a non-Tag ref-array. `Article.authors` is the
25
+ // case the generic create flow currently fails on: the picker creates an
26
+ // Author in the DB but never wires the new Ref into the array slot.
27
+ //
28
+
29
+ const Author = Schema.Struct({
30
+ name: Schema.String,
31
+ }).pipe(
32
+ Type.object({
33
+ typename: 'org.dxos.test.author',
34
+ version: '0.1.0',
35
+ }),
36
+ LabelAnnotation.set(['name']),
37
+ Annotation.IconAnnotation.set({ icon: 'ph--user--regular', hue: 'blue' }),
38
+ );
39
+ type Author = Schema.Schema.Type<typeof Author>;
40
+
41
+ const Article = Schema.Struct({
42
+ title: Schema.String.pipe(Schema.optional),
43
+ authors: Schema.Array(Ref.Ref(Author)),
44
+ }).pipe(
45
+ Type.object({
46
+ typename: 'org.dxos.test.article',
47
+ version: '0.1.0',
48
+ }),
49
+ LabelAnnotation.set(['title']),
50
+ Annotation.IconAnnotation.set({ icon: 'ph--article--regular', hue: 'green' }),
51
+ );
52
+ type Article = Schema.Schema.Type<typeof Article>;
53
+
54
+ //
55
+ // `Note` mirrors the `Subscription.Feed` shape: a required `Ref` field
56
+ // (`backing`) is hidden from the form via `FormInputAnnotation.set(false)`,
57
+ // so `Obj.make(Note, values)` from the picker would reject because the form
58
+ // values can't satisfy the schema. A `FactoryAnnotation` constructs the
59
+ // backing object and links it at create time. `cursor` is an optional hidden
60
+ // field included to exercise the same path for non-required hidden fields.
61
+ //
62
+ const NoteBacking = Schema.Struct({}).pipe(Type.object({ typename: 'org.dxos.test.note-backing', version: '0.1.0' }));
63
+ type NoteBacking = Schema.Schema.Type<typeof NoteBacking>;
64
+
65
+ const Note = Schema.Struct({
66
+ title: Schema.String,
67
+ cursor: Schema.String.pipe(FormInputAnnotation.set(false), Schema.optional),
68
+ backing: Ref.Ref(NoteBacking).pipe(FormInputAnnotation.set(false)),
69
+ }).pipe(
70
+ Type.object({
71
+ typename: 'org.dxos.test.note',
72
+ version: '0.1.0',
73
+ }),
74
+ LabelAnnotation.set(['title']),
75
+ Annotation.IconAnnotation.set({ icon: 'ph--note--regular', hue: 'amber' }),
76
+ FactoryAnnotation.set(((values: any) =>
77
+ Obj.make(Note, { ...values, backing: Ref.make(Obj.make(NoteBacking, {})) })) as FactoryFn),
78
+ );
79
+ type Note = Schema.Schema.Type<typeof Note>;
80
+
81
+ const Notebook = Schema.Struct({
82
+ name: Schema.String.pipe(Schema.optional),
83
+ notes: Schema.Array(Ref.Ref(Note)),
84
+ }).pipe(
85
+ Type.object({
86
+ typename: 'org.dxos.test.notebook',
87
+ version: '0.1.0',
88
+ }),
89
+ LabelAnnotation.set(['name']),
90
+ Annotation.IconAnnotation.set({ icon: 'ph--notebook--regular', hue: 'amber' }),
91
+ );
92
+ type Notebook = Schema.Schema.Type<typeof Notebook>;
93
+
94
+ //
95
+ // Stories.
96
+ //
97
+
98
+ export type ObjectPropertiesDebug = {
99
+ /** Live database the story is rendering against. */
100
+ db: ReturnType<typeof useClientStory>['space'] extends infer S ? (S extends { db: infer D } ? D : never) : never;
101
+ /** Object whose properties are being edited. */
102
+ object: Obj.Unknown;
103
+ };
104
+
105
+ const PipelineStory = () => {
106
+ const { space } = useClientStory();
107
+ const [object] = useQuery(space?.db, Filter.type(Pipeline.Pipeline));
108
+
109
+ useEffect(() => {
110
+ if (typeof window !== 'undefined' && space?.db && object) {
111
+ (window as any)[OBJECT_PROPERTIES_DEBUG_SYMBOL] = { db: space.db, object } satisfies ObjectPropertiesDebug;
112
+ }
113
+ }, [space, object]);
114
+
115
+ if (!object) {
116
+ return <Loading />;
117
+ }
118
+ return (
119
+ <Panel.Root>
120
+ <Panel.Content asChild>
121
+ <ObjectProperties object={object} />
122
+ </Panel.Content>
123
+ </Panel.Root>
124
+ );
125
+ };
126
+
127
+ const ArticleStory = () => {
128
+ const { space } = useClientStory();
129
+ const [object] = useQuery(space?.db, Filter.type(Article));
130
+
131
+ useEffect(() => {
132
+ if (typeof window !== 'undefined' && space?.db && object) {
133
+ (window as any)[OBJECT_PROPERTIES_DEBUG_SYMBOL] = { db: space.db, object } satisfies ObjectPropertiesDebug;
134
+ }
135
+ }, [space, object]);
136
+
137
+ if (!object) {
138
+ return <Loading />;
139
+ }
140
+ return (
141
+ <Panel.Root>
142
+ <Panel.Content asChild>
143
+ <ObjectProperties object={object} />
144
+ </Panel.Content>
145
+ </Panel.Root>
146
+ );
147
+ };
148
+
149
+ const meta = {
150
+ title: 'ui/react-ui-form/ObjectProperties',
151
+ component: ObjectProperties as any,
152
+ decorators: [withTheme(), withLayout({ layout: 'column' })],
153
+ parameters: {
154
+ layout: 'fullscreen',
155
+ translations,
156
+ },
157
+ } satisfies Meta<typeof PipelineStory>;
158
+
159
+ export default meta;
160
+
161
+ type Story = StoryObj<typeof meta>;
162
+
163
+ const pipelineDecorators = [
164
+ withClientProvider({
165
+ createIdentity: true,
166
+ createSpace: true,
167
+ types: [Pipeline.Pipeline, Tag.Tag],
168
+ onCreateSpace: async ({ space }) => {
169
+ space.db.add(Pipeline.make());
170
+ space.db.add(Tag.make({ label: 'Tag 1' }));
171
+ space.db.add(Tag.make({ label: 'Tag 2' }));
172
+ space.db.add(Tag.make({ label: 'Tag 3' }));
173
+ },
174
+ }),
175
+ ];
176
+
177
+ const articleDecorators = [
178
+ withClientProvider({
179
+ createIdentity: true,
180
+ createSpace: true,
181
+ types: [Article, Author, Tag.Tag],
182
+ onCreateSpace: async ({ space }) => {
183
+ space.db.add(Obj.make(Article, { title: 'Untitled article', authors: [] }));
184
+ },
185
+ }),
186
+ ];
187
+
188
+ const NotebookStory = () => {
189
+ const { space } = useClientStory();
190
+ const [object] = useQuery(space?.db, Filter.type(Notebook));
191
+
192
+ useEffect(() => {
193
+ if (typeof window !== 'undefined' && space?.db && object) {
194
+ (window as any)[OBJECT_PROPERTIES_DEBUG_SYMBOL] = { db: space.db, object } satisfies ObjectPropertiesDebug;
195
+ }
196
+ }, [space, object]);
197
+
198
+ if (!object) {
199
+ return <Loading />;
200
+ }
201
+ return (
202
+ <Panel.Root>
203
+ <Panel.Content asChild>
204
+ <ObjectProperties object={object} />
205
+ </Panel.Content>
206
+ </Panel.Root>
207
+ );
208
+ };
209
+
210
+ const notebookDecorators = [
211
+ withClientProvider({
212
+ createIdentity: true,
213
+ createSpace: true,
214
+ types: [Notebook, Note, NoteBacking, Tag.Tag],
215
+ onCreateSpace: async ({ space }) => {
216
+ space.db.add(Obj.make(Notebook, { name: 'Untitled notebook', notes: [] }));
217
+ },
218
+ }),
219
+ ];
220
+
221
+ /**
222
+ * Default Pipeline form — manual exploration. Includes the Tags picker (which
223
+ * is the only ref-array path that currently works end-to-end, by virtue of
224
+ * `Tag.Tag` being special-cased in `ObjectProperties.handleCreate`).
225
+ */
226
+ export const Default: Story = {
227
+ render: PipelineStory,
228
+ decorators: pipelineDecorators,
229
+ };
230
+
231
+ /**
232
+ * Tag-creation play. Walks the inline create flow against the Tags picker and
233
+ * leaves the magazine in the post-Save state for assertions to inspect via
234
+ * `OBJECT_PROPERTIES_DEBUG_SYMBOL`.
235
+ *
236
+ * Expected end-state (currently passes):
237
+ * - new Tag.Tag exists in the DB with `label === "PinnedTag"`
238
+ * - `Obj.getMeta(object).tags` includes its DXN.
239
+ */
240
+ export const CreateTagPlay: Story = {
241
+ render: PipelineStory,
242
+ decorators: pipelineDecorators,
243
+ play: async ({ canvasElement }) => {
244
+ const canvas = within(canvasElement);
245
+ const body = within(document.body);
246
+
247
+ // Tags is `Schema.Array(Ref.Ref(Tag.Tag))` and starts empty, so it renders
248
+ // as an "Add item" button — same shape as any other ref-array. Click it
249
+ // to materialise an empty slot, then open the slot's picker.
250
+ const addButton = await canvas.findByRole('button', { name: /add/i }, { timeout: 10_000 });
251
+ await userEvent.click(addButton);
252
+ await new Promise((resolve) => setTimeout(resolve, 250));
253
+
254
+ const comboboxes = await canvas.findAllByRole('combobox', undefined, { timeout: 10_000 });
255
+ await expect(comboboxes.length).toBeGreaterThan(0);
256
+ await userEvent.click(comboboxes[0]);
257
+
258
+ const search = await body.findByPlaceholderText(/search/i, undefined, { timeout: 5000 });
259
+ await userEvent.type(search, 'PinnedTag');
260
+
261
+ // The Tag picker localises the create option as "Add tag" (no text suffix
262
+ // — the translation has no `{{text}}` placeholder), so match on /add/.
263
+ const createOption = await body.findByRole('option', { name: /add/i }, { timeout: 3000 });
264
+ await userEvent.click(createOption);
265
+
266
+ const form = await body.findByTestId('create-referenced-object-form', undefined, { timeout: 5000 });
267
+ const labelInput = await within(form).findByLabelText(/^label$/i);
268
+ await userEvent.clear(labelInput);
269
+ await userEvent.type(labelInput, 'PinnedTag');
270
+ await userEvent.click(await within(form).findByTestId('save-button'));
271
+
272
+ // Allow async state updates to flush before the test asserts.
273
+ await new Promise((resolve) => setTimeout(resolve, 250));
274
+ },
275
+ };
276
+
277
+ /**
278
+ * Ref-array creation play. Walks Add → Open → Create → Save against
279
+ * `Article.authors`. After the fix, the new Author should be persisted AND
280
+ * wired into `article.authors[0]`. Today it persists to the DB but the array
281
+ * slot stays empty.
282
+ */
283
+ export const CreateRefArrayPlay: Story = {
284
+ render: ArticleStory,
285
+ decorators: articleDecorators,
286
+ play: async ({ canvasElement }) => {
287
+ const canvas = within(canvasElement);
288
+ const body = within(document.body);
289
+
290
+ // ObjectProperties extends Article with the BaseSchema's `tags` array, so
291
+ // the form has two "Add item" buttons (Tags + Authors). Pick the last one
292
+ // — Authors comes after Tags in the rendered order.
293
+ const addButtons = await canvas.findAllByRole('button', { name: /add/i }, { timeout: 10_000 });
294
+ await userEvent.click(addButtons[addButtons.length - 1]);
295
+ await new Promise((resolve) => setTimeout(resolve, 250));
296
+
297
+ // Open the empty slot's combobox.
298
+ const comboboxes = await canvas.findAllByRole('combobox', undefined, { timeout: 10_000 });
299
+ await expect(comboboxes.length).toBeGreaterThan(0);
300
+ await userEvent.click(comboboxes[0]);
301
+
302
+ const search = await body.findByPlaceholderText(/search/i, undefined, { timeout: 5000 });
303
+ await userEvent.type(search, 'Ada Lovelace');
304
+
305
+ // Author has no `createOptionLabel` → option falls back to "Create".
306
+ const createOption = await body.findByRole('option', { name: /create/i }, { timeout: 3000 });
307
+ await userEvent.click(createOption);
308
+
309
+ const form = await body.findByTestId('create-referenced-object-form', undefined, { timeout: 5000 });
310
+ const nameInput = await within(form).findByLabelText(/^name$/i);
311
+ await userEvent.clear(nameInput);
312
+ await userEvent.type(nameInput, 'Ada Lovelace');
313
+ await userEvent.click(await within(form).findByTestId('save-button'));
314
+
315
+ await new Promise((resolve) => setTimeout(resolve, 250));
316
+ },
317
+ };
318
+
319
+ /**
320
+ * Ref-array creation against a schema with a hidden required field. `Note`
321
+ * mirrors the shape of `Subscription.Feed`: the form omits `backing` (a
322
+ * required `Ref` annotated `FormInputAnnotation.set(false)`) but the schema
323
+ * requires it. Without `omitHiddenFormFields` + `FactoryAnnotation`, the
324
+ * form's validator would reject Save and the popover would never close.
325
+ *
326
+ * Expected end-state after the fix:
327
+ * - new Note exists in DB with a synthesised `backing` ref
328
+ * - `notebook.notes[0]` references the new Note
329
+ * - the create form is gone
330
+ */
331
+ export const CreateHiddenFieldPlay: Story = {
332
+ render: NotebookStory,
333
+ decorators: notebookDecorators,
334
+ play: async ({ canvasElement }) => {
335
+ const canvas = within(canvasElement);
336
+ const body = within(document.body);
337
+
338
+ // Two "Add item" buttons (Tags + Notes) — Notes comes second.
339
+ const addButtons = await canvas.findAllByRole('button', { name: /add/i }, { timeout: 10_000 });
340
+ await userEvent.click(addButtons[addButtons.length - 1]);
341
+ await new Promise((resolve) => setTimeout(resolve, 250));
342
+
343
+ const comboboxes = await canvas.findAllByRole('combobox', undefined, { timeout: 10_000 });
344
+ await expect(comboboxes.length).toBeGreaterThan(0);
345
+ await userEvent.click(comboboxes[0]);
346
+
347
+ const search = await body.findByPlaceholderText(/search/i, undefined, { timeout: 5000 });
348
+ await userEvent.type(search, 'Ideas');
349
+
350
+ const createOption = await body.findByRole('option', { name: /create/i }, { timeout: 3000 });
351
+ await userEvent.click(createOption);
352
+
353
+ const form = await body.findByTestId('create-referenced-object-form', undefined, { timeout: 5000 });
354
+ const titleInput = await within(form).findByLabelText(/^title$/i);
355
+ await userEvent.clear(titleInput);
356
+ await userEvent.type(titleInput, 'Ideas');
357
+ await userEvent.click(await within(form).findByTestId('save-button'));
358
+
359
+ await new Promise((resolve) => setTimeout(resolve, 250));
360
+ },
361
+ };
@@ -0,0 +1,101 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { composeStories } from '@storybook/react';
6
+ import { cleanup } from '@testing-library/react';
7
+ import { afterEach, describe, expect, test } from 'vitest';
8
+
9
+ import { Filter, Obj, Tag } from '@dxos/echo';
10
+
11
+ import { OBJECT_PROPERTIES_DEBUG_SYMBOL } from '../testing';
12
+ import * as stories from './ObjectProperties.stories';
13
+ import { type ObjectPropertiesDebug } from './ObjectProperties.stories';
14
+
15
+ const { CreateTagPlay, CreateRefArrayPlay, CreateHiddenFieldPlay } = composeStories(stories);
16
+
17
+ const getDebug = (): ObjectPropertiesDebug => {
18
+ const debug = (window as any)[OBJECT_PROPERTIES_DEBUG_SYMBOL] as ObjectPropertiesDebug | undefined;
19
+ expect(debug).toBeDefined();
20
+ return debug!;
21
+ };
22
+
23
+ describe('ObjectProperties — inline create flow', () => {
24
+ afterEach(() => {
25
+ cleanup();
26
+ delete (window as any)[OBJECT_PROPERTIES_DEBUG_SYMBOL];
27
+ });
28
+
29
+ test('Tag.Tag: Create + Save persists tag and assigns it to the object', { timeout: 30_000 }, async () => {
30
+ await CreateTagPlay.run();
31
+ const { db, object } = getDebug();
32
+
33
+ // The new Tag should exist in the DB.
34
+ const tags = (await db.query(Filter.type(Tag.Tag)).run()) as Tag.Tag[];
35
+ const created = tags.find((tag) => tag.label === 'PinnedTag');
36
+ expect(created, 'new Tag.Tag with label "PinnedTag" should be in the database').toBeDefined();
37
+
38
+ // And `object.meta.tags` should contain a DXN that resolves to that tag.
39
+ // Compare by the DXN's object-id tail since refs may be stored as relative
40
+ // (`dxn:echo:@:<id>`) or absolute (`dxn:echo:<spaceId>:<id>`) forms.
41
+ const meta = Obj.getMeta(object);
42
+ const createdId = (created as any).id as string;
43
+ const matched = (meta.tags ?? []).some((dxn) => dxn.endsWith(`:${createdId}`));
44
+ expect(matched, "meta.tags should contain a DXN ending with the new tag's id").toBe(true);
45
+ });
46
+
47
+ test('non-Tag ref-array: Create + Save assigns the new ref to the array slot', { timeout: 30_000 }, async () => {
48
+ await CreateRefArrayPlay.run();
49
+ const { db, object } = getDebug();
50
+
51
+ // The new Author should exist in the DB.
52
+ const authors = (await db.query(Filter.typename('org.dxos.test.author' as any)).run()) as any[];
53
+ const created = authors.find((author: any) => author.name === 'Ada Lovelace');
54
+ expect(created, 'new Author with name "Ada Lovelace" should be in the database').toBeDefined();
55
+
56
+ // And the article's authors array should contain a Ref to it. This is the
57
+ // assertion that fails before the fix — handleCreate persists the object
58
+ // but never wires the new ref into the array slot.
59
+ const article = object as any;
60
+ expect(article.authors ?? [], 'article.authors should contain a single Ref').toHaveLength(1);
61
+ // Compare by DXN tail to be agnostic to relative vs absolute Ref form.
62
+ const createdId = (created as any).id as string;
63
+ expect(
64
+ article.authors[0]?.dxn?.toString().endsWith(`:${createdId}`),
65
+ 'article.authors[0] should reference the newly-created Author',
66
+ ).toBe(true);
67
+ });
68
+
69
+ test(
70
+ 'hidden required field via FactoryAnnotation: Create + Save closes form and assigns ref',
71
+ { timeout: 30_000 },
72
+ async () => {
73
+ await CreateHiddenFieldPlay.run();
74
+ const { db, object } = getDebug();
75
+
76
+ // The new Note should exist in the DB with the synthesised backing ref.
77
+ const notes = (await db.query(Filter.typename('org.dxos.test.note' as any)).run()) as any[];
78
+ const created = notes.find((note: any) => note.title === 'Ideas');
79
+ expect(created, 'new Note with title "Ideas" should be in the database').toBeDefined();
80
+ expect(
81
+ (created as any).backing,
82
+ 'FactoryAnnotation should have populated the hidden `backing` ref',
83
+ ).toBeDefined();
84
+
85
+ // The notebook's notes array should reference the new Note.
86
+ const notebook = object as any;
87
+ expect(notebook.notes ?? [], 'notebook.notes should contain a single Ref').toHaveLength(1);
88
+ const createdId = (created as any).id as string;
89
+ expect(
90
+ notebook.notes[0]?.dxn?.toString().endsWith(`:${createdId}`),
91
+ 'notebook.notes[0] should reference the newly-created Note',
92
+ ).toBe(true);
93
+
94
+ // The picker's create form should be gone.
95
+ expect(
96
+ document.querySelector('[data-testid="create-referenced-object-form"]'),
97
+ 'create form should be dismissed after a successful save',
98
+ ).toBeNull();
99
+ },
100
+ );
101
+ });
@@ -0,0 +1,140 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import * as Function from 'effect/Function';
6
+ import * as Option from 'effect/Option';
7
+ import * as Schema from 'effect/Schema';
8
+ import React, { type PropsWithChildren, useCallback, useMemo } from 'react';
9
+
10
+ import { DXN, Obj, Ref, Tag, Type } from '@dxos/echo';
11
+ import { type JsonPath, splitJsonPath } from '@dxos/echo/internal';
12
+ import { invariant } from '@dxos/invariant';
13
+ import { HuePicker } from '@dxos/react-ui-pickers';
14
+ import { FactoryAnnotation } from '@dxos/schema';
15
+ import { composable, composableProps } from '@dxos/ui-theme';
16
+ import { isNonNullable } from '@dxos/util';
17
+
18
+ import { translationKey } from '../../translations';
19
+ import { Form, type FormFieldMap, omitId } from '../Form';
20
+
21
+ export type ObjectPropertiesProps = PropsWithChildren<{ object: Obj.Unknown }>;
22
+
23
+ // TODO(wittjosiah): Reconcile w/ ObjectForm.
24
+ export const ObjectProperties = composable<HTMLDivElement, ObjectPropertiesProps>(
25
+ ({ children, object, ...props }, forwardedRef) => {
26
+ const db = Obj.getDatabase(object);
27
+ const meta = Obj.getMeta(object);
28
+ const tags = (meta.tags ?? []).map((tag) => db?.makeRef(DXN.parse(tag))).filter(isNonNullable);
29
+ const values = useMemo(() => ({ tags, ...object }), [object, tags]);
30
+
31
+ const formSchema = useMemo(() => {
32
+ return Function.pipe(
33
+ Obj.getSchema(object),
34
+ Option.fromNullable,
35
+ Option.map((schema) => omitId(BaseSchema.pipe(Schema.extend(schema)))),
36
+ Option.getOrUndefined,
37
+ );
38
+ }, [object]);
39
+
40
+ // Persist a newly-created object referenced by one of the form's ref
41
+ // fields and return it. The calling RefField wires the new Ref into its
42
+ // own slot's form value; for the BaseSchema `tags` array, the resulting
43
+ // form change is then synced back to `Obj.getMeta(object).tags` by
44
+ // `handleChange` below — so Tag.Tag follows the same generic path as any
45
+ // other ref-array field, no type-specific branching required here.
46
+ //
47
+ // Schemas whose required structure can't be produced by `Obj.make(schema,
48
+ // values)` alone (e.g. types with a required ref to a backing object) can
49
+ // declare a `FactoryAnnotation` to take over construction.
50
+ const handleCreate = useCallback(
51
+ (schema: Type.AnyEntity, values: any): Obj.Unknown => {
52
+ invariant(db);
53
+ invariant(Type.isObjectSchema(schema));
54
+ const factory = Option.getOrUndefined(FactoryAnnotation.get(schema));
55
+ const newObject = factory ? (factory(values) as Obj.Unknown) : Obj.make(schema, values);
56
+ return db.add(newObject) as Obj.Unknown;
57
+ },
58
+ [db],
59
+ );
60
+
61
+ // TODO(wittjosiah): Use FormRootProps type.
62
+ const handleChange = useCallback(
63
+ (
64
+ { tags, ...values }: Schema.Schema.Type<typeof formSchema>,
65
+ { isValid, changed }: { isValid: boolean; changed: Record<JsonPath, boolean> },
66
+ ) => {
67
+ if (!isValid) {
68
+ return;
69
+ }
70
+
71
+ const changedPaths = Object.keys(changed).filter((path) => changed[path as JsonPath]) as JsonPath[];
72
+
73
+ // Handle tags separately using Obj.change.
74
+ const hasTagsChange = changedPaths.some((path) => splitJsonPath(path)[0] === 'tags');
75
+ if (hasTagsChange) {
76
+ Obj.change(object, (object) => {
77
+ Obj.getMeta(object).tags = tags?.map((tag: Ref.Ref<Tag.Tag>) => tag.dxn.toString()) ?? [];
78
+ });
79
+ }
80
+
81
+ // Handle other property changes.
82
+ const nonTagPaths = changedPaths.filter((path) => splitJsonPath(path)[0] !== 'tags');
83
+ if (nonTagPaths.length > 0) {
84
+ Obj.change(object, () => {
85
+ for (const path of nonTagPaths) {
86
+ const parts = splitJsonPath(path);
87
+ const value = Obj.getValue(values as any, parts);
88
+ Obj.setValue(object, parts, value);
89
+ }
90
+ });
91
+ }
92
+ },
93
+ [object],
94
+ );
95
+
96
+ if (!formSchema) {
97
+ return null;
98
+ }
99
+
100
+ return (
101
+ <Form.Root
102
+ schema={formSchema}
103
+ defaultValues={values as any}
104
+ createTypename={Type.getTypename(Tag.Tag)}
105
+ createOptionIcon='ph--plus--regular'
106
+ createOptionLabel={['add-tag.label', { ns: translationKey }]}
107
+ createInitialValuePath='label'
108
+ createFieldMap={createFieldMap}
109
+ db={db}
110
+ onValuesChanged={handleChange}
111
+ onCreate={handleCreate}
112
+ >
113
+ <Form.Viewport {...composableProps(props)} ref={forwardedRef}>
114
+ <Form.Content>
115
+ <Form.FieldSet />
116
+ {children}
117
+ </Form.Content>
118
+ </Form.Viewport>
119
+ </Form.Root>
120
+ );
121
+ },
122
+ );
123
+
124
+ const createFieldMap: FormFieldMap = {
125
+ hue: ({ type, label, layout, getValue, onValueChange }) => {
126
+ const handleChange = useCallback((nextHue: string) => onValueChange(type, nextHue), [onValueChange, type]);
127
+ const handleReset = useCallback(() => onValueChange(type, undefined), [onValueChange, type]);
128
+ return (
129
+ <>
130
+ {layout !== 'inline' && <Form.Label label={label} />}
131
+ <HuePicker value={getValue()} onChange={handleChange} onReset={handleReset} />
132
+ </>
133
+ );
134
+ },
135
+ };
136
+
137
+ // TODO(wittjosiah): Would be nice to control order when extending so this isn't always first/last.
138
+ const BaseSchema = Schema.Struct({
139
+ tags: Schema.Array(Ref.Ref(Tag.Tag)).pipe(Schema.optional),
140
+ });
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export * from './ObjectProperties';