@dxos/react-ui-form 0.8.4-main.8360d9e660 → 0.8.4-main.8baae0fced

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 (147) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +1009 -612
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/translations.mjs +82 -0
  6. package/dist/lib/browser/translations.mjs.map +7 -0
  7. package/dist/lib/node-esm/index.mjs +1009 -612
  8. package/dist/lib/node-esm/index.mjs.map +4 -4
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/lib/node-esm/translations.mjs +84 -0
  11. package/dist/lib/node-esm/translations.mjs.map +7 -0
  12. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  13. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +63 -61
  14. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
  15. package/dist/types/src/components/Form/Form.d.ts +40 -12
  16. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  17. package/dist/types/src/components/Form/Form.stories.d.ts +76 -74
  18. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  19. package/dist/types/src/components/Form/FormField.d.ts +11 -5
  20. package/dist/types/src/components/Form/FormField.d.ts.map +1 -1
  21. package/dist/types/src/components/Form/FormFieldComponent.d.ts +8 -6
  22. package/dist/types/src/components/Form/FormFieldComponent.d.ts.map +1 -1
  23. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts.map +1 -1
  24. package/dist/types/src/components/Form/FormFieldSet.d.ts +5 -8
  25. package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -1
  26. package/dist/types/src/components/Form/fields/ArrayField.d.ts +4 -4
  27. package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -1
  28. package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -1
  29. package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -1
  30. package/dist/types/src/components/Form/fields/MarkdownField.d.ts +8 -1
  31. package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -1
  32. package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts +84 -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.map +1 -1
  35. package/dist/types/src/components/Form/fields/RefField.d.ts +12 -6
  36. package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -1
  37. package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -1
  38. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -1
  39. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +63 -61
  40. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -1
  41. package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -1
  42. package/dist/types/src/components/Form/fields/TextField.d.ts +1 -1
  43. package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -1
  44. package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -1
  45. package/dist/types/src/components/ObjectForm/ObjectForm.d.ts +9 -0
  46. package/dist/types/src/components/ObjectForm/ObjectForm.d.ts.map +1 -0
  47. package/dist/types/src/components/ObjectForm/index.d.ts +2 -0
  48. package/dist/types/src/components/ObjectForm/index.d.ts.map +1 -0
  49. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +26 -4
  50. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -1
  51. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +63 -61
  52. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -1
  53. package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts +9 -0
  54. package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts.map +1 -0
  55. package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts +127 -0
  56. package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts.map +1 -0
  57. package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts +2 -0
  58. package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts.map +1 -0
  59. package/dist/types/src/components/ObjectProperties/index.d.ts +2 -0
  60. package/dist/types/src/components/ObjectProperties/index.d.ts.map +1 -0
  61. package/dist/types/src/components/Settings/Settings.d.ts +28 -33
  62. package/dist/types/src/components/Settings/Settings.d.ts.map +1 -1
  63. package/dist/types/src/components/Settings/Settings.stories.d.ts +64 -61
  64. package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -1
  65. package/dist/types/src/components/Settings/SettingsFieldSet.d.ts +43 -0
  66. package/dist/types/src/components/Settings/SettingsFieldSet.d.ts.map +1 -0
  67. package/dist/types/src/components/Settings/SettingsItem.d.ts +16 -0
  68. package/dist/types/src/components/Settings/SettingsItem.d.ts.map +1 -0
  69. package/dist/types/src/components/Settings/SettingsSelectField.d.ts +18 -0
  70. package/dist/types/src/components/Settings/SettingsSelectField.d.ts.map +1 -0
  71. package/dist/types/src/components/Settings/index.d.ts +2 -0
  72. package/dist/types/src/components/Settings/index.d.ts.map +1 -1
  73. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +3 -4
  74. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  75. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +65 -63
  76. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  77. package/dist/types/src/components/index.d.ts +2 -0
  78. package/dist/types/src/components/index.d.ts.map +1 -1
  79. package/dist/types/src/components/testing.d.ts +4 -4
  80. package/dist/types/src/components/testing.d.ts.map +1 -1
  81. package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -1
  82. package/dist/types/src/hooks/useKeyHandler.d.ts +2 -1
  83. package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -1
  84. package/dist/types/src/index.d.ts +0 -2
  85. package/dist/types/src/index.d.ts.map +1 -1
  86. package/dist/types/src/translations.d.ts +63 -61
  87. package/dist/types/src/translations.d.ts.map +1 -1
  88. package/dist/types/src/util/field-type.d.ts +19 -0
  89. package/dist/types/src/util/field-type.d.ts.map +1 -0
  90. package/dist/types/src/util/field.d.ts.map +1 -1
  91. package/dist/types/src/util/formatting.d.ts.map +1 -1
  92. package/dist/types/src/util/index.d.ts +1 -0
  93. package/dist/types/src/util/index.d.ts.map +1 -1
  94. package/dist/types/src/util/properties.d.ts +7 -1
  95. package/dist/types/src/util/properties.d.ts.map +1 -1
  96. package/dist/types/src/util/properties.test.d.ts +2 -0
  97. package/dist/types/src/util/properties.test.d.ts.map +1 -0
  98. package/dist/types/src/util/refs.d.ts +1 -0
  99. package/dist/types/src/util/refs.d.ts.map +1 -1
  100. package/dist/types/tsconfig.tsbuildinfo +1 -1
  101. package/package.json +43 -41
  102. package/src/components/FieldEditor/FieldEditor.stories.tsx +2 -2
  103. package/src/components/FieldEditor/FieldEditor.test.tsx +0 -1
  104. package/src/components/FieldEditor/FieldEditor.tsx +4 -3
  105. package/src/components/Form/AUDIT.md +17 -0
  106. package/src/components/Form/Form.stories.tsx +23 -36
  107. package/src/components/Form/Form.tsx +77 -48
  108. package/src/components/Form/FormField.tsx +27 -15
  109. package/src/components/Form/FormFieldComponent.tsx +6 -3
  110. package/src/components/Form/FormFieldSet.tsx +49 -32
  111. package/src/components/Form/fields/ArrayField.tsx +14 -8
  112. package/src/components/Form/fields/GeoPointField.tsx +8 -7
  113. package/src/components/Form/fields/MarkdownField.stories.tsx +206 -0
  114. package/src/components/Form/fields/MarkdownField.tsx +135 -45
  115. package/src/components/Form/fields/RefField.tsx +70 -35
  116. package/src/components/Form/fields/SelectOptionField.stories.tsx +8 -6
  117. package/src/components/Form/fields/SelectOptionField.tsx +12 -12
  118. package/src/components/Form/fields/TextField.tsx +2 -2
  119. package/src/components/Form/fields/TupleField.tsx +1 -1
  120. package/src/components/ObjectForm/ObjectForm.tsx +114 -0
  121. package/src/components/ObjectForm/index.ts +5 -0
  122. package/src/components/ObjectPicker/ObjectPicker.stories.tsx +12 -10
  123. package/src/components/ObjectPicker/ObjectPicker.tsx +92 -65
  124. package/src/components/ObjectProperties/ObjectProperties.stories.tsx +362 -0
  125. package/src/components/ObjectProperties/ObjectProperties.test.tsx +101 -0
  126. package/src/components/ObjectProperties/ObjectProperties.tsx +148 -0
  127. package/src/components/ObjectProperties/index.ts +5 -0
  128. package/src/components/Settings/AUDIT.md +51 -0
  129. package/src/components/Settings/Settings.stories.tsx +69 -21
  130. package/src/components/Settings/Settings.tsx +33 -183
  131. package/src/components/Settings/SettingsFieldSet.tsx +257 -0
  132. package/src/components/Settings/SettingsItem.tsx +58 -0
  133. package/src/components/Settings/SettingsSelectField.tsx +59 -0
  134. package/src/components/Settings/index.ts +2 -0
  135. package/src/components/ViewEditor/ViewEditor.stories.tsx +12 -13
  136. package/src/components/ViewEditor/ViewEditor.test.tsx +0 -1
  137. package/src/components/ViewEditor/ViewEditor.tsx +25 -32
  138. package/src/components/index.ts +2 -0
  139. package/src/components/testing.tsx +19 -11
  140. package/src/hooks/useKeyHandler.tsx +7 -7
  141. package/src/index.ts +0 -4
  142. package/src/translations.ts +66 -63
  143. package/src/util/field-type.ts +60 -0
  144. package/src/util/index.ts +1 -0
  145. package/src/util/properties.test.ts +63 -0
  146. package/src/util/properties.ts +14 -3
  147. package/src/util/refs.ts +7 -3
