@dxos/react-ui-form 0.8.4-main.84f28bd → 0.8.4-main.8baae0fced
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/LICENSE +102 -5
- package/README.md +0 -1
- package/dist/lib/browser/index.mjs +2773 -2140
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/translations.mjs +82 -0
- package/dist/lib/browser/translations.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +2773 -2140
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/translations.mjs +84 -0
- package/dist/lib/node-esm/translations.mjs.map +7 -0
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +9 -9
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +86 -7
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +109 -25
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +130 -56
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormField.d.ts +43 -0
- package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts +79 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts +9 -0
- package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldSet.d.ts +15 -0
- package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/ArrayField.d.ts +18 -0
- package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/BooleanField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/GeoPointField.d.ts +5 -0
- package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/MarkdownField.d.ts +11 -0
- package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts +84 -0
- package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/RefField.d.ts +22 -0
- package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectField.d.ts +10 -0
- package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +84 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
- package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/index.d.ts +12 -0
- package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
- package/dist/types/src/components/Form/index.d.ts +2 -4
- package/dist/types/src/components/Form/index.d.ts.map +1 -1
- package/dist/types/src/components/ObjectForm/ObjectForm.d.ts +9 -0
- package/dist/types/src/components/ObjectForm/ObjectForm.d.ts.map +1 -0
- package/dist/types/src/components/ObjectForm/index.d.ts +2 -0
- package/dist/types/src/components/ObjectForm/index.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +68 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +87 -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/ObjectProperties/ObjectProperties.d.ts +9 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts +127 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts +2 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/index.d.ts +2 -0
- package/dist/types/src/components/ObjectProperties/index.d.ts.map +1 -0
- package/dist/types/src/components/Settings/Settings.d.ts +51 -0
- package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
- package/dist/types/src/components/Settings/Settings.stories.d.ts +84 -0
- package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsFieldSet.d.ts +43 -0
- package/dist/types/src/components/Settings/SettingsFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsItem.d.ts +16 -0
- package/dist/types/src/components/Settings/SettingsItem.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsSelectField.d.ts +18 -0
- package/dist/types/src/components/Settings/SettingsSelectField.d.ts.map +1 -0
- package/dist/types/src/components/Settings/index.d.ts +4 -0
- package/dist/types/src/components/Settings/index.d.ts.map +1 -0
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +21 -10
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +87 -9
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +4 -2
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +5 -4
- package/dist/types/src/components/testing.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +2 -3
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useFormHandler.d.ts +82 -0
- package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -0
- package/dist/types/src/hooks/useKeyHandler.d.ts +7 -0
- package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +65 -54
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/util/field-type.d.ts +19 -0
- package/dist/types/src/util/field-type.d.ts.map +1 -0
- 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/index.d.ts +2 -1
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/properties.d.ts +13 -0
- package/dist/types/src/util/properties.d.ts.map +1 -0
- package/dist/types/src/util/properties.test.d.ts +2 -0
- package/dist/types/src/util/properties.test.d.ts.map +1 -0
- package/dist/types/src/util/refs.d.ts +3 -3
- package/dist/types/src/util/refs.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +58 -47
- package/src/components/FieldEditor/FieldEditor.stories.tsx +52 -31
- package/src/components/FieldEditor/FieldEditor.test.tsx +13 -13
- package/src/components/FieldEditor/FieldEditor.tsx +90 -95
- package/src/components/Form/AUDIT.md +17 -0
- package/src/components/Form/Form.stories.tsx +147 -223
- package/src/components/Form/Form.tsx +375 -95
- package/src/components/Form/FormField.tsx +306 -0
- package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
- package/src/components/Form/FormFieldComponent.tsx +174 -0
- package/src/components/Form/FormFieldSet.tsx +144 -0
- package/src/components/Form/README.md +3 -0
- package/src/components/Form/fields/ArrayField.tsx +155 -0
- package/src/components/Form/fields/BooleanField.tsx +22 -0
- package/src/components/Form/fields/GeoPointField.tsx +121 -0
- package/src/components/Form/fields/MarkdownField.stories.tsx +206 -0
- package/src/components/Form/fields/MarkdownField.tsx +168 -0
- package/src/components/Form/fields/NumberField.tsx +39 -0
- package/src/components/Form/fields/RefField.tsx +229 -0
- package/src/components/Form/fields/SelectField.tsx +65 -0
- package/src/components/Form/fields/SelectOptionField.stories.tsx +66 -0
- package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +51 -46
- package/src/components/Form/fields/TextAreaField.tsx +38 -0
- package/src/components/Form/fields/TextField.tsx +38 -0
- package/src/components/Form/{custom/TupleInput.tsx → fields/TupleField.tsx} +17 -10
- package/src/components/Form/fields/index.ts +15 -0
- package/src/components/Form/index.ts +3 -4
- package/src/components/ObjectForm/ObjectForm.tsx +114 -0
- package/src/components/{ControlSection → ObjectForm}/index.ts +1 -1
- package/src/components/ObjectPicker/ObjectPicker.stories.tsx +212 -0
- package/src/components/ObjectPicker/ObjectPicker.tsx +202 -0
- package/src/components/ObjectPicker/index.ts +5 -0
- package/src/components/ObjectProperties/ObjectProperties.stories.tsx +362 -0
- package/src/components/ObjectProperties/ObjectProperties.test.tsx +101 -0
- package/src/components/ObjectProperties/ObjectProperties.tsx +148 -0
- package/src/components/ObjectProperties/index.ts +5 -0
- package/src/components/Settings/AUDIT.md +51 -0
- package/src/components/Settings/Settings.stories.tsx +104 -0
- package/src/components/Settings/Settings.tsx +80 -0
- package/src/components/Settings/SettingsFieldSet.tsx +257 -0
- package/src/components/Settings/SettingsItem.tsx +58 -0
- package/src/components/Settings/SettingsSelectField.tsx +59 -0
- package/src/components/Settings/index.ts +7 -0
- package/src/components/ViewEditor/ViewEditor.stories.tsx +143 -60
- package/src/components/ViewEditor/ViewEditor.test.tsx +22 -22
- package/src/components/ViewEditor/ViewEditor.tsx +348 -220
- package/src/components/index.ts +4 -3
- package/src/components/testing.tsx +31 -12
- package/src/hooks/index.ts +2 -3
- package/src/hooks/useFormHandler.ts +380 -0
- package/src/hooks/useKeyHandler.tsx +37 -0
- package/src/index.ts +0 -4
- package/src/translations.ts +70 -58
- package/src/util/field-type.ts +60 -0
- package/src/util/field.ts +33 -43
- package/src/util/formatting.ts +22 -22
- package/src/util/index.ts +2 -1
- package/src/util/properties.test.ts +63 -0
- package/src/util/properties.ts +29 -0
- package/src/util/refs.ts +17 -19
- package/dist/types/src/components/ControlSection/ControlSection.d.ts +0 -23
- package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +0 -1
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +0 -8
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +0 -1
- package/dist/types/src/components/ControlSection/index.d.ts +0 -2
- package/dist/types/src/components/ControlSection/index.d.ts.map +0 -1
- package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
- package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
- package/dist/types/src/components/Form/Defaults.d.ts +0 -14
- package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
- package/dist/types/src/components/Form/FormActions.d.ts +0 -9
- package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
- package/dist/types/src/components/Form/FormContent.d.ts +0 -57
- package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
- package/dist/types/src/components/Form/FormContext.d.ts +0 -23
- package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
- package/dist/types/src/components/Form/Input.d.ts +0 -26
- package/dist/types/src/components/Form/Input.d.ts.map +0 -1
- package/dist/types/src/components/Form/RefField.d.ts +0 -12
- package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -7
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
- package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/index.d.ts +0 -4
- package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
- package/dist/types/src/components/Form/factory.d.ts +0 -8
- package/dist/types/src/components/Form/factory.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/Form.d.ts +0 -15
- package/dist/types/src/components/deprecated/Form.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/Form.stories.d.ts +0 -13
- package/dist/types/src/components/deprecated/Form.stories.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/FormInput.d.ts +0 -16
- package/dist/types/src/components/deprecated/FormInput.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/index.d.ts +0 -2
- package/dist/types/src/components/deprecated/index.d.ts.map +0 -1
- package/dist/types/src/hooks/useForm.d.ts +0 -67
- package/dist/types/src/hooks/useForm.d.ts.map +0 -1
- package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -23
- package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
- package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
- package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
- package/dist/types/src/util/schema.d.ts +0 -3
- package/dist/types/src/util/schema.d.ts.map +0 -1
- package/src/components/ControlSection/ControlSection.stories.tsx +0 -41
- package/src/components/ControlSection/ControlSection.tsx +0 -139
- package/src/components/Form/ArrayField.tsx +0 -124
- package/src/components/Form/Defaults.tsx +0 -203
- package/src/components/Form/FormActions.tsx +0 -53
- package/src/components/Form/FormContent.tsx +0 -240
- package/src/components/Form/FormContext.tsx +0 -110
- package/src/components/Form/Input.tsx +0 -51
- package/src/components/Form/RefField.tsx +0 -213
- package/src/components/Form/custom/GeoPointInput.tsx +0 -72
- package/src/components/Form/custom/SelectOptionsInput.stories.tsx +0 -61
- package/src/components/Form/custom/index.ts +0 -7
- package/src/components/Form/factory.tsx +0 -31
- package/src/components/deprecated/Form.stories.tsx +0 -62
- package/src/components/deprecated/Form.tsx +0 -119
- package/src/components/deprecated/FormInput.tsx +0 -71
- package/src/components/deprecated/index.ts +0 -5
- package/src/hooks/useForm.ts +0 -291
- package/src/hooks/useQueryRefOptions.ts +0 -53
- package/src/hooks/useRefQueryLookupHandler.ts +0 -39
- package/src/util/schema.ts +0 -14
package/src/hooks/useForm.ts
DELETED
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { SchemaAST, type Schema } from 'effect';
|
|
6
|
-
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
7
|
-
|
|
8
|
-
import { type BaseObject, getValue, setValue } from '@dxos/echo-schema';
|
|
9
|
-
import { type SimpleType, type JsonPath, createJsonPath, fromEffectValidationPath } from '@dxos/effect';
|
|
10
|
-
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import { log } from '@dxos/log';
|
|
12
|
-
import { validateSchema, type ValidationError } from '@dxos/schema';
|
|
13
|
-
import { type MaybePromise } from '@dxos/util';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Return type from `useForm` hook.
|
|
17
|
-
*/
|
|
18
|
-
export type FormHandler<T extends BaseObject> = {
|
|
19
|
-
//
|
|
20
|
-
// Form state management.
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
values: Partial<T>;
|
|
24
|
-
errors: Record<JsonPath, string>;
|
|
25
|
-
touched: Record<JsonPath, boolean>;
|
|
26
|
-
changed: Record<JsonPath, boolean>;
|
|
27
|
-
canSave: boolean;
|
|
28
|
-
formIsValid: boolean;
|
|
29
|
-
handleSave: () => void;
|
|
30
|
-
|
|
31
|
-
//
|
|
32
|
-
// Form input component helpers.
|
|
33
|
-
//
|
|
34
|
-
|
|
35
|
-
getStatus: (path: string | (string | number)[]) => { status?: 'error'; error?: string };
|
|
36
|
-
getValue: <V>(path: (string | number)[]) => V | undefined;
|
|
37
|
-
onValueChange: <V>(path: (string | number)[], type: SimpleType, value: V) => void;
|
|
38
|
-
onTouched: (path: (string | number)[]) => void;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Hook options.
|
|
43
|
-
*/
|
|
44
|
-
export interface FormOptions<T extends BaseObject> {
|
|
45
|
-
/**
|
|
46
|
-
* Effect schema (Type literal).
|
|
47
|
-
*/
|
|
48
|
-
schema: Schema.Schema<T, any>;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Initial values (which may not pass validation).
|
|
52
|
-
*/
|
|
53
|
-
initialValues: Partial<T>;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Callback for value changes. Note: This is called even when values are invalid.
|
|
57
|
-
* Sometimes the parent component may want to know about changes even if the form is
|
|
58
|
-
* in an invalid state.
|
|
59
|
-
*/
|
|
60
|
-
onValuesChanged?: (values: Partial<T>) => void;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Custom validation function that runs only after schema validation passes.
|
|
64
|
-
* Use this for complex validation logic that can't be expressed in the schema.
|
|
65
|
-
* @returns Array of validation errors, or undefined if validation passes
|
|
66
|
-
*/
|
|
67
|
-
// TODO(burdon): Change to key x value?
|
|
68
|
-
onValidate?: (values: T) => ValidationError[] | undefined;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Called when the form is valid.
|
|
72
|
-
*/
|
|
73
|
-
onValid?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => void;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Called when the form is submitted and passes validation.
|
|
77
|
-
*/
|
|
78
|
-
onSave?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => MaybePromise<void>;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Creates a hook for managing form state, including values, validation, and submission.
|
|
83
|
-
* Deeply integrated with `@dxos/schema` for schema-based validation.
|
|
84
|
-
*/
|
|
85
|
-
export const useForm = <T extends BaseObject>({
|
|
86
|
-
schema,
|
|
87
|
-
initialValues,
|
|
88
|
-
onValuesChanged,
|
|
89
|
-
onValidate,
|
|
90
|
-
onValid,
|
|
91
|
-
onSave,
|
|
92
|
-
}: FormOptions<T>): FormHandler<T> => {
|
|
93
|
-
invariant(SchemaAST.isTypeLiteral(schema.ast));
|
|
94
|
-
|
|
95
|
-
const [values, setValues] = useState<Partial<T>>(initialValues);
|
|
96
|
-
|
|
97
|
-
useEffect(() => {
|
|
98
|
-
setValues(initialValues);
|
|
99
|
-
}, [initialValues]);
|
|
100
|
-
|
|
101
|
-
const [touched, setTouched] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
102
|
-
const [changed, setChanged] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
103
|
-
const [errors, setErrors] = useState<Record<JsonPath, string>>({});
|
|
104
|
-
const [saving, setSaving] = useState(false);
|
|
105
|
-
|
|
106
|
-
//
|
|
107
|
-
// Validation.
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
const validate = useCallback(
|
|
111
|
-
(values: Partial<T>): values is T => {
|
|
112
|
-
let errors: ValidationError[] = validateSchema(schema, values) ?? [];
|
|
113
|
-
if (errors.length === 0 && onValidate) {
|
|
114
|
-
const validatedValues = values as T;
|
|
115
|
-
errors = onValidate(validatedValues) ?? [];
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
setErrors(flatMap(errors));
|
|
119
|
-
const valid = errors.length === 0;
|
|
120
|
-
return valid;
|
|
121
|
-
},
|
|
122
|
-
[schema, onValidate],
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
// Validate on schema change.
|
|
126
|
-
const [schemaDirty, setSchemaDirty] = useState(false);
|
|
127
|
-
useEffect(() => setSchemaDirty(true), [schema]);
|
|
128
|
-
useEffect(() => {
|
|
129
|
-
if (schemaDirty) {
|
|
130
|
-
validate(values);
|
|
131
|
-
setSchemaDirty(false);
|
|
132
|
-
}
|
|
133
|
-
}, [values, schemaDirty]);
|
|
134
|
-
|
|
135
|
-
const formIsValid = useMemo(() => {
|
|
136
|
-
return Object.keys(errors).length === 0;
|
|
137
|
-
}, [errors]);
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* NOTE: We can submit if there is no touched field that has an error.
|
|
141
|
-
* Basically, if there's a validation message visible in the form, submit should be disabled.
|
|
142
|
-
*/
|
|
143
|
-
const canSave = useMemo(
|
|
144
|
-
() =>
|
|
145
|
-
!saving &&
|
|
146
|
-
// Check if any error paths that are touched have errors.
|
|
147
|
-
!Object.entries(touched).some(
|
|
148
|
-
([path, isTouched]) =>
|
|
149
|
-
isTouched &&
|
|
150
|
-
Object.keys(errors).some(
|
|
151
|
-
(errorPath) => errorPath === path || errorPath.startsWith(`${path}.`) || errorPath.startsWith(`${path}[`),
|
|
152
|
-
),
|
|
153
|
-
),
|
|
154
|
-
[touched, errors, saving],
|
|
155
|
-
);
|
|
156
|
-
|
|
157
|
-
const handleSave = useCallback(async () => {
|
|
158
|
-
if (validate(values)) {
|
|
159
|
-
setSaving(true);
|
|
160
|
-
try {
|
|
161
|
-
await onSave?.(values, { changed });
|
|
162
|
-
} finally {
|
|
163
|
-
setSaving(false);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}, [values, validate, onSave, changed]);
|
|
167
|
-
|
|
168
|
-
//
|
|
169
|
-
// Fields.
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
const getStatus = useCallback<FormHandler<T>['getStatus']>(
|
|
173
|
-
(path) => {
|
|
174
|
-
const jsonPath = Array.isArray(path) ? createJsonPath(path) : path;
|
|
175
|
-
const matchingError = Object.entries(errors).find(
|
|
176
|
-
([errorPath]) =>
|
|
177
|
-
errorPath === jsonPath || errorPath.startsWith(`${jsonPath}.`) || errorPath.startsWith(`${jsonPath}[`),
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
// Only show errors for touched fields.
|
|
181
|
-
const isTouched = touched[jsonPath as JsonPath];
|
|
182
|
-
if (!isTouched) {
|
|
183
|
-
return {
|
|
184
|
-
status: undefined,
|
|
185
|
-
error: undefined,
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return {
|
|
190
|
-
status: matchingError ? 'error' : undefined,
|
|
191
|
-
error: matchingError ? matchingError[1] : undefined,
|
|
192
|
-
};
|
|
193
|
-
},
|
|
194
|
-
[errors, touched],
|
|
195
|
-
);
|
|
196
|
-
|
|
197
|
-
const getFormValue = useCallback<FormHandler<T>['getValue']>(
|
|
198
|
-
<V>(path: (string | number)[]): V | undefined => {
|
|
199
|
-
return getValue(values, createJsonPath(path));
|
|
200
|
-
},
|
|
201
|
-
[values],
|
|
202
|
-
);
|
|
203
|
-
|
|
204
|
-
const onValueChange = useCallback<FormHandler<T>['onValueChange']>(
|
|
205
|
-
(path: (string | number)[], type: SimpleType, value: any) => {
|
|
206
|
-
const jsonPath = createJsonPath(path);
|
|
207
|
-
let parsedValue = value;
|
|
208
|
-
try {
|
|
209
|
-
if (type === 'number') {
|
|
210
|
-
parsedValue = parseFloat(value as string) || 0;
|
|
211
|
-
}
|
|
212
|
-
} catch (err) {
|
|
213
|
-
log.catch(err);
|
|
214
|
-
parsedValue = undefined;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const newValues = { ...setValue(values, jsonPath, parsedValue) };
|
|
218
|
-
setValues(newValues);
|
|
219
|
-
setChanged((prev) => ({ ...prev, [jsonPath]: true }));
|
|
220
|
-
onValuesChanged?.(newValues);
|
|
221
|
-
|
|
222
|
-
const isValid = validate(newValues);
|
|
223
|
-
if (isValid && onValid) {
|
|
224
|
-
onValid(newValues, { changed });
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
[values, onValuesChanged, validate, onValid, changed],
|
|
228
|
-
);
|
|
229
|
-
|
|
230
|
-
const onTouched = useCallback(
|
|
231
|
-
(path: (string | number)[]) => {
|
|
232
|
-
const jsonPath = createJsonPath(path);
|
|
233
|
-
setTouched((touched) => ({ ...touched, [jsonPath]: true }));
|
|
234
|
-
validate(values);
|
|
235
|
-
},
|
|
236
|
-
[validate, values],
|
|
237
|
-
);
|
|
238
|
-
|
|
239
|
-
return useMemo(
|
|
240
|
-
() => ({
|
|
241
|
-
// State.
|
|
242
|
-
values,
|
|
243
|
-
errors,
|
|
244
|
-
touched,
|
|
245
|
-
changed,
|
|
246
|
-
canSave,
|
|
247
|
-
formIsValid,
|
|
248
|
-
|
|
249
|
-
// Actions.
|
|
250
|
-
handleSave,
|
|
251
|
-
|
|
252
|
-
// Field utils.
|
|
253
|
-
getStatus,
|
|
254
|
-
getValue: getFormValue,
|
|
255
|
-
onValueChange,
|
|
256
|
-
onTouched,
|
|
257
|
-
}),
|
|
258
|
-
[
|
|
259
|
-
values,
|
|
260
|
-
errors,
|
|
261
|
-
touched,
|
|
262
|
-
changed,
|
|
263
|
-
canSave,
|
|
264
|
-
formIsValid,
|
|
265
|
-
handleSave,
|
|
266
|
-
getStatus,
|
|
267
|
-
getFormValue,
|
|
268
|
-
onValueChange,
|
|
269
|
-
onTouched,
|
|
270
|
-
],
|
|
271
|
-
);
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPath, V> => {
|
|
275
|
-
invariant(obj);
|
|
276
|
-
return Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: value }), {} as Record<JsonPath, V>);
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const flatMap = (errors: ValidationError[]) => {
|
|
280
|
-
return errors.reduce(
|
|
281
|
-
(result, { path, message }) => {
|
|
282
|
-
// Convert the validation error path format to our JsonPath format.
|
|
283
|
-
const jsonPath = fromEffectValidationPath(path);
|
|
284
|
-
if (!(jsonPath in result)) {
|
|
285
|
-
result[jsonPath] = message;
|
|
286
|
-
}
|
|
287
|
-
return result;
|
|
288
|
-
},
|
|
289
|
-
{} as Record<JsonPath, string>,
|
|
290
|
-
);
|
|
291
|
-
};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { useEffect, useState } from 'react';
|
|
6
|
-
|
|
7
|
-
import { type TypeAnnotation } from '@dxos/echo-schema';
|
|
8
|
-
import { type DXN } from '@dxos/keys';
|
|
9
|
-
import { log } from '@dxos/log';
|
|
10
|
-
import { type TagPickerItemData } from '@dxos/react-ui-tag-picker';
|
|
11
|
-
import { type MaybePromise } from '@dxos/util';
|
|
12
|
-
|
|
13
|
-
export type RefOption = { dxn: DXN; label?: string };
|
|
14
|
-
export type QueryRefOptions = (type: TypeAnnotation) => MaybePromise<RefOption[]>;
|
|
15
|
-
|
|
16
|
-
type UseQueryRefOptionsProps = { refTypeInfo: TypeAnnotation | undefined; onQueryRefOptions?: QueryRefOptions };
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Hook to query reference options based on type information.
|
|
20
|
-
* Used internally within forms to fetch and format reference options for reference fields.
|
|
21
|
-
*/
|
|
22
|
-
export const useQueryRefOptions = ({ refTypeInfo, onQueryRefOptions }: UseQueryRefOptionsProps) => {
|
|
23
|
-
const [options, setOptions] = useState<TagPickerItemData[]>([]);
|
|
24
|
-
const [loading, setLoading] = useState(false);
|
|
25
|
-
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
if (!refTypeInfo || !onQueryRefOptions) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const fetchOptions = async () => {
|
|
32
|
-
setLoading(true);
|
|
33
|
-
try {
|
|
34
|
-
const fetchedOptions = await onQueryRefOptions(refTypeInfo);
|
|
35
|
-
setOptions(
|
|
36
|
-
fetchedOptions.map((option) => {
|
|
37
|
-
const dxn = option.dxn.toString() as string;
|
|
38
|
-
return { id: dxn, label: option.label ?? dxn, hue: 'neutral' as any };
|
|
39
|
-
}),
|
|
40
|
-
);
|
|
41
|
-
} catch (error) {
|
|
42
|
-
log.error('Failed to fetch ref options:', error);
|
|
43
|
-
setOptions([]);
|
|
44
|
-
} finally {
|
|
45
|
-
setLoading(false);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
void fetchOptions();
|
|
50
|
-
}, [refTypeInfo, onQueryRefOptions]);
|
|
51
|
-
|
|
52
|
-
return { options, loading };
|
|
53
|
-
};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { useCallback } from 'react';
|
|
6
|
-
|
|
7
|
-
import { getObjectDXN, type TypeAnnotation } from '@dxos/echo-schema';
|
|
8
|
-
import { Filter, type Space } from '@dxos/react-client/echo';
|
|
9
|
-
import { isNonNullable } from '@dxos/util';
|
|
10
|
-
|
|
11
|
-
import { type QueryRefOptions } from './useQueryRefOptions';
|
|
12
|
-
|
|
13
|
-
type UseRefQueryLookupProps = { space?: Space };
|
|
14
|
-
|
|
15
|
-
export const useRefQueryLookupHandler = ({ space }: UseRefQueryLookupProps): QueryRefOptions => {
|
|
16
|
-
return useCallback(
|
|
17
|
-
async (typeInfo: TypeAnnotation) => {
|
|
18
|
-
if (!space) {
|
|
19
|
-
return [];
|
|
20
|
-
}
|
|
21
|
-
const query = space.db.query(Filter.typename(typeInfo.typename));
|
|
22
|
-
const { objects } = await query.run();
|
|
23
|
-
|
|
24
|
-
return objects
|
|
25
|
-
.map((object) => {
|
|
26
|
-
const dxn = getObjectDXN(object);
|
|
27
|
-
if (!dxn) {
|
|
28
|
-
return undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// TODO(Zaymon): Better fallback object names?
|
|
32
|
-
const item = { dxn, label: object?.name ?? object?.id ?? '' };
|
|
33
|
-
return item;
|
|
34
|
-
})
|
|
35
|
-
.filter(isNonNullable);
|
|
36
|
-
},
|
|
37
|
-
[space],
|
|
38
|
-
);
|
|
39
|
-
};
|
package/src/util/schema.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { SchemaAST } from 'effect';
|
|
6
|
-
|
|
7
|
-
import { findNode } from '@dxos/effect';
|
|
8
|
-
|
|
9
|
-
export const findArrayElementType = (ast: SchemaAST.AST) => {
|
|
10
|
-
const tupleType = findNode(ast, SchemaAST.isTupleType);
|
|
11
|
-
const elementType = (tupleType as SchemaAST.TupleType | undefined)?.rest[0]?.type;
|
|
12
|
-
|
|
13
|
-
return elementType;
|
|
14
|
-
};
|