@dxos/react-ui-form 0.8.4-main.2e9d522 → 0.8.4-main.3c1ae3b
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 +2138 -1738
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +2138 -1738
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/ControlSection/ControlSection.d.ts +2 -2
- package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +1 -1
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +84 -5
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +5 -5
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +82 -5
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form-old.stories.d.ts +172 -0
- package/dist/types/src/components/Form/Form-old.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/Form.d.ts +78 -25
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +123 -51
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormField.d.ts +37 -0
- package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts +78 -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 +19 -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 +4 -0
- package/dist/types/src/components/Form/fields/MarkdownField.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 +18 -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 +82 -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/ObjectPicker/ObjectPicker.d.ts +41 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +85 -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/ViewEditor/ViewEditor.d.ts +21 -9
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +83 -8
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +1 -1
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +4 -3
- package/dist/types/src/components/testing.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +3 -2
- 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 +6 -0
- package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
- package/dist/types/src/hooks/useQueryRefOptions.d.ts +13 -6
- package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +1 -1
- package/dist/types/src/hooks/useRefQueryOptions.d.ts +8 -0
- package/dist/types/src/hooks/useRefQueryOptions.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +17 -8
- package/dist/types/src/translations.d.ts.map +1 -1
- 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 +1 -1
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/properties.d.ts +7 -0
- package/dist/types/src/util/properties.d.ts.map +1 -0
- package/dist/types/src/util/refs.d.ts +2 -3
- package/dist/types/src/util/refs.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +43 -36
- package/src/components/ControlSection/ControlSection.stories.tsx +11 -14
- package/src/components/ControlSection/ControlSection.tsx +26 -12
- package/src/components/FieldEditor/FieldEditor.stories.tsx +20 -20
- package/src/components/FieldEditor/FieldEditor.test.tsx +3 -2
- package/src/components/FieldEditor/FieldEditor.tsx +75 -77
- package/src/components/Form/Form-old.stories.tsx +314 -0
- package/src/components/Form/Form.stories.tsx +163 -219
- package/src/components/Form/Form.tsx +306 -98
- package/src/components/Form/FormField.tsx +280 -0
- package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
- package/src/components/Form/FormFieldComponent.tsx +173 -0
- package/src/components/Form/FormFieldSet.tsx +124 -0
- package/src/components/Form/README.md +3 -0
- package/src/components/Form/fields/ArrayField.tsx +154 -0
- package/src/components/Form/fields/BooleanField.tsx +22 -0
- package/src/components/Form/fields/GeoPointField.tsx +120 -0
- package/src/components/Form/fields/MarkdownField.tsx +78 -0
- package/src/components/Form/fields/NumberField.tsx +39 -0
- package/src/components/Form/fields/RefField.tsx +206 -0
- package/src/components/Form/fields/SelectField.tsx +65 -0
- package/src/components/Form/{custom/SelectOptionsInput.stories.tsx → fields/SelectOptionField.stories.tsx} +25 -22
- package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +47 -42
- 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} +15 -8
- package/src/components/Form/fields/index.ts +15 -0
- package/src/components/Form/index.ts +3 -4
- package/src/components/ObjectPicker/ObjectPicker.stories.tsx +207 -0
- package/src/components/ObjectPicker/ObjectPicker.tsx +166 -0
- package/src/components/ObjectPicker/index.ts +5 -0
- package/src/components/ViewEditor/ViewEditor.stories.tsx +131 -61
- package/src/components/ViewEditor/ViewEditor.test.tsx +19 -17
- package/src/components/ViewEditor/ViewEditor.tsx +277 -197
- package/src/components/index.ts +1 -2
- package/src/components/testing.tsx +20 -10
- package/src/hooks/index.ts +3 -2
- package/src/hooks/useFormHandler.ts +348 -0
- package/src/hooks/useKeyHandler.tsx +37 -0
- package/src/hooks/useQueryRefOptions.ts +49 -32
- package/src/hooks/useRefQueryOptions.ts +37 -0
- package/src/translations.ts +20 -11
- package/src/util/field.ts +33 -43
- package/src/util/formatting.ts +22 -22
- package/src/util/index.ts +1 -1
- package/src/util/properties.ts +18 -0
- package/src/util/refs.ts +12 -18
- 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/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/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/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/useRefQueryLookupHandler.ts +0 -39
- package/src/util/schema.ts +0 -14
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { useControllableState } from '@radix-ui/react-use-controllable-state';
|
|
6
|
+
import type * as Schema from 'effect/Schema';
|
|
7
|
+
import type * as SchemaAST from 'effect/SchemaAST';
|
|
8
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
9
|
+
|
|
10
|
+
import { type AnyProperties, getValue as getValue$, setValue as setValue$ } from '@dxos/echo/internal';
|
|
11
|
+
import { type JsonPath, createJsonPath, fromEffectValidationPath } from '@dxos/effect';
|
|
12
|
+
import { log } from '@dxos/log';
|
|
13
|
+
import { useDefaultValue } from '@dxos/react-ui';
|
|
14
|
+
import { type ValidationError, validateSchema } from '@dxos/schema';
|
|
15
|
+
import { type MaybePromise } from '@dxos/util';
|
|
16
|
+
|
|
17
|
+
export type FormFieldStatus = {
|
|
18
|
+
status?: 'error';
|
|
19
|
+
error?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Form properties.
|
|
24
|
+
*/
|
|
25
|
+
export interface FormHandlerProps<T extends AnyProperties> {
|
|
26
|
+
/**
|
|
27
|
+
* Effect schema (Type literal).
|
|
28
|
+
*/
|
|
29
|
+
schema?: Schema.Schema<T, any>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Values for the form if controlled.
|
|
33
|
+
*/
|
|
34
|
+
values?: Partial<T>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Default values for the form which will be used if values is not provided.
|
|
38
|
+
*/
|
|
39
|
+
defaultValues?: Partial<T>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Auto-save the form when the values change.
|
|
43
|
+
*/
|
|
44
|
+
autoSave?: boolean;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Callback for value changes. Note: This is called even when values are invalid.
|
|
48
|
+
* Sometimes the parent component may want to know about changes even if the form is
|
|
49
|
+
* in an invalid state.
|
|
50
|
+
*/
|
|
51
|
+
onValuesChanged?: (values: Partial<T>, meta: FormUpdateMeta<T>) => void;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Custom validation function that runs only after schema validation passes.
|
|
55
|
+
* Use this for complex validation logic that can't be expressed in the schema.
|
|
56
|
+
* @returns Array of validation errors, or undefined if validation passes
|
|
57
|
+
*/
|
|
58
|
+
onValidate?: (values: T) => ValidationError[] | undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Called when the form is submitted and passes validation.
|
|
62
|
+
*/
|
|
63
|
+
onSave?: (values: T, meta: FormUpdateMeta<T>) => MaybePromise<void>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Called when the form is canceled.
|
|
67
|
+
*/
|
|
68
|
+
onCancel?: () => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type FormUpdateMeta<T extends AnyProperties> = {
|
|
72
|
+
changed: FormHandler<T>['changed'];
|
|
73
|
+
isValid: boolean;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Form handler properties and methods.
|
|
78
|
+
*/
|
|
79
|
+
export type FormHandler<T extends AnyProperties> = Pick<FormHandlerProps<T>, 'schema' | 'autoSave'> & {
|
|
80
|
+
/** Initial values (which may not pass validation). */
|
|
81
|
+
values: Partial<T>;
|
|
82
|
+
|
|
83
|
+
/** Map of changed fields. */
|
|
84
|
+
changed: Record<JsonPath, boolean>;
|
|
85
|
+
// TODO(burdon): How is this different from above?
|
|
86
|
+
touched: Record<JsonPath, boolean>;
|
|
87
|
+
|
|
88
|
+
/** Map of error strings. */
|
|
89
|
+
errors: Record<JsonPath, string>;
|
|
90
|
+
|
|
91
|
+
/** Whether the form can be saved (i.e., data is valid). */
|
|
92
|
+
isValid: boolean;
|
|
93
|
+
// TODO(burdon): Why is this needed separately from isValid?
|
|
94
|
+
canSave: boolean;
|
|
95
|
+
|
|
96
|
+
onSave: () => void;
|
|
97
|
+
onCancel: () => void;
|
|
98
|
+
|
|
99
|
+
//
|
|
100
|
+
// Form field state management
|
|
101
|
+
//
|
|
102
|
+
|
|
103
|
+
getStatus: (path: string | (string | number)[]) => FormFieldStatus;
|
|
104
|
+
getValue: <V>(path: (string | number)[]) => V | undefined;
|
|
105
|
+
onBlur: (path: (string | number)[]) => void;
|
|
106
|
+
onValueChange: <V>(path: (string | number)[], type: SchemaAST.AST, value: V) => void;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Creates a hook for managing form state, including values, validation, and submission.
|
|
111
|
+
* Deeply integrated with `@dxos/schema` for schema-based validation.
|
|
112
|
+
*/
|
|
113
|
+
export const useFormHandler = <T extends AnyProperties>({
|
|
114
|
+
schema,
|
|
115
|
+
autoSave,
|
|
116
|
+
values: valuesProp,
|
|
117
|
+
defaultValues: defaultValuesProp,
|
|
118
|
+
onValuesChanged,
|
|
119
|
+
onValidate,
|
|
120
|
+
onSave,
|
|
121
|
+
onCancel,
|
|
122
|
+
...props
|
|
123
|
+
}: FormHandlerProps<T>): FormHandler<T> => {
|
|
124
|
+
const [changed, setChanged] = useState<Record<JsonPath, boolean>>({});
|
|
125
|
+
const [touched, setTouched] = useState<Record<JsonPath, boolean>>({});
|
|
126
|
+
const [errors, setErrors] = useState<Record<JsonPath, string>>({});
|
|
127
|
+
const [saving, setSaving] = useState(false);
|
|
128
|
+
const defaultValues = useDefaultValue<Partial<T>>(defaultValuesProp, () => ({}));
|
|
129
|
+
const [values, setValues] = useControllableState<Partial<T>>({
|
|
130
|
+
prop: valuesProp,
|
|
131
|
+
defaultProp: defaultValues,
|
|
132
|
+
onChange: () => {
|
|
133
|
+
setValues(valuesProp ?? defaultValues);
|
|
134
|
+
setChanged({});
|
|
135
|
+
setTouched({});
|
|
136
|
+
setErrors({});
|
|
137
|
+
setSaving(false);
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Validate.
|
|
142
|
+
const validate = useCallback(
|
|
143
|
+
(values: Partial<T>): values is T => {
|
|
144
|
+
if (!schema) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let errors: ValidationError[] = validateSchema(schema, values) ?? [];
|
|
149
|
+
log('validate', { values, errors });
|
|
150
|
+
if (errors.length === 0 && onValidate) {
|
|
151
|
+
const validatedValues = values as T;
|
|
152
|
+
errors = onValidate(validatedValues) ?? [];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
setErrors(flatMap(errors));
|
|
156
|
+
const valid = errors.length === 0;
|
|
157
|
+
return valid;
|
|
158
|
+
},
|
|
159
|
+
[schema, onValidate],
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
// Validate on schema change.
|
|
163
|
+
const [schemaChanged, setSchemaChanged] = useState(false);
|
|
164
|
+
useEffect(() => setSchemaChanged(true), [schema]);
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
if (schemaChanged) {
|
|
167
|
+
validate(values);
|
|
168
|
+
setSchemaChanged(false);
|
|
169
|
+
}
|
|
170
|
+
}, [validate, values, schemaChanged]);
|
|
171
|
+
|
|
172
|
+
const isValid = useMemo(() => {
|
|
173
|
+
return Object.keys(errors).length === 0;
|
|
174
|
+
}, [errors]);
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* NOTE: We can submit if there is no touched field that has an error.
|
|
178
|
+
* If there's a validation message visible in the form, submit should be disabled.
|
|
179
|
+
*/
|
|
180
|
+
const canSave = useMemo(
|
|
181
|
+
() =>
|
|
182
|
+
!saving &&
|
|
183
|
+
// Check if any error paths that are touched have errors.
|
|
184
|
+
!Object.entries(touched).some(
|
|
185
|
+
([path, isTouched]) =>
|
|
186
|
+
isTouched &&
|
|
187
|
+
Object.keys(errors).some(
|
|
188
|
+
(errorPath) => errorPath === path || errorPath.startsWith(`${path}.`) || errorPath.startsWith(`${path}[`),
|
|
189
|
+
),
|
|
190
|
+
),
|
|
191
|
+
[touched, errors, saving],
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
const handleSave = useCallback(async () => {
|
|
195
|
+
if (validate(values)) {
|
|
196
|
+
setSaving(true);
|
|
197
|
+
try {
|
|
198
|
+
await onSave?.(values, { changed, isValid });
|
|
199
|
+
} finally {
|
|
200
|
+
setSaving(false);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}, [values, validate, onSave, changed, isValid]);
|
|
204
|
+
|
|
205
|
+
const handleCancel = useCallback(() => {
|
|
206
|
+
onCancel?.();
|
|
207
|
+
}, [onCancel]);
|
|
208
|
+
|
|
209
|
+
//
|
|
210
|
+
// Fields.
|
|
211
|
+
//
|
|
212
|
+
|
|
213
|
+
const getStatus = useCallback<FormHandler<T>['getStatus']>(
|
|
214
|
+
(path) => {
|
|
215
|
+
const jsonPath = Array.isArray(path) ? createJsonPath(path) : path;
|
|
216
|
+
const [_, error] =
|
|
217
|
+
Object.entries(errors).find(
|
|
218
|
+
([errorPath]) =>
|
|
219
|
+
errorPath === jsonPath || errorPath.startsWith(`${jsonPath}.`) || errorPath.startsWith(`${jsonPath}[`),
|
|
220
|
+
) ?? [];
|
|
221
|
+
|
|
222
|
+
// Only show errors for touched fields.
|
|
223
|
+
const isTouched = touched[jsonPath as JsonPath];
|
|
224
|
+
if (!isTouched) {
|
|
225
|
+
return {
|
|
226
|
+
status: undefined,
|
|
227
|
+
error: undefined,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
status: error ? 'error' : undefined,
|
|
233
|
+
error: error ? (error ?? undefined) : undefined,
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
[errors, touched],
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
const getValue = useCallback<FormHandler<T>['getValue']>(
|
|
240
|
+
(path) => {
|
|
241
|
+
return getValue$(values, createJsonPath(path));
|
|
242
|
+
},
|
|
243
|
+
[values],
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const onValueChange = useCallback<FormHandler<T>['onValueChange']>(
|
|
247
|
+
(path, type, value) => {
|
|
248
|
+
log('onValueChange', { path, value });
|
|
249
|
+
|
|
250
|
+
const jsonPath = createJsonPath(path);
|
|
251
|
+
let parsedValue = value as any;
|
|
252
|
+
try {
|
|
253
|
+
if (type._tag === 'NumberKeyword') {
|
|
254
|
+
parsedValue = parseFloat(value as string) || 0;
|
|
255
|
+
}
|
|
256
|
+
} catch (err) {
|
|
257
|
+
log.catch(err);
|
|
258
|
+
parsedValue = undefined;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Update.
|
|
262
|
+
const newValues = { ...setValue$(values, jsonPath, parsedValue) };
|
|
263
|
+
setValues(newValues);
|
|
264
|
+
|
|
265
|
+
// TODO(burdon): Check value has changed from original.
|
|
266
|
+
const newChanged = { ...changed, [jsonPath]: true };
|
|
267
|
+
setChanged({ ...changed, [jsonPath]: true });
|
|
268
|
+
|
|
269
|
+
// Validate.
|
|
270
|
+
const isValid = validate(newValues);
|
|
271
|
+
|
|
272
|
+
// Callback.
|
|
273
|
+
onValuesChanged?.(newValues, { isValid, changed: newChanged });
|
|
274
|
+
},
|
|
275
|
+
[values, changed, validate, onValuesChanged],
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const onBlur = useCallback(
|
|
279
|
+
async (path: (string | number)[]) => {
|
|
280
|
+
const jsonPath = createJsonPath(path);
|
|
281
|
+
|
|
282
|
+
// TODO(burdon): Check value has changed from original.
|
|
283
|
+
setTouched((touched) => ({ ...touched, [jsonPath]: true }));
|
|
284
|
+
const isValid = validate(values);
|
|
285
|
+
|
|
286
|
+
// Auto-save when a field is blurred and is valid.
|
|
287
|
+
if (Object.keys(changed).length > 0 && isValid && autoSave) {
|
|
288
|
+
await onSave?.(values as T, { changed, isValid });
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
[validate, values, changed, autoSave, onSave],
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
return useMemo<FormHandler<T>>(
|
|
295
|
+
() => ({
|
|
296
|
+
// State.
|
|
297
|
+
schema,
|
|
298
|
+
values,
|
|
299
|
+
errors,
|
|
300
|
+
touched,
|
|
301
|
+
changed,
|
|
302
|
+
isValid,
|
|
303
|
+
canSave,
|
|
304
|
+
autoSave,
|
|
305
|
+
|
|
306
|
+
// Actions.
|
|
307
|
+
onSave: handleSave,
|
|
308
|
+
onCancel: handleCancel,
|
|
309
|
+
|
|
310
|
+
// Field utils.
|
|
311
|
+
getStatus,
|
|
312
|
+
getValue,
|
|
313
|
+
onBlur,
|
|
314
|
+
onValueChange,
|
|
315
|
+
|
|
316
|
+
...props,
|
|
317
|
+
}),
|
|
318
|
+
[
|
|
319
|
+
schema,
|
|
320
|
+
values,
|
|
321
|
+
errors,
|
|
322
|
+
touched,
|
|
323
|
+
changed,
|
|
324
|
+
canSave,
|
|
325
|
+
isValid,
|
|
326
|
+
getStatus,
|
|
327
|
+
getValue,
|
|
328
|
+
onBlur,
|
|
329
|
+
onValueChange,
|
|
330
|
+
onSave,
|
|
331
|
+
onCancel,
|
|
332
|
+
],
|
|
333
|
+
);
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const flatMap = (errors: ValidationError[]) => {
|
|
337
|
+
return errors.reduce(
|
|
338
|
+
(result, { path, message }) => {
|
|
339
|
+
// Convert the validation error path format to our JsonPath format.
|
|
340
|
+
const jsonPath = fromEffectValidationPath(path);
|
|
341
|
+
if (!(jsonPath in result)) {
|
|
342
|
+
result[jsonPath] = message;
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
},
|
|
346
|
+
{} as Record<JsonPath, string>,
|
|
347
|
+
);
|
|
348
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { useCallback, useEffect } from 'react';
|
|
6
|
+
|
|
7
|
+
import { addEventListener } from '@dxos/async';
|
|
8
|
+
|
|
9
|
+
import { type FormHandler } from './useFormHandler';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Key handler.
|
|
13
|
+
*/
|
|
14
|
+
export const useKeyHandler = (formElement: HTMLDivElement | null, form: FormHandler<any>) => {
|
|
15
|
+
const handleKeyDown = useCallback(
|
|
16
|
+
(event: KeyboardEvent) => {
|
|
17
|
+
switch (event.key) {
|
|
18
|
+
case 'Enter': {
|
|
19
|
+
if (event.metaKey && form.canSave) {
|
|
20
|
+
form.onSave();
|
|
21
|
+
}
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
[form.isValid, form.canSave, form.onSave, form.autoSave],
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!formElement) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// TODO(burdon): Move to @dxos/dom-util.
|
|
35
|
+
return addEventListener(formElement, 'keydown', handleKeyDown);
|
|
36
|
+
}, [formElement, handleKeyDown]);
|
|
37
|
+
};
|
|
@@ -2,52 +2,69 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { useCallback, useState } from 'react';
|
|
6
6
|
|
|
7
|
-
import { type TypeAnnotation } from '@dxos/echo-schema';
|
|
8
7
|
import { type DXN } from '@dxos/keys';
|
|
9
8
|
import { log } from '@dxos/log';
|
|
10
|
-
import {
|
|
9
|
+
import { useAsyncEffect } from '@dxos/react-ui';
|
|
10
|
+
import { type Palette } from '@dxos/react-ui-types';
|
|
11
11
|
import { type MaybePromise } from '@dxos/util';
|
|
12
12
|
|
|
13
|
-
export type RefOption = {
|
|
14
|
-
|
|
13
|
+
export type RefOption = {
|
|
14
|
+
dxn: DXN;
|
|
15
|
+
label?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type QueryRefOptions = ({ typename }: { typename: string }) => MaybePromise<RefOption[]>;
|
|
15
19
|
|
|
16
|
-
type
|
|
20
|
+
export type QueryTag = {
|
|
21
|
+
id: string;
|
|
22
|
+
label: string;
|
|
23
|
+
hue?: Palette;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type UseQueryRefOptionsProps = {
|
|
27
|
+
typename?: string;
|
|
28
|
+
onQueryRefOptions?: QueryRefOptions;
|
|
29
|
+
};
|
|
17
30
|
|
|
18
31
|
/**
|
|
19
32
|
* Hook to query reference options based on type information.
|
|
20
33
|
* Used internally within forms to fetch and format reference options for reference fields.
|
|
21
34
|
*/
|
|
22
|
-
export const useQueryRefOptions = ({
|
|
23
|
-
const [options, setOptions] = useState<
|
|
35
|
+
export const useQueryRefOptions = ({ typename, onQueryRefOptions }: UseQueryRefOptionsProps) => {
|
|
36
|
+
const [options, setOptions] = useState<QueryTag[]>([]);
|
|
24
37
|
const [loading, setLoading] = useState(false);
|
|
38
|
+
const [state, setState] = useState({});
|
|
39
|
+
const update = useCallback(() => setState({}), []);
|
|
25
40
|
|
|
26
|
-
|
|
27
|
-
if (!
|
|
41
|
+
useAsyncEffect(async () => {
|
|
42
|
+
if (!typename || !onQueryRefOptions) {
|
|
28
43
|
return;
|
|
29
44
|
}
|
|
30
45
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
setLoading(true);
|
|
47
|
+
try {
|
|
48
|
+
// TODO(wittjosiah): This should be a reactive query so that the options are always up to date.
|
|
49
|
+
const options = await onQueryRefOptions({ typename });
|
|
50
|
+
log('options', { options: options.length });
|
|
51
|
+
setOptions(
|
|
52
|
+
options.map((option) => {
|
|
53
|
+
const dxn = option.dxn.toString() as string;
|
|
54
|
+
return {
|
|
55
|
+
id: dxn,
|
|
56
|
+
label: option.label ?? dxn,
|
|
57
|
+
hue: 'neutral' as any,
|
|
58
|
+
};
|
|
59
|
+
}),
|
|
60
|
+
);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
log.error('Failed to fetch ref options:', error);
|
|
63
|
+
setOptions([]);
|
|
64
|
+
} finally {
|
|
65
|
+
setLoading(false);
|
|
66
|
+
}
|
|
67
|
+
}, [typename, onQueryRefOptions, state]);
|
|
68
|
+
|
|
69
|
+
return { options, update, loading };
|
|
53
70
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { useCallback } from 'react';
|
|
6
|
+
|
|
7
|
+
import { Obj } from '@dxos/echo';
|
|
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 UseRefQueryOptionsProps = { space?: Space };
|
|
14
|
+
|
|
15
|
+
export const useRefQueryOptions = ({ space }: UseRefQueryOptionsProps): QueryRefOptions => {
|
|
16
|
+
return useCallback<QueryRefOptions>(
|
|
17
|
+
async ({ typename }) => {
|
|
18
|
+
if (!space) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const query = space.db.query(Filter.typename(typename));
|
|
23
|
+
const objects = await query.run();
|
|
24
|
+
return objects
|
|
25
|
+
.map((object) => {
|
|
26
|
+
const dxn = Obj.getDXN(object);
|
|
27
|
+
if (!dxn) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return { dxn, label: Obj.getLabel(object) ?? object?.id ?? '' };
|
|
32
|
+
})
|
|
33
|
+
.filter(isNonNullable);
|
|
34
|
+
},
|
|
35
|
+
[space],
|
|
36
|
+
);
|
|
37
|
+
};
|
package/src/translations.ts
CHANGED
|
@@ -15,7 +15,7 @@ export const translations = [
|
|
|
15
15
|
'empty readonly ref field label': '(none)',
|
|
16
16
|
|
|
17
17
|
// TODO(burdon): Standardize field/property.
|
|
18
|
-
'
|
|
18
|
+
'add property button label': 'Add property',
|
|
19
19
|
'field property label': 'Property',
|
|
20
20
|
'field property placeholder': 'Property name',
|
|
21
21
|
'field format label': 'Type',
|
|
@@ -25,10 +25,19 @@ export const translations = [
|
|
|
25
25
|
'field limit reached': 'Maximum number of fields reached',
|
|
26
26
|
'boolean input true value': 'Yes',
|
|
27
27
|
'boolean input false value': 'No',
|
|
28
|
+
'show field label': 'Show field',
|
|
29
|
+
'hide field label': 'Hide field',
|
|
30
|
+
'delete field label': 'Delete field',
|
|
31
|
+
'ref field combobox input placeholder': 'Search…',
|
|
32
|
+
'ref field placeholder_one': 'Select…',
|
|
33
|
+
'ref field placeholder_other': 'Select items…',
|
|
28
34
|
|
|
29
|
-
|
|
30
|
-
'
|
|
31
|
-
'
|
|
35
|
+
'example placeholder': 'Example',
|
|
36
|
+
'latitude placeholder': 'Latitude (e.g., 40.7128)',
|
|
37
|
+
'longitude placeholder': 'Longitude (e.g., -74.0060)',
|
|
38
|
+
|
|
39
|
+
'cancel button label': 'Cancel',
|
|
40
|
+
'save button label': 'Save',
|
|
32
41
|
|
|
33
42
|
// FormatEnum
|
|
34
43
|
'format boolean': 'Boolean',
|
|
@@ -43,7 +52,7 @@ export const translations = [
|
|
|
43
52
|
'format hostname': 'Hostname',
|
|
44
53
|
'format integer': 'Integer',
|
|
45
54
|
'format json': 'JSON',
|
|
46
|
-
'format
|
|
55
|
+
'format lnglat': 'Geopoint',
|
|
47
56
|
'format markdown': 'Markdown',
|
|
48
57
|
'format percent': 'Percent',
|
|
49
58
|
'format ref': 'Reference',
|
|
@@ -60,11 +69,11 @@ export const translations = [
|
|
|
60
69
|
'format url': 'URL',
|
|
61
70
|
'format uuid': 'UUID',
|
|
62
71
|
|
|
63
|
-
//
|
|
64
|
-
'
|
|
65
|
-
'
|
|
72
|
+
// GeoPointField.
|
|
73
|
+
'latitude label': 'Latitude',
|
|
74
|
+
'longitude label': 'Longitude',
|
|
66
75
|
|
|
67
|
-
//
|
|
76
|
+
// SelectOptionsField.
|
|
68
77
|
'select option label': 'Label',
|
|
69
78
|
'select option label placeholder': 'Option label',
|
|
70
79
|
'select option color': 'Color',
|
|
@@ -72,8 +81,8 @@ export const translations = [
|
|
|
72
81
|
'select option add': 'Add option',
|
|
73
82
|
|
|
74
83
|
// System schema message.
|
|
75
|
-
'system schema title': 'System
|
|
76
|
-
'system schema description': '
|
|
84
|
+
'system schema title': 'System type',
|
|
85
|
+
'system schema description': 'System type: cannot add or remove fields.',
|
|
77
86
|
},
|
|
78
87
|
},
|
|
79
88
|
},
|