@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
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
|
+
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
8
|
+
import { expect, fn, userEvent, within } from 'storybook/test';
|
|
9
|
+
|
|
10
|
+
import { Obj } from '@dxos/echo';
|
|
11
|
+
import { faker } from '@dxos/random';
|
|
12
|
+
import { Filter, useQuery } from '@dxos/react-client/echo';
|
|
13
|
+
import { useClientProvider, withClientProvider } from '@dxos/react-client/testing';
|
|
14
|
+
import { Button } from '@dxos/react-ui';
|
|
15
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
16
|
+
import { Person } from '@dxos/types';
|
|
17
|
+
|
|
18
|
+
import { translations } from '../../translations';
|
|
19
|
+
|
|
20
|
+
import { ObjectPicker } from './ObjectPicker';
|
|
21
|
+
|
|
22
|
+
faker.seed(1);
|
|
23
|
+
|
|
24
|
+
const createPerson = () =>
|
|
25
|
+
Obj.make(Person.Person, {
|
|
26
|
+
fullName: faker.person.fullName(),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const omitId = Schema.omit<any, any, ['id']>('id');
|
|
30
|
+
const personSchema = omitId(Person.Person);
|
|
31
|
+
|
|
32
|
+
// Mock functions for testing
|
|
33
|
+
const mockHandleSelect = fn();
|
|
34
|
+
const mockHandleCreate = fn();
|
|
35
|
+
|
|
36
|
+
const DefaultStory = () => {
|
|
37
|
+
const { space } = useClientProvider();
|
|
38
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
39
|
+
|
|
40
|
+
// Create sample Person objects
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (space) {
|
|
43
|
+
// Create some sample Person objects for testing
|
|
44
|
+
Array.from({ length: 10 }).forEach(() => {
|
|
45
|
+
space.db.add(createPerson());
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}, [space]);
|
|
49
|
+
|
|
50
|
+
// Get all objects in the space (similar to BoardContainer)
|
|
51
|
+
const allObjects = useQuery(space, Filter.everything());
|
|
52
|
+
|
|
53
|
+
// Map objects to options format expected by ObjectPicker
|
|
54
|
+
const options = useMemo(
|
|
55
|
+
() =>
|
|
56
|
+
allObjects.map((obj) => ({
|
|
57
|
+
id: obj.id,
|
|
58
|
+
label: obj.fullName || obj.name || obj.title || obj.id,
|
|
59
|
+
hue: 'neutral' as const,
|
|
60
|
+
})),
|
|
61
|
+
[allObjects],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const handleCreateCallback = useCallback(
|
|
65
|
+
(values: any) => {
|
|
66
|
+
console.log('[on create]', values);
|
|
67
|
+
if (!space) return;
|
|
68
|
+
const newPerson = space.db.add(Obj.make(Person.Person, values));
|
|
69
|
+
mockHandleCreate(values);
|
|
70
|
+
return newPerson;
|
|
71
|
+
},
|
|
72
|
+
[space],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<div className='p-4'>
|
|
77
|
+
<ObjectPicker.Root open={isOpen} onOpenChange={setIsOpen}>
|
|
78
|
+
<ObjectPicker.Trigger asChild>
|
|
79
|
+
<Button variant='primary' data-testid='trigger'>
|
|
80
|
+
Select Person
|
|
81
|
+
</Button>
|
|
82
|
+
</ObjectPicker.Trigger>
|
|
83
|
+
<ObjectPicker.Content
|
|
84
|
+
options={options}
|
|
85
|
+
onSelect={mockHandleSelect}
|
|
86
|
+
createSchema={personSchema}
|
|
87
|
+
createOptionLabel={['create new person label', { ns: 'os' }]}
|
|
88
|
+
createOptionIcon='ph--user-plus--regular'
|
|
89
|
+
createInitialValuePath='fullName'
|
|
90
|
+
onCreate={handleCreateCallback}
|
|
91
|
+
classNames='popover-card-width'
|
|
92
|
+
/>
|
|
93
|
+
</ObjectPicker.Root>
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const meta = {
|
|
99
|
+
title: 'ui/react-ui-form/ObjectPicker',
|
|
100
|
+
render: DefaultStory,
|
|
101
|
+
decorators: [
|
|
102
|
+
withTheme,
|
|
103
|
+
withClientProvider({
|
|
104
|
+
types: [Person.Person],
|
|
105
|
+
createIdentity: true,
|
|
106
|
+
createSpace: true,
|
|
107
|
+
}),
|
|
108
|
+
],
|
|
109
|
+
parameters: {
|
|
110
|
+
layout: 'centered',
|
|
111
|
+
controls: { disable: true },
|
|
112
|
+
translations,
|
|
113
|
+
},
|
|
114
|
+
} satisfies Meta<typeof DefaultStory>;
|
|
115
|
+
|
|
116
|
+
export default meta;
|
|
117
|
+
|
|
118
|
+
type Story = StoryObj<typeof meta>;
|
|
119
|
+
|
|
120
|
+
export const Default: Story = {};
|
|
121
|
+
|
|
122
|
+
export const WithTest: Story = {
|
|
123
|
+
play: async ({ canvasElement }) => {
|
|
124
|
+
const canvas = within(canvasElement);
|
|
125
|
+
const body = within(document.body);
|
|
126
|
+
|
|
127
|
+
// Find and click the trigger button.
|
|
128
|
+
const triggerButton = await canvas.findByTestId('trigger', undefined, { timeout: 5000 });
|
|
129
|
+
await expect(triggerButton).toBeVisible();
|
|
130
|
+
await userEvent.click(triggerButton);
|
|
131
|
+
|
|
132
|
+
// Check that the listbox renders when the trigger is clicked.
|
|
133
|
+
const listbox = await body.findByRole('listbox', undefined, { timeout: 5000 });
|
|
134
|
+
await expect(listbox).toBeVisible();
|
|
135
|
+
|
|
136
|
+
// Find the search input.
|
|
137
|
+
const searchInput = await body.findByPlaceholderText('Search…');
|
|
138
|
+
await expect(searchInput).toBeVisible();
|
|
139
|
+
|
|
140
|
+
// Test searching filters the results - get initial options count.
|
|
141
|
+
const initialOptions = await body.findAllByRole('option');
|
|
142
|
+
const initialCount = initialOptions.length;
|
|
143
|
+
await expect(initialCount).toBeGreaterThan(0);
|
|
144
|
+
|
|
145
|
+
// Type a search term that should filter results.
|
|
146
|
+
await userEvent.type(searchInput, 'John');
|
|
147
|
+
|
|
148
|
+
// Wait a moment for filtering.
|
|
149
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
150
|
+
|
|
151
|
+
// Check that results are filtered (should be fewer options)
|
|
152
|
+
const _filteredOptions = await body.findAllByRole('option');
|
|
153
|
+
// Note: We expect either fewer results or the same if no John exists
|
|
154
|
+
|
|
155
|
+
// Clear search and test selecting a result
|
|
156
|
+
await userEvent.clear(searchInput);
|
|
157
|
+
|
|
158
|
+
// Get the first option and click it
|
|
159
|
+
const updatedOptions = await body.findAllByRole('option');
|
|
160
|
+
const firstOption = updatedOptions[0];
|
|
161
|
+
await expect(firstOption).toBeVisible();
|
|
162
|
+
|
|
163
|
+
const _firstOptionText = firstOption.textContent;
|
|
164
|
+
await userEvent.click(firstOption);
|
|
165
|
+
|
|
166
|
+
// Check that clicking a result fires onSelect with that id
|
|
167
|
+
// The onSelect should have been called with the selected option's id
|
|
168
|
+
await expect(mockHandleSelect).toHaveBeenCalled();
|
|
169
|
+
|
|
170
|
+
// Re-open the picker to test create functionality.
|
|
171
|
+
await userEvent.click(triggerButton);
|
|
172
|
+
|
|
173
|
+
// Wait for listbox to appear again.
|
|
174
|
+
const newListbox = await body.findByRole('listbox', undefined, { timeout: 5000 });
|
|
175
|
+
await expect(newListbox).toBeVisible();
|
|
176
|
+
|
|
177
|
+
const newSearchInput = await body.findByPlaceholderText('Search…');
|
|
178
|
+
|
|
179
|
+
// Test searching an unrelated term provides the __create__ option.
|
|
180
|
+
const unrelatedTerm = 'ZxyUnrelatedName123';
|
|
181
|
+
await userEvent.type(newSearchInput, unrelatedTerm);
|
|
182
|
+
|
|
183
|
+
// Wait for the create option to appear.
|
|
184
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
185
|
+
|
|
186
|
+
// Look for the create option.
|
|
187
|
+
const createOption = await body.findByRole('option', undefined, { timeout: 3000 });
|
|
188
|
+
await expect(createOption).toBeVisible();
|
|
189
|
+
|
|
190
|
+
// Click the create option - this should change state to the form field.
|
|
191
|
+
await userEvent.click(createOption);
|
|
192
|
+
|
|
193
|
+
// Check that clicking that option changes state to the form field.
|
|
194
|
+
const form = await body.findByTestId('create-referenced-object-form', undefined, { timeout: 5000 });
|
|
195
|
+
await expect(form).toBeVisible();
|
|
196
|
+
|
|
197
|
+
// Find and click the save button.
|
|
198
|
+
const saveButton = await within(form).findByTestId('save-button');
|
|
199
|
+
await expect(saveButton).toBeVisible();
|
|
200
|
+
await userEvent.click(saveButton);
|
|
201
|
+
|
|
202
|
+
// Check that clicking the save button calls onCreate with expected values.
|
|
203
|
+
await expect(mockHandleCreate).toHaveBeenCalledWith({
|
|
204
|
+
fullName: unrelatedTerm,
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
};
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Schema from 'effect/Schema';
|
|
6
|
+
import React, { type KeyboardEvent, type MouseEvent, forwardRef, useCallback, useMemo, useState } from 'react';
|
|
7
|
+
|
|
8
|
+
import { Icon, Popover, type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
9
|
+
import { Combobox } from '@dxos/react-ui-searchlist';
|
|
10
|
+
|
|
11
|
+
import { type QueryTag } from '../../hooks';
|
|
12
|
+
import { translationKey } from '../../translations';
|
|
13
|
+
import { Form } from '../Form';
|
|
14
|
+
|
|
15
|
+
export type ObjectPickerContentProps = ThemedClassName<{
|
|
16
|
+
options: QueryTag[];
|
|
17
|
+
selectedIds?: string[];
|
|
18
|
+
createSchema?: Schema.Schema.AnyNoContext;
|
|
19
|
+
createOptionLabel?: [string, { ns: string }];
|
|
20
|
+
createOptionIcon?: string;
|
|
21
|
+
createInitialValuePath?: string;
|
|
22
|
+
onSelect: (id: string) => void;
|
|
23
|
+
onCreate?: (values: any) => void;
|
|
24
|
+
}>;
|
|
25
|
+
|
|
26
|
+
const ObjectPickerContent = forwardRef<HTMLDivElement, ObjectPickerContentProps>(
|
|
27
|
+
(
|
|
28
|
+
{
|
|
29
|
+
options,
|
|
30
|
+
selectedIds,
|
|
31
|
+
createSchema,
|
|
32
|
+
createOptionLabel,
|
|
33
|
+
createOptionIcon,
|
|
34
|
+
createInitialValuePath,
|
|
35
|
+
onSelect,
|
|
36
|
+
onCreate,
|
|
37
|
+
...props
|
|
38
|
+
},
|
|
39
|
+
ref,
|
|
40
|
+
) => {
|
|
41
|
+
const { t } = useTranslation(translationKey);
|
|
42
|
+
|
|
43
|
+
const [searchString, setSearchString] = useState('');
|
|
44
|
+
const [showForm, setShowForm] = useState(false);
|
|
45
|
+
|
|
46
|
+
const handleFormSave = useCallback(
|
|
47
|
+
(values: any) => {
|
|
48
|
+
onCreate?.(values);
|
|
49
|
+
setShowForm(false);
|
|
50
|
+
setSearchString('');
|
|
51
|
+
},
|
|
52
|
+
[onCreate],
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const handleFormCancel = useCallback(() => {
|
|
56
|
+
setShowForm(false);
|
|
57
|
+
setSearchString('');
|
|
58
|
+
}, []);
|
|
59
|
+
|
|
60
|
+
// TODO(thure): The following click and keydown handlers are necessary because `onSelect` is called after the
|
|
61
|
+
// Popover is already closed. Augment/refactor CmdK, if possible, to facilitate stopping event default
|
|
62
|
+
// and propagation.
|
|
63
|
+
|
|
64
|
+
const handleClick = useCallback(
|
|
65
|
+
(event: MouseEvent) => {
|
|
66
|
+
if (createSchema && (event.target as HTMLElement).closest('[data-value="__create__"]')) {
|
|
67
|
+
event.stopPropagation();
|
|
68
|
+
event.preventDefault();
|
|
69
|
+
setShowForm(true);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
[createSchema],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const handleKeyDown = useCallback(
|
|
76
|
+
(event: KeyboardEvent) => {
|
|
77
|
+
if (
|
|
78
|
+
event.key === 'Enter' &&
|
|
79
|
+
createSchema &&
|
|
80
|
+
(event.currentTarget as HTMLElement).querySelector(
|
|
81
|
+
'[role="option"][aria-selected="true"][data-value="__create__"]',
|
|
82
|
+
)
|
|
83
|
+
) {
|
|
84
|
+
event.stopPropagation();
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
setShowForm(true);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
[createSchema],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const labelById = useMemo(
|
|
93
|
+
() =>
|
|
94
|
+
options.reduce((acc: Record<string, string>, option) => {
|
|
95
|
+
acc[option.id.toLowerCase()] = option.label.toLowerCase();
|
|
96
|
+
return acc;
|
|
97
|
+
}, {}),
|
|
98
|
+
[options],
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<Combobox.Content
|
|
103
|
+
{...props}
|
|
104
|
+
ref={ref}
|
|
105
|
+
filter={(value, search) => (value === '__create__' || labelById[value]?.includes(search.toLowerCase()) ? 1 : 0)}
|
|
106
|
+
onClickCapture={handleClick}
|
|
107
|
+
onKeyDownCapture={handleKeyDown}
|
|
108
|
+
>
|
|
109
|
+
{showForm && createSchema ? (
|
|
110
|
+
<Popover.Viewport>
|
|
111
|
+
<Form
|
|
112
|
+
schema={createSchema}
|
|
113
|
+
values={createInitialValuePath ? { [createInitialValuePath]: searchString } : {}}
|
|
114
|
+
onSave={handleFormSave}
|
|
115
|
+
onCancel={handleFormCancel}
|
|
116
|
+
testId='create-referenced-object-form'
|
|
117
|
+
/>
|
|
118
|
+
</Popover.Viewport>
|
|
119
|
+
) : (
|
|
120
|
+
<>
|
|
121
|
+
<Combobox.Input
|
|
122
|
+
placeholder={t('ref field combobox input placeholder')}
|
|
123
|
+
value={searchString}
|
|
124
|
+
onValueChange={setSearchString}
|
|
125
|
+
autoFocus
|
|
126
|
+
/>
|
|
127
|
+
<Combobox.List>
|
|
128
|
+
{options.map((option) => (
|
|
129
|
+
<Combobox.Item
|
|
130
|
+
key={option.id}
|
|
131
|
+
value={option.id}
|
|
132
|
+
onSelect={() => onSelect(option.id)}
|
|
133
|
+
classNames='flex items-center gap-2'
|
|
134
|
+
>
|
|
135
|
+
<span className='grow'>{option.label}</span>
|
|
136
|
+
{selectedIds?.includes(option.id) && <Icon icon='ph--check--regular' />}
|
|
137
|
+
</Combobox.Item>
|
|
138
|
+
))}
|
|
139
|
+
{searchString.length > 0 && createOptionLabel && createOptionIcon && createSchema && onCreate && (
|
|
140
|
+
<Combobox.Item value='__create__' classNames='flex items-center gap-2'>
|
|
141
|
+
<Icon icon={createOptionIcon} />
|
|
142
|
+
{t(createOptionLabel[0], { ns: createOptionLabel[1].ns, text: searchString })}
|
|
143
|
+
</Combobox.Item>
|
|
144
|
+
)}
|
|
145
|
+
</Combobox.List>
|
|
146
|
+
</>
|
|
147
|
+
)}
|
|
148
|
+
<Combobox.Arrow />
|
|
149
|
+
</Combobox.Content>
|
|
150
|
+
);
|
|
151
|
+
},
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
ObjectPickerContent.displayName = 'ObjectPickerContent';
|
|
155
|
+
|
|
156
|
+
export const ObjectPicker = {
|
|
157
|
+
Root: Combobox.Root,
|
|
158
|
+
Trigger: Combobox.Trigger,
|
|
159
|
+
VirtualTrigger: Combobox.VirtualTrigger,
|
|
160
|
+
Content: ObjectPickerContent,
|
|
161
|
+
};
|
|
@@ -2,94 +2,139 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@dxos-theme';
|
|
6
|
-
|
|
7
5
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
8
|
-
import
|
|
9
|
-
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
10
8
|
|
|
11
|
-
import { Obj, Type } from '@dxos/echo';
|
|
12
|
-
import { type EchoSchema, Format
|
|
13
|
-
import {
|
|
14
|
-
import { withClientProvider } from '@dxos/react-client/testing';
|
|
9
|
+
import { DXN, Filter, Obj, Query, type QueryAST, Tag, Type } from '@dxos/echo';
|
|
10
|
+
import { type EchoSchema, Format } from '@dxos/echo/internal';
|
|
11
|
+
import { useQuery } from '@dxos/react-client/echo';
|
|
12
|
+
import { useClientProvider, withClientProvider } from '@dxos/react-client/testing';
|
|
15
13
|
import { useAsyncEffect } from '@dxos/react-ui';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
14
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
15
|
+
import { type ProjectionModel, View, getTypenameFromQuery } from '@dxos/schema';
|
|
16
|
+
import { Employer, Organization, Person, Project } from '@dxos/types';
|
|
18
17
|
|
|
19
18
|
import { translations } from '../../translations';
|
|
20
19
|
import { TestLayout, TestPanel, VIEW_EDITOR_DEBUG_SYMBOL } from '../testing';
|
|
21
20
|
|
|
22
21
|
import { ViewEditor, type ViewEditorProps } from './ViewEditor';
|
|
23
22
|
|
|
23
|
+
const types = [
|
|
24
|
+
// TODO(burdon): Get label from annotation.
|
|
25
|
+
{ value: Organization.Organization.typename, label: 'Organization' },
|
|
26
|
+
{ value: Person.Person.typename, label: 'Person' },
|
|
27
|
+
{ value: Type.getTypename(Project.Project), label: 'Project' },
|
|
28
|
+
{ value: Employer.Employer.typename, label: 'Employer' },
|
|
29
|
+
];
|
|
30
|
+
|
|
24
31
|
// Type definition for debug objects exposed to tests.
|
|
25
32
|
export type ViewEditorDebugObjects = {
|
|
26
33
|
schema: EchoSchema;
|
|
27
|
-
view:
|
|
34
|
+
view: View.View;
|
|
28
35
|
projection: ProjectionModel;
|
|
29
36
|
};
|
|
30
37
|
|
|
31
|
-
type StoryProps = Pick<ViewEditorProps, 'readonly'>;
|
|
38
|
+
type StoryProps = Pick<ViewEditorProps, 'readonly' | 'mode'>;
|
|
32
39
|
|
|
33
40
|
const DefaultStory = (props: StoryProps) => {
|
|
34
|
-
const space =
|
|
41
|
+
const { space } = useClientProvider();
|
|
35
42
|
const [schema, setSchema] = useState<EchoSchema>();
|
|
36
|
-
const [view, setView] = useState<
|
|
37
|
-
const
|
|
43
|
+
const [view, setView] = useState<View.View>();
|
|
44
|
+
const projectionRef = useRef<ProjectionModel>(null);
|
|
45
|
+
|
|
46
|
+
const tags = useQuery(space, Filter.type(Tag.Tag));
|
|
47
|
+
|
|
38
48
|
useAsyncEffect(async () => {
|
|
39
49
|
if (space) {
|
|
40
|
-
|
|
50
|
+
const TestSchema = Schema.Struct({
|
|
41
51
|
name: Schema.String,
|
|
42
52
|
email: Format.Email,
|
|
43
53
|
salary: Format.Currency(),
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
}).pipe(
|
|
55
|
+
Type.Obj({
|
|
56
|
+
typename: 'example.com/type/Test',
|
|
57
|
+
version: '0.1.0',
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const AlternateSchema = Schema.Struct({
|
|
62
|
+
title: Schema.String,
|
|
63
|
+
description: Schema.String,
|
|
64
|
+
completed: Schema.Boolean,
|
|
65
|
+
}).pipe(
|
|
66
|
+
Type.Obj({
|
|
67
|
+
typename: 'example.com/type/Alternate',
|
|
68
|
+
version: '0.1.0',
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const [testSchema] = await space.db.schemaRegistry.register([TestSchema, AlternateSchema]);
|
|
73
|
+
const view = View.make({
|
|
48
74
|
name: 'Test',
|
|
49
|
-
|
|
50
|
-
jsonSchema: toJsonSchema(TestSchema),
|
|
51
|
-
presentation: Obj.make(Type.Expando, {}),
|
|
75
|
+
query: Query.select(Filter.type(TestSchema)),
|
|
76
|
+
jsonSchema: Type.toJsonSchema(TestSchema),
|
|
52
77
|
});
|
|
53
|
-
const projection = new ProjectionModel(schema.jsonSchema, view.projection);
|
|
54
78
|
|
|
55
|
-
setSchema(
|
|
79
|
+
setSchema(testSchema);
|
|
56
80
|
setView(view);
|
|
57
|
-
setProjection(projection);
|
|
58
81
|
}
|
|
59
82
|
}, [space]);
|
|
60
83
|
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
(newTypename: string) => {
|
|
65
|
-
if (!schema) {
|
|
84
|
+
const updateViewQuery = useCallback(
|
|
85
|
+
async (newQuery: QueryAST.Query, target?: string) => {
|
|
86
|
+
if (!schema || !view || !space) {
|
|
66
87
|
return;
|
|
67
88
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
89
|
+
|
|
90
|
+
if (props.mode === 'tag') {
|
|
91
|
+
const queue = target && DXN.tryParse(target) ? target : undefined;
|
|
92
|
+
const query = queue ? Query.fromAst(newQuery).options({ queues: [queue] }) : Query.fromAst(newQuery);
|
|
93
|
+
view.query.ast = query.ast;
|
|
94
|
+
|
|
95
|
+
const typename = getTypenameFromQuery(query.ast);
|
|
96
|
+
const [newSchema] = await space.db.schemaRegistry.query({ typename }).run();
|
|
97
|
+
if (!newSchema) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const newView = View.make({
|
|
102
|
+
query,
|
|
103
|
+
jsonSchema: newSchema.jsonSchema,
|
|
104
|
+
});
|
|
105
|
+
view.projection = Obj.getSnapshot(newView).projection;
|
|
106
|
+
setSchema(() => newSchema);
|
|
107
|
+
} else {
|
|
108
|
+
view.query.ast = newQuery;
|
|
109
|
+
schema.updateTypename(getTypenameFromQuery(newQuery));
|
|
71
110
|
}
|
|
72
|
-
schema.updateTypename(newTypename);
|
|
73
111
|
},
|
|
74
|
-
[
|
|
112
|
+
[view, schema],
|
|
75
113
|
);
|
|
76
114
|
|
|
77
|
-
const handleDelete = useCallback(
|
|
115
|
+
const handleDelete = useCallback(
|
|
116
|
+
(property: string) => projectionRef.current?.deleteFieldProjection(property),
|
|
117
|
+
[projectionRef],
|
|
118
|
+
);
|
|
78
119
|
|
|
79
120
|
// Expose objects on window for test access.
|
|
80
121
|
useEffect(() => {
|
|
81
|
-
if (typeof window !== 'undefined' && schema && view &&
|
|
82
|
-
(window as any)[VIEW_EDITOR_DEBUG_SYMBOL] = {
|
|
122
|
+
if (typeof window !== 'undefined' && schema && view && projectionRef.current) {
|
|
123
|
+
(window as any)[VIEW_EDITOR_DEBUG_SYMBOL] = {
|
|
124
|
+
schema,
|
|
125
|
+
view,
|
|
126
|
+
projection: projectionRef.current,
|
|
127
|
+
} satisfies ViewEditorDebugObjects;
|
|
83
128
|
}
|
|
84
|
-
}, [schema, view
|
|
129
|
+
}, [schema, view]);
|
|
85
130
|
|
|
86
131
|
// NOTE(ZaymonFC): This looks awkward but it resolves an infinite parsing issue with sb.
|
|
87
132
|
const json = useMemo(
|
|
88
|
-
() => JSON.parse(JSON.stringify({ schema, view, projection })),
|
|
89
|
-
[JSON.stringify(schema), JSON.stringify(view)
|
|
133
|
+
() => JSON.parse(JSON.stringify({ schema, view, projection: projectionRef.current })),
|
|
134
|
+
[JSON.stringify(schema), JSON.stringify(view)],
|
|
90
135
|
);
|
|
91
136
|
|
|
92
|
-
if (!schema || !view
|
|
137
|
+
if (!schema || !view) {
|
|
93
138
|
return <div />;
|
|
94
139
|
}
|
|
95
140
|
|
|
@@ -97,11 +142,15 @@ const DefaultStory = (props: StoryProps) => {
|
|
|
97
142
|
<TestLayout json={json}>
|
|
98
143
|
<TestPanel>
|
|
99
144
|
<ViewEditor
|
|
145
|
+
ref={projectionRef}
|
|
100
146
|
schema={schema}
|
|
101
147
|
view={view}
|
|
102
148
|
registry={space?.db.schemaRegistry}
|
|
149
|
+
mode={props.mode}
|
|
103
150
|
readonly={props.readonly}
|
|
104
|
-
|
|
151
|
+
types={types}
|
|
152
|
+
tags={tags}
|
|
153
|
+
onQueryChanged={updateViewQuery}
|
|
105
154
|
onDelete={handleDelete}
|
|
106
155
|
/>
|
|
107
156
|
</TestPanel>
|
|
@@ -115,13 +164,19 @@ const meta = {
|
|
|
115
164
|
decorators: [
|
|
116
165
|
withClientProvider({
|
|
117
166
|
createSpace: true,
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
167
|
+
types: [Tag.Tag],
|
|
168
|
+
onCreateSpace: ({ space }) => {
|
|
169
|
+
space.db.add(Tag.make({ label: 'Important' }));
|
|
170
|
+
space.db.add(Tag.make({ label: 'Investor' }));
|
|
171
|
+
space.db.add(Tag.make({ label: 'New' }));
|
|
172
|
+
},
|
|
121
173
|
}),
|
|
122
174
|
withTheme,
|
|
123
175
|
],
|
|
124
|
-
parameters: {
|
|
176
|
+
parameters: {
|
|
177
|
+
layout: 'fullscreen',
|
|
178
|
+
translations,
|
|
179
|
+
},
|
|
125
180
|
} satisfies Meta<typeof DefaultStory>;
|
|
126
181
|
|
|
127
182
|
export default meta;
|
|
@@ -131,5 +186,13 @@ type Story = StoryObj<typeof meta>;
|
|
|
131
186
|
export const Default: Story = {};
|
|
132
187
|
|
|
133
188
|
export const Readonly: Story = {
|
|
134
|
-
args: {
|
|
189
|
+
args: {
|
|
190
|
+
readonly: true,
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export const TagQueryMode: Story = {
|
|
195
|
+
args: {
|
|
196
|
+
mode: 'tag',
|
|
197
|
+
},
|
|
135
198
|
};
|
|
@@ -6,8 +6,8 @@ import { composeStories } from '@storybook/react';
|
|
|
6
6
|
import { cleanup, fireEvent, screen, waitFor } from '@testing-library/react';
|
|
7
7
|
import { afterEach, describe, expect, test } from 'vitest';
|
|
8
8
|
|
|
9
|
-
import { EchoSchema, isInstanceOf } from '@dxos/echo
|
|
10
|
-
import {
|
|
9
|
+
import { EchoSchema, isInstanceOf } from '@dxos/echo/internal';
|
|
10
|
+
import { ProjectionModel, View } from '@dxos/schema';
|
|
11
11
|
|
|
12
12
|
import { VIEW_EDITOR_DEBUG_SYMBOL } from '../testing';
|
|
13
13
|
|
|
@@ -20,7 +20,7 @@ const getViewEditorDebugObjects = (): ViewEditorDebugObjects => {
|
|
|
20
20
|
const debugObjects = (window as any)[VIEW_EDITOR_DEBUG_SYMBOL] as ViewEditorDebugObjects;
|
|
21
21
|
expect(debugObjects).toBeDefined();
|
|
22
22
|
expect(debugObjects.schema).toBeInstanceOf(EchoSchema);
|
|
23
|
-
expect(isInstanceOf(
|
|
23
|
+
expect(isInstanceOf(View.View, debugObjects.view)).toBeTruthy();
|
|
24
24
|
expect(debugObjects.projection).toBeInstanceOf(ProjectionModel);
|
|
25
25
|
return debugObjects;
|
|
26
26
|
};
|
|
@@ -134,7 +134,8 @@ describe('ViewEditor', () => {
|
|
|
134
134
|
expect(addedPropertyProjection!.props.property).toBe('added_property');
|
|
135
135
|
});
|
|
136
136
|
|
|
137
|
-
|
|
137
|
+
// TODO(mykola): Fix flaky test.
|
|
138
|
+
test('delete property', { retry: 2 }, async () => {
|
|
138
139
|
await Default.run();
|
|
139
140
|
await waitForViewEditor();
|
|
140
141
|
|
|
@@ -200,4 +201,4 @@ describe('ViewEditor', () => {
|
|
|
200
201
|
// Also verify that the field is back in the visible fields list.
|
|
201
202
|
expect(debugObjects.view.projection.fields.filter(({ visible }) => visible !== false).length).toBeGreaterThan(0);
|
|
202
203
|
});
|
|
203
|
-
});
|
|
204
|
+
}, 30_000);
|