@dxos/react-ui-form 0.8.4-main.c1de068 → 0.8.4-main.c85a9c8dae

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 (204) hide show
  1. package/README.md +0 -1
  2. package/dist/lib/browser/index.mjs +2314 -2078
  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 +2314 -2078
  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 +82 -5
  11. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
  12. package/dist/types/src/components/Form/Form.d.ts +81 -25
  13. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  14. package/dist/types/src/components/Form/Form.stories.d.ts +127 -55
  15. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/FormField.d.ts +37 -0
  17. package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
  18. package/dist/types/src/components/Form/FormFieldComponent.d.ts +78 -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 +19 -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 +4 -0
  31. package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
  32. package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
  33. package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
  34. package/dist/types/src/components/Form/fields/RefField.d.ts +14 -0
  35. package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
  36. package/dist/types/src/components/Form/fields/SelectField.d.ts +10 -0
  37. package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
  38. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
  39. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
  40. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +82 -0
  41. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
  42. package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
  43. package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
  44. package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
  45. package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
  46. package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
  47. package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
  48. package/dist/types/src/components/Form/fields/index.d.ts +12 -0
  49. package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
  50. package/dist/types/src/components/Form/index.d.ts +2 -4
  51. package/dist/types/src/components/Form/index.d.ts.map +1 -1
  52. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +46 -0
  53. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
  54. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +85 -0
  55. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -0
  56. package/dist/types/src/components/ObjectPicker/index.d.ts +2 -0
  57. package/dist/types/src/components/ObjectPicker/index.d.ts.map +1 -0
  58. package/dist/types/src/components/Settings/Settings.d.ts +56 -0
  59. package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
  60. package/dist/types/src/components/Settings/Settings.stories.d.ts +81 -0
  61. package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
  62. package/dist/types/src/components/Settings/index.d.ts +2 -0
  63. package/dist/types/src/components/Settings/index.d.ts.map +1 -0
  64. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +22 -10
  65. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  66. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +84 -8
  67. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  68. package/dist/types/src/components/index.d.ts +2 -2
  69. package/dist/types/src/components/index.d.ts.map +1 -1
  70. package/dist/types/src/components/testing.d.ts +4 -3
  71. package/dist/types/src/components/testing.d.ts.map +1 -1
  72. package/dist/types/src/hooks/index.d.ts +2 -3
  73. package/dist/types/src/hooks/index.d.ts.map +1 -1
  74. package/dist/types/src/hooks/useFormHandler.d.ts +82 -0
  75. package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -0
  76. package/dist/types/src/hooks/useKeyHandler.d.ts +6 -0
  77. package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
  78. package/dist/types/src/translations.d.ts +19 -10
  79. package/dist/types/src/translations.d.ts.map +1 -1
  80. package/dist/types/src/util/field.d.ts +3 -3
  81. package/dist/types/src/util/field.d.ts.map +1 -1
  82. package/dist/types/src/util/formatting.d.ts +2 -2
  83. package/dist/types/src/util/formatting.d.ts.map +1 -1
  84. package/dist/types/src/util/index.d.ts +1 -1
  85. package/dist/types/src/util/index.d.ts.map +1 -1
  86. package/dist/types/src/util/properties.d.ts +7 -0
  87. package/dist/types/src/util/properties.d.ts.map +1 -0
  88. package/dist/types/src/util/refs.d.ts +2 -3
  89. package/dist/types/src/util/refs.d.ts.map +1 -1
  90. package/dist/types/tsconfig.tsbuildinfo +1 -1
  91. package/package.json +51 -42
  92. package/src/components/FieldEditor/FieldEditor.stories.tsx +51 -32
  93. package/src/components/FieldEditor/FieldEditor.test.tsx +3 -2
  94. package/src/components/FieldEditor/FieldEditor.tsx +87 -83
  95. package/src/components/Form/Form.stories.tsx +156 -219
  96. package/src/components/Form/Form.tsx +347 -96
  97. package/src/components/Form/FormField.tsx +294 -0
  98. package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
  99. package/src/components/Form/FormFieldComponent.tsx +172 -0
  100. package/src/components/Form/FormFieldSet.tsx +123 -0
  101. package/src/components/Form/README.md +3 -0
  102. package/src/components/Form/fields/ArrayField.tsx +149 -0
  103. package/src/components/Form/fields/BooleanField.tsx +22 -0
  104. package/src/components/Form/fields/GeoPointField.tsx +120 -0
  105. package/src/components/Form/fields/MarkdownField.tsx +78 -0
  106. package/src/components/Form/fields/NumberField.tsx +39 -0
  107. package/src/components/Form/fields/RefField.tsx +189 -0
  108. package/src/components/Form/fields/SelectField.tsx +65 -0
  109. package/src/components/Form/fields/SelectOptionField.stories.tsx +64 -0
  110. package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +46 -41
  111. package/src/components/Form/fields/TextAreaField.tsx +38 -0
  112. package/src/components/Form/fields/TextField.tsx +38 -0
  113. package/src/components/Form/{custom/TupleInput.tsx → fields/TupleField.tsx} +16 -9
  114. package/src/components/Form/fields/index.ts +15 -0
  115. package/src/components/Form/index.ts +3 -4
  116. package/src/components/ObjectPicker/ObjectPicker.stories.tsx +210 -0
  117. package/src/components/ObjectPicker/ObjectPicker.tsx +175 -0
  118. package/src/components/{ControlSection → ObjectPicker}/index.ts +1 -1
  119. package/src/components/Settings/Settings.stories.tsx +56 -0
  120. package/src/components/Settings/Settings.tsx +230 -0
  121. package/src/components/Settings/index.ts +5 -0
  122. package/src/components/ViewEditor/ViewEditor.stories.tsx +140 -62
  123. package/src/components/ViewEditor/ViewEditor.test.tsx +17 -18
  124. package/src/components/ViewEditor/ViewEditor.tsx +341 -202
  125. package/src/components/index.ts +2 -3
  126. package/src/components/testing.tsx +22 -11
  127. package/src/hooks/index.ts +2 -3
  128. package/src/hooks/useFormHandler.ts +380 -0
  129. package/src/hooks/useKeyHandler.tsx +37 -0
  130. package/src/translations.ts +21 -12
  131. package/src/util/field.ts +33 -43
  132. package/src/util/formatting.ts +22 -22
  133. package/src/util/index.ts +1 -1
  134. package/src/util/properties.ts +18 -0
  135. package/src/util/refs.ts +12 -18
  136. package/dist/types/src/components/ControlSection/ControlSection.d.ts +0 -23
  137. package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +0 -1
  138. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +0 -8
  139. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +0 -1
  140. package/dist/types/src/components/ControlSection/index.d.ts +0 -2
  141. package/dist/types/src/components/ControlSection/index.d.ts.map +0 -1
  142. package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
  143. package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
  144. package/dist/types/src/components/Form/Defaults.d.ts +0 -14
  145. package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
  146. package/dist/types/src/components/Form/FormActions.d.ts +0 -9
  147. package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
  148. package/dist/types/src/components/Form/FormContent.d.ts +0 -57
  149. package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
  150. package/dist/types/src/components/Form/FormContext.d.ts +0 -23
  151. package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
  152. package/dist/types/src/components/Form/Input.d.ts +0 -26
  153. package/dist/types/src/components/Form/Input.d.ts.map +0 -1
  154. package/dist/types/src/components/Form/RefField.d.ts +0 -12
  155. package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
  156. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
  157. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
  158. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
  159. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
  160. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -7
  161. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
  162. package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
  163. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
  164. package/dist/types/src/components/Form/custom/index.d.ts +0 -4
  165. package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
  166. package/dist/types/src/components/Form/factory.d.ts +0 -8
  167. package/dist/types/src/components/Form/factory.d.ts.map +0 -1
  168. package/dist/types/src/components/deprecated/Form.d.ts +0 -15
  169. package/dist/types/src/components/deprecated/Form.d.ts.map +0 -1
  170. package/dist/types/src/components/deprecated/Form.stories.d.ts +0 -13
  171. package/dist/types/src/components/deprecated/Form.stories.d.ts.map +0 -1
  172. package/dist/types/src/components/deprecated/FormInput.d.ts +0 -16
  173. package/dist/types/src/components/deprecated/FormInput.d.ts.map +0 -1
  174. package/dist/types/src/components/deprecated/index.d.ts +0 -2
  175. package/dist/types/src/components/deprecated/index.d.ts.map +0 -1
  176. package/dist/types/src/hooks/useForm.d.ts +0 -67
  177. package/dist/types/src/hooks/useForm.d.ts.map +0 -1
  178. package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -23
  179. package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
  180. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
  181. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
  182. package/dist/types/src/util/schema.d.ts +0 -3
  183. package/dist/types/src/util/schema.d.ts.map +0 -1
  184. package/src/components/ControlSection/ControlSection.stories.tsx +0 -41
  185. package/src/components/ControlSection/ControlSection.tsx +0 -139
  186. package/src/components/Form/ArrayField.tsx +0 -124
  187. package/src/components/Form/Defaults.tsx +0 -203
  188. package/src/components/Form/FormActions.tsx +0 -53
  189. package/src/components/Form/FormContent.tsx +0 -240
  190. package/src/components/Form/FormContext.tsx +0 -110
  191. package/src/components/Form/Input.tsx +0 -51
  192. package/src/components/Form/RefField.tsx +0 -213
  193. package/src/components/Form/custom/GeoPointInput.tsx +0 -72
  194. package/src/components/Form/custom/SelectOptionsInput.stories.tsx +0 -61
  195. package/src/components/Form/custom/index.ts +0 -7
  196. package/src/components/Form/factory.tsx +0 -31
  197. package/src/components/deprecated/Form.stories.tsx +0 -62
  198. package/src/components/deprecated/Form.tsx +0 -119
  199. package/src/components/deprecated/FormInput.tsx +0 -71
  200. package/src/components/deprecated/index.ts +0 -5
  201. package/src/hooks/useForm.ts +0 -291
  202. package/src/hooks/useQueryRefOptions.ts +0 -53
  203. package/src/hooks/useRefQueryLookupHandler.ts +0 -39
  204. package/src/util/schema.ts +0 -14
