@dxos/react-ui-form 0.7.2 → 0.7.3-main.2dd075e
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 +39 -36
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +39 -36
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +39 -36
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +2 -2
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +3 -2
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +9 -5
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/Form.stories.d.ts.map +1 -1
- package/dist/types/src/hooks/useForm.d.ts +10 -4
- package/dist/types/src/hooks/useForm.d.ts.map +1 -1
- package/package.json +23 -22
- package/src/components/FieldEditor/FieldEditor.stories.tsx +3 -3
- package/src/components/FieldEditor/FieldEditor.tsx +8 -8
- package/src/components/Form/Form.stories.tsx +41 -11
- package/src/components/Form/Form.tsx +23 -12
- package/src/components/ViewEditor/ViewEditor.tsx +8 -7
- package/src/components/deprecated/Form.stories.tsx +4 -4
- package/src/hooks/useForm.ts +20 -13
|
@@ -22,7 +22,7 @@ const ViewMetaSchema = S.Struct({
|
|
|
22
22
|
name: S.String.annotations({
|
|
23
23
|
[AST.TitleAnnotationId]: 'View',
|
|
24
24
|
}),
|
|
25
|
-
|
|
25
|
+
typename: Format.DXN.annotations({
|
|
26
26
|
[AST.TitleAnnotationId]: 'Typename',
|
|
27
27
|
}),
|
|
28
28
|
}).pipe(S.mutable);
|
|
@@ -60,16 +60,16 @@ export const ViewEditor = ({
|
|
|
60
60
|
name: view.name,
|
|
61
61
|
// TODO(burdon): Need to warn user of possible consequences of editing.
|
|
62
62
|
// TODO(burdon): Settings should have domain name owned by user.
|
|
63
|
-
|
|
63
|
+
typename: view.query.typename,
|
|
64
64
|
};
|
|
65
65
|
}, [view]);
|
|
66
66
|
|
|
67
67
|
const handleViewUpdate = useCallback(
|
|
68
|
-
({ name,
|
|
68
|
+
({ name, typename }: ViewMetaType) => {
|
|
69
69
|
requestAnimationFrame(() => {
|
|
70
70
|
view.name = name;
|
|
71
|
-
view.query.
|
|
72
|
-
schema.updateTypename(
|
|
71
|
+
view.query.typename = typename;
|
|
72
|
+
schema.updateTypename(typename);
|
|
73
73
|
});
|
|
74
74
|
},
|
|
75
75
|
[view, schema],
|
|
@@ -95,7 +95,7 @@ export const ViewEditor = ({
|
|
|
95
95
|
|
|
96
96
|
return (
|
|
97
97
|
<div role='none' className={mx('flex flex-col w-full divide-y divide-separator', classNames)}>
|
|
98
|
-
<Form<ViewMetaType> autoFocus autoSave schema={ViewMetaSchema} values={viewValues}
|
|
98
|
+
<Form<ViewMetaType> autoFocus autoSave schema={ViewMetaSchema} values={viewValues} onSave={handleViewUpdate} />
|
|
99
99
|
|
|
100
100
|
<div>
|
|
101
101
|
{/* TODO(burdon): Clean up common form ux. */}
|
|
@@ -139,9 +139,10 @@ export const ViewEditor = ({
|
|
|
139
139
|
projection={projection}
|
|
140
140
|
field={field}
|
|
141
141
|
registry={registry}
|
|
142
|
-
|
|
142
|
+
onSave={handleClose}
|
|
143
143
|
/>
|
|
144
144
|
)}
|
|
145
|
+
|
|
145
146
|
{!readonly && !field && (
|
|
146
147
|
<div className='flex p-2 justify-center'>
|
|
147
148
|
<IconButton
|
|
@@ -7,11 +7,11 @@ import '@dxos-theme';
|
|
|
7
7
|
import { type Meta, type StoryObj } from '@storybook/react';
|
|
8
8
|
import React from 'react';
|
|
9
9
|
|
|
10
|
-
import { create } from '@dxos/
|
|
11
|
-
import { testData,
|
|
12
|
-
import {
|
|
10
|
+
import { create } from '@dxos/live-object';
|
|
11
|
+
import { testData, TestSchema, testView, type TestType } from '@dxos/schema/testing';
|
|
12
|
+
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
13
13
|
|
|
14
|
-
import { type DeprecatedFormProps
|
|
14
|
+
import { DeprecatedForm, type DeprecatedFormProps } from './Form';
|
|
15
15
|
import translations from '../../translations';
|
|
16
16
|
import { TestLayout, TestPanel } from '../testing';
|
|
17
17
|
|
package/src/hooks/useForm.ts
CHANGED
|
@@ -22,8 +22,8 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
22
22
|
errors: Record<PropertyKey<T>, string>;
|
|
23
23
|
touched: Record<PropertyKey<T>, boolean>;
|
|
24
24
|
changed: Record<PropertyKey<T>, boolean>;
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
canSave: boolean;
|
|
26
|
+
handleSave: () => void;
|
|
27
27
|
|
|
28
28
|
//
|
|
29
29
|
// Form input component helpers.
|
|
@@ -39,9 +39,16 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
39
39
|
* Hook options.
|
|
40
40
|
*/
|
|
41
41
|
export interface FormOptions<T extends BaseObject> {
|
|
42
|
+
/**
|
|
43
|
+
* Effect schema.
|
|
44
|
+
*/
|
|
45
|
+
// TODO(burdon): Change to S.Struct<T>?
|
|
42
46
|
schema: S.Schema<T>;
|
|
43
47
|
|
|
44
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Initial values (which may not pass validation).
|
|
50
|
+
*/
|
|
51
|
+
// TODO(burdon): Should be partial?
|
|
45
52
|
initialValues: T;
|
|
46
53
|
|
|
47
54
|
/**
|
|
@@ -56,7 +63,7 @@ export interface FormOptions<T extends BaseObject> {
|
|
|
56
63
|
* Use this for complex validation logic that can't be expressed in the schema.
|
|
57
64
|
* @returns Array of validation errors, or undefined if validation passes
|
|
58
65
|
*/
|
|
59
|
-
// TODO(burdon): Change to key x value
|
|
66
|
+
// TODO(burdon): Change to key x value?
|
|
60
67
|
onValidate?: (values: T) => ValidationError[] | undefined;
|
|
61
68
|
|
|
62
69
|
/**
|
|
@@ -67,7 +74,7 @@ export interface FormOptions<T extends BaseObject> {
|
|
|
67
74
|
/**
|
|
68
75
|
* Called when the form is submitted and passes validation.
|
|
69
76
|
*/
|
|
70
|
-
|
|
77
|
+
onSave?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => void;
|
|
71
78
|
}
|
|
72
79
|
|
|
73
80
|
/**
|
|
@@ -80,7 +87,7 @@ export const useForm = <T extends BaseObject>({
|
|
|
80
87
|
onValuesChanged,
|
|
81
88
|
onValidate,
|
|
82
89
|
onValid,
|
|
83
|
-
|
|
90
|
+
onSave,
|
|
84
91
|
}: FormOptions<T>): FormHandler<T> => {
|
|
85
92
|
const [values, setValues] = useState<T>(initialValues);
|
|
86
93
|
useEffect(() => {
|
|
@@ -123,7 +130,7 @@ export const useForm = <T extends BaseObject>({
|
|
|
123
130
|
* NOTE: We can submit if there is no touched field that has an error.
|
|
124
131
|
* Basically, if there's a validation message visible in the form, submit should be disabled.
|
|
125
132
|
*/
|
|
126
|
-
const
|
|
133
|
+
const canSave = useMemo(
|
|
127
134
|
() =>
|
|
128
135
|
Object.keys(values).every(
|
|
129
136
|
(property) => touched[property as PropertyKey<T>] === false || !errors[property as PropertyKey<T>],
|
|
@@ -131,11 +138,11 @@ export const useForm = <T extends BaseObject>({
|
|
|
131
138
|
[values, touched, errors],
|
|
132
139
|
);
|
|
133
140
|
|
|
134
|
-
const
|
|
141
|
+
const handleSave = useCallback(() => {
|
|
135
142
|
if (validate(values)) {
|
|
136
|
-
|
|
143
|
+
onSave?.(values, { changed });
|
|
137
144
|
}
|
|
138
|
-
}, [values, validate,
|
|
145
|
+
}, [values, validate, onSave]);
|
|
139
146
|
|
|
140
147
|
//
|
|
141
148
|
// Fields.
|
|
@@ -185,7 +192,7 @@ export const useForm = <T extends BaseObject>({
|
|
|
185
192
|
|
|
186
193
|
// TODO(Zan): This should be configurable behavior.
|
|
187
194
|
if (event.relatedTarget?.getAttribute('type') === 'submit') {
|
|
188
|
-
// NOTE: We do this here instead of
|
|
195
|
+
// NOTE: We do this here instead of onSave because the blur event is triggered before the submit event
|
|
189
196
|
// and results in the submit button being disabled when the form is invalid.
|
|
190
197
|
setTouched(createKeySet(values, true));
|
|
191
198
|
}
|
|
@@ -201,8 +208,8 @@ export const useForm = <T extends BaseObject>({
|
|
|
201
208
|
errors,
|
|
202
209
|
touched,
|
|
203
210
|
changed,
|
|
204
|
-
|
|
205
|
-
|
|
211
|
+
canSave,
|
|
212
|
+
handleSave,
|
|
206
213
|
|
|
207
214
|
// Field utils.
|
|
208
215
|
getStatus,
|