@dxos/react-ui-form 0.7.5-main.9d26e3a → 0.7.5-main.e9bb01b
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 +743 -466
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +777 -515
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +743 -466
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/Form/ArrayField.d.ts +16 -0
- package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -0
- package/dist/types/src/components/Form/Defaults.d.ts +5 -6
- package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +13 -16
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +2 -1
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormActions.d.ts +7 -0
- package/dist/types/src/components/Form/FormActions.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormContent.d.ts +26 -0
- package/dist/types/src/components/Form/FormContent.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormContext.d.ts +20 -0
- package/dist/types/src/components/Form/FormContext.d.ts.map +1 -0
- package/dist/types/src/components/Form/Input.d.ts +5 -6
- package/dist/types/src/components/Form/Input.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +1 -2
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +4 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +1 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +7 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/custom/TupleInput.d.ts +1 -2
- package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/index.d.ts +1 -0
- package/dist/types/src/components/Form/custom/index.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/ViewEditor/ViewEditor.d.ts +2 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/FormInput.d.ts +5 -0
- package/dist/types/src/components/deprecated/FormInput.d.ts.map +1 -1
- package/dist/types/src/hooks/useForm.d.ts +9 -10
- package/dist/types/src/hooks/useForm.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +6 -0
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +25 -24
- package/src/components/FieldEditor/FieldEditor.stories.tsx +1 -1
- package/src/components/FieldEditor/FieldEditor.tsx +58 -40
- package/src/components/Form/ArrayField.tsx +111 -0
- package/src/components/Form/Defaults.tsx +18 -33
- package/src/components/Form/Form.stories.tsx +15 -5
- package/src/components/Form/Form.tsx +39 -330
- package/src/components/Form/FormActions.tsx +41 -0
- package/src/components/Form/FormContent.tsx +173 -0
- package/src/components/Form/FormContext.tsx +48 -0
- package/src/components/Form/Input.tsx +5 -6
- package/src/components/Form/custom/GeoPointInput.tsx +6 -15
- package/src/components/Form/custom/SelectOptionsInput.stories.tsx +58 -0
- package/src/components/Form/custom/SelectOptionsInput.tsx +192 -0
- package/src/components/Form/custom/TupleInput.tsx +5 -9
- package/src/components/Form/custom/index.ts +1 -0
- package/src/components/Form/factory.tsx +2 -5
- package/src/components/ViewEditor/ViewEditor.stories.tsx +25 -4
- package/src/components/ViewEditor/ViewEditor.tsx +14 -6
- package/src/components/deprecated/FormInput.tsx +19 -3
- package/src/hooks/useForm.ts +54 -47
- package/src/translations.ts +8 -0
package/src/hooks/useForm.ts
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
6
6
|
|
|
7
|
-
import { type BaseObject,
|
|
8
|
-
import { type SimpleType, type S, type JsonPath } from '@dxos/effect';
|
|
7
|
+
import { type BaseObject, getValue, setValue } from '@dxos/echo-schema';
|
|
8
|
+
import { type SimpleType, type S, type JsonPath, createJsonPath, fromEffectValidationPath } from '@dxos/effect';
|
|
9
9
|
import { invariant } from '@dxos/invariant';
|
|
10
10
|
import { log } from '@dxos/log';
|
|
11
11
|
import { validateSchema, type ValidationError } from '@dxos/schema';
|
|
@@ -20,9 +20,9 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
20
20
|
//
|
|
21
21
|
|
|
22
22
|
values: Partial<T>;
|
|
23
|
-
errors: Record<
|
|
24
|
-
touched: Record<
|
|
25
|
-
changed: Record<
|
|
23
|
+
errors: Record<JsonPath, string>;
|
|
24
|
+
touched: Record<JsonPath, boolean>;
|
|
25
|
+
changed: Record<JsonPath, boolean>;
|
|
26
26
|
canSave: boolean;
|
|
27
27
|
handleSave: () => void;
|
|
28
28
|
|
|
@@ -30,10 +30,10 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
30
30
|
// Form input component helpers.
|
|
31
31
|
//
|
|
32
32
|
|
|
33
|
-
getStatus: (
|
|
34
|
-
getValue: <V>(
|
|
35
|
-
onValueChange: <V>(
|
|
36
|
-
|
|
33
|
+
getStatus: (path: string | (string | number)[]) => { status?: 'error'; error?: string };
|
|
34
|
+
getValue: <V>(path: (string | number)[]) => V | undefined;
|
|
35
|
+
onValueChange: <V>(path: (string | number)[], type: SimpleType, value: V) => void;
|
|
36
|
+
onTouched: (path: (string | number)[]) => void;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
/**
|
|
@@ -94,16 +94,15 @@ export const useForm = <T extends BaseObject>({
|
|
|
94
94
|
setValues(initialValues);
|
|
95
95
|
}, [initialValues]);
|
|
96
96
|
|
|
97
|
-
const [touched, setTouched] = useState<Record<
|
|
98
|
-
const [changed, setChanged] = useState<Record<
|
|
99
|
-
const [errors, setErrors] = useState<Record<
|
|
97
|
+
const [touched, setTouched] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
98
|
+
const [changed, setChanged] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
99
|
+
const [errors, setErrors] = useState<Record<JsonPath, string>>({});
|
|
100
100
|
const [saving, setSaving] = useState(false);
|
|
101
101
|
|
|
102
102
|
//
|
|
103
103
|
// Validation.
|
|
104
104
|
//
|
|
105
105
|
|
|
106
|
-
// TODO(burdon): Validate each property separately.
|
|
107
106
|
const validate = useCallback(
|
|
108
107
|
(values: Partial<T>): values is T => {
|
|
109
108
|
let errors: ValidationError[] = validateSchema(schema, values) ?? [];
|
|
@@ -136,8 +135,15 @@ export const useForm = <T extends BaseObject>({
|
|
|
136
135
|
const canSave = useMemo(
|
|
137
136
|
() =>
|
|
138
137
|
!saving &&
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
// Check if any error paths that are touched have errors
|
|
139
|
+
!Object.entries(touched).some(
|
|
140
|
+
([path, isTouched]) =>
|
|
141
|
+
isTouched &&
|
|
142
|
+
Object.keys(errors).some(
|
|
143
|
+
(errorPath) => errorPath === path || errorPath.startsWith(`${path}.`) || errorPath.startsWith(`${path}[`),
|
|
144
|
+
),
|
|
145
|
+
),
|
|
146
|
+
[touched, errors, saving],
|
|
141
147
|
);
|
|
142
148
|
|
|
143
149
|
const handleSave = useCallback(async () => {
|
|
@@ -156,19 +162,27 @@ export const useForm = <T extends BaseObject>({
|
|
|
156
162
|
//
|
|
157
163
|
|
|
158
164
|
const getStatus = useCallback<FormHandler<T>['getStatus']>(
|
|
159
|
-
(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
165
|
+
(path) => {
|
|
166
|
+
const jsonPath = Array.isArray(path) ? createJsonPath(path) : path;
|
|
167
|
+
const matchingError = Object.entries(errors).find(
|
|
168
|
+
([errorPath]) =>
|
|
169
|
+
errorPath === jsonPath || errorPath.startsWith(`${jsonPath}.`) || errorPath.startsWith(`${jsonPath}[`),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
status: matchingError ? 'error' : undefined,
|
|
174
|
+
error: matchingError ? matchingError[1] : undefined,
|
|
175
|
+
};
|
|
176
|
+
},
|
|
177
|
+
[errors],
|
|
164
178
|
);
|
|
165
179
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return getValue(values, property as any as JsonPath);
|
|
180
|
+
const getFormValue = <V>(path: (string | number)[]): V | undefined => {
|
|
181
|
+
return getValue(values, createJsonPath(path));
|
|
169
182
|
};
|
|
170
183
|
|
|
171
|
-
const onValueChange = (
|
|
184
|
+
const onValueChange = (path: (string | number)[], type: SimpleType, value: any) => {
|
|
185
|
+
const jsonPath = createJsonPath(path);
|
|
172
186
|
let parsedValue = value;
|
|
173
187
|
try {
|
|
174
188
|
if (type === 'number') {
|
|
@@ -179,9 +193,9 @@ export const useForm = <T extends BaseObject>({
|
|
|
179
193
|
parsedValue = undefined;
|
|
180
194
|
}
|
|
181
195
|
|
|
182
|
-
const newValues = { ...setValue(values,
|
|
196
|
+
const newValues = { ...setValue(values, jsonPath, parsedValue) };
|
|
183
197
|
setValues(newValues);
|
|
184
|
-
setChanged((prev) => ({ ...prev, [
|
|
198
|
+
setChanged((prev) => ({ ...prev, [jsonPath]: true }));
|
|
185
199
|
onValuesChanged?.(newValues);
|
|
186
200
|
|
|
187
201
|
const isValid = validate(newValues);
|
|
@@ -190,19 +204,10 @@ export const useForm = <T extends BaseObject>({
|
|
|
190
204
|
}
|
|
191
205
|
};
|
|
192
206
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
setTouched((touched) => ({ ...touched, [name]: true }));
|
|
198
|
-
|
|
199
|
-
// TODO(Zan): This should be configurable behavior.
|
|
200
|
-
if (event.relatedTarget?.getAttribute('type') === 'submit') {
|
|
201
|
-
// NOTE: We do this here instead of onSave because the blur event is triggered before the submit event
|
|
202
|
-
// and results in the submit button being disabled when the form is invalid.
|
|
203
|
-
setTouched(createKeySet(values, true));
|
|
204
|
-
}
|
|
205
|
-
|
|
207
|
+
const onTouched = useCallback(
|
|
208
|
+
(path: (string | number)[]) => {
|
|
209
|
+
const jsonPath = createJsonPath(path);
|
|
210
|
+
setTouched((touched) => ({ ...touched, [jsonPath]: true }));
|
|
206
211
|
validate(values);
|
|
207
212
|
},
|
|
208
213
|
[validate, values],
|
|
@@ -221,23 +226,25 @@ export const useForm = <T extends BaseObject>({
|
|
|
221
226
|
getStatus,
|
|
222
227
|
getValue: getFormValue,
|
|
223
228
|
onValueChange,
|
|
224
|
-
|
|
229
|
+
onTouched,
|
|
225
230
|
} satisfies FormHandler<T>;
|
|
226
231
|
};
|
|
227
232
|
|
|
228
|
-
const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<
|
|
233
|
+
const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPath, V> => {
|
|
229
234
|
invariant(obj);
|
|
230
|
-
return Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: value }), {} as Record<
|
|
235
|
+
return Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: value }), {} as Record<JsonPath, V>);
|
|
231
236
|
};
|
|
232
237
|
|
|
233
|
-
const flatMap =
|
|
238
|
+
const flatMap = (errors: ValidationError[]) => {
|
|
234
239
|
return errors.reduce(
|
|
235
240
|
(result, { path, message }) => {
|
|
236
|
-
|
|
237
|
-
|
|
241
|
+
// Convert the validation error path format to our JsonPath format
|
|
242
|
+
const jsonPath = fromEffectValidationPath(path);
|
|
243
|
+
if (!(jsonPath in result)) {
|
|
244
|
+
result[jsonPath] = message;
|
|
238
245
|
}
|
|
239
246
|
return result;
|
|
240
247
|
},
|
|
241
|
-
{} as Record<
|
|
248
|
+
{} as Record<JsonPath, string>,
|
|
242
249
|
);
|
|
243
250
|
};
|
package/src/translations.ts
CHANGED
|
@@ -42,6 +42,7 @@ export default [
|
|
|
42
42
|
'format ref': 'Reference',
|
|
43
43
|
'format regex': 'RegExp',
|
|
44
44
|
'format string': 'String',
|
|
45
|
+
'format single-select': 'Select',
|
|
45
46
|
'format text': 'Long text',
|
|
46
47
|
'format time': 'Time',
|
|
47
48
|
'format timestamp': 'Timestamp',
|
|
@@ -53,6 +54,13 @@ export default [
|
|
|
53
54
|
// Placeholders
|
|
54
55
|
'placeholder latitude': 'Latitude',
|
|
55
56
|
'placeholder longitude': 'Longitude',
|
|
57
|
+
|
|
58
|
+
// SelectOptionsInput
|
|
59
|
+
'select option label': 'Label',
|
|
60
|
+
'select option label placeholder': 'Option label',
|
|
61
|
+
'select option color': 'Color',
|
|
62
|
+
'select option delete': 'Delete',
|
|
63
|
+
'select option add': 'Add option',
|
|
56
64
|
},
|
|
57
65
|
},
|
|
58
66
|
},
|