@@ -2,269 +2,408 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { Schema } from 'effect';
6
- import React, { useCallback, useMemo, useState } from 'react';
5
+ import { RegistryContext } from '@effect-atom/atom-react';
6
+ import * as Array from 'effect/Array';
7
+ import * as Match from 'effect/Match';
8
+ import * as Option from 'effect/Option';
9
+ import * as Schema from 'effect/Schema';
10
+ import React, { forwardRef, useCallback, useContext, useImperativeHandle, useMemo, useState } from 'react';
7
11
 
8
- import { type SchemaRegistry } from '@dxos/echo-db';
9
- import { EchoSchema, Format, type JsonProp, isMutable, toJsonSchema } from '@dxos/echo-schema';
12
+ import { Entity, Feed, Filter, Format, Obj, Query, QueryAST, Ref, type SchemaRegistry } from '@dxos/echo';
13
+ import { View } from '@dxos/echo';
14
+ import { EchoSchema, type JsonProp, isMutable, toJsonSchema } from '@dxos/echo/internal';
10
15
  import { invariant } from '@dxos/invariant';
11
- import { Callout, IconButton, type ThemedClassName, useTranslation } from '@dxos/react-ui';
16
+ import { useObject, useQuery } from '@dxos/react-client/echo';
17
+ import { IconButton, Input, Message, type ThemedClassName, useTranslation } from '@dxos/react-ui';
18
+ import { QueryForm, type QueryFormProps } from '@dxos/react-ui-components';
12
19
  import { List } from '@dxos/react-ui-list';
