@dxos/react-ui-form 0.8.4-main.5ea62a8 → 0.8.4-main.66e292d
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.
- package/dist/lib/browser/index.mjs +534 -348
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +534 -348
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +8 -7
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +8 -7
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +6 -5
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +52 -37
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormActions.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormContent.d.ts +5 -5
- package/dist/types/src/components/Form/FormContent.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormContext.d.ts +4 -4
- package/dist/types/src/components/Form/FormContext.d.ts.map +1 -1
- package/dist/types/src/components/Form/Input.d.ts +2 -2
- package/dist/types/src/components/Form/Input.d.ts.map +1 -1
- package/dist/types/src/components/Form/RefField.d.ts +10 -6
- package/dist/types/src/components/Form/RefField.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +8 -7
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/factory.d.ts +2 -2
- package/dist/types/src/components/Form/factory.d.ts.map +1 -1
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +41 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +82 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/index.d.ts +2 -0
- package/dist/types/src/components/ObjectPicker/index.d.ts.map +1 -0
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +20 -6
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +13 -11
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useForm.d.ts +5 -5
- package/dist/types/src/hooks/useForm.d.ts.map +1 -1
- package/dist/types/src/hooks/useQueryRefOptions.d.ts +9 -3
- package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +1 -1
- package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +7 -6
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/util/field.d.ts +3 -3
- package/dist/types/src/util/field.d.ts.map +1 -1
- package/dist/types/src/util/formatting.d.ts +2 -2
- package/dist/types/src/util/formatting.d.ts.map +1 -1
- package/dist/types/src/util/refs.d.ts +1 -1
- package/dist/types/src/util/refs.d.ts.map +1 -1
- package/dist/types/src/util/schema.d.ts +1 -1
- package/dist/types/src/util/schema.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +40 -37
- package/src/components/ControlSection/ControlSection.stories.tsx +4 -5
- package/src/components/ControlSection/ControlSection.tsx +1 -1
- package/src/components/FieldEditor/FieldEditor.stories.tsx +8 -7
- package/src/components/FieldEditor/FieldEditor.tsx +7 -4
- package/src/components/Form/ArrayField.tsx +7 -4
- package/src/components/Form/Defaults.tsx +1 -1
- package/src/components/Form/Form.stories.tsx +52 -52
- package/src/components/Form/Form.tsx +19 -9
- package/src/components/Form/FormActions.tsx +7 -2
- package/src/components/Form/FormContent.tsx +83 -23
- package/src/components/Form/FormContext.tsx +6 -6
- package/src/components/Form/Input.tsx +2 -2
- package/src/components/Form/RefField.tsx +50 -66
- package/src/components/Form/custom/GeoPointInput.tsx +1 -1
- package/src/components/Form/custom/SelectOptionsInput.stories.tsx +5 -6
- package/src/components/Form/custom/SelectOptionsInput.tsx +2 -2
- package/src/components/Form/factory.tsx +7 -4
- package/src/components/ObjectPicker/ObjectPicker.stories.tsx +207 -0
- package/src/components/ObjectPicker/ObjectPicker.tsx +161 -0
- package/src/components/ObjectPicker/index.ts +5 -0
- package/src/components/ViewEditor/ViewEditor.stories.tsx +112 -49
- package/src/components/ViewEditor/ViewEditor.test.tsx +6 -5
- package/src/components/ViewEditor/ViewEditor.tsx +277 -208
- package/src/components/index.ts +1 -0
- package/src/components/testing.tsx +2 -2
- package/src/hooks/useForm.ts +11 -8
- package/src/hooks/useQueryRefOptions.ts +15 -6
- package/src/hooks/useRefQueryLookupHandler.ts +5 -5
- package/src/translations.ts +7 -6
- package/src/util/field.ts +25 -25
- package/src/util/formatting.ts +22 -22
- package/src/util/refs.ts +7 -7
- package/src/util/schema.ts +2 -4
|
@@ -2,252 +2,321 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Array from 'effect/Array';
|
|
6
|
+
import * as Match from 'effect/Match';
|
|
7
|
+
import * as Option from 'effect/Option';
|
|
8
|
+
import * as Schema from 'effect/Schema';
|
|
9
|
+
import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useState } from 'react';
|
|
7
10
|
|
|
8
|
-
import {
|
|
9
|
-
import { EchoSchema, Format, type JsonProp, isMutable, toJsonSchema } from '@dxos/echo
|
|
11
|
+
import { QueryAST } from '@dxos/echo';
|
|
12
|
+
import { EchoSchema, Format, type JsonProp, isMutable, toJsonSchema } from '@dxos/echo/internal';
|
|
13
|
+
import { Filter, Query, type SchemaRegistry } from '@dxos/echo-db';
|
|
10
14
|
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import { Callout, IconButton, type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
15
|
+
import { Callout, IconButton, Input, type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
16
|
+
import { QueryForm, type QueryFormProps } from '@dxos/react-ui-components';
|
|
12
17
|
import { List } from '@dxos/react-ui-list';
|
|
13
18
|
import { cardSpacing } from '@dxos/react-ui-stack';
|
|
14
19
|
import { inputTextLabel, mx, subtleHover } from '@dxos/react-ui-theme';
|
|
15
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
FieldSchema,
|
|
22
|
+
type FieldType,
|
|
23
|
+
ProjectionModel,
|
|
24
|
+
VIEW_FIELD_LIMIT,
|
|
25
|
+
type View,
|
|
26
|
+
getTypenameFromQuery,
|
|
27
|
+
} from '@dxos/schema';
|
|
16
28
|
|
|
17
29
|
import { translationKey } from '../../translations';
|
|
18
30
|
import { FieldEditor } from '../FieldEditor';
|
|
19
|
-
import { Form, type FormProps } from '../Form';
|
|
31
|
+
import { Form, type FormProps, type InputComponent, InputHeader, type InputProps } from '../Form';
|
|
20
32
|
|
|
21
33
|
const listGrid = 'grid grid-cols-[min-content_1fr_min-content_min-content_min-content]';
|
|
22
34
|
const listItemGrid = 'grid grid-cols-subgrid col-span-5';
|
|
23
35
|
|
|
24
|
-
const ViewMetaSchema = Schema.Struct({
|
|
25
|
-
typename: Format.URL.annotations({
|
|
26
|
-
title: 'Record type',
|
|
27
|
-
}),
|
|
28
|
-
}).pipe(Schema.mutable);
|
|
29
|
-
|
|
30
|
-
type ViewMetaType = Schema.Schema.Type<typeof ViewMetaSchema>;
|
|
31
|
-
|
|
32
36
|
export type ViewEditorProps = ThemedClassName<
|
|
33
37
|
{
|
|
34
38
|
schema: Schema.Schema.AnyNoContext;
|
|
35
|
-
view:
|
|
39
|
+
view: View.View;
|
|
40
|
+
mode?: 'schema' | 'tag';
|
|
36
41
|
registry?: SchemaRegistry;
|
|
37
42
|
readonly?: boolean;
|
|
38
43
|
showHeading?: boolean;
|
|
39
|
-
|
|
44
|
+
onQueryChanged?: (query: QueryAST.Query, target?: string) => void;
|
|
40
45
|
onDelete?: (fieldId: string) => void;
|
|
41
|
-
} & Pick<FormProps<any>, 'outerSpacing'>
|
|
46
|
+
} & Pick<FormProps<any>, 'outerSpacing'> &
|
|
47
|
+
Pick<QueryFormProps, 'types' | 'tags'>
|
|
42
48
|
>;
|
|
43
49
|
|
|
44
50
|
/**
|
|
45
51
|
* Schema-based object form.
|
|
46
52
|
*/
|
|
47
|
-
export const ViewEditor = (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const jsonSchema = schema instanceof EchoSchema ? schema.jsonSchema : toJsonSchema(schema);
|
|
63
|
-
return new ProjectionModel(jsonSchema, view.projection);
|
|
64
|
-
}, [schema, view.projection]);
|
|
65
|
-
const [expandedField, setExpandedField] = useState<FieldType['id']>();
|
|
66
|
-
|
|
67
|
-
// TODO(burdon): Should be reactive.
|
|
68
|
-
const viewValues = useMemo(() => {
|
|
69
|
-
return {
|
|
70
|
-
// TODO(burdon): Need to warn user of possible consequences of editing.
|
|
71
|
-
// TODO(burdon): Settings should have domain name owned by user.
|
|
72
|
-
typename: view.query.typename,
|
|
73
|
-
};
|
|
74
|
-
}, [view.query.typename]);
|
|
75
|
-
|
|
76
|
-
const handleToggleField = useCallback(
|
|
77
|
-
(field: FieldType) => {
|
|
78
|
-
setExpandedField((prevExpandedFieldId) => (prevExpandedFieldId === field.id ? undefined : field.id));
|
|
53
|
+
export const ViewEditor = forwardRef<ProjectionModel, ViewEditorProps>(
|
|
54
|
+
(
|
|
55
|
+
{
|
|
56
|
+
classNames,
|
|
57
|
+
schema,
|
|
58
|
+
view,
|
|
59
|
+
mode = 'schema',
|
|
60
|
+
registry,
|
|
61
|
+
readonly,
|
|
62
|
+
showHeading = false,
|
|
63
|
+
types,
|
|
64
|
+
tags,
|
|
65
|
+
onQueryChanged,
|
|
66
|
+
onDelete,
|
|
67
|
+
outerSpacing = true,
|
|
79
68
|
},
|
|
80
|
-
|
|
81
|
-
)
|
|
69
|
+
forwardedRef,
|
|
70
|
+
) => {
|
|
71
|
+
const schemaReadonly = !isMutable(schema);
|
|
72
|
+
const { t } = useTranslation(translationKey);
|
|
73
|
+
const projectionModel = useMemo(() => {
|
|
74
|
+
// Use reactive and mutable version of json schema when schema is mutable.
|
|
75
|
+
const jsonSchema = schema instanceof EchoSchema ? schema.jsonSchema : toJsonSchema(schema);
|
|
76
|
+
return new ProjectionModel(jsonSchema, view.projection);
|
|
77
|
+
}, [schema, JSON.stringify(view.projection)]);
|
|
78
|
+
useImperativeHandle(forwardedRef, () => projectionModel, [projectionModel]);
|
|
79
|
+
const [expandedField, setExpandedField] = useState<FieldType['id']>();
|
|
80
|
+
|
|
81
|
+
const queueTarget = Match.value(view.query.ast).pipe(
|
|
82
|
+
Match.when({ type: 'options' }, ({ options }) => {
|
|
83
|
+
return Option.fromNullable(options.queues).pipe(
|
|
84
|
+
Option.flatMap((queues) => Array.head(queues)),
|
|
85
|
+
Option.getOrUndefined,
|
|
86
|
+
);
|
|
87
|
+
}),
|
|
88
|
+
Match.orElse(() => undefined),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const viewSchema = useMemo(() => {
|
|
92
|
+
const base = Schema.Struct({
|
|
93
|
+
query:
|
|
94
|
+
mode === 'schema'
|
|
95
|
+
? Format.URL.annotations({ title: 'Record type' })
|
|
96
|
+
: QueryAST.Query.annotations({ title: 'Query' }),
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (mode === 'tag') {
|
|
100
|
+
return Schema.Struct({
|
|
101
|
+
...base.fields,
|
|
102
|
+
target: Schema.optional(Schema.String.annotations({ title: 'Target Queue' })),
|
|
103
|
+
}).pipe(Schema.mutable);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return base.pipe(Schema.mutable);
|
|
107
|
+
}, [mode]);
|
|
82
108
|
|
|
83
|
-
|
|
109
|
+
// TODO(burdon): Need to warn user of possible consequences of editing.
|
|
110
|
+
// TODO(burdon): Settings should have domain name owned by user.
|
|
111
|
+
const viewValues = useMemo(
|
|
112
|
+
() => ({
|
|
113
|
+
query: mode === 'schema' ? getTypenameFromQuery(view.query.ast) : view.query.ast,
|
|
114
|
+
target: queueTarget,
|
|
115
|
+
}),
|
|
116
|
+
[mode, view.query.ast, queueTarget],
|
|
117
|
+
);
|
|
84
118
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
119
|
+
const handleToggleField = useCallback(
|
|
120
|
+
(field: FieldType) => {
|
|
121
|
+
setExpandedField((prevExpandedFieldId) => (prevExpandedFieldId === field.id ? undefined : field.id));
|
|
122
|
+
},
|
|
123
|
+
[readonly],
|
|
124
|
+
);
|
|
90
125
|
|
|
91
|
-
|
|
92
|
-
|
|
126
|
+
// TODO(burdon): Check if mutable; variant of useCallback that return undefined if readonly?
|
|
127
|
+
|
|
128
|
+
const handleAdd = useCallback(() => {
|
|
93
129
|
invariant(!readonly);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
130
|
+
const field = projectionModel.createFieldProjection();
|
|
131
|
+
setExpandedField(field.id);
|
|
132
|
+
}, [schema, projectionModel, readonly]);
|
|
133
|
+
|
|
134
|
+
const handleUpdate = useCallback(
|
|
135
|
+
(values: any) => {
|
|
136
|
+
requestAnimationFrame(() => {
|
|
137
|
+
const query = mode === 'schema' ? Query.select(Filter.typename(values.query)).ast : values.query;
|
|
138
|
+
onQueryChanged?.(query, values.target);
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
[onQueryChanged, view, queueTarget, mode],
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const handleDelete = useCallback(
|
|
145
|
+
(fieldId: string) => {
|
|
146
|
+
invariant(!readonly);
|
|
147
|
+
if (fieldId === expandedField) {
|
|
148
|
+
setExpandedField(undefined);
|
|
97
149
|
}
|
|
98
|
-
});
|
|
99
|
-
},
|
|
100
|
-
[view.query.typename, onTypenameChanged, readonly],
|
|
101
|
-
);
|
|
102
150
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
151
|
+
onDelete?.(fieldId);
|
|
152
|
+
},
|
|
153
|
+
[expandedField, onDelete, readonly],
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const handleMove = useCallback(
|
|
157
|
+
(fromIndex: number, toIndex: number) => {
|
|
158
|
+
invariant(!readonly);
|
|
159
|
+
// NOTE(ZaymonFC): Using arrayMove here causes a race condition with the kanban model.
|
|
160
|
+
const fields = [...view.projection.fields];
|
|
161
|
+
const [moved] = fields.splice(fromIndex, 1);
|
|
162
|
+
fields.splice(toIndex, 0, moved);
|
|
163
|
+
view.projection.fields = fields;
|
|
164
|
+
},
|
|
165
|
+
[view.projection.fields, readonly],
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const handleClose = useCallback(() => setExpandedField(undefined), []);
|
|
169
|
+
|
|
170
|
+
const handleHide = useCallback(
|
|
171
|
+
(fieldId: string) => {
|
|
107
172
|
setExpandedField(undefined);
|
|
108
|
-
|
|
173
|
+
projectionModel.hideFieldProjection(fieldId);
|
|
174
|
+
},
|
|
175
|
+
[projectionModel],
|
|
176
|
+
);
|
|
109
177
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
178
|
+
const handleShow = useCallback(
|
|
179
|
+
(property: string) => {
|
|
180
|
+
setExpandedField(undefined);
|
|
181
|
+
projectionModel.showFieldProjection(property as JsonProp);
|
|
182
|
+
},
|
|
183
|
+
[projectionModel],
|
|
184
|
+
);
|
|
114
185
|
|
|
115
|
-
|
|
116
|
-
(fromIndex: number, toIndex: number) => {
|
|
117
|
-
invariant(!readonly);
|
|
118
|
-
// NOTE(ZaymonFC): Using arrayMove here causes a race condition with the kanban model.
|
|
119
|
-
const fields = [...view.projection.fields];
|
|
120
|
-
const [moved] = fields.splice(fromIndex, 1);
|
|
121
|
-
fields.splice(toIndex, 0, moved);
|
|
122
|
-
view.projection.fields = fields;
|
|
123
|
-
},
|
|
124
|
-
[view.projection.fields, readonly],
|
|
125
|
-
);
|
|
186
|
+
const custom = useMemo(() => (mode === 'tag' ? customFields({ types, tags }) : undefined), [types, tags, mode]);
|
|
126
187
|
|
|
127
|
-
|
|
188
|
+
return (
|
|
189
|
+
<div role='none' className={mx(classNames)}>
|
|
190
|
+
{/* If readonlyProp is set, then the callout is not needed. */}
|
|
191
|
+
{schemaReadonly && !readonly && (
|
|
192
|
+
<Callout.Root valence='info' classNames={['mlb-cardSpacingBlock', outerSpacing && 'mli-cardSpacingInline']}>
|
|
193
|
+
<Callout.Title>{t('system schema description')}</Callout.Title>
|
|
194
|
+
</Callout.Root>
|
|
195
|
+
)}
|
|
128
196
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
197
|
+
{/* TODO(burdon): Is the form read-only or just the schema? */}
|
|
198
|
+
{/* TODO(burdon): Readonly fields should take up the same space as editable fields (just be ghosted). */}
|
|
199
|
+
<Form<Schema.Schema.Type<typeof viewSchema>>
|
|
200
|
+
autoSave
|
|
201
|
+
schema={viewSchema}
|
|
202
|
+
values={viewValues}
|
|
203
|
+
onSave={handleUpdate}
|
|
204
|
+
outerSpacing={outerSpacing}
|
|
205
|
+
Custom={custom}
|
|
206
|
+
/>
|
|
136
207
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
<>
|
|
179
|
-
{showHeading && <h3 className='text-sm'>{t('field path label')}</h3>}
|
|
180
|
-
<div role='list' className={listGrid}>
|
|
181
|
-
{fields?.map((field) => {
|
|
182
|
-
const hidden = field.visible === false;
|
|
183
|
-
return (
|
|
184
|
-
<List.Item<FieldType>
|
|
185
|
-
key={field.id}
|
|
186
|
-
item={field}
|
|
187
|
-
classNames={listItemGrid}
|
|
188
|
-
aria-expanded={expandedField === field.id}
|
|
189
|
-
>
|
|
190
|
-
<div role='none' className={mx(subtleHover, listItemGrid, 'rounded-sm cursor-pointer min-bs-10')}>
|
|
191
|
-
<List.ItemDragHandle disabled={readonly} />
|
|
192
|
-
<List.ItemTitle classNames={hidden && 'text-subdued'} onClick={() => handleToggleField(field)}>
|
|
193
|
-
{field.path}
|
|
194
|
-
</List.ItemTitle>
|
|
195
|
-
<List.ItemButton
|
|
196
|
-
label={t(hidden ? 'show field label' : 'hide field label')}
|
|
197
|
-
data-testid={hidden ? 'show-field-button' : 'hide-field-button'}
|
|
198
|
-
icon={hidden ? 'ph--eye-closed--regular' : 'ph--eye--regular'}
|
|
199
|
-
autoHide={false}
|
|
200
|
-
disabled={readonly || (!hidden && projectionModel.fields.length <= 1)}
|
|
201
|
-
onClick={() => (hidden ? handleShow(field.path) : handleHide(field.id))}
|
|
202
|
-
/>
|
|
203
|
-
{/* TODO(burdon): Remove unless implement undo. */}
|
|
204
|
-
<List.ItemDeleteButton
|
|
205
|
-
label={t('delete field label')}
|
|
206
|
-
autoHide={false}
|
|
207
|
-
disabled={readonly || schemaReadonly || view.projection.fields.length <= 1}
|
|
208
|
-
onClick={() => handleDelete(field.id)}
|
|
209
|
-
data-testid='field.delete'
|
|
210
|
-
/>
|
|
211
|
-
<IconButton
|
|
212
|
-
iconOnly
|
|
213
|
-
variant='ghost'
|
|
214
|
-
label={t('toggle expand label', { ns: 'os' })}
|
|
215
|
-
icon={expandedField === field.id ? 'ph--caret-down--regular' : 'ph--caret-right--regular'}
|
|
216
|
-
onClick={() => handleToggleField(field)}
|
|
217
|
-
/>
|
|
218
|
-
</div>
|
|
219
|
-
{expandedField === field.id && (
|
|
220
|
-
<div role='none' className='col-span-5 mbs-1 mbe-1 border border-separator rounded-md'>
|
|
221
|
-
<FieldEditor
|
|
222
|
-
readonly={readonly || schemaReadonly ? 'disabled-input' : false}
|
|
223
|
-
projection={projectionModel}
|
|
224
|
-
field={field}
|
|
225
|
-
registry={registry}
|
|
226
|
-
onSave={handleClose}
|
|
208
|
+
<div role='none' className={outerSpacing ? cardSpacing : 'mlb-cardSpacingBlock'}>
|
|
209
|
+
<h2 className={mx(inputTextLabel)}>{t('fields label')}</h2>
|
|
210
|
+
|
|
211
|
+
<List.Root<FieldType>
|
|
212
|
+
items={view.projection.fields}
|
|
213
|
+
isItem={Schema.is(FieldSchema)}
|
|
214
|
+
getId={(field) => field.id}
|
|
215
|
+
onMove={readonly ? undefined : handleMove}
|
|
216
|
+
readonly={readonly}
|
|
217
|
+
>
|
|
218
|
+
{({ items: fields }) => (
|
|
219
|
+
<>
|
|
220
|
+
{showHeading && <h3 className='text-sm'>{t('field path label')}</h3>}
|
|
221
|
+
<div role='list' className={listGrid}>
|
|
222
|
+
{fields?.map((field) => {
|
|
223
|
+
const hidden = field.visible === false;
|
|
224
|
+
return (
|
|
225
|
+
<List.Item<FieldType>
|
|
226
|
+
key={field.id}
|
|
227
|
+
item={field}
|
|
228
|
+
classNames={listItemGrid}
|
|
229
|
+
aria-expanded={expandedField === field.id}
|
|
230
|
+
>
|
|
231
|
+
<div
|
|
232
|
+
role='none'
|
|
233
|
+
className={mx(subtleHover, listItemGrid, 'rounded-sm cursor-pointer min-bs-10')}
|
|
234
|
+
>
|
|
235
|
+
<List.ItemDragHandle disabled={readonly || schemaReadonly} />
|
|
236
|
+
<List.ItemTitle
|
|
237
|
+
classNames={hidden && 'text-subdued'}
|
|
238
|
+
onClick={() => handleToggleField(field)}
|
|
239
|
+
>
|
|
240
|
+
{field.path}
|
|
241
|
+
</List.ItemTitle>
|
|
242
|
+
<List.ItemButton
|
|
243
|
+
label={t(hidden ? 'show field label' : 'hide field label')}
|
|
244
|
+
data-testid={hidden ? 'show-field-button' : 'hide-field-button'}
|
|
245
|
+
icon={hidden ? 'ph--eye-closed--regular' : 'ph--eye--regular'}
|
|
246
|
+
autoHide={false}
|
|
247
|
+
disabled={readonly || (!hidden && projectionModel.fields.length <= 1)}
|
|
248
|
+
onClick={() => (hidden ? handleShow(field.path) : handleHide(field.id))}
|
|
227
249
|
/>
|
|
250
|
+
{!readonly && (
|
|
251
|
+
<>
|
|
252
|
+
<List.ItemDeleteButton
|
|
253
|
+
label={t('delete field label')}
|
|
254
|
+
autoHide={false}
|
|
255
|
+
disabled={readonly || schemaReadonly || view.projection.fields.length <= 1}
|
|
256
|
+
onClick={() => handleDelete(field.id)}
|
|
257
|
+
data-testid='field.delete'
|
|
258
|
+
/>
|
|
259
|
+
<IconButton
|
|
260
|
+
iconOnly
|
|
261
|
+
variant='ghost'
|
|
262
|
+
label={t('toggle expand label', { ns: 'os' })}
|
|
263
|
+
icon={
|
|
264
|
+
expandedField === field.id ? 'ph--caret-down--regular' : 'ph--caret-right--regular'
|
|
265
|
+
}
|
|
266
|
+
onClick={() => handleToggleField(field)}
|
|
267
|
+
/>
|
|
268
|
+
</>
|
|
269
|
+
)}
|
|
228
270
|
</div>
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
271
|
+
{expandedField === field.id && !readonly && (
|
|
272
|
+
<div role='none' className='col-span-5 mbs-1 mbe-1 border border-separator rounded-md'>
|
|
273
|
+
<FieldEditor
|
|
274
|
+
readonly={readonly || schemaReadonly ? 'disabled-input' : false}
|
|
275
|
+
projection={projectionModel}
|
|
276
|
+
field={field}
|
|
277
|
+
registry={registry}
|
|
278
|
+
onSave={handleClose}
|
|
279
|
+
/>
|
|
280
|
+
</div>
|
|
281
|
+
)}
|
|
282
|
+
</List.Item>
|
|
283
|
+
);
|
|
284
|
+
})}
|
|
285
|
+
</div>
|
|
286
|
+
</>
|
|
287
|
+
)}
|
|
288
|
+
</List.Root>
|
|
289
|
+
</div>
|
|
290
|
+
|
|
291
|
+
{!readonly && !expandedField && (
|
|
292
|
+
<div role='none' className={outerSpacing ? cardSpacing : 'mlb-cardSpacingBlock'}>
|
|
293
|
+
<IconButton
|
|
294
|
+
icon='ph--plus--regular'
|
|
295
|
+
label={t('add property button label')}
|
|
296
|
+
onClick={readonly ? undefined : handleAdd}
|
|
297
|
+
// TODO(burdon): Show field limit in ux (not tooltip).
|
|
298
|
+
disabled={view.projection.fields.length >= VIEW_FIELD_LIMIT}
|
|
299
|
+
classNames='is-full'
|
|
300
|
+
/>
|
|
301
|
+
</div>
|
|
302
|
+
)}
|
|
237
303
|
</div>
|
|
304
|
+
);
|
|
305
|
+
},
|
|
306
|
+
);
|
|
238
307
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
};
|
|
308
|
+
const customFields = ({ types, tags }: Pick<ViewEditorProps, 'types' | 'tags'>): Record<string, InputComponent> => ({
|
|
309
|
+
query: (props: InputProps) => {
|
|
310
|
+
const handleChange = useCallback(
|
|
311
|
+
(query: Query.Any) => props.onValueChange('object', query.ast),
|
|
312
|
+
[props.onValueChange],
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
return (
|
|
316
|
+
<Input.Root>
|
|
317
|
+
<InputHeader label={props.label} />
|
|
318
|
+
<QueryForm initialQuery={props.getValue()} types={types} tags={tags} onChange={handleChange} />
|
|
319
|
+
</Input.Root>
|
|
320
|
+
);
|
|
321
|
+
},
|
|
322
|
+
});
|
package/src/components/index.ts
CHANGED
|
@@ -13,9 +13,9 @@ type TestLayoutProps = ThemedClassName<PropsWithChildren<{ json?: any }>>;
|
|
|
13
13
|
|
|
14
14
|
export const TestLayout = ({ classNames, children, json }: TestLayoutProps) => {
|
|
15
15
|
return (
|
|
16
|
-
<div className='
|
|
16
|
+
<div className='is-full bs-full grid grid-cols-[1fr_1fr] gap-2 overflow-hidden'>
|
|
17
17
|
<div className={mx('p-4 justify-center overflow-y-auto', classNames)}>{children}</div>
|
|
18
|
-
<JsonFilter data={json} classNames='
|
|
18
|
+
<JsonFilter data={json} classNames='is-full text-xs' testId='debug' />
|
|
19
19
|
</div>
|
|
20
20
|
);
|
|
21
21
|
};
|
package/src/hooks/useForm.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import type * as Schema from 'effect/Schema';
|
|
6
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
7
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
7
8
|
|
|
8
|
-
import { type
|
|
9
|
+
import { type AnyProperties, getValue, setValue } from '@dxos/echo/internal';
|
|
9
10
|
import { type JsonPath, type SimpleType, createJsonPath, fromEffectValidationPath } from '@dxos/effect';
|
|
10
11
|
import { invariant } from '@dxos/invariant';
|
|
11
12
|
import { log } from '@dxos/log';
|
|
@@ -15,7 +16,7 @@ import { type MaybePromise } from '@dxos/util';
|
|
|
15
16
|
/**
|
|
16
17
|
* Return type from `useForm` hook.
|
|
17
18
|
*/
|
|
18
|
-
export type FormHandler<T extends
|
|
19
|
+
export type FormHandler<T extends AnyProperties> = {
|
|
19
20
|
//
|
|
20
21
|
// Form state management.
|
|
21
22
|
//
|
|
@@ -43,7 +44,7 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
43
44
|
/**
|
|
44
45
|
* Hook options.
|
|
45
46
|
*/
|
|
46
|
-
export interface FormOptions<T extends
|
|
47
|
+
export interface FormOptions<T extends AnyProperties> {
|
|
47
48
|
/**
|
|
48
49
|
* Effect schema (Type literal).
|
|
49
50
|
*/
|
|
@@ -84,7 +85,7 @@ export interface FormOptions<T extends BaseObject> {
|
|
|
84
85
|
* Creates a hook for managing form state, including values, validation, and submission.
|
|
85
86
|
* Deeply integrated with `@dxos/schema` for schema-based validation.
|
|
86
87
|
*/
|
|
87
|
-
export const useForm = <T extends
|
|
88
|
+
export const useForm = <T extends AnyProperties>({
|
|
88
89
|
schema,
|
|
89
90
|
initialValues,
|
|
90
91
|
onValuesChanged,
|
|
@@ -203,6 +204,7 @@ export const useForm = <T extends BaseObject>({
|
|
|
203
204
|
[values],
|
|
204
205
|
);
|
|
205
206
|
|
|
207
|
+
// TODO(wittjosiah): This is causing the entire form to re-render on every change.
|
|
206
208
|
const onValueChange = useCallback<FormHandler<T>['onValueChange']>(
|
|
207
209
|
(path: (string | number)[], type: SimpleType, value: any) => {
|
|
208
210
|
const jsonPath = createJsonPath(path);
|
|
@@ -218,12 +220,13 @@ export const useForm = <T extends BaseObject>({
|
|
|
218
220
|
|
|
219
221
|
const newValues = { ...setValue(values, jsonPath, parsedValue) };
|
|
220
222
|
setValues(newValues);
|
|
221
|
-
|
|
223
|
+
const newChanged = { ...changed, [jsonPath]: true };
|
|
224
|
+
setChanged(newChanged);
|
|
222
225
|
onValuesChanged?.(newValues);
|
|
223
226
|
|
|
224
227
|
const isValid = validate(newValues);
|
|
225
228
|
if (isValid && onValid) {
|
|
226
|
-
onValid(newValues, { changed });
|
|
229
|
+
onValid(newValues, { changed: newChanged });
|
|
227
230
|
}
|
|
228
231
|
},
|
|
229
232
|
[values, onValuesChanged, validate, onValid, changed],
|
|
@@ -273,7 +276,7 @@ export const useForm = <T extends BaseObject>({
|
|
|
273
276
|
);
|
|
274
277
|
};
|
|
275
278
|
|
|
276
|
-
const createKeySet = <T extends
|
|
279
|
+
const createKeySet = <T extends AnyProperties, V>(obj: T, value: V): Record<JsonPath, V> => {
|
|
277
280
|
invariant(obj);
|
|
278
281
|
return Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: value }), {} as Record<JsonPath, V>);
|
|
279
282
|
};
|