@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.
Files changed (65) hide show
  1. package/dist/lib/browser/index.mjs +546 -362
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +589 -401
  5. package/dist/lib/node/index.cjs.map +4 -4
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +546 -362
  8. package/dist/lib/node-esm/index.mjs.map +4 -4
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +1 -2
  11. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  12. package/dist/types/src/components/Form/ArrayField.d.ts +1 -2
  13. package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -1
  14. package/dist/types/src/components/Form/Defaults.d.ts +5 -5
  15. package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/Form.d.ts +2 -2
  17. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  18. package/dist/types/src/components/Form/FormActions.d.ts +1 -2
  19. package/dist/types/src/components/Form/FormActions.d.ts.map +1 -1
  20. package/dist/types/src/components/Form/FormContent.d.ts +2 -3
  21. package/dist/types/src/components/Form/FormContent.d.ts.map +1 -1
  22. package/dist/types/src/components/Form/FormContext.d.ts +4 -1
  23. package/dist/types/src/components/Form/FormContext.d.ts.map +1 -1
  24. package/dist/types/src/components/Form/Input.d.ts +2 -2
  25. package/dist/types/src/components/Form/Input.d.ts.map +1 -1
  26. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +1 -2
  27. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +1 -1
  28. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +1 -2
  29. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +1 -1
  30. package/dist/types/src/components/Form/custom/TupleInput.d.ts +1 -2
  31. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +1 -1
  32. package/dist/types/src/components/Form/factory.d.ts.map +1 -1
  33. package/dist/types/src/components/Form/index.d.ts +2 -0
  34. package/dist/types/src/components/Form/index.d.ts.map +1 -1
  35. package/dist/types/src/components/ViewEditor/Test.stories.d.ts +1 -2
  36. package/dist/types/src/components/ViewEditor/Test.stories.d.ts.map +1 -1
  37. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +1 -2
  38. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  39. package/dist/types/src/components/deprecated/Form.d.ts +1 -2
  40. package/dist/types/src/components/deprecated/Form.d.ts.map +1 -1
  41. package/dist/types/src/components/deprecated/Form.stories.d.ts +1 -2
  42. package/dist/types/src/components/deprecated/Form.stories.d.ts.map +1 -1
  43. package/dist/types/src/components/deprecated/FormInput.d.ts +2 -2
  44. package/dist/types/src/components/deprecated/FormInput.d.ts.map +1 -1
  45. package/dist/types/src/components/testing.d.ts +3 -3
  46. package/dist/types/src/components/testing.d.ts.map +1 -1
  47. package/dist/types/src/hooks/useForm.d.ts +1 -0
  48. package/dist/types/src/hooks/useForm.d.ts.map +1 -1
  49. package/dist/types/src/util/field.d.ts.map +1 -1
  50. package/dist/types/src/util/formatting.d.ts.map +1 -1
  51. package/package.json +25 -24
  52. package/src/components/Form/Defaults.tsx +61 -4
  53. package/src/components/Form/Form.tsx +9 -5
  54. package/src/components/Form/FormActions.tsx +1 -1
  55. package/src/components/Form/FormContent.tsx +10 -13
  56. package/src/components/Form/FormContext.tsx +58 -10
  57. package/src/components/Form/custom/GeoPointInput.tsx +42 -12
  58. package/src/components/Form/custom/SelectOptionsInput.tsx +7 -9
  59. package/src/components/Form/factory.tsx +4 -2
  60. package/src/components/Form/index.ts +3 -0
  61. package/src/components/ViewEditor/ViewEditor.tsx +2 -2
  62. package/src/hooks/useForm.ts +69 -40
  63. package/src/translations.ts +1 -1
  64. package/src/util/field.ts +19 -1
  65. package/src/util/formatting.ts +29 -1
@@ -1,5 +1,5 @@
1
1
  //
2
- // Copyright 2024 DXOS.org
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 = <V>(path: (string | number)[]): V | undefined => {
182
- return getValue(values, createJsonPath(path));
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 = (path: (string | number)[], type: SimpleType, value: any) => {
186
- const jsonPath = createJsonPath(path);
187
- let parsedValue = value;
188
- try {
189
- if (type === 'number') {
190
- parsedValue = parseFloat(value as string) || 0;
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
- const newValues = { ...setValue(values, jsonPath, parsedValue) };
198
- setValues(newValues);
199
- setChanged((prev) => ({ ...prev, [jsonPath]: true }));
200
- onValuesChanged?.(newValues);
206
+ const newValues = { ...setValue(values, jsonPath, parsedValue) };
207
+ setValues(newValues);
208
+ setChanged((prev) => ({ ...prev, [jsonPath]: true }));
209
+ onValuesChanged?.(newValues);
201
210
 
202
- const isValid = validate(newValues);
203
- if (isValid && onValid) {
204
- onValid(newValues, { changed });
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
- // State.
219
- values,
220
- errors,
221
- touched,
222
- changed,
223
- canSave,
224
- handleSave,
225
-
226
- // Field utils.
227
- getStatus,
228
- getValue: getFormValue,
229
- onValueChange,
230
- onTouched,
231
- } satisfies FormHandler<T>;
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;
@@ -36,7 +36,7 @@ export default [
36
36
  'format hostname': 'Hostname',
37
37
  'format integer': 'Integer',
38
38
  'format json': 'JSON',
39
- 'format latlng': '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
  }
@@ -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
  }