13
- import { cardSpacing } from '@dxos/react-ui-stack';
14
- import { ghostHover, inputTextLabel, mx } from '@dxos/react-ui-theme';
15
- import { FieldSchema, type FieldType, VIEW_FIELD_LIMIT, type DataType, ProjectionModel } from '@dxos/schema';
20
+ import { ProjectionModel, VIEW_FIELD_LIMIT, createEchoChangeCallback, getTypenameFromQuery } from '@dxos/schema';
21
+ import { mx, osTranslations, subtleHover } from '@dxos/ui-theme';
16
22
 
17
23
  import { translationKey } from '../../translations';
18
24
  import { FieldEditor } from '../FieldEditor';
19
- import { Form, type FormProps } from '../Form';
20
-
21
- const grid = 'grid grid-cols-[32px_1fr_32px_32px] min-bs-[2.5rem]';
22
-
23
- const ViewMetaSchema = Schema.Struct({
24
- typename: Format.URL.annotations({
25
- title: 'Typename',
26
- }),
27
- }).pipe(Schema.mutable);
28
-
29
- type ViewMetaType = Schema.Schema.Type<typeof ViewMetaSchema>;
25
+ import {
26
+ Form,
27
+ type FormFieldComponent,
28
+ type FormFieldComponentProps,
29
+ FormFieldLabel,
30
+ type FormFieldMap,
31
+ type FormRootProps,
32
+ } from '../Form';
30
33
 
31
34
  export type ViewEditorProps = ThemedClassName<
32
35
  {
33
36
  schema: Schema.Schema.AnyNoContext;
34
- view: DataType.View;
35
- registry?: SchemaRegistry;
36
- readonly?: boolean;
37
+ view: View.View;
38
+ mode?: 'schema' | 'tag';
39
+ registry?: SchemaRegistry.SchemaRegistry;
37
40
  showHeading?: boolean;
38
- onTypenameChanged?: (typename: string) => void;
41
+ onQueryChanged?: (query: QueryAST.Query, target?: string) => void;
39
42
  onDelete?: (fieldId: string) => void;
40
- } & Pick<FormProps<any>, 'outerSpacing'>
43
+ } & Pick<QueryFormProps, 'types' | 'tags'> &
44
+ Pick<FormRootProps<any>, 'readonly' | 'db'>
41
45
  >;
42
46
 
43
47
  /**
44
48
  * Schema-based object form.
45
49
  */
