@dxos/react-ui-form 0.8.4-main.e8ec1fe → 0.8.4-main.effb148878
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 +3240 -2345
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/translations.mjs +83 -0
- package/dist/lib/browser/translations.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +3240 -2345
- 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 +85 -0
- package/dist/lib/node-esm/translations.mjs.map +7 -0
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +8 -7
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +69 -59
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +123 -27
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +175 -137
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormField.d.ts +48 -0
- package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts +95 -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 +21 -0
- package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormTooltipsContext.d.ts +19 -0
- package/dist/types/src/components/Form/FormTooltipsContext.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/FormLayout.d.ts +25 -0
- package/dist/types/src/components/Form/Layout/FormLayout.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/FormLayout.stories.d.ts +116 -0
- package/dist/types/src/components/Form/Layout/FormLayout.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/annotation.d.ts +30 -0
- package/dist/types/src/components/Form/Layout/annotation.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/index.d.ts +4 -0
- package/dist/types/src/components/Form/Layout/index.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/parser.d.ts +31 -0
- package/dist/types/src/components/Form/Layout/parser.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/parser.test.d.ts +2 -0
- package/dist/types/src/components/Form/Layout/parser.test.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/DateField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/DateField.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 +85 -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 +13 -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 +85 -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 +13 -0
- package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
- package/dist/types/src/components/Form/index.d.ts +4 -4
- package/dist/types/src/components/Form/index.d.ts.map +1 -1
- package/dist/types/src/components/ObjectForm/ObjectForm.d.ts +8 -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 +37 -10
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -1
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +65 -58
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -1
- 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 +128 -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/ObjectTree/ObjectTree.d.ts +9 -0
- package/dist/types/src/components/ObjectTree/ObjectTree.d.ts.map +1 -0
- package/dist/types/src/components/ObjectTree/ObjectTree.stories.d.ts +87 -0
- package/dist/types/src/components/ObjectTree/ObjectTree.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectTree/index.d.ts +2 -0
- package/dist/types/src/components/ObjectTree/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 +85 -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 +14 -18
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +71 -64
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +4 -1
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +13 -6
- 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 +66 -59
- 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 +17 -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 +2 -2
- package/dist/types/src/util/refs.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +61 -52
- package/src/components/FieldEditor/FieldEditor.stories.tsx +44 -25
- package/src/components/FieldEditor/FieldEditor.test.tsx +0 -1
- package/src/components/FieldEditor/FieldEditor.tsx +84 -84
- package/src/components/Form/AUDIT.md +17 -0
- package/src/components/Form/Form.stories.tsx +134 -252
- package/src/components/Form/Form.tsx +417 -109
- package/src/components/Form/FormField.tsx +332 -0
- package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
- package/src/components/Form/FormFieldComponent.tsx +206 -0
- package/src/components/Form/FormFieldSet.tsx +173 -0
- package/src/components/Form/FormTooltipsContext.ts +25 -0
- package/src/components/Form/Layout/FormLayout.stories.tsx +339 -0
- package/src/components/Form/Layout/FormLayout.tsx +123 -0
- package/src/components/Form/Layout/annotation.ts +38 -0
- package/src/components/Form/Layout/index.ts +7 -0
- package/src/components/Form/Layout/parser.test.ts +120 -0
- package/src/components/Form/Layout/parser.ts +167 -0
- package/src/components/Form/README.md +3 -0
- package/src/components/Form/fields/ArrayField.tsx +185 -0
- package/src/components/Form/fields/BooleanField.tsx +22 -0
- package/src/components/Form/fields/DateField.tsx +95 -0
- package/src/components/Form/fields/GeoPointField.tsx +124 -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 +232 -0
- package/src/components/Form/fields/SelectField.tsx +77 -0
- package/src/components/Form/{custom/SelectOptionsInput.stories.tsx → fields/SelectOptionField.stories.tsx} +23 -18
- package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +32 -25
- 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 -9
- package/src/components/Form/fields/index.ts +16 -0
- package/src/components/Form/index.ts +5 -4
- package/src/components/ObjectForm/ObjectForm.tsx +115 -0
- package/src/components/{ControlSection → ObjectForm}/index.ts +1 -1
- package/src/components/ObjectPicker/ObjectPicker.stories.tsx +26 -21
- package/src/components/ObjectPicker/ObjectPicker.tsx +122 -81
- package/src/components/ObjectProperties/ObjectProperties.stories.tsx +350 -0
- package/src/components/ObjectProperties/ObjectProperties.test.tsx +108 -0
- package/src/components/ObjectProperties/ObjectProperties.tsx +147 -0
- package/src/components/ObjectProperties/index.ts +5 -0
- package/src/components/ObjectTree/ObjectTree.stories.tsx +134 -0
- package/src/components/ObjectTree/ObjectTree.tsx +190 -0
- package/src/components/ObjectTree/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 +81 -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 +62 -71
- package/src/components/ViewEditor/ViewEditor.test.tsx +10 -10
- package/src/components/ViewEditor/ViewEditor.tsx +308 -217
- package/src/components/index.ts +4 -1
- package/src/components/testing.tsx +24 -11
- 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 +71 -64
- package/src/util/field-type.ts +60 -0
- package/src/util/field.ts +25 -25
- package/src/util/formatting.ts +22 -22
- package/src/util/index.ts +2 -1
- package/src/util/properties.test.ts +78 -0
- package/src/util/properties.ts +38 -0
- package/src/util/refs.ts +13 -15
- package/src/vite-env.d.ts +10 -0
- 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 -83
- 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 -63
- 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 -29
- package/dist/types/src/components/Form/Input.d.ts.map +0 -1
- package/dist/types/src/components/Form/RefField.d.ts +0 -22
- 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 -78
- 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/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 -29
- 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 -38
- package/src/components/ControlSection/ControlSection.tsx +0 -153
- package/src/components/Form/ArrayField.tsx +0 -125
- package/src/components/Form/Defaults.tsx +0 -205
- package/src/components/Form/FormActions.tsx +0 -54
- package/src/components/Form/FormContent.tsx +0 -362
- package/src/components/Form/FormContext.tsx +0 -110
- package/src/components/Form/Input.tsx +0 -54
- package/src/components/Form/RefField.tsx +0 -283
- package/src/components/Form/custom/GeoPointInput.tsx +0 -80
- package/src/components/Form/custom/index.ts +0 -7
- package/src/components/Form/factory.tsx +0 -31
- package/src/hooks/useForm.ts +0 -296
- package/src/hooks/useQueryRefOptions.ts +0 -62
- package/src/hooks/useRefQueryLookupHandler.ts +0 -39
- package/src/util/schema.ts +0 -12
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxos/lit-ui/dx-tag-picker.pcss';
|
|
6
|
-
|
|
7
|
-
import type * as Schema from 'effect/Schema';
|
|
8
|
-
import type * as SchemaAST from 'effect/SchemaAST';
|
|
9
|
-
import React, { useCallback, useMemo } from 'react';
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
type EchoSchema,
|
|
13
|
-
Expando,
|
|
14
|
-
Ref,
|
|
15
|
-
ReferenceAnnotationId,
|
|
16
|
-
type ReferenceAnnotationValue,
|
|
17
|
-
getTypeAnnotation,
|
|
18
|
-
} from '@dxos/echo/internal';
|
|
19
|
-
import { findAnnotation } from '@dxos/effect';
|
|
20
|
-
import { DXN } from '@dxos/keys';
|
|
21
|
-
import { type DxTagPickerItemClick } from '@dxos/lit-ui';
|
|
22
|
-
import { DxAnchor, DxTagPickerItem } from '@dxos/lit-ui/react';
|
|
23
|
-
import { Button, Icon, Input, useTranslation } from '@dxos/react-ui';
|
|
24
|
-
import { descriptionText, mx } from '@dxos/react-ui-theme';
|
|
25
|
-
import { isNonNullable } from '@dxos/util';
|
|
26
|
-
|
|
27
|
-
import { type QueryRefOptions, useQueryRefOptions } from '../../hooks';
|
|
28
|
-
import { translationKey } from '../../translations';
|
|
29
|
-
import { ObjectPicker } from '../ObjectPicker';
|
|
30
|
-
|
|
31
|
-
import { TextInput } from './Defaults';
|
|
32
|
-
import { InputHeader, type InputProps } from './Input';
|
|
33
|
-
|
|
34
|
-
export type RefFieldProps = InputProps & {
|
|
35
|
-
ast?: SchemaAST.AST;
|
|
36
|
-
array?: boolean;
|
|
37
|
-
createOptionLabel?: [string, { ns: string }];
|
|
38
|
-
createOptionIcon?: string;
|
|
39
|
-
onCreate?: (values: any) => void;
|
|
40
|
-
createSchema?: Schema.Schema.AnyNoContext;
|
|
41
|
-
createInitialValuePath?: string;
|
|
42
|
-
onQueryRefOptions?: QueryRefOptions;
|
|
43
|
-
schema?: EchoSchema;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// TODO(thure): Is this a standard that should be better canonized?
|
|
47
|
-
const isRefSnapShot = (val: any): val is { '/': string } => {
|
|
48
|
-
return typeof val === 'object' && typeof (val as any)?.['/'] === 'string';
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const RefField = ({
|
|
52
|
-
type,
|
|
53
|
-
label,
|
|
54
|
-
readonly,
|
|
55
|
-
placeholder,
|
|
56
|
-
inputOnly,
|
|
57
|
-
array,
|
|
58
|
-
ast,
|
|
59
|
-
getValue,
|
|
60
|
-
createOptionLabel,
|
|
61
|
-
createOptionIcon,
|
|
62
|
-
onBlur,
|
|
63
|
-
onCreate,
|
|
64
|
-
createSchema,
|
|
65
|
-
createInitialValuePath,
|
|
66
|
-
onQueryRefOptions,
|
|
67
|
-
onValueChange,
|
|
68
|
-
...restInputProps
|
|
69
|
-
}: RefFieldProps) => {
|
|
70
|
-
const { t } = useTranslation(translationKey);
|
|
71
|
-
const refTypeInfo = useMemo(
|
|
72
|
-
() => (ast ? findAnnotation<ReferenceAnnotationValue>(ast, ReferenceAnnotationId) : undefined),
|
|
73
|
-
[ast],
|
|
74
|
-
);
|
|
75
|
-
const {
|
|
76
|
-
options: availableOptions,
|
|
77
|
-
update: updateOptions,
|
|
78
|
-
loading: _loading,
|
|
79
|
-
} = useQueryRefOptions({ refTypeInfo, onQueryRefOptions });
|
|
80
|
-
|
|
81
|
-
if ((refTypeInfo && refTypeInfo?.typename === getTypeAnnotation(Expando)?.typename) || !onQueryRefOptions) {
|
|
82
|
-
// If ref type is expando, fall back to taking a DXN in string format.
|
|
83
|
-
return (
|
|
84
|
-
<RefFieldFallback
|
|
85
|
-
{...{ type, label, placeholder, readonly, inputOnly, getValue, onBlur, onValueChange, ...restInputProps }}
|
|
86
|
-
/>
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const handleGetValue = useCallback(() => {
|
|
91
|
-
const formValue = getValue();
|
|
92
|
-
|
|
93
|
-
const unknownToRefOption = (val: unknown) => {
|
|
94
|
-
const isRef = Ref.isRef(val);
|
|
95
|
-
if (isRef || isRefSnapShot(val)) {
|
|
96
|
-
const dxnString = isRef ? val.dxn.toString() : val['/'];
|
|
97
|
-
const matchingOption = availableOptions.find((option) => option.id === dxnString);
|
|
98
|
-
if (matchingOption) {
|
|
99
|
-
return matchingOption;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return undefined;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
if (array && Array.isArray(formValue)) {
|
|
106
|
-
return formValue.map(unknownToRefOption).filter(isNonNullable) ?? [];
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const option = unknownToRefOption(formValue);
|
|
110
|
-
if (option) {
|
|
111
|
-
return [option];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return [];
|
|
115
|
-
}, [getValue, availableOptions, array]);
|
|
116
|
-
|
|
117
|
-
const handleUpdate = useCallback(
|
|
118
|
-
(ids: string[]) => {
|
|
119
|
-
if (ids.length === 0) {
|
|
120
|
-
onValueChange('object', undefined);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const refs = ids
|
|
125
|
-
.map((id) => {
|
|
126
|
-
const item = availableOptions.find((option) => option.id === id);
|
|
127
|
-
if (item) {
|
|
128
|
-
const dxn = DXN.parse(item.id);
|
|
129
|
-
return Ref.fromDXN(dxn);
|
|
130
|
-
}
|
|
131
|
-
return null;
|
|
132
|
-
})
|
|
133
|
-
.filter(isNonNullable);
|
|
134
|
-
|
|
135
|
-
if (array) {
|
|
136
|
-
onValueChange('object', refs);
|
|
137
|
-
} else {
|
|
138
|
-
onValueChange('object', refs[0]);
|
|
139
|
-
}
|
|
140
|
-
},
|
|
141
|
-
[availableOptions, array, onValueChange],
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
const handleCreate = useCallback(
|
|
145
|
-
(values: any) => {
|
|
146
|
-
onCreate?.(values);
|
|
147
|
-
updateOptions();
|
|
148
|
-
},
|
|
149
|
-
[onCreate, updateOptions],
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
if (!refTypeInfo) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const { status, error } = restInputProps.getStatus();
|
|
157
|
-
|
|
158
|
-
const items = handleGetValue();
|
|
159
|
-
const selectedIds = useMemo(() => items.map((i: any) => i.id), [items]);
|
|
160
|
-
const toggleSelect = useCallback(
|
|
161
|
-
(id: string) => {
|
|
162
|
-
if (array) {
|
|
163
|
-
const nextIds = selectedIds.includes(id) ? selectedIds.filter((x) => x !== id) : [...selectedIds, id];
|
|
164
|
-
handleUpdate(nextIds);
|
|
165
|
-
} else {
|
|
166
|
-
if (selectedIds[0] === id) {
|
|
167
|
-
handleUpdate([]);
|
|
168
|
-
} else {
|
|
169
|
-
handleUpdate([id]);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
[array, selectedIds, handleUpdate],
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
// NOTE(thure): I left both predicates in-place in case we decide to add variants which do render readonly but empty values.
|
|
177
|
-
return readonly && items.length < 1 ? null : (
|
|
178
|
-
<Input.Root validationValence={status}>
|
|
179
|
-
{!inputOnly && <InputHeader error={error} label={label} />}
|
|
180
|
-
<div data-no-submit>
|
|
181
|
-
{readonly ? (
|
|
182
|
-
items.length < 1 ? (
|
|
183
|
-
<p className={mx(descriptionText, 'mbe-2')}>{t('empty readonly ref field label')}</p>
|
|
184
|
-
) : (
|
|
185
|
-
items.map((item) => (
|
|
186
|
-
<DxAnchor key={item.id} refid={item.id} rootclassname='mie-1'>
|
|
187
|
-
{item.label}
|
|
188
|
-
</DxAnchor>
|
|
189
|
-
))
|
|
190
|
-
)
|
|
191
|
-
) : (
|
|
192
|
-
<ObjectPicker.Root>
|
|
193
|
-
<ObjectPicker.Trigger asChild>
|
|
194
|
-
<Button variant='ghost' classNames='is-full text-start gap-2'>
|
|
195
|
-
<div role='none' className='grow'>
|
|
196
|
-
{items?.length ? (
|
|
197
|
-
items?.map((item) => (
|
|
198
|
-
<DxTagPickerItem
|
|
199
|
-
key={item.id}
|
|
200
|
-
itemId={item.id}
|
|
201
|
-
label={item.label}
|
|
202
|
-
{...(item.hue ? { hue: item.hue } : {})}
|
|
203
|
-
removeLabel={t('remove item label')}
|
|
204
|
-
onItemClick={(event: DxTagPickerItemClick) => {
|
|
205
|
-
if (event.action === 'remove') {
|
|
206
|
-
toggleSelect(item.id);
|
|
207
|
-
}
|
|
208
|
-
}}
|
|
209
|
-
/>
|
|
210
|
-
))
|
|
211
|
-
) : (
|
|
212
|
-
<span className='text-description'>
|
|
213
|
-
{placeholder ?? t('ref field placeholder', { count: array ? 99 : 1 })}
|
|
214
|
-
</span>
|
|
215
|
-
)}
|
|
216
|
-
</div>
|
|
217
|
-
<Icon size={3} icon='ph--caret-down--bold' />
|
|
218
|
-
</Button>
|
|
219
|
-
</ObjectPicker.Trigger>
|
|
220
|
-
<ObjectPicker.Content
|
|
221
|
-
options={availableOptions}
|
|
222
|
-
selectedIds={selectedIds}
|
|
223
|
-
onSelect={toggleSelect}
|
|
224
|
-
createSchema={createSchema}
|
|
225
|
-
createOptionLabel={createOptionLabel}
|
|
226
|
-
createOptionIcon={createOptionIcon}
|
|
227
|
-
createInitialValuePath={createInitialValuePath}
|
|
228
|
-
onCreate={handleCreate}
|
|
229
|
-
/>
|
|
230
|
-
</ObjectPicker.Root>
|
|
231
|
-
)}
|
|
232
|
-
</div>
|
|
233
|
-
{inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
|
|
234
|
-
</Input.Root>
|
|
235
|
-
);
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
const RefFieldFallback = ({
|
|
239
|
-
type,
|
|
240
|
-
label,
|
|
241
|
-
readonly,
|
|
242
|
-
placeholder,
|
|
243
|
-
inputOnly,
|
|
244
|
-
getValue,
|
|
245
|
-
onValueChange,
|
|
246
|
-
...restInputProps
|
|
247
|
-
}: InputProps) => {
|
|
248
|
-
const handleOnValueChange = (_type: any, dxnString: string) => {
|
|
249
|
-
const dxn = DXN.tryParse(dxnString);
|
|
250
|
-
if (dxn) {
|
|
251
|
-
onValueChange?.('object', Ref.fromDXN(dxn));
|
|
252
|
-
} else if (dxnString === '') {
|
|
253
|
-
onValueChange?.('object', undefined);
|
|
254
|
-
} else {
|
|
255
|
-
onValueChange?.('string', dxnString);
|
|
256
|
-
}
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
const handleGetValue = () => {
|
|
260
|
-
const formValue = getValue();
|
|
261
|
-
if (typeof formValue === 'string') {
|
|
262
|
-
return formValue;
|
|
263
|
-
}
|
|
264
|
-
if (Ref.isRef(formValue)) {
|
|
265
|
-
return formValue.dxn.toString();
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return undefined;
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
return (
|
|
272
|
-
<TextInput
|
|
273
|
-
type={type}
|
|
274
|
-
label={label}
|
|
275
|
-
readonly={readonly}
|
|
276
|
-
placeholder={placeholder}
|
|
277
|
-
inputOnly={inputOnly}
|
|
278
|
-
getValue={handleGetValue as <V>() => V | undefined}
|
|
279
|
-
onValueChange={handleOnValueChange}
|
|
280
|
-
{...restInputProps}
|
|
281
|
-
/>
|
|
282
|
-
);
|
|
283
|
-
};
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import React, { type ChangeEvent, useCallback, useEffect, useMemo, useState } from 'react';
|
|
6
|
-
|
|
7
|
-
import { GeoLocation, type GeoPoint } from '@dxos/echo/internal';
|
|
8
|
-
import { Input, useTranslation } from '@dxos/react-ui';
|
|
9
|
-
import { safeParseFloat } from '@dxos/util';
|
|
10
|
-
|
|
11
|
-
import { translationKey } from '../../../translations';
|
|
12
|
-
import { InputHeader, type InputProps } from '../Input';
|
|
13
|
-
|
|
14
|
-
export const GeoPointInput = ({ type, label, readonly, getStatus, getValue, onValueChange, onBlur }: InputProps) => {
|
|
15
|
-
const { t } = useTranslation(translationKey);
|
|
16
|
-
const { status, error } = getStatus();
|
|
17
|
-
const geoPoint = useMemo<GeoPoint>(() => getValue<GeoPoint>() ?? [0, 0], [getValue]);
|
|
18
|
-
const geoLocation = useMemo(() => GeoLocation.fromGeoPoint(geoPoint), [geoPoint]);
|
|
19
|
-
|
|
20
|
-
const [longitudeText, setLongitudeText] = useState(geoLocation.longitude?.toString());
|
|
21
|
-
const [latitudeText, setLatitudeText] = useState(geoLocation.latitude?.toString());
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
const location = GeoLocation.fromGeoPoint(geoPoint);
|
|
25
|
-
setLongitudeText(location.longitude.toString());
|
|
26
|
-
setLatitudeText(location.latitude.toString());
|
|
27
|
-
}, [geoPoint]);
|
|
28
|
-
|
|
29
|
-
const handleCoordinateChange = useCallback(
|
|
30
|
-
(coordinateType: keyof Pick<GeoLocation, 'longitude' | 'latitude'>, setText: (text: string) => void) =>
|
|
31
|
-
(event: ChangeEvent<HTMLInputElement>) => {
|
|
32
|
-
const inputText = event.target.value;
|
|
33
|
-
setText(inputText);
|
|
34
|
-
if (inputText !== '' && inputText !== '-') {
|
|
35
|
-
const coord = safeParseFloat(inputText);
|
|
36
|
-
if (coord !== undefined && !isNaN(coord)) {
|
|
37
|
-
const currentLocation = GeoLocation.fromGeoPoint(getValue<GeoPoint>() ?? [0, 0]);
|
|
38
|
-
const newLocation = { ...currentLocation, [coordinateType]: coord };
|
|
39
|
-
const newValue = GeoLocation.toGeoPoint(newLocation);
|
|
40
|
-
onValueChange(type, newValue);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
[getValue, onValueChange, type],
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
<Input.Root validationValence={status}>
|
|
49
|
-
<InputHeader error={error} label={label} />
|
|
50
|
-
<div role='none' className='grid grid-cols-2 gap-2'>
|
|
51
|
-
<div role='none'>
|
|
52
|
-
<Input.Label>{t('latitude label')}</Input.Label>
|
|
53
|
-
<Input.TextInput
|
|
54
|
-
type='number'
|
|
55
|
-
step='0.00001'
|
|
56
|
-
min='-90'
|
|
57
|
-
max='90'
|
|
58
|
-
disabled={!!readonly}
|
|
59
|
-
value={latitudeText}
|
|
60
|
-
onChange={handleCoordinateChange('latitude', setLatitudeText)}
|
|
61
|
-
onBlur={onBlur}
|
|
62
|
-
/>
|
|
63
|
-
</div>
|
|
64
|
-
<div role='none'>
|
|
65
|
-
<Input.Label>{t('longitude label')}</Input.Label>
|
|
66
|
-
<Input.TextInput
|
|
67
|
-
type='number'
|
|
68
|
-
step='0.00001'
|
|
69
|
-
min='-180'
|
|
70
|
-
max='180'
|
|
71
|
-
disabled={!!readonly}
|
|
72
|
-
value={longitudeText}
|
|
73
|
-
onChange={handleCoordinateChange('longitude', setLongitudeText)}
|
|
74
|
-
onBlur={onBlur}
|
|
75
|
-
/>
|
|
76
|
-
</div>
|
|
77
|
-
</div>
|
|
78
|
-
</Input.Root>
|
|
79
|
-
);
|
|
80
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { FormatEnum } from '@dxos/echo/internal';
|
|
6
|
-
import { type SimpleType } from '@dxos/effect';
|
|
7
|
-
|
|
8
|
-
import { GeoPointInput } from './custom';
|
|
9
|
-
import { BooleanInput, MarkdownInput, NumberInput, TextInput } from './Defaults';
|
|
10
|
-
import { type InputComponent } from './Input';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Get property input component.
|
|
14
|
-
*/
|
|
15
|
-
export const getInputComponent = (type: SimpleType, format?: FormatEnum | string): InputComponent | undefined => {
|
|
16
|
-
switch (format) {
|
|
17
|
-
case FormatEnum.GeoPoint:
|
|
18
|
-
return GeoPointInput;
|
|
19
|
-
case FormatEnum.Markdown:
|
|
20
|
-
return MarkdownInput;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
switch (type) {
|
|
24
|
-
case 'string':
|
|
25
|
-
return TextInput;
|
|
26
|
-
case 'number':
|
|
27
|
-
return NumberInput;
|
|
28
|
-
case 'boolean':
|
|
29
|
-
return BooleanInput;
|
|
30
|
-
}
|
|
31
|
-
};
|
package/src/hooks/useForm.ts
DELETED
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import type * as Schema from 'effect/Schema';
|
|
6
|
-
import * as SchemaAST from 'effect/SchemaAST';
|
|
7
|
-
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
8
|
-
|
|
9
|
-
import { type BaseObject, getValue, setValue } from '@dxos/echo/internal';
|
|
10
|
-
import { type JsonPath, type SimpleType, createJsonPath, fromEffectValidationPath } from '@dxos/effect';
|
|
11
|
-
import { invariant } from '@dxos/invariant';
|
|
12
|
-
import { log } from '@dxos/log';
|
|
13
|
-
import { type ValidationError, validateSchema } from '@dxos/schema';
|
|
14
|
-
import { type MaybePromise } from '@dxos/util';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Return type from `useForm` hook.
|
|
18
|
-
*/
|
|
19
|
-
export type FormHandler<T extends BaseObject> = {
|
|
20
|
-
//
|
|
21
|
-
// Form state management.
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
values: Partial<T>;
|
|
25
|
-
errors: Record<JsonPath, string>;
|
|
26
|
-
touched: Record<JsonPath, boolean>;
|
|
27
|
-
changed: Record<JsonPath, boolean>;
|
|
28
|
-
canSave: boolean;
|
|
29
|
-
formIsValid: boolean;
|
|
30
|
-
|
|
31
|
-
// TODO(burdon): Return onSave.
|
|
32
|
-
handleSave: () => void;
|
|
33
|
-
|
|
34
|
-
//
|
|
35
|
-
// Form input component helpers.
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
getStatus: (path: string | (string | number)[]) => { status?: 'error'; error?: string };
|
|
39
|
-
getValue: <V>(path: (string | number)[]) => V | undefined;
|
|
40
|
-
onValueChange: <V>(path: (string | number)[], type: SimpleType, value: V) => void;
|
|
41
|
-
onTouched: (path: (string | number)[]) => void;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Hook options.
|
|
46
|
-
*/
|
|
47
|
-
export interface FormOptions<T extends BaseObject> {
|
|
48
|
-
/**
|
|
49
|
-
* Effect schema (Type literal).
|
|
50
|
-
*/
|
|
51
|
-
schema: Schema.Schema<T, any>;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Initial values (which may not pass validation).
|
|
55
|
-
*/
|
|
56
|
-
initialValues: Partial<T>;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Callback for value changes. Note: This is called even when values are invalid.
|
|
60
|
-
* Sometimes the parent component may want to know about changes even if the form is
|
|
61
|
-
* in an invalid state.
|
|
62
|
-
*/
|
|
63
|
-
onValuesChanged?: (values: Partial<T>) => void;
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Custom validation function that runs only after schema validation passes.
|
|
67
|
-
* Use this for complex validation logic that can't be expressed in the schema.
|
|
68
|
-
* @returns Array of validation errors, or undefined if validation passes
|
|
69
|
-
*/
|
|
70
|
-
// TODO(burdon): Change to key x value?
|
|
71
|
-
onValidate?: (values: T) => ValidationError[] | undefined;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Called when the form is valid.
|
|
75
|
-
*/
|
|
76
|
-
onValid?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => void;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Called when the form is submitted and passes validation.
|
|
80
|
-
*/
|
|
81
|
-
onSave?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => MaybePromise<void>;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Creates a hook for managing form state, including values, validation, and submission.
|
|
86
|
-
* Deeply integrated with `@dxos/schema` for schema-based validation.
|
|
87
|
-
*/
|
|
88
|
-
export const useForm = <T extends BaseObject>({
|
|
89
|
-
schema,
|
|
90
|
-
initialValues,
|
|
91
|
-
onValuesChanged,
|
|
92
|
-
onValidate,
|
|
93
|
-
onValid,
|
|
94
|
-
onSave,
|
|
95
|
-
}: FormOptions<T>): FormHandler<T> => {
|
|
96
|
-
invariant(SchemaAST.isTypeLiteral(schema.ast));
|
|
97
|
-
|
|
98
|
-
const [values, setValues] = useState<Partial<T>>(initialValues);
|
|
99
|
-
|
|
100
|
-
useEffect(() => {
|
|
101
|
-
setValues(initialValues);
|
|
102
|
-
}, [initialValues]);
|
|
103
|
-
|
|
104
|
-
const [touched, setTouched] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
105
|
-
const [changed, setChanged] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
106
|
-
const [errors, setErrors] = useState<Record<JsonPath, string>>({});
|
|
107
|
-
const [saving, setSaving] = useState(false);
|
|
108
|
-
|
|
109
|
-
//
|
|
110
|
-
// Validation.
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
const validate = useCallback(
|
|
114
|
-
(values: Partial<T>): values is T => {
|
|
115
|
-
let errors: ValidationError[] = validateSchema(schema, values) ?? [];
|
|
116
|
-
if (errors.length === 0 && onValidate) {
|
|
117
|
-
const validatedValues = values as T;
|
|
118
|
-
errors = onValidate(validatedValues) ?? [];
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
setErrors(flatMap(errors));
|
|
122
|
-
const valid = errors.length === 0;
|
|
123
|
-
return valid;
|
|
124
|
-
},
|
|
125
|
-
[schema, onValidate],
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
// Validate on schema change.
|
|
129
|
-
const [schemaDirty, setSchemaDirty] = useState(false);
|
|
130
|
-
useEffect(() => setSchemaDirty(true), [schema]);
|
|
131
|
-
useEffect(() => {
|
|
132
|
-
if (schemaDirty) {
|
|
133
|
-
validate(values);
|
|
134
|
-
setSchemaDirty(false);
|
|
135
|
-
}
|
|
136
|
-
}, [values, schemaDirty]);
|
|
137
|
-
|
|
138
|
-
const formIsValid = useMemo(() => {
|
|
139
|
-
return Object.keys(errors).length === 0;
|
|
140
|
-
}, [errors]);
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* NOTE: We can submit if there is no touched field that has an error.
|
|
144
|
-
* Basically, if there's a validation message visible in the form, submit should be disabled.
|
|
145
|
-
*/
|
|
146
|
-
const canSave = useMemo(
|
|
147
|
-
() =>
|
|
148
|
-
!saving &&
|
|
149
|
-
// Check if any error paths that are touched have errors.
|
|
150
|
-
!Object.entries(touched).some(
|
|
151
|
-
([path, isTouched]) =>
|
|
152
|
-
isTouched &&
|
|
153
|
-
Object.keys(errors).some(
|
|
154
|
-
(errorPath) => errorPath === path || errorPath.startsWith(`${path}.`) || errorPath.startsWith(`${path}[`),
|
|
155
|
-
),
|
|
156
|
-
),
|
|
157
|
-
[touched, errors, saving],
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
const handleSave = useCallback(async () => {
|
|
161
|
-
if (validate(values)) {
|
|
162
|
-
setSaving(true);
|
|
163
|
-
try {
|
|
164
|
-
await onSave?.(values, { changed });
|
|
165
|
-
} finally {
|
|
166
|
-
setSaving(false);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}, [values, validate, onSave, changed]);
|
|
170
|
-
|
|
171
|
-
//
|
|
172
|
-
// Fields.
|
|
173
|
-
//
|
|
174
|
-
|
|
175
|
-
const getStatus = useCallback<FormHandler<T>['getStatus']>(
|
|
176
|
-
(path) => {
|
|
177
|
-
const jsonPath = Array.isArray(path) ? createJsonPath(path) : path;
|
|
178
|
-
const matchingError = Object.entries(errors).find(
|
|
179
|
-
([errorPath]) =>
|
|
180
|
-
errorPath === jsonPath || errorPath.startsWith(`${jsonPath}.`) || errorPath.startsWith(`${jsonPath}[`),
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
// Only show errors for touched fields.
|
|
184
|
-
const isTouched = touched[jsonPath as JsonPath];
|
|
185
|
-
if (!isTouched) {
|
|
186
|
-
return {
|
|
187
|
-
status: undefined,
|
|
188
|
-
error: undefined,
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return {
|
|
193
|
-
status: matchingError ? 'error' : undefined,
|
|
194
|
-
error: matchingError ? matchingError[1] : undefined,
|
|
195
|
-
};
|
|
196
|
-
},
|
|
197
|
-
[errors, touched],
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
const getFormValue = useCallback<FormHandler<T>['getValue']>(
|
|
201
|
-
<V>(path: (string | number)[]): V | undefined => {
|
|
202
|
-
return getValue(values, createJsonPath(path));
|
|
203
|
-
},
|
|
204
|
-
[values],
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
// TODO(wittjosiah): This is causing the entire form to re-render on every change.
|
|
208
|
-
const onValueChange = useCallback<FormHandler<T>['onValueChange']>(
|
|
209
|
-
(path: (string | number)[], type: SimpleType, value: any) => {
|
|
210
|
-
const jsonPath = createJsonPath(path);
|
|
211
|
-
let parsedValue = value;
|
|
212
|
-
try {
|
|
213
|
-
if (type === 'number') {
|
|
214
|
-
parsedValue = parseFloat(value as string) || 0;
|
|
215
|
-
}
|
|
216
|
-
} catch (err) {
|
|
217
|
-
log.catch(err);
|
|
218
|
-
parsedValue = undefined;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const newValues = { ...setValue(values, jsonPath, parsedValue) };
|
|
222
|
-
setValues(newValues);
|
|
223
|
-
const newChanged = { ...changed, [jsonPath]: true };
|
|
224
|
-
setChanged(newChanged);
|
|
225
|
-
onValuesChanged?.(newValues);
|
|
226
|
-
|
|
227
|
-
const isValid = validate(newValues);
|
|
228
|
-
if (isValid && onValid) {
|
|
229
|
-
onValid(newValues, { changed: newChanged });
|
|
230
|
-
}
|
|
231
|
-
},
|
|
232
|
-
[values, onValuesChanged, validate, onValid, changed],
|
|
233
|
-
);
|
|
234
|
-
|
|
235
|
-
const onTouched = useCallback(
|
|
236
|
-
(path: (string | number)[]) => {
|
|
237
|
-
const jsonPath = createJsonPath(path);
|
|
238
|
-
setTouched((touched) => ({ ...touched, [jsonPath]: true }));
|
|
239
|
-
validate(values);
|
|
240
|
-
},
|
|
241
|
-
[validate, values],
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
return useMemo(
|
|
245
|
-
() => ({
|
|
246
|
-
// State.
|
|
247
|
-
values,
|
|
248
|
-
errors,
|
|
249
|
-
touched,
|
|
250
|
-
changed,
|
|
251
|
-
canSave,
|
|
252
|
-
formIsValid,
|
|
253
|
-
|
|
254
|
-
// Actions.
|
|
255
|
-
handleSave,
|
|
256
|
-
|
|
257
|
-
// Field utils.
|
|
258
|
-
getStatus,
|
|
259
|
-
getValue: getFormValue,
|
|
260
|
-
onValueChange,
|
|
261
|
-
onTouched,
|
|
262
|
-
}),
|
|
263
|
-
[
|
|
264
|
-
values,
|
|
265
|
-
errors,
|
|
266
|
-
touched,
|
|
267
|
-
changed,
|
|
268
|
-
canSave,
|
|
269
|
-
formIsValid,
|
|
270
|
-
handleSave,
|
|
271
|
-
getStatus,
|
|
272
|
-
getFormValue,
|
|
273
|
-
onValueChange,
|
|
274
|
-
onTouched,
|
|
275
|
-
],
|
|
276
|
-
);
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPath, V> => {
|
|
280
|
-
invariant(obj);
|
|
281
|
-
return Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: value }), {} as Record<JsonPath, V>);
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
const flatMap = (errors: ValidationError[]) => {
|
|
285
|
-
return errors.reduce(
|
|
286
|
-
(result, { path, message }) => {
|
|
287
|
-
// Convert the validation error path format to our JsonPath format.
|
|
288
|
-
const jsonPath = fromEffectValidationPath(path);
|
|
289
|
-
if (!(jsonPath in result)) {
|
|
290
|
-
result[jsonPath] = message;
|
|
291
|
-
}
|
|
292
|
-
return result;
|
|
293
|
-
},
|
|
294
|
-
{} as Record<JsonPath, string>,
|
|
295
|
-
);
|
|
296
|
-
};
|