@dxos/react-ui-form 0.7.5-labs.e27f9b9 → 0.7.5-labs.f400bbc
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 +546 -362
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +589 -401
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +546 -362
- 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 +1 -2
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/Form/ArrayField.d.ts +1 -2
- package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -1
- package/dist/types/src/components/Form/Defaults.d.ts +5 -5
- package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +2 -2
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormActions.d.ts +1 -2
- package/dist/types/src/components/Form/FormActions.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormContent.d.ts +2 -3
- package/dist/types/src/components/Form/FormContent.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormContext.d.ts +4 -1
- 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/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 +1 -2
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +1 -1
- 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/factory.d.ts.map +1 -1
- package/dist/types/src/components/Form/index.d.ts +2 -0
- package/dist/types/src/components/Form/index.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/Test.stories.d.ts +1 -2
- package/dist/types/src/components/ViewEditor/Test.stories.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +1 -2
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/Form.d.ts +1 -2
- package/dist/types/src/components/deprecated/Form.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/Form.stories.d.ts +1 -2
- package/dist/types/src/components/deprecated/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/FormInput.d.ts +2 -2
- package/dist/types/src/components/deprecated/FormInput.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +3 -3
- package/dist/types/src/components/testing.d.ts.map +1 -1
- package/dist/types/src/hooks/useForm.d.ts +1 -0
- package/dist/types/src/hooks/useForm.d.ts.map +1 -1
- package/dist/types/src/util/field.d.ts.map +1 -1
- package/dist/types/src/util/formatting.d.ts.map +1 -1
- package/package.json +25 -24
- package/src/components/Form/Defaults.tsx +61 -4
- package/src/components/Form/Form.tsx +9 -5
- package/src/components/Form/FormActions.tsx +1 -1
- package/src/components/Form/FormContent.tsx +10 -13
- package/src/components/Form/FormContext.tsx +58 -10
- package/src/components/Form/custom/GeoPointInput.tsx +42 -12
- package/src/components/Form/custom/SelectOptionsInput.tsx +7 -9
- package/src/components/Form/factory.tsx +4 -2
- package/src/components/Form/index.ts +3 -0
- package/src/components/ViewEditor/ViewEditor.tsx +2 -2
- package/src/hooks/useForm.ts +69 -40
- package/src/translations.ts +1 -1
- package/src/util/field.ts +19 -1
- package/src/util/formatting.ts +29 -1
package/src/hooks/useForm.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//
|
|
2
|
-
// Copyright
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
@@ -24,6 +24,7 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
24
24
|
touched: Record<JsonPath, boolean>;
|
|
25
25
|
changed: Record<JsonPath, boolean>;
|
|
26
26
|
canSave: boolean;
|
|
27
|
+
formIsValid: boolean;
|
|
27
28
|
handleSave: () => void;
|
|
28
29
|
|
|
29
30
|
//
|
|
@@ -129,6 +130,10 @@ export const useForm = <T extends BaseObject>({
|
|
|
129
130
|
}
|
|
130
131
|
}, [values, schemaDirty]);
|
|
131
132
|
|
|
133
|
+
const formIsValid = useMemo(() => {
|
|
134
|
+
return Object.keys(errors).length === 0;
|
|
135
|
+
}, [errors]);
|
|
136
|
+
|
|
132
137
|
/**
|
|
133
138
|
* NOTE: We can submit if there is no touched field that has an error.
|
|
134
139
|
* Basically, if there's a validation message visible in the form, submit should be disabled.
|
|
@@ -136,7 +141,7 @@ export const useForm = <T extends BaseObject>({
|
|
|
136
141
|
const canSave = useMemo(
|
|
137
142
|
() =>
|
|
138
143
|
!saving &&
|
|
139
|
-
// Check if any error paths that are touched have errors
|
|
144
|
+
// Check if any error paths that are touched have errors.
|
|
140
145
|
!Object.entries(touched).some(
|
|
141
146
|
([path, isTouched]) =>
|
|
142
147
|
isTouched &&
|
|
@@ -178,32 +183,38 @@ export const useForm = <T extends BaseObject>({
|
|
|
178
183
|
[errors],
|
|
179
184
|
);
|
|
180
185
|
|
|
181
|
-
const getFormValue = <
|
|
182
|
-
|
|
183
|
-
|
|
186
|
+
const getFormValue = useCallback<FormHandler<T>['getValue']>(
|
|
187
|
+
<V>(path: (string | number)[]): V | undefined => {
|
|
188
|
+
return getValue(values, createJsonPath(path));
|
|
189
|
+
},
|
|
190
|
+
[values],
|
|
191
|
+
);
|
|
184
192
|
|
|
185
|
-
const onValueChange =
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
193
|
+
const onValueChange = useCallback<FormHandler<T>['onValueChange']>(
|
|
194
|
+
(path: (string | number)[], type: SimpleType, value: any) => {
|
|
195
|
+
const jsonPath = createJsonPath(path);
|
|
196
|
+
let parsedValue = value;
|
|
197
|
+
try {
|
|
198
|
+
if (type === 'number') {
|
|
199
|
+
parsedValue = parseFloat(value as string) || 0;
|
|
200
|
+
}
|
|
201
|
+
} catch (err) {
|
|
202
|
+
log.catch(err);
|
|
203
|
+
parsedValue = undefined;
|
|
191
204
|
}
|
|
192
|
-
} catch (err) {
|
|
193
|
-
log.catch(err);
|
|
194
|
-
parsedValue = undefined;
|
|
195
|
-
}
|
|
196
205
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
206
|
+
const newValues = { ...setValue(values, jsonPath, parsedValue) };
|
|
207
|
+
setValues(newValues);
|
|
208
|
+
setChanged((prev) => ({ ...prev, [jsonPath]: true }));
|
|
209
|
+
onValuesChanged?.(newValues);
|
|
201
210
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
211
|
+
const isValid = validate(newValues);
|
|
212
|
+
if (isValid && onValid) {
|
|
213
|
+
onValid(newValues, { changed });
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
[values, onValuesChanged, validate, onValid, changed],
|
|
217
|
+
);
|
|
207
218
|
|
|
208
219
|
const onTouched = useCallback(
|
|
209
220
|
(path: (string | number)[]) => {
|
|
@@ -214,21 +225,39 @@ export const useForm = <T extends BaseObject>({
|
|
|
214
225
|
[validate, values],
|
|
215
226
|
);
|
|
216
227
|
|
|
217
|
-
return
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
228
|
+
return useMemo(
|
|
229
|
+
() => ({
|
|
230
|
+
// State.
|
|
231
|
+
values,
|
|
232
|
+
errors,
|
|
233
|
+
touched,
|
|
234
|
+
changed,
|
|
235
|
+
canSave,
|
|
236
|
+
formIsValid,
|
|
237
|
+
|
|
238
|
+
// Actions.
|
|
239
|
+
handleSave,
|
|
240
|
+
|
|
241
|
+
// Field utils.
|
|
242
|
+
getStatus,
|
|
243
|
+
getValue: getFormValue,
|
|
244
|
+
onValueChange,
|
|
245
|
+
onTouched,
|
|
246
|
+
}),
|
|
247
|
+
[
|
|
248
|
+
values,
|
|
249
|
+
errors,
|
|
250
|
+
touched,
|
|
251
|
+
changed,
|
|
252
|
+
canSave,
|
|
253
|
+
formIsValid,
|
|
254
|
+
handleSave,
|
|
255
|
+
getStatus,
|
|
256
|
+
getFormValue,
|
|
257
|
+
onValueChange,
|
|
258
|
+
onTouched,
|
|
259
|
+
],
|
|
260
|
+
);
|
|
232
261
|
};
|
|
233
262
|
|
|
234
263
|
const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPath, V> => {
|
|
@@ -239,7 +268,7 @@ const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPat
|
|
|
239
268
|
const flatMap = (errors: ValidationError[]) => {
|
|
240
269
|
return errors.reduce(
|
|
241
270
|
(result, { path, message }) => {
|
|
242
|
-
// Convert the validation error path format to our JsonPath format
|
|
271
|
+
// Convert the validation error path format to our JsonPath format.
|
|
243
272
|
const jsonPath = fromEffectValidationPath(path);
|
|
244
273
|
if (!(jsonPath in result)) {
|
|
245
274
|
result[jsonPath] = message;
|
package/src/translations.ts
CHANGED
|
@@ -36,7 +36,7 @@ export default [
|
|
|
36
36
|
'format hostname': 'Hostname',
|
|
37
37
|
'format integer': 'Integer',
|
|
38
38
|
'format json': 'JSON',
|
|
39
|
-
'format latlng': '
|
|
39
|
+
'format latlng': 'Geopoint',
|
|
40
40
|
'format markdown': 'Markdown',
|
|
41
41
|
'format percent': 'Percent',
|
|
42
42
|
'format ref': 'Reference',
|
package/src/util/field.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { FormatEnum, TypeEnum } from '@dxos/echo-schema';
|
|
5
|
+
import { FormatEnum, GeoLocation, TypeEnum } from '@dxos/echo-schema';
|
|
6
6
|
import { type ValidationError } from '@dxos/schema';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -81,6 +81,24 @@ export const parseValue = ({ type, format, value }: ParseProps) => {
|
|
|
81
81
|
return isNaN(date.getTime()) ? null : date;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
case FormatEnum.GeoPoint: {
|
|
85
|
+
// Parse string in format "lat,long" to GeoPoint [longitude, latitude].
|
|
86
|
+
if (typeof value === 'string') {
|
|
87
|
+
// Only keep digits, decimal points, minus signs, and commas.
|
|
88
|
+
const cleaned = value.replace(/[^0-9.,-]+/g, '');
|
|
89
|
+
const parts = cleaned.split(',');
|
|
90
|
+
if (parts.length === 2) {
|
|
91
|
+
const lat = parseFloat(parts[0]);
|
|
92
|
+
const long = parseFloat(parts[1]);
|
|
93
|
+
if (!isNaN(lat) && !isNaN(long)) {
|
|
94
|
+
return GeoLocation.toGeoPoint({ latitude: lat, longitude: long });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return [0, 0];
|
|
100
|
+
}
|
|
101
|
+
|
|
84
102
|
default: {
|
|
85
103
|
return value;
|
|
86
104
|
}
|
package/src/util/formatting.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { FormatEnum, TypeEnum } from '@dxos/echo-schema';
|
|
5
|
+
import { FormatEnum, GeoLocation, TypeEnum } from '@dxos/echo-schema';
|
|
6
6
|
|
|
7
7
|
type ValueFormatProps = {
|
|
8
8
|
type: TypeEnum;
|
|
@@ -73,6 +73,19 @@ export const formatForDisplay = ({ type, format, value, locale = undefined }: Va
|
|
|
73
73
|
return date.toLocaleTimeString(locale);
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
+
case FormatEnum.GeoPoint: {
|
|
77
|
+
if (value === null || value === undefined) {
|
|
78
|
+
return '';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// For GeoPoint format [longitude, latitude].
|
|
82
|
+
if (Array.isArray(value) && value.length >= 2 && value.every(Number.isFinite)) {
|
|
83
|
+
const { latitude, longitude } = GeoLocation.fromGeoPoint(value as [number, number]);
|
|
84
|
+
return `${latitude},${longitude}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return String(value);
|
|
88
|
+
}
|
|
76
89
|
default: {
|
|
77
90
|
if (value === null || value === 'undefined') {
|
|
78
91
|
return '';
|
|
@@ -125,6 +138,21 @@ export const formatForEditing = ({ type, format, value, locale = undefined }: Va
|
|
|
125
138
|
const date = new Date(value as number);
|
|
126
139
|
return date.toISOString().split('T')[1].split('.')[0];
|
|
127
140
|
}
|
|
141
|
+
case FormatEnum.GeoPoint: {
|
|
142
|
+
// Handle null or undefined
|
|
143
|
+
if (value === null || value === undefined) {
|
|
144
|
+
return '';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// For GeoPoint format [longitude, latitude].
|
|
148
|
+
if (Array.isArray(value) && value.length >= 2 && value.every(Number.isFinite)) {
|
|
149
|
+
const { latitude, longitude } = GeoLocation.fromGeoPoint(value as [number, number]);
|
|
150
|
+
return `${latitude},${longitude}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return String(value);
|
|
154
|
+
}
|
|
155
|
+
|
|
128
156
|
default: {
|
|
129
157
|
return String(value);
|
|
130
158
|
}
|