46
- export const ViewEditor = ({
47
- classNames,
48
- schema,
49
- view,
50
- registry,
51
- readonly: _readonly,
52
- showHeading = false,
53
- onTypenameChanged,
54
- onDelete,
55
- outerSpacing = true,
56
- }: ViewEditorProps) => {
50
+ export const ViewEditor = forwardRef<ProjectionModel, ViewEditorProps>(
51
+ (
52
+ {
53
+ classNames,
54
+ schema,
55
+ view,
56
+ mode = 'schema',
57
+ registry,
58
+ db,
59
+ readonly,
60
+ showHeading = false,
61
+ types,
62
+ tags,
63
+ onQueryChanged,
64
+ onDelete,
65
+ },
66
+ forwardedRef,
67
+ ) => {
68
+ const atomRegistry = useContext(RegistryContext);
69
+ const schemaReadonly = !isMutable(schema);
70
+ const { t } = useTranslation(translationKey);
71
+
72
+ const projectionModel = useMemo(() => {
73
+ // Use reactive and mutable version of json schema when schema is mutable.
74
+ const jsonSchema = schema instanceof EchoSchema ? schema.jsonSchema : toJsonSchema(schema);
75
+
76
+ // Always use createEchoChangeCallback since the view is ECHO-backed.
77
+ // Pass schema only when mutable to allow schema mutations.
78
+ const change = createEchoChangeCallback(view, schema instanceof EchoSchema ? schema : undefined);
79
+
80
+ const model = new ProjectionModel({
81
+ registry: atomRegistry,
82
+ view,
83
+ baseSchema: jsonSchema,
84
+ change,
85
+ });
86
+
87
+ return model;
88
+ }, [atomRegistry, schema, view]);
89
+
90
+ useImperativeHandle(forwardedRef, () => projectionModel, [projectionModel]);
91
+
92
+ const queueTarget = Match.value(view.query.ast).pipe(
93
+ Match.when({ type: 'from' }, ({ from }) => {
94
+ if (from._tag !== 'scope') {
95
+ return undefined;
96
+ }
97
+ return Option.fromNullable(from.scope.queues).pipe(
98
+ Option.flatMap((queues) => Array.head(queues)),
99
+ Option.map(String),
100
+ Option.getOrUndefined,
101
+ );
102
+ }),
103
+ Match.orElse(() => undefined),
104
+ );
105
+
106
+ const feeds = useQuery(db, Filter.type(Feed.Feed));
107
+
108
+ const targetRef = useMemo(() => {
109
+ if (!queueTarget) {
110
+ return undefined;
111
+ }
112
+ const feed = feeds.find((feed) => Feed.getQueueDxn(feed)?.toString() === queueTarget);
113
+ return feed ? Ref.fromDXN(Entity.getDXN(feed)) : undefined;
114
+ }, [queueTarget, feeds]);
115
+
116
+ const viewSchema = useMemo(() => {
117
+ const base = Schema.Struct({
118
+ query:
119
+ mode === 'schema'
120
+ ? Format.URL.annotations({ title: 'Record type' })
121
+ : QueryAST.Query.annotations({ title: 'Query' }),
122
+ });
123
+
124
+ if (mode === 'tag') {
125
+ return Schema.Struct({
126
+ ...base.fields,
127
+ target: Schema.optional(Ref.Ref(Feed.Feed).annotations({ title: 'Target Feed' })),
128
+ }).pipe(Schema.mutable);
129
+ }
130
+
131
+ return base.pipe(Schema.mutable);
132
+ }, [mode]);
133
+
134
+ // TODO(burdon): Need to warn user of possible consequences of editing.
135
+ // TODO(burdon): Settings should have domain name owned by user.
136
+ const viewValues = useMemo(
137
+ () => ({
138
+ query: mode === 'schema' ? getTypenameFromQuery(view.query.ast) : view.query.ast,
139
+ target: targetRef,
140
+ }),
141
+ [mode, view.query.ast, targetRef],
142
+ );
143
+
144
+ const fieldMap = useMemo<FormFieldMap | undefined>(
145
+ () => (mode === 'tag' ? customFields({ types, tags }) : undefined),
146
+ [mode, types, tags],
147
+ );
148
+
149
+ const handleUpdate = useCallback(
150
+ (values: any) => {
151
+ const targetValue = values.target;
152
+ let queueDxn: string | undefined;
153
+
154
+ if (Ref.isRef(targetValue)) {
155
+ const feedDxn = targetValue.dxn.toString();
156
+ const feed = feeds.find((feed) => Obj.getDXN(feed).toString() === feedDxn);
157
+ if (feed) {
158
+ queueDxn = Feed.getQueueDxn(feed)?.toString();
159
+ }
160
+ }
161
+
162
+ // TODO(wittjosiah): Deep-clone the AST to plain JS or ECHO proxy arrays become objects with numeric keys.
163
+ const query =
164
+ mode === 'schema'
165
+ ? Query.select(Filter.typename(values.query)).ast
166
+ : JSON.parse(JSON.stringify(values.query));
167
+ onQueryChanged?.(query, queueDxn);
168
+ },
169
+ [onQueryChanged, mode, feeds],
170
+ );
171
+
172
+ const handleDelete = useCallback(
173
+ (fieldId: string) => {
174
+ invariant(!readonly);
175
+ onDelete?.(fieldId);
176
+ },
177
+ [onDelete, readonly],
178
+ );
179
+
180
+ return (
181
+ <div role='none' className={mx(classNames)}>
182
+ {/* If readonly is set, then the callout is not needed. */}
183
+ {schemaReadonly && !readonly && (
184
+ <Message.Root valence='info' classNames='my-form-padding'>
185
+ <Message.Title>{t('system schema description')}</Message.Title>
186
+ </Message.Root>
187
+ )}
188
+
189
+ {/* TODO(burdon): Is the form read-only or just the schema? */}
190
+ <Form.Root schema={viewSchema} values={viewValues} fieldMap={fieldMap} db={db} onValuesChanged={handleUpdate}>
191
+ <Form.FieldSet />
192
+
193
+ <FormFieldLabel label={t('fields label')} asChild />
194
+ <FieldList
195
+ schema={schema}
196
+ view={view}
197
+ registry={registry}
198
+ readonly={readonly}
199
+ showHeading={showHeading}
200
+ onDelete={handleDelete}
201
+ />
202
+ </Form.Root>
203
+ </div>
204
+ );
205
+ },
206
+ );
207
+
208
+ type FieldListProps = Pick<ViewEditorProps, 'schema' | 'view' | 'registry' | 'readonly' | 'showHeading' | 'onDelete'>;
209
+
210
+ const FieldList = ({ schema, view, registry, readonly, showHeading = false, onDelete }: FieldListProps) => {
211
+ const atomRegistry = useContext(RegistryContext);
212
+ const schemaReadonly = !isMutable(schema);
57
213
  const { t } = useTranslation(translationKey);
58
- const projection = useMemo(() => {
214
+
215
+ // Subscribe to view changes for reactivity.
216
+ const [viewSnapshot] = useObject(view);
217
+
218
+ const projectionModel = useMemo(() => {
59
219
  // Use reactive and mutable version of json schema when schema is mutable.
60
220
  const jsonSchema = schema instanceof EchoSchema ? schema.jsonSchema : toJsonSchema(schema);
61
- return new ProjectionModel(jsonSchema, view.projection);
62
- }, [schema, view.projection]);
63
- const [field, setField] = useState<FieldType>();
64
- const readonly = _readonly || !isMutable(schema);
65
-
66
- // TODO(burdon): Should be reactive.
67
- const viewValues = useMemo(() => {
68
- return {
69
- // TODO(burdon): Need to warn user of possible consequences of editing.
70
- // TODO(burdon): Settings should have domain name owned by user.
71
- typename: view.query.typename,
72
- };
73
- }, [view.query.typename]);
74
-
75
- const handleSelect = useCallback(
76
- (field: FieldType) => {
77
- if (readonly) {
78
- return;
79
- }
80
- setField((f) => (f === field ? undefined : field));
81
- },
82
- [readonly],
83
- );
84
221
 
85
- // TODO(burdon): Check if mutable; variant of useCallback that return undefined if readonly?
222
+ // Always use createEchoChangeCallback since the view is ECHO-backed.
223
+ // Pass schema only when mutable to allow schema mutations.
224
+ const change = createEchoChangeCallback(view, schema instanceof EchoSchema ? schema : undefined);
225
+
226
+ const model = new ProjectionModel({
227
+ registry: atomRegistry,
228
+ view,
229
+ baseSchema: jsonSchema,
230
+ change,
231
+ });
232
+
233
+ return model;
234
+ }, [atomRegistry, schema, view]);
235
+
236
+ const [expandedField, setExpandedField] = useState<View.FieldType['id']>();
86
237
 
87
238
  const handleAdd = useCallback(() => {
88
239
  invariant(!readonly);
89
- const field = projection.createFieldProjection();
90
- setField(field);
91
- }, [schema, projection, readonly]);
240
+ const field = projectionModel.createFieldProjection();
241
+ setExpandedField(field.id);
242
+ }, [projectionModel, readonly]);
92
243
 