@@ -7,24 +7,24 @@ import * as Schema from 'effect/Schema';
7
7
  import React, { useCallback, useEffect, useMemo, useState } from 'react';
8
8
  import { expect, fn, userEvent, within } from 'storybook/test';
9
9
 
10
- import { Obj } from '@dxos/echo';
11
- import { faker } from '@dxos/random';
12
- import { Filter, useQuery } from '@dxos/react-client/echo';
10
+ import { Filter, Obj } from '@dxos/echo';
11
+ import { random } from '@dxos/random';
12
+ import { useQuery } from '@dxos/react-client/echo';
13
13
  import { useClientStory, withClientProvider } from '@dxos/react-client/testing';
14
14
  import { Button } from '@dxos/react-ui';
15
15
  import { withTheme } from '@dxos/react-ui/testing';
16
16
  import { Person } from '@dxos/types';
17
17
  import { osTranslations } from '@dxos/ui-theme';
18
18
 
19
- import { translations } from '../../translations';
19
+ import { translations } from '#translations';
20
20
 
21
21
  import { ObjectPicker } from './ObjectPicker';
22
22
 
23
- faker.seed(1);
23
+ random.seed(1);
24
24
 
25
25
  const createPerson = () =>
26
26
  Obj.make(Person.Person, {
27
- fullName: faker.person.fullName(),
27
+ fullName: random.person.fullName(),
28
28
  });
