@dxos/react-ui-form 0.7.5-main.9d2a38b → 0.7.5-main.e94eead
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 +1014 -571
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +1031 -599
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +1014 -571
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +1 -2
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/Form/ArrayField.d.ts +15 -0
- package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -0
- package/dist/types/src/components/Form/Defaults.d.ts +6 -7
- package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +12 -21
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +2 -1
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormActions.d.ts +6 -0
- package/dist/types/src/components/Form/FormActions.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormContent.d.ts +25 -0
- package/dist/types/src/components/Form/FormContent.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormContext.d.ts +23 -0
- package/dist/types/src/components/Form/FormContext.d.ts.map +1 -0
- package/dist/types/src/components/Form/Input.d.ts +7 -8
- package/dist/types/src/components/Form/Input.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +1 -3
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +3 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +1 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +7 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/custom/TupleInput.d.ts +2 -4
- package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/index.d.ts +1 -0
- package/dist/types/src/components/Form/custom/index.d.ts.map +1 -1
- package/dist/types/src/components/Form/factory.d.ts +2 -2
- package/dist/types/src/components/Form/factory.d.ts.map +1 -1
- package/dist/types/src/components/Form/index.d.ts +2 -0
- package/dist/types/src/components/Form/index.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/Test.stories.d.ts +1 -2
- package/dist/types/src/components/ViewEditor/Test.stories.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +1 -2
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/Form.d.ts +1 -2
- package/dist/types/src/components/deprecated/Form.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/Form.stories.d.ts +1 -2
- package/dist/types/src/components/deprecated/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/FormInput.d.ts +6 -1
- package/dist/types/src/components/deprecated/FormInput.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +3 -3
- package/dist/types/src/components/testing.d.ts.map +1 -1
- package/dist/types/src/hooks/useForm.d.ts +11 -11
- package/dist/types/src/hooks/useForm.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +6 -0
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/util/field.d.ts.map +1 -1
- package/dist/types/src/util/formatting.d.ts.map +1 -1
- package/package.json +29 -27
- package/src/components/FieldEditor/FieldEditor.stories.tsx +1 -1
- package/src/components/FieldEditor/FieldEditor.tsx +7 -6
- package/src/components/Form/ArrayField.tsx +111 -0
- package/src/components/Form/Defaults.tsx +78 -34
- package/src/components/Form/Form.stories.tsx +15 -5
- package/src/components/Form/Form.tsx +50 -355
- package/src/components/Form/FormActions.tsx +41 -0
- package/src/components/Form/FormContent.tsx +170 -0
- package/src/components/Form/FormContext.tsx +96 -0
- package/src/components/Form/Input.tsx +5 -6
- package/src/components/Form/custom/GeoPointInput.tsx +44 -23
- package/src/components/Form/custom/SelectOptionsInput.stories.tsx +58 -0
- package/src/components/Form/custom/SelectOptionsInput.tsx +190 -0
- package/src/components/Form/custom/TupleInput.tsx +5 -9
- package/src/components/Form/custom/index.ts +1 -0
- package/src/components/Form/factory.tsx +6 -7
- package/src/components/Form/index.ts +3 -0
- package/src/components/ViewEditor/ViewEditor.tsx +7 -4
- package/src/components/deprecated/FormInput.tsx +20 -7
- package/src/hooks/useForm.ts +116 -79
- package/src/translations.ts +9 -1
- package/src/util/field.ts +19 -1
- package/src/util/formatting.ts +29 -1
|
@@ -2,23 +2,22 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { FormatEnum } from '@dxos/echo-schema';
|
|
6
6
|
import { type SimpleType } from '@dxos/effect';
|
|
7
7
|
|
|
8
|
-
import { BooleanInput, NumberInput, TextInput } from './Defaults';
|
|
8
|
+
import { BooleanInput, MarkdownInput, NumberInput, TextInput } from './Defaults';
|
|
9
9
|
import { type InputComponent } from './Input';
|
|
10
10
|
import { GeoPointInput } from './custom';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Get property input component.
|
|
14
14
|
*/
|
|
15
|
-
export const getInputComponent =
|
|
16
|
-
type: SimpleType,
|
|
17
|
-
format?: FormatEnum,
|
|
18
|
-
): InputComponent<T> | undefined => {
|
|
15
|
+
export const getInputComponent = (type: SimpleType, format?: FormatEnum): InputComponent | undefined => {
|
|
19
16
|
switch (format) {
|
|
20
|
-
case FormatEnum.
|
|
17
|
+
case FormatEnum.GeoPoint:
|
|
21
18
|
return GeoPointInput;
|
|
19
|
+
case FormatEnum.Markdown:
|
|
20
|
+
return MarkdownInput;
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
switch (type) {
|
|
@@ -10,7 +10,6 @@ import { IconButton, type ThemedClassName, useTranslation } from '@dxos/react-ui
|
|
|
10
10
|
import { List } from '@dxos/react-ui-list';
|
|
11
11
|
import { ghostHover, inputTextLabel, mx } from '@dxos/react-ui-theme';
|
|
12
12
|
import { FieldSchema, type FieldType, type ViewType, ViewProjection, VIEW_FIELD_LIMIT } from '@dxos/schema';
|
|
13
|
-
import { arrayMove } from '@dxos/util';
|
|
14
13
|
|
|
15
14
|
import { translationKey } from '../../translations';
|
|
16
15
|
import { FieldEditor } from '../FieldEditor';
|
|
@@ -22,7 +21,7 @@ const ViewMetaSchema = S.Struct({
|
|
|
22
21
|
name: S.String.annotations({
|
|
23
22
|
[AST.TitleAnnotationId]: 'View',
|
|
24
23
|
}),
|
|
25
|
-
typename: Format.
|
|
24
|
+
typename: Format.URL.annotations({
|
|
26
25
|
[AST.TitleAnnotationId]: 'Typename',
|
|
27
26
|
}),
|
|
28
27
|
}).pipe(S.mutable);
|
|
@@ -91,7 +90,11 @@ export const ViewEditor = ({
|
|
|
91
90
|
|
|
92
91
|
const handleMove = useCallback(
|
|
93
92
|
(fromIndex: number, toIndex: number) => {
|
|
94
|
-
|
|
93
|
+
// NOTE(ZaymonFC): Using arrayMove here causes a race condition with the kanban model.
|
|
94
|
+
const fields = [...view.fields];
|
|
95
|
+
const [moved] = fields.splice(fromIndex, 1);
|
|
96
|
+
fields.splice(toIndex, 0, moved);
|
|
97
|
+
view.fields = fields;
|
|
95
98
|
},
|
|
96
99
|
[view.fields],
|
|
97
100
|
);
|
|
@@ -100,7 +103,7 @@ export const ViewEditor = ({
|
|
|
100
103
|
|
|
101
104
|
return (
|
|
102
105
|
<div role='none' className={mx('flex flex-col w-full divide-y divide-separator', classNames)}>
|
|
103
|
-
<Form<ViewMetaType>
|
|
106
|
+
<Form<ViewMetaType> autoSave schema={ViewMetaSchema} values={viewValues} onSave={handleViewUpdate} />
|
|
104
107
|
|
|
105
108
|
<div>
|
|
106
109
|
{/* TODO(burdon): Clean up common form ux. */}
|
|
@@ -4,7 +4,21 @@
|
|
|
4
4
|
|
|
5
5
|
import React, { type JSX, type PropsWithChildren } from 'react';
|
|
6
6
|
|
|
7
|
-
import { Input } from '@dxos/react-ui';
|
|
7
|
+
import { Input, type ThemedClassName } from '@dxos/react-ui';
|
|
8
|
+
import { mx, textBlockWidth } from '@dxos/react-ui-theme';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated
|
|
12
|
+
*/
|
|
13
|
+
export const DeprecatedFormContainer = ({ children, classNames }: ThemedClassName<PropsWithChildren>) => {
|
|
14
|
+
return (
|
|
15
|
+
<div role='none' className='w-full p-4 justify-center overflow-x-hidden overflow-y-auto'>
|
|
16
|
+
<div role='form' className={mx('flex flex-col', textBlockWidth, classNames)}>
|
|
17
|
+
{children}
|
|
18
|
+
</div>
|
|
19
|
+
</div>
|
|
20
|
+
);
|
|
21
|
+
};
|
|
8
22
|
|
|
9
23
|
export type DeprecatedFormInputProps = {
|
|
10
24
|
label: string;
|
|
@@ -15,7 +29,6 @@ export type DeprecatedFormInputProps = {
|
|
|
15
29
|
/**
|
|
16
30
|
* @deprecated
|
|
17
31
|
*/
|
|
18
|
-
// TODO(burdon): Still used in ObservabilitySettings and all settings.
|
|
19
32
|
export const DeprecatedFormInput = ({
|
|
20
33
|
label,
|
|
21
34
|
description,
|
|
@@ -26,8 +39,9 @@ export const DeprecatedFormInput = ({
|
|
|
26
39
|
<div role='none' className='flex w-full gap-4 py-1'>
|
|
27
40
|
<Input.Root>
|
|
28
41
|
<div role='none' className='flex flex-col w-full'>
|
|
29
|
-
|
|
30
|
-
|
|
42
|
+
<Input.Label classNames='flex min-bs-[--rail-action] items-center' style={{ fontSize: 'unset' }}>
|
|
43
|
+
{label}
|
|
44
|
+
</Input.Label>
|
|
31
45
|
{description && (
|
|
32
46
|
<Input.DescriptionAndValidation classNames='mbs-0.5'>
|
|
33
47
|
<Input.Description>{description}</Input.Description>
|
|
@@ -36,7 +50,7 @@ export const DeprecatedFormInput = ({
|
|
|
36
50
|
</div>
|
|
37
51
|
|
|
38
52
|
<div role='none'>
|
|
39
|
-
<div role='none' className='flex min-
|
|
53
|
+
<div role='none' className='flex min-bs-[--rail-action] items-center'>
|
|
40
54
|
{children}
|
|
41
55
|
</div>
|
|
42
56
|
</div>
|
|
@@ -45,9 +59,8 @@ export const DeprecatedFormInput = ({
|
|
|
45
59
|
);
|
|
46
60
|
|
|
47
61
|
if (secondary) {
|
|
48
|
-
// console.log(secondary);
|
|
49
62
|
return (
|
|
50
|
-
<div role='none'
|
|
63
|
+
<div role='none'>
|
|
51
64
|
{primary}
|
|
52
65
|
{secondary}
|
|
53
66
|
</div>
|
package/src/hooks/useForm.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
//
|
|
2
|
-
// Copyright
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
6
6
|
|
|
7
|
-
import { type BaseObject,
|
|
8
|
-
import { type
|
|
7
|
+
import { type BaseObject, getValue, setValue } from '@dxos/echo-schema';
|
|
8
|
+
import { AST, type S, type SimpleType, type JsonPath, createJsonPath, fromEffectValidationPath } from '@dxos/effect';
|
|
9
9
|
import { invariant } from '@dxos/invariant';
|
|
10
10
|
import { log } from '@dxos/log';
|
|
11
11
|
import { validateSchema, type ValidationError } from '@dxos/schema';
|
|
@@ -20,20 +20,21 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
20
20
|
//
|
|
21
21
|
|
|
22
22
|
values: Partial<T>;
|
|
23
|
-
errors: Record<
|
|
24
|
-
touched: Record<
|
|
25
|
-
changed: Record<
|
|
23
|
+
errors: Record<JsonPath, string>;
|
|
24
|
+
touched: Record<JsonPath, boolean>;
|
|
25
|
+
changed: Record<JsonPath, boolean>;
|
|
26
26
|
canSave: boolean;
|
|
27
|
+
formIsValid: boolean;
|
|
27
28
|
handleSave: () => void;
|
|
28
29
|
|
|
29
30
|
//
|
|
30
31
|
// Form input component helpers.
|
|
31
32
|
//
|
|
32
33
|
|
|
33
|
-
getStatus: (
|
|
34
|
-
getValue: <V>(
|
|
35
|
-
onValueChange: <V>(
|
|
36
|
-
|
|
34
|
+
getStatus: (path: string | (string | number)[]) => { status?: 'error'; error?: string };
|
|
35
|
+
getValue: <V>(path: (string | number)[]) => V | undefined;
|
|
36
|
+
onValueChange: <V>(path: (string | number)[], type: SimpleType, value: V) => void;
|
|
37
|
+
onTouched: (path: (string | number)[]) => void;
|
|
37
38
|
};
|
|
38
39
|
|
|
39
40
|
/**
|
|
@@ -41,9 +42,8 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
41
42
|
*/
|
|
42
43
|
export interface FormOptions<T extends BaseObject> {
|
|
43
44
|
/**
|
|
44
|
-
* Effect schema.
|
|
45
|
+
* Effect schema (Type literal).
|
|
45
46
|
*/
|
|
46
|
-
// TODO(burdon): Change to S.Struct<T>?
|
|
47
47
|
schema: S.Schema<T>;
|
|
48
48
|
|
|
49
49
|
/**
|
|
@@ -89,21 +89,22 @@ export const useForm = <T extends BaseObject>({
|
|
|
89
89
|
onValid,
|
|
90
90
|
onSave,
|
|
91
91
|
}: FormOptions<T>): FormHandler<T> => {
|
|
92
|
+
invariant(AST.isTypeLiteral(schema.ast));
|
|
93
|
+
|
|
92
94
|
const [values, setValues] = useState<Partial<T>>(initialValues);
|
|
93
95
|
useEffect(() => {
|
|
94
96
|
setValues(initialValues);
|
|
95
97
|
}, [initialValues]);
|
|
96
98
|
|
|
97
|
-
const [touched, setTouched] = useState<Record<
|
|
98
|
-
const [changed, setChanged] = useState<Record<
|
|
99
|
-
const [errors, setErrors] = useState<Record<
|
|
99
|
+
const [touched, setTouched] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
100
|
+
const [changed, setChanged] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
101
|
+
const [errors, setErrors] = useState<Record<JsonPath, string>>({});
|
|
100
102
|
const [saving, setSaving] = useState(false);
|
|
101
103
|
|
|
102
104
|
//
|
|
103
105
|
// Validation.
|
|
104
106
|
//
|
|
105
107
|
|
|
106
|
-
// TODO(burdon): Validate each property separately.
|
|
107
108
|
const validate = useCallback(
|
|
108
109
|
(values: Partial<T>): values is T => {
|
|
109
110
|
let errors: ValidationError[] = validateSchema(schema, values) ?? [];
|
|
@@ -129,6 +130,10 @@ export const useForm = <T extends BaseObject>({
|
|
|
129
130
|
}
|
|
130
131
|
}, [values, schemaDirty]);
|
|
131
132
|
|
|
133
|
+
const formIsValid = useMemo(() => {
|
|
134
|
+
return Object.keys(errors).length === 0;
|
|
135
|
+
}, [errors]);
|
|
136
|
+
|
|
132
137
|
/**
|
|
133
138
|
* NOTE: We can submit if there is no touched field that has an error.
|
|
134
139
|
* Basically, if there's a validation message visible in the form, submit should be disabled.
|
|
@@ -136,8 +141,15 @@ export const useForm = <T extends BaseObject>({
|
|
|
136
141
|
const canSave = useMemo(
|
|
137
142
|
() =>
|
|
138
143
|
!saving &&
|
|
139
|
-
|
|
140
|
-
|
|
144
|
+
// Check if any error paths that are touched have errors.
|
|
145
|
+
!Object.entries(touched).some(
|
|
146
|
+
([path, isTouched]) =>
|
|
147
|
+
isTouched &&
|
|
148
|
+
Object.keys(errors).some(
|
|
149
|
+
(errorPath) => errorPath === path || errorPath.startsWith(`${path}.`) || errorPath.startsWith(`${path}[`),
|
|
150
|
+
),
|
|
151
|
+
),
|
|
152
|
+
[touched, errors, saving],
|
|
141
153
|
);
|
|
142
154
|
|
|
143
155
|
const handleSave = useCallback(async () => {
|
|
@@ -156,88 +168,113 @@ export const useForm = <T extends BaseObject>({
|
|
|
156
168
|
//
|
|
157
169
|
|
|
158
170
|
const getStatus = useCallback<FormHandler<T>['getStatus']>(
|
|
159
|
-
(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
171
|
+
(path) => {
|
|
172
|
+
const jsonPath = Array.isArray(path) ? createJsonPath(path) : path;
|
|
173
|
+
const matchingError = Object.entries(errors).find(
|
|
174
|
+
([errorPath]) =>
|
|
175
|
+
errorPath === jsonPath || errorPath.startsWith(`${jsonPath}.`) || errorPath.startsWith(`${jsonPath}[`),
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
status: matchingError ? 'error' : undefined,
|
|
180
|
+
error: matchingError ? matchingError[1] : undefined,
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
[errors],
|
|
164
184
|
);
|
|
165
185
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
186
|
+
const getFormValue = useCallback<FormHandler<T>['getValue']>(
|
|
187
|
+
<V>(path: (string | number)[]): V | undefined => {
|
|
188
|
+
return getValue(values, createJsonPath(path));
|
|
189
|
+
},
|
|
190
|
+
[values],
|
|
191
|
+
);
|
|
170
192
|
|
|
171
|
-
const onValueChange =
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
193
|
+
const onValueChange = useCallback<FormHandler<T>['onValueChange']>(
|
|
194
|
+
(path: (string | number)[], type: SimpleType, value: any) => {
|
|
195
|
+
const jsonPath = createJsonPath(path);
|
|
196
|
+
let parsedValue = value;
|
|
197
|
+
try {
|
|
198
|
+
if (type === 'number') {
|
|
199
|
+
parsedValue = parseFloat(value as string) || 0;
|
|
200
|
+
}
|
|
201
|
+
} catch (err) {
|
|
202
|
+
log.catch(err);
|
|
203
|
+
parsedValue = undefined;
|
|
176
204
|
}
|
|
177
|
-
} catch (err) {
|
|
178
|
-
log.catch(err);
|
|
179
|
-
parsedValue = undefined;
|
|
180
|
-
}
|
|
181
205
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
206
|
+
const newValues = { ...setValue(values, jsonPath, parsedValue) };
|
|
207
|
+
setValues(newValues);
|
|
208
|
+
setChanged((prev) => ({ ...prev, [jsonPath]: true }));
|
|
209
|
+
onValuesChanged?.(newValues);
|
|
186
210
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
// TODO(burdon): This is a leaky abstraction: the hook ideally shouldn't get involved in UX.
|
|
194
|
-
const onBlur = useCallback(
|
|
195
|
-
(event: FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
196
|
-
const { name } = event.target;
|
|
197
|
-
setTouched((touched) => ({ ...touched, [name]: true }));
|
|
198
|
-
|
|
199
|
-
// TODO(Zan): This should be configurable behavior.
|
|
200
|
-
if (event.relatedTarget?.getAttribute('type') === 'submit') {
|
|
201
|
-
// NOTE: We do this here instead of onSave because the blur event is triggered before the submit event
|
|
202
|
-
// and results in the submit button being disabled when the form is invalid.
|
|
203
|
-
setTouched(createKeySet(values, true));
|
|
211
|
+
const isValid = validate(newValues);
|
|
212
|
+
if (isValid && onValid) {
|
|
213
|
+
onValid(newValues, { changed });
|
|
204
214
|
}
|
|
215
|
+
},
|
|
216
|
+
[values, onValuesChanged, validate, onValid, changed],
|
|
217
|
+
);
|
|
205
218
|
|
|
219
|
+
const onTouched = useCallback(
|
|
220
|
+
(path: (string | number)[]) => {
|
|
221
|
+
const jsonPath = createJsonPath(path);
|
|
222
|
+
setTouched((touched) => ({ ...touched, [jsonPath]: true }));
|
|
206
223
|
validate(values);
|
|
207
224
|
},
|
|
208
225
|
[validate, values],
|
|
209
226
|
);
|
|
210
227
|
|
|
211
|
-
return
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
228
|
+
return useMemo(
|
|
229
|
+
() => ({
|
|
230
|
+
// State.
|
|
231
|
+
values,
|
|
232
|
+
errors,
|
|
233
|
+
touched,
|
|
234
|
+
changed,
|
|
235
|
+
canSave,
|
|
236
|
+
formIsValid,
|
|
237
|
+
|
|
238
|
+
// Actions.
|
|
239
|
+
handleSave,
|
|
240
|
+
|
|
241
|
+
// Field utils.
|
|
242
|
+
getStatus,
|
|
243
|
+
getValue: getFormValue,
|
|
244
|
+
onValueChange,
|
|
245
|
+
onTouched,
|
|
246
|
+
}),
|
|
247
|
+
[
|
|
248
|
+
values,
|
|
249
|
+
errors,
|
|
250
|
+
touched,
|
|
251
|
+
changed,
|
|
252
|
+
canSave,
|
|
253
|
+
formIsValid,
|
|
254
|
+
handleSave,
|
|
255
|
+
getStatus,
|
|
256
|
+
getFormValue,
|
|
257
|
+
onValueChange,
|
|
258
|
+
onTouched,
|
|
259
|
+
],
|
|
260
|
+
);
|
|
226
261
|
};
|
|
227
262
|
|
|
228
|
-
const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<
|
|
263
|
+
const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPath, V> => {
|
|
229
264
|
invariant(obj);
|
|
230
|
-
return Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: value }), {} as Record<
|
|
265
|
+
return Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: value }), {} as Record<JsonPath, V>);
|
|
231
266
|
};
|
|
232
267
|
|
|
233
|
-
const flatMap =
|
|
268
|
+
const flatMap = (errors: ValidationError[]) => {
|
|
234
269
|
return errors.reduce(
|
|
235
270
|
(result, { path, message }) => {
|
|
236
|
-
|
|
237
|
-
|
|
271
|
+
// Convert the validation error path format to our JsonPath format.
|
|
272
|
+
const jsonPath = fromEffectValidationPath(path);
|
|
273
|
+
if (!(jsonPath in result)) {
|
|
274
|
+
result[jsonPath] = message;
|
|
238
275
|
}
|
|
239
276
|
return result;
|
|
240
277
|
},
|
|
241
|
-
{} as Record<
|
|
278
|
+
{} as Record<JsonPath, string>,
|
|
242
279
|
);
|
|
243
280
|
};
|
package/src/translations.ts
CHANGED
|
@@ -36,12 +36,13 @@ export default [
|
|
|
36
36
|
'format hostname': 'Hostname',
|
|
37
37
|
'format integer': 'Integer',
|
|
38
38
|
'format json': 'JSON',
|
|
39
|
-
'format latlng': '
|
|
39
|
+
'format latlng': 'Geopoint',
|
|
40
40
|
'format markdown': 'Markdown',
|
|
41
41
|
'format percent': 'Percent',
|
|
42
42
|
'format ref': 'Reference',
|
|
43
43
|
'format regex': 'RegExp',
|
|
44
44
|
'format string': 'String',
|
|
45
|
+
'format single-select': 'Select',
|
|
45
46
|
'format text': 'Long text',
|
|
46
47
|
'format time': 'Time',
|
|
47
48
|
'format timestamp': 'Timestamp',
|
|
@@ -53,6 +54,13 @@ export default [
|
|
|
53
54
|
// Placeholders
|
|
54
55
|
'placeholder latitude': 'Latitude',
|
|
55
56
|
'placeholder longitude': 'Longitude',
|
|
57
|
+
|
|
58
|
+
// SelectOptionsInput
|
|
59
|
+
'select option label': 'Label',
|
|
60
|
+
'select option label placeholder': 'Option label',
|
|
61
|
+
'select option color': 'Color',
|
|
62
|
+
'select option delete': 'Delete',
|
|
63
|
+
'select option add': 'Add option',
|
|
56
64
|
},
|
|
57
65
|
},
|
|
58
66
|
},
|
package/src/util/field.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { FormatEnum, TypeEnum } from '@dxos/echo-schema';
|
|
5
|
+
import { FormatEnum, GeoLocation, TypeEnum } from '@dxos/echo-schema';
|
|
6
6
|
import { type ValidationError } from '@dxos/schema';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -81,6 +81,24 @@ export const parseValue = ({ type, format, value }: ParseProps) => {
|
|
|
81
81
|
return isNaN(date.getTime()) ? null : date;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
case FormatEnum.GeoPoint: {
|
|
85
|
+
// Parse string in format "lat,long" to GeoPoint [longitude, latitude].
|
|
86
|
+
if (typeof value === 'string') {
|
|
87
|
+
// Only keep digits, decimal points, minus signs, and commas.
|
|
88
|
+
const cleaned = value.replace(/[^0-9.,-]+/g, '');
|
|
89
|
+
const parts = cleaned.split(',');
|
|
90
|
+
if (parts.length === 2) {
|
|
91
|
+
const lat = parseFloat(parts[0]);
|
|
92
|
+
const long = parseFloat(parts[1]);
|
|
93
|
+
if (!isNaN(lat) && !isNaN(long)) {
|
|
94
|
+
return GeoLocation.toGeoPoint({ latitude: lat, longitude: long });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return [0, 0];
|
|
100
|
+
}
|
|
101
|
+
|
|
84
102
|
default: {
|
|
85
103
|
return value;
|
|
86
104
|
}
|
package/src/util/formatting.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { FormatEnum, TypeEnum } from '@dxos/echo-schema';
|
|
5
|
+
import { FormatEnum, GeoLocation, TypeEnum } from '@dxos/echo-schema';
|
|
6
6
|
|
|
7
7
|
type ValueFormatProps = {
|
|
8
8
|
type: TypeEnum;
|
|
@@ -73,6 +73,19 @@ export const formatForDisplay = ({ type, format, value, locale = undefined }: Va
|
|
|
73
73
|
return date.toLocaleTimeString(locale);
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
+
case FormatEnum.GeoPoint: {
|
|
77
|
+
if (value === null || value === undefined) {
|
|
78
|
+
return '';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// For GeoPoint format [longitude, latitude].
|
|
82
|
+
if (Array.isArray(value) && value.length >= 2 && value.every(Number.isFinite)) {
|
|
83
|
+
const { latitude, longitude } = GeoLocation.fromGeoPoint(value as [number, number]);
|
|
84
|
+
return `${latitude},${longitude}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return String(value);
|
|
88
|
+
}
|
|
76
89
|
default: {
|
|
77
90
|
if (value === null || value === 'undefined') {
|
|
78
91
|
return '';
|
|
@@ -125,6 +138,21 @@ export const formatForEditing = ({ type, format, value, locale = undefined }: Va
|
|
|
125
138
|
const date = new Date(value as number);
|
|
126
139
|
return date.toISOString().split('T')[1].split('.')[0];
|
|
127
140
|
}
|
|
141
|
+
case FormatEnum.GeoPoint: {
|
|
142
|
+
// Handle null or undefined
|
|
143
|
+
if (value === null || value === undefined) {
|
|
144
|
+
return '';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// For GeoPoint format [longitude, latitude].
|
|
148
|
+
if (Array.isArray(value) && value.length >= 2 && value.every(Number.isFinite)) {
|
|
149
|
+
const { latitude, longitude } = GeoLocation.fromGeoPoint(value as [number, number]);
|
|
150
|
+
return `${latitude},${longitude}`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return String(value);
|
|
154
|
+
}
|
|
155
|
+
|
|
128
156
|
default: {
|
|
129
157
|
return String(value);
|
|
130
158
|
}
|