93
- const handleUpdate = useCallback(
94
- ({ typename }: ViewMetaType) => {
95
- invariant(!readonly);
96
- requestAnimationFrame(() => {
97
- if (view.query.typename !== typename && !readonly) {
98
- onTypenameChanged?.(typename);
99
- }
100
- });
244
+ const handleToggleField = useCallback(
245
+ (field: View.FieldType) => {
246
+ setExpandedField((prevExpandedFieldId) => (prevExpandedFieldId === field.id ? undefined : field.id));
101
247
  },
102
- [view.query.typename, onTypenameChanged, readonly],
248
+ [readonly],
103
249
  );
104
250
 
105
251
  const handleDelete = useCallback(
106
252
  (fieldId: string) => {
107
253
  invariant(!readonly);
108
- if (fieldId === field?.id) {
109
- setField(undefined);
254
+ if (fieldId === expandedField) {
255
+ setExpandedField(undefined);
110
256
  }
111
257
 
112
258
  onDelete?.(fieldId);
113
259
  },
114
- [field, onDelete, readonly],
260
+ [expandedField, onDelete, readonly],
115
261
  );
116
262
 
117
263
  const handleMove = useCallback(
118
264
  (fromIndex: number, toIndex: number) => {
119
265
  invariant(!readonly);
120
- // NOTE(ZaymonFC): Using arrayMove here causes a race condition with the kanban model.
121
- const fields = [...view.projection.fields];
122
- const [moved] = fields.splice(fromIndex, 1);
123
- fields.splice(toIndex, 0, moved);
124
- view.projection.fields = fields;
266
+ Obj.change(view, (v) => {
267
+ // NOTE(ZaymonFC): Using arrayMove here causes a race condition with the kanban model.
268
+ const fields = [...v.projection.fields];
269
+ const [moved] = fields.splice(fromIndex, 1);
270
+ fields.splice(toIndex, 0, moved);
271
+ v.projection.fields = fields;
272
+ });
125
273
  },
126
- [view.projection.fields, readonly],
274
+ [view, readonly],
127
275
  );
128
276
 
129
- const handleClose = useCallback(() => setField(undefined), []);
130
-
131
- const hiddenProperties = projection.getHiddenProperties();
277
+ const handleClose = useCallback(() => setExpandedField(undefined), []);
132
278
 
133
279
  const handleHide = useCallback(
134
280
  (fieldId: string) => {
135
- setField(undefined);
136
- projection.hideFieldProjection(fieldId);
281
+ setExpandedField(undefined);
282
+ projectionModel.hideFieldProjection(fieldId);
137
283
  },
138
- [projection],
284
+ [projectionModel],
139
285
  );
140
286
 
141
287
  const handleShow = useCallback(
142
288
  (property: string) => {
143
- setField(undefined);
144
- projection.showFieldProjection(property as JsonProp);
289
+ setExpandedField(undefined);
290
+ projectionModel.showFieldProjection(property as JsonProp);
145
291
  },
146
- [projection],
292
+ [projectionModel],
147
293
  );
148
294
 
149
- return (
150
- <div role='none' className={mx(classNames)}>
151
- {readonly && (
152
- <div role='none' className={mx('is-full', outerSpacing && 'plb-cardSpacingBlock pli-cardSpacingInline')}>
153
- <Callout.Root valence='info'>
154
- <Callout.Title>{t('system schema description')}</Callout.Title>
155
- </Callout.Root>
156
- </div>
157
- )}
295
+ if (!viewSnapshot) {
296
+ return null;
297
+ }
158
298
 
159
- {/* TODO(burdon): Is the form read-only or just the schema? */}
160
- {/* TODO(burdon): Readonly fields should take up the same space as editable fields (just be ghosted). */}
161
- <Form<ViewMetaType>
162
- autoSave
163
- schema={ViewMetaSchema}
164
- values={viewValues}
165
- readonly={readonly}
166
- onSave={handleUpdate}
167
- outerSpacing={outerSpacing}
168
- />
169
-
170
- <div role='none' className={outerSpacing ? cardSpacing : 'mlb-cardSpacingBlock'}>
171
- <label className={mx(inputTextLabel)}>{t('fields label')}</label>
172
-
173
- <List.Root<FieldType>
174
- items={view.projection.fields}
175
- isItem={Schema.is(FieldSchema)}
176
- getId={(field) => field.id}
177
- readonly={readonly}
178
- onMove={readonly ? undefined : handleMove}
179
- >
180
- {({ items: fields }) => (
181
- <>
182
- {showHeading && (
183
- <div role='heading' className={grid}>
184
- <div />
185
- <div className='flex items-center text-sm'>{t('field path label')}</div>
186
- </div>
187
- )}
188
-
189
- <div role='list' className='flex flex-col is-full'>
190
- {fields?.map((field) => (
191
- <List.Item<FieldType>
192
- key={field.id}
193
- item={field}
194
- classNames={mx(grid, ghostHover, 'overflow-hidden', !readonly && 'cursor-pointer')}
299
+ return (
300
+ <List.Root<View.FieldType>
301
+ items={viewSnapshot.projection.fields as View.FieldType[]}
302
+ isItem={Schema.is(View.FieldSchema)}
303
+ getId={(field) => field.id}
304
+ onMove={readonly ? undefined : handleMove}
305
+ readonly={readonly}
306
+ >
307
+ {({ items: fields }) => (
308
+ <>
309
+ {showHeading && <h3 className='text-sm'>{t('field path label')}</h3>}
310
+ <div role='list' className='grid grid-cols-[min-content_1fr_min-content_min-content_min-content]'>
311
+ {fields?.map((field) => {
312
+ const hidden = field.visible === false;
313
+ return (
314
+ <List.Item<View.FieldType>
315
+ key={field.id}
316
+ item={field}
317
+ classNames={'grid grid-cols-subgrid col-span-5'}
318
+ aria-expanded={expandedField === field.id}
319
+ >
320
+ <div
321
+ role='none'
322
+ className={mx(
323
+ subtleHover,
324
+ 'grid grid-cols-subgrid col-span-5',
325
+ 'rounded-xs cursor-pointer min-h-10',
326
+ )}
195
327
  >
196
- <List.ItemDragHandle />
197
- <List.ItemTitle onClick={() => handleSelect(field)}>{field.path}</List.ItemTitle>
328
+ <List.ItemDragHandle disabled={readonly || schemaReadonly} />
329
+ <List.ItemTitle classNames={hidden && 'text-subdued'} onClick={() => handleToggleField(field)}>
330
+ {field.path}
331
+ </List.ItemTitle>
198
332
  <List.ItemButton
199
- data-testid='hide-field-button'
200
- icon='ph--eye-slash--regular'
201
- // TDOO(burdon): Is this the correct test?
202
- disabled={view.projection.fields.length <= 1}
203
- onClick={() => handleHide(field.id)}
333
+ label={t(hidden ? 'show field label' : 'hide field label')}
334
+ data-testid={hidden ? 'show-field-button' : 'hide-field-button'}
335
+ icon={hidden ? 'ph--eye-closed--regular' : 'ph--eye--regular'}
336
+ autoHide={false}
337
+ disabled={readonly || (!hidden && projectionModel.getFields().length <= 1)}
338
+ onClick={() => (hidden ? handleShow(field.path) : handleHide(field.id))}
204
339
  />
205
- {/* TODO(burdon): Remove unless implement undo. */}
206
340
  {!readonly && (
207
- <List.ItemDeleteButton
208
- disabled={view.projection.fields.length <= 1}
209
- onClick={() => handleDelete(field.id)}
210
- />
341
+ <>
342
+ <List.ItemDeleteButton
343
+ label={t('delete field label')}
344
+ autoHide={false}
345
+ disabled={readonly || schemaReadonly || viewSnapshot.projection.fields.length <= 1}
346
+ onClick={() => handleDelete(field.id)}
347
+ data-testid='field.delete'
348
+ />
349
+ <IconButton
350
+ iconOnly
351
+ variant='ghost'
352
+ label={t('toggle expand label', { ns: osTranslations })}
353
+ icon={expandedField === field.id ? 'ph--caret-down--regular' : 'ph--caret-right--regular'}
354
+ onClick={() => handleToggleField(field)}
355
+ data-testid='field.toggle'
356
+ />
357
+ </>
211
358
  )}
212
- </List.Item>
213
- ))}
214
- </div>
215
- </>
216
- )}
217
- </List.Root>
218
-
219
- {hiddenProperties.length > 0 && (
220
- <>
221
- <label className={mx(inputTextLabel)}>{t('hidden fields label')}</label>
222
-
223
- <List.Root<string>
224
- items={hiddenProperties}
225
- isItem={(item): item is string => typeof item === 'string'}
226
- getId={(property) => property}
227
- >
228
- {({ items: properties }) => (
229
- <div role='list' className='flex flex-col is-full'>
230
- {properties?.map((property) => (
231
- <List.Item<string>
232
- key={property}
233
- item={property}
234
- classNames={mx(grid, ghostHover, !readonly && 'cursor-pointer')}
235
- >
236
- <List.ItemDragHandle disabled />
237
- <List.ItemTitle>{property}</List.ItemTitle>
238
- <List.ItemButton
239
- icon='ph--eye--regular'
240
- onClick={() => handleShow(property)}
241
- data-testid='show-field-button'
359
+ </div>
360
+ {expandedField === field.id && !readonly && (
361
+ <div role='none' className='col-span-5 mt-1 mb-1 border border-separator rounded-md'>
362
+ <FieldEditor
363
+ readonly={readonly || schemaReadonly}
364
+ registry={registry}
365
+ projection={projectionModel}
366
+ field={field}
367
+ onSave={handleClose}
242
368
  />
243
- </List.Item>
244
- ))}
245
- </div>
246
- )}
247
- </List.Root>
248
- </>
249
- )}
250
- </div>
251
-
252
- {field && (
253
- <FieldEditor key={field.id} projection={projection} field={field} registry={registry} onSave={handleClose} />
254
- )}
255
-
256
- {!readonly && !field && (
257
- <div role='none' className={outerSpacing ? cardSpacing : 'mlb-cardSpacingBlock'}>
258
- <IconButton
259
- icon='ph--plus--regular'
260
- label={t('button add property')}
261
- onClick={readonly ? undefined : handleAdd}
262
- // TODO(burdon): Show field limit in ux (not tooltip).
263
- disabled={view.projection.fields.length >= VIEW_FIELD_LIMIT}
264
- classNames='flex is-full'
265
- />
266
- </div>
369
+ </div>
370
+ )}
371
+ </List.Item>
372
+ );
373
+ })}
374
+ </div>
375
+ {!readonly && !expandedField && (
376
+ <div role='none' className='my-form-padding'>
377
+ <IconButton
378
+ icon='ph--plus--regular'
379
+ label={t('add property button label')}
380
+ onClick={handleAdd}
381
+ disabled={viewSnapshot.projection.fields.length >= VIEW_FIELD_LIMIT}
382
+ classNames='w-full'
383
+ />
384
+ </div>
385
+ )}
386
+ </>
267
387
  )}
268
- </div>
388
+ </List.Root>
269
389
  );
270
390
  };
391
+
392
+ const customFields = ({
393
+ types,
394
+ tags,
395
+ }: Pick<ViewEditorProps, 'types' | 'tags'>): Record<string, FormFieldComponent> => ({
396
+ query: ({ type, readonly, label, getValue, onValueChange }: FormFieldComponentProps) => {
397
+ const handleChange = useCallback<NonNullable<QueryFormProps['onChange']>>(
398
+ (query) => onValueChange(type, query.ast),
399
+ [onValueChange],
400
+ );
401
+
402
+ return (
403
+ <Input.Root>
404
+ <FormFieldLabel readonly={readonly} label={label} />
405
+ <QueryForm initialQuery={getValue()} types={types} tags={tags} onChange={handleChange} />
406
+ </Input.Root>
407
+ );
408
+ },
409
+ });
@@ -2,9 +2,8 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './deprecated';
6
-
7
- export * from './ControlSection';
8
5
  export * from './FieldEditor';
9
6
  export * from './Form';
7
+ export * from './ObjectPicker';
8
+ export * from './Settings';
10
9
  export * from './ViewEditor';
@@ -5,20 +5,31 @@
5
5
  import React, { type PropsWithChildren } from 'react';
6
6
 
7
7
  import { type ThemedClassName } from '@dxos/react-ui';
8
- import { Card } from '@dxos/react-ui-stack';
9
8
  import { JsonFilter } from '@dxos/react-ui-syntax-highlighter';
10
- import { mx, textBlockWidth } from '@dxos/react-ui-theme';
9
+ import { mx, textBlockWidth } from '@dxos/ui-theme';
11
10
 
12
- export const TestLayout = ({ classNames, json, children }: ThemedClassName<PropsWithChildren<{ json?: any }>>) => (
13
- <div className='w-full h-full grid grid-cols-[1fr_1fr] gap-2 overflow-hidden'>
14
- <div className={mx('p-4 justify-center overflow-y-auto', classNames)}>{children}</div>
15
- <JsonFilter data={json} classNames='w-full text-xs' testId='debug' />
16
- </div>
17
- );
11
+ type TestLayoutProps = ThemedClassName<PropsWithChildren<{ json?: any }>>;
18
12
 
19
- export const TestPanel = ({ classNames, children }: ThemedClassName<PropsWithChildren>) => (
20
- <Card.StaticRoot classNames={[textBlockWidth, classNames]}>{children}</Card.StaticRoot>
21
- );
13
+ export const TestLayout = ({ classNames, children, json }: TestLayoutProps) => {
14
+ return (
15
+ <div className='w-full h-full grid grid-cols-[1fr_1fr] p-4 gap-4 overflow-hidden'>
16
+ <TestPanel classNames={['flex flex-col h-full overflow-y-auto', classNames]}>{children}</TestPanel>
17
+ <TestPanel classNames={'overflow-hidden'}>
18
+ <JsonFilter testId='debug' data={json} classNames='h-full text-xs' />
19
+ </TestPanel>
20
+ </div>
21
+ );
22
+ };
23
+
24
+ type TestPanelProps = ThemedClassName<PropsWithChildren>;
25
+
26
+ const TestPanel = ({ classNames, children }: TestPanelProps) => {
27
+ return (
28
+ <div role='none' className={mx(['h-full bg-modal-surface rounded-md', textBlockWidth, classNames])}>
29
+ {children}
30
+ </div>
31
+ );
32
+ };
22
33
 
23
34
  // Symbol for accessing debug objects in tests.
24
35
  export const VIEW_EDITOR_DEBUG_SYMBOL = Symbol('viewEditorDebug');