29
29
 
30
30
  const omitId = Schema.omit<any, any, ['id']>('id');
@@ -48,7 +48,7 @@ const DefaultStory = () => {
48
48
  }
49
49
  }, [space]);
50
50
 
51
- // Get all objects in the space (similar to BoardContainer)
51
+ // Get all objects in the space (similar to BoardArticle)
52
52
  const allObjects = useQuery(space?.db, Filter.everything());
53
53
 
54
54
  // Map objects to options format expected by ObjectPicker
@@ -65,7 +65,9 @@ const DefaultStory = () => {
65
65
  const handleCreateCallback = useCallback(
66
66
  (values: any) => {
67
67
  console.log('[on create]', values);
68
- if (!space) return;
68
+ if (!space) {
69
+ return;
70
+ }
69
71
  const newPerson = space.db.add(Obj.make(Person.Person, values));
70
72
  mockHandleCreate(values);
71
73
  return newPerson;
@@ -74,7 +76,7 @@ const DefaultStory = () => {
74
76
  );
75
77
 
76
78
  return (
77
- <div role='none' className='flex w-96'>
79
+ <div className='w-96'>
78
80
  <ObjectPicker.Root open={isOpen} onOpenChange={setIsOpen}>
79
81
  <ObjectPicker.Trigger asChild>
80
82
  <Button variant='primary' data-testid='trigger' classNames='w-full'>
@@ -85,7 +87,7 @@ const DefaultStory = () => {
85
87
  classNames='dx-card-popover-width'
86
88
  options={options}
87
89
  createSchema={personSchema}
88
- createOptionLabel={['create new person label', { ns: osTranslations }]}
90
+ createOptionLabel={['create-new-person.label', { ns: osTranslations }]}
89
91
  createOptionIcon='ph--user-plus--regular'
90
92
  createInitialValuePath='fullName'
91
93
  onCreate={handleCreateCallback}
@@ -6,10 +6,13 @@ import type * as Schema from 'effect/Schema';
6
6
  import React, { type KeyboardEvent, forwardRef, useCallback, useState } from 'react';
7
7
 
8
8
  import { type Palette, Popover, type ThemedClassName, useTranslation } from '@dxos/react-ui';
9
- import { Combobox, useSearchListInput, useSearchListResults } from '@dxos/react-ui-searchlist';
9
+ import { Combobox } from '@dxos/react-ui-list';
10
+ import { useSearchListResults } from '@dxos/react-ui-search';
11
+
12
+ import { translationKey } from '#translations';
10
13
 
11
- import { translationKey } from '../../translations';
12
14
  import { Form } from '../Form';
15
+ import { type FormFieldMap } from '../Form/FormFieldComponent';
13
16
 
14
17
  export type RefOption = {
15
18
  id: string;
@@ -24,36 +27,18 @@ export type ObjectPickerContentProps = ThemedClassName<{
24
27
  createOptionIcon?: string;
25
28
  createSchema?: Schema.Schema.AnyNoContext;
26
29
  createInitialValuePath?: string;
27
- onCreate?: (values: any) => void;
30
+ createFieldMap?: FormFieldMap;
31
+ /**
32
+ * Persist a newly-created object given the form values. May be async (e.g.
33
+ * to write to a database). The Promise is awaited before the inline create
34
+ * form is collapsed back to the picker list, so consumers can complete
35
+ * follow-up work (assign the new ref to a parent slot, close the popover)
36
+ * before the picker UI re-renders.
37
+ */
38
+ onCreate?: (values: any) => unknown | Promise<unknown>;
28
39
  onSelect: (id: string) => void;
29
40
  }>;
30
41
 
31
- const CreateItem = ({
32
- createOptionLabel,
33
- createOptionIcon,
34
- onCreateItemSelect,
35
- }: {
36
- createOptionLabel: [string, { ns: string }];
37
- createOptionIcon: string;
38
- onCreateItemSelect: (query: string) => void;
39
- }) => {
40
- const { t } = useTranslation(translationKey);
41
- const { query } = useSearchListInput();
42
-
43
- return (
44
- <Combobox.Item
45
- value='__create__'
46
- label={t(createOptionLabel[0], { ns: createOptionLabel[1].ns, text: query })}
47
- icon={createOptionIcon}
48
- classNames='flex items-center gap-2'
49
- closeOnSelect={false}
50
- onSelect={() => {
51
- onCreateItemSelect(query);
52
- }}
53
- />
54
- );
55
- };
56
-
57
42
  const ObjectPickerContent = forwardRef<HTMLDivElement, ObjectPickerContentProps>(
58
43
  (
59
44
  {
@@ -63,23 +48,24 @@ const ObjectPickerContent = forwardRef<HTMLDivElement, ObjectPickerContentProps>
63
48
  createOptionLabel,
64
49
  createOptionIcon,
65
50
  createInitialValuePath,
51
+ createFieldMap,
66
52
  onSelect,
67
53
  onCreate,
68
54
  ...props
69
55
  },
70
- ref,
56
+ forwardedRef,
71
57
  ) => {
72
58
  const { t } = useTranslation(translationKey);
73
59
  const [showForm, setShowForm] = useState(false);
74
60
  const [formInitialValue, setFormInitialValue] = useState<string>('');
75
61
 
76
- const { results, handleSearch } = useSearchListResults({
62
+ const { results, query, handleSearch } = useSearchListResults({
77
63
  items: options,
78
64
  });
79
65
 
80
66
  const handleFormSave = useCallback(
81
- (values: any) => {
82
- onCreate?.(values);
67
+ async (values: any) => {
68
+ await onCreate?.(values);
83
69
  setShowForm(false);
84
70
  setFormInitialValue('');
85
71
  },
@@ -112,14 +98,15 @@ const ObjectPickerContent = forwardRef<HTMLDivElement, ObjectPickerContentProps>
112
98
  [createSchema],
113
99
  );
114
100
 
115
- return (
116
- <Combobox.Content {...props} ref={ref} onSearch={handleSearch} onKeyDownCapture={handleKeyDown}>
117
- {showForm && createSchema ? (
101
+ if (showForm && createSchema) {
102
+ return (
103
+ <Combobox.Content {...props} onKeyDownCapture={handleKeyDown} ref={forwardedRef}>
118
104
  <Popover.Viewport>
119
105
  <Form.Root
120
106
  testId='create-referenced-object-form'
121
107
  schema={createSchema}
122
- values={createInitialValuePath ? { [createInitialValuePath]: formInitialValue } : {}}
108
+ defaultValues={createInitialValuePath ? { [createInitialValuePath]: formInitialValue } : {}}
109
+ fieldMap={createFieldMap}
123
110
  onSave={handleFormSave}
124
111
  onCancel={handleFormCancel}
125
112
  >
@@ -131,40 +118,80 @@ const ObjectPickerContent = forwardRef<HTMLDivElement, ObjectPickerContentProps>
131
118
  </Form.Viewport>
132
119
  </Form.Root>
133
120
  </Popover.Viewport>
134
- ) : (
135
- <>
136
- <Combobox.Input placeholder={t('ref field combobox input placeholder')} autoFocus />
137
- <Combobox.List>
138
- {results.map((option) => (
139
- <Combobox.Item
140
- key={option.id}
141
- value={option.id}
142
- label={option.label}
143
- checked={selectedIds?.includes(option.id)}
144
- onSelect={() => onSelect(option.id)}
145
- classNames='flex items-center gap-2'
146
- />
147
- ))}
148
- {createOptionLabel && createOptionIcon && createSchema && onCreate && (
149
- <CreateItem
150
- createOptionLabel={createOptionLabel}
151
- createOptionIcon={createOptionIcon}
152
- onCreateItemSelect={(query: string) => {
153
- setFormInitialValue(query);
154
- setShowForm(true);
155
- }}
156
- />
157
- )}
158
- </Combobox.List>
159
- </>
160
- )}
121
+ <Combobox.Arrow />
122
+ </Combobox.Content>
123
+ );
124
+ }
125
+
126
+ return (
127
+ <Combobox.Content {...props} onKeyDownCapture={handleKeyDown} ref={forwardedRef}>
128
+ <Combobox.Input
129
+ placeholder={t('ref-field-combobox-input.placeholder')}
130
+ autoFocus
131
+ value={query}
132
+ onValueChange={handleSearch}
133
+ />
134
+ <Combobox.List>
135
+ {results.map((option) => (
136
+ <Combobox.Item
137
+ key={option.id}
138
+ value={option.id}
139
+ label={option.label}
140
+ checked={selectedIds?.includes(option.id)}
141
+ onSelect={() => onSelect(option.id)}
142
+ classNames='flex items-center gap-2'
143
+ />
144
+ ))}
145
+ {createSchema && onCreate && (
146
+ <CreateItem
147
+ query={query}
148
+ createOptionLabel={createOptionLabel}
149
+ createOptionIcon={createOptionIcon ?? 'ph--plus--regular'}
150
+ onCreateItemSelect={(currentQuery: string) => {
151
+ setFormInitialValue(currentQuery);
152
+ setShowForm(true);
153
+ }}
154
+ />
155
+ )}
156
+ </Combobox.List>
161
157
  <Combobox.Arrow />
162
158
  </Combobox.Content>
163
159
  );
164
160
  },
165
161
  );
166
162
 
167
- ObjectPickerContent.displayName = 'ObjectPickerContent';
163
+ const CreateItem = ({
164
+ query,
165
+ createOptionLabel,
166
+ createOptionIcon,
167
+ onCreateItemSelect,
168
+ }: {
169
+ query: string;
170
+ createOptionLabel?: [string, { ns: string }];
171
+ createOptionIcon: string;
172
+ onCreateItemSelect: (query: string) => void;
173
+ }) => {
174
+ const { t } = useTranslation(translationKey);
175
+
176
+ const label = createOptionLabel
177
+ ? t(createOptionLabel[0], { ns: createOptionLabel[1].ns, text: query })
178
+ : t('create-option.label');
179
+
180
+ return (
181
+ <Combobox.Item
182
+ value='__create__'
183
+ label={label}
184
+ icon={createOptionIcon}
185
+ classNames='flex items-center gap-2'
186
+ closeOnSelect={false}
187
+ onSelect={() => {
188
+ onCreateItemSelect(query);
189
+ }}
190
+ />
191
+ );
192
+ };
193
+
194
+ ObjectPickerContent.displayName = 'ObjectPicker.Content';
168
195
 
169
196
  export const ObjectPicker = {
170
197
  Root: Combobox.Root,
@@ -0,0 +1,362 @@
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
+
21
+ import { OBJECT_PROPERTIES_DEBUG_SYMBOL } from '../testing';
22
+ import { ObjectProperties } from './ObjectProperties';
23
+
24
+ //
25
+ // Test-only schemas exercising a non-Tag ref-array. `Article.authors` is the
26
+ // case the generic create flow currently fails on: the picker creates an
27
+ // Author in the DB but never wires the new Ref into the array slot.
28
+ //
29
+
30
+ const Author = Schema.Struct({
31
+ name: Schema.String,
32
+ }).pipe(
33
+ Type.object({
34
+ typename: 'org.dxos.test.author',
35
+ version: '0.1.0',
36
+ }),
37
+ LabelAnnotation.set(['name']),
38
+ Annotation.IconAnnotation.set({ icon: 'ph--user--regular', hue: 'blue' }),
39
+ );
40
+ type Author = Schema.Schema.Type<typeof Author>;
41
+
42
+ const Article = Schema.Struct({
43
+ title: Schema.String.pipe(Schema.optional),
44
+ authors: Schema.Array(Ref.Ref(Author)),
45
+ }).pipe(
46
+ Type.object({
47
+ typename: 'org.dxos.test.article',
48
+ version: '0.1.0',
49
+ }),
50
+ LabelAnnotation.set(['title']),
51
+ Annotation.IconAnnotation.set({ icon: 'ph--article--regular', hue: 'green' }),
52
+ );
53
+ type Article = Schema.Schema.Type<typeof Article>;
54
+
55
+ //
56
+ // `Note` mirrors the `Subscription.Feed` shape: a required `Ref` field
57
+ // (`backing`) is hidden from the form via `FormInputAnnotation.set(false)`,
58
+ // so `Obj.make(Note, values)` from the picker would reject because the form
59
+ // values can't satisfy the schema. A `FactoryAnnotation` constructs the
60
+ // backing object and links it at create time. `cursor` is an optional hidden
61
+ // field included to exercise the same path for non-required hidden fields.
62
+ //
63
+ const NoteBacking = Schema.Struct({}).pipe(Type.object({ typename: 'org.dxos.test.note-backing', version: '0.1.0' }));
64
+ type NoteBacking = Schema.Schema.Type<typeof NoteBacking>;
65
+
66
+ const Note = Schema.Struct({
67
+ title: Schema.String,
68
+ cursor: Schema.String.pipe(FormInputAnnotation.set(false), Schema.optional),
69
+ backing: Ref.Ref(NoteBacking).pipe(FormInputAnnotation.set(false)),
70
+ }).pipe(
71
+ Type.object({
72
+ typename: 'org.dxos.test.note',
73
+ version: '0.1.0',
74
+ }),
75
+ LabelAnnotation.set(['title']),
76
+ Annotation.IconAnnotation.set({ icon: 'ph--note--regular', hue: 'amber' }),
77
+ FactoryAnnotation.set(((values: any) =>
78
+ Obj.make(Note, { ...values, backing: Ref.make(Obj.make(NoteBacking, {})) })) as FactoryFn),
79
+ );
80
+ type Note = Schema.Schema.Type<typeof Note>;
81
+
82
+ const Notebook = Schema.Struct({
83
+ name: Schema.String.pipe(Schema.optional),
84
+ notes: Schema.Array(Ref.Ref(Note)),
85
+ }).pipe(
86
+ Type.object({
87
+ typename: 'org.dxos.test.notebook',
88
+ version: '0.1.0',
89
+ }),
90
+ LabelAnnotation.set(['name']),
91
+ Annotation.IconAnnotation.set({ icon: 'ph--notebook--regular', hue: 'amber' }),
92
+ );
93
+ type Notebook = Schema.Schema.Type<typeof Notebook>;
94
+
95
+ //
96
+ // Stories.
97
+ //
98
+
99
+ export type ObjectPropertiesDebug = {
100
+ /** Live database the story is rendering against. */
101
+ db: ReturnType<typeof useClientStory>['space'] extends infer S ? (S extends { db: infer D } ? D : never) : never;
102
+ /** Object whose properties are being edited. */
103
+ object: Obj.Unknown;
104
+ };
105
+
106
+ const PipelineStory = () => {
107
+ const { space } = useClientStory();
108
+ const [object] = useQuery(space?.db, Filter.type(Pipeline.Pipeline));
109
+
110
+ useEffect(() => {
111
+ if (typeof window !== 'undefined' && space?.db && object) {
112
+ (window as any)[OBJECT_PROPERTIES_DEBUG_SYMBOL] = { db: space.db, object } satisfies ObjectPropertiesDebug;
113
+ }
114
+ }, [space, object]);
115
+
116
+ if (!object) {
117
+ return <Loading />;
118
+ }
119
+ return (
120
+ <Panel.Root>
121
+ <Panel.Content asChild>
122
+ <ObjectProperties object={object} />
123
+ </Panel.Content>
124
+ </Panel.Root>
125
+ );
126
+ };
127
+
128
+ const ArticleStory = () => {
129
+ const { space } = useClientStory();
130
+ const [object] = useQuery(space?.db, Filter.type(Article));
131
+
132
+ useEffect(() => {
133
+ if (typeof window !== 'undefined' && space?.db && object) {
134
+ (window as any)[OBJECT_PROPERTIES_DEBUG_SYMBOL] = { db: space.db, object } satisfies ObjectPropertiesDebug;
135
+ }
136
+ }, [space, object]);
137
+
138
+ if (!object) {
139
+ return <Loading />;
140
+ }
141
+ return (
142
+ <Panel.Root>
143
+ <Panel.Content asChild>
144
+ <ObjectProperties object={object} />
145
+ </Panel.Content>
146
+ </Panel.Root>
147
+ );
148
+ };
149
+
150
+ const meta = {
151
+ title: 'ui/react-ui-form/ObjectProperties',
152
+ component: ObjectProperties as any,
153
+ decorators: [withTheme(), withLayout({ layout: 'column' })],
154
+ parameters: {
155
+ layout: 'fullscreen',
156
+ translations,
157
+ },
158
+ } satisfies Meta<typeof PipelineStory>;
159
+
160
+ export default meta;
161
+
162
+ type Story = StoryObj<typeof meta>;
163
+
164
+ const pipelineDecorators = [
165
+ withClientProvider({
166
+ createIdentity: true,
167
+ createSpace: true,
168
+ types: [Pipeline.Pipeline, Tag.Tag],
169
+ onCreateSpace: async ({ space }) => {
170
+ space.db.add(Pipeline.make());
171
+ space.db.add(Tag.make({ label: 'Tag 1' }));
172
+ space.db.add(Tag.make({ label: 'Tag 2' }));
173
+ space.db.add(Tag.make({ label: 'Tag 3' }));
174
+ },
175
+ }),
176
+ ];
177
+
178
+ const articleDecorators = [
179
+ withClientProvider({
180
+ createIdentity: true,
181
+ createSpace: true,
182
+ types: [Article, Author, Tag.Tag],
183
+ onCreateSpace: async ({ space }) => {
184
+ space.db.add(Obj.make(Article, { title: 'Untitled article', authors: [] }));
185
+ },
186
+ }),
187
+ ];
188
+
189
+ const NotebookStory = () => {
190
+ const { space } = useClientStory();
191
+ const [object] = useQuery(space?.db, Filter.type(Notebook));
192
+
193
+ useEffect(() => {
194
+ if (typeof window !== 'undefined' && space?.db && object) {
195
+ (window as any)[OBJECT_PROPERTIES_DEBUG_SYMBOL] = { db: space.db, object } satisfies ObjectPropertiesDebug;
196
+ }
197
+ }, [space, object]);
198
+
199
+ if (!object) {
200
+ return <Loading />;
201
+ }
202
+ return (
203
+ <Panel.Root>
204
+ <Panel.Content asChild>
205
+ <ObjectProperties object={object} />
206
+ </Panel.Content>
207
+ </Panel.Root>
208
+ );
209
+ };
210
+
211
+ const notebookDecorators = [
212
+ withClientProvider({
213
+ createIdentity: true,
214
+ createSpace: true,
215
+ types: [Notebook, Note, NoteBacking, Tag.Tag],
216
+ onCreateSpace: async ({ space }) => {
217
+ space.db.add(Obj.make(Notebook, { name: 'Untitled notebook', notes: [] }));
218
+ },
219
+ }),
220
+ ];
221
+
222
+ /**
223
+ * Default Pipeline form — manual exploration. Includes the Tags picker (which
224
+ * is the only ref-array path that currently works end-to-end, by virtue of
225
+ * `Tag.Tag` being special-cased in `ObjectProperties.handleCreate`).
226
+ */
227
+ export const Default: Story = {
228
+ render: PipelineStory,
229
+ decorators: pipelineDecorators,
230
+ };
231
+
232
+ /**
233
+ * Tag-creation play. Walks the inline create flow against the Tags picker and
234
+ * leaves the magazine in the post-Save state for assertions to inspect via
235
+ * `OBJECT_PROPERTIES_DEBUG_SYMBOL`.
236
+ *
237
+ * Expected end-state (currently passes):
238
+ * - new Tag.Tag exists in the DB with `label === "PinnedTag"`
239
+ * - `Obj.getMeta(object).tags` includes its DXN.
240
+ */
241
+ export const CreateTagPlay: Story = {
242
+ render: PipelineStory,
243
+ decorators: pipelineDecorators,
244
+ play: async ({ canvasElement }) => {
245
+ const canvas = within(canvasElement);
246
+ const body = within(document.body);
247
+
248
+ // Tags is `Schema.Array(Ref.Ref(Tag.Tag))` and starts empty, so it renders
249
+ // as an "Add item" button — same shape as any other ref-array. Click it
250
+ // to materialise an empty slot, then open the slot's picker.
251
+ const addButton = await canvas.findByRole('button', { name: /add/i }, { timeout: 10_000 });
252
+ await userEvent.click(addButton);
253
+ await new Promise((resolve) => setTimeout(resolve, 250));
254
+
255
+ const comboboxes = await canvas.findAllByRole('combobox', undefined, { timeout: 10_000 });
256
+ await expect(comboboxes.length).toBeGreaterThan(0);
257
+ await userEvent.click(comboboxes[0]);
258
+
259
+ const search = await body.findByPlaceholderText(/search/i, undefined, { timeout: 5000 });
260
+ await userEvent.type(search, 'PinnedTag');
261
+
262
+ // The Tag picker localises the create option as "Add tag" (no text suffix
263
+ // — the translation has no `{{text}}` placeholder), so match on /add/.
264
+ const createOption = await body.findByRole('option', { name: /add/i }, { timeout: 3000 });
265
+ await userEvent.click(createOption);
266
+
267
+ const form = await body.findByTestId('create-referenced-object-form', undefined, { timeout: 5000 });
268
+ const labelInput = await within(form).findByLabelText(/^label$/i);
269
+ await userEvent.clear(labelInput);
270
+ await userEvent.type(labelInput, 'PinnedTag');
271
+ await userEvent.click(await within(form).findByTestId('save-button'));
272
+
273
+ // Allow async state updates to flush before the test asserts.
274
+ await new Promise((resolve) => setTimeout(resolve, 250));
275
+ },
276
+ };
277
+
278
+ /**
279
+ * Ref-array creation play. Walks Add → Open → Create → Save against
280
+ * `Article.authors`. After the fix, the new Author should be persisted AND
281
+ * wired into `article.authors[0]`. Today it persists to the DB but the array
282
+ * slot stays empty.
283
+ */
284
+ export const CreateRefArrayPlay: Story = {
285
+ render: ArticleStory,
286
+ decorators: articleDecorators,
287
+ play: async ({ canvasElement }) => {
288
+ const canvas = within(canvasElement);
289
+ const body = within(document.body);
290
+
291
+ // ObjectProperties extends Article with the BaseSchema's `tags` array, so
292
+ // the form has two "Add item" buttons (Tags + Authors). Pick the last one
293
+ // — Authors comes after Tags in the rendered order.
294
+ const addButtons = await canvas.findAllByRole('button', { name: /add/i }, { timeout: 10_000 });
295
+ await userEvent.click(addButtons[addButtons.length - 1]);
296
+ await new Promise((resolve) => setTimeout(resolve, 250));
297
+
298
+ // Open the empty slot's combobox.
299
+ const comboboxes = await canvas.findAllByRole('combobox', undefined, { timeout: 10_000 });
300
+ await expect(comboboxes.length).toBeGreaterThan(0);
301
+ await userEvent.click(comboboxes[0]);
302
+
303
+ const search = await body.findByPlaceholderText(/search/i, undefined, { timeout: 5000 });
304
+ await userEvent.type(search, 'Ada Lovelace');
305
+
306
+ // Author has no `createOptionLabel` → option falls back to "Create".
307
+ const createOption = await body.findByRole('option', { name: /create/i }, { timeout: 3000 });
308
+ await userEvent.click(createOption);
309
+
310
+ const form = await body.findByTestId('create-referenced-object-form', undefined, { timeout: 5000 });
311
+ const nameInput = await within(form).findByLabelText(/^name$/i);
312
+ await userEvent.clear(nameInput);
313
+ await userEvent.type(nameInput, 'Ada Lovelace');
314
+ await userEvent.click(await within(form).findByTestId('save-button'));
315
+
316
+ await new Promise((resolve) => setTimeout(resolve, 250));
317
+ },
318
+ };
319
+
320
+ /**
321
+ * Ref-array creation against a schema with a hidden required field. `Note`
322
+ * mirrors the shape of `Subscription.Feed`: the form omits `backing` (a
323
+ * required `Ref` annotated `FormInputAnnotation.set(false)`) but the schema
324
+ * requires it. Without `omitHiddenFormFields` + `FactoryAnnotation`, the
325
+ * form's validator would reject Save and the popover would never close.
326
+ *
327
+ * Expected end-state after the fix:
328
+ * - new Note exists in DB with a synthesised `backing` ref
329
+ * - `notebook.notes[0]` references the new Note
330
+ * - the create form is gone
331
+ */
332
+ export const CreateHiddenFieldPlay: Story = {
333
+ render: NotebookStory,
334
+ decorators: notebookDecorators,
335
+ play: async ({ canvasElement }) => {
336
+ const canvas = within(canvasElement);
337
+ const body = within(document.body);
338
+
339
+ // Two "Add item" buttons (Tags + Notes) — Notes comes second.
340
+ const addButtons = await canvas.findAllByRole('button', { name: /add/i }, { timeout: 10_000 });
341
+ await userEvent.click(addButtons[addButtons.length - 1]);
342
+ await new Promise((resolve) => setTimeout(resolve, 250));
343
+
344
+ const comboboxes = await canvas.findAllByRole('combobox', undefined, { timeout: 10_000 });
345
+ await expect(comboboxes.length).toBeGreaterThan(0);
346
+ await userEvent.click(comboboxes[0]);
347
+
348
+ const search = await body.findByPlaceholderText(/search/i, undefined, { timeout: 5000 });
349
+ await userEvent.type(search, 'Ideas');
350
+
351
+ const createOption = await body.findByRole('option', { name: /create/i }, { timeout: 3000 });
352
+ await userEvent.click(createOption);
353
+
354
+ const form = await body.findByTestId('create-referenced-object-form', undefined, { timeout: 5000 });
355
+ const titleInput = await within(form).findByLabelText(/^title$/i);
356
+ await userEvent.clear(titleInput);
357
+ await userEvent.type(titleInput, 'Ideas');
358
+ await userEvent.click(await within(form).findByTestId('save-button'));
359
+
360
+ await new Promise((resolve) => setTimeout(resolve, 250));
361
+ },
362
+ };