@dxos/react-ui-form 0.7.5-main.e9bb01b → 0.7.5-staging.2ff1350
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 +532 -360
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +567 -391
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +532 -360
- 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 +1 -2
- package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -1
- package/dist/types/src/components/Form/Defaults.d.ts +5 -5
- package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +2 -2
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormActions.d.ts +1 -2
- package/dist/types/src/components/Form/FormActions.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormContent.d.ts +2 -3
- package/dist/types/src/components/Form/FormContent.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormContext.d.ts +4 -1
- package/dist/types/src/components/Form/FormContext.d.ts.map +1 -1
- package/dist/types/src/components/Form/Input.d.ts +2 -2
- package/dist/types/src/components/Form/Input.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +1 -2
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +1 -2
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/TupleInput.d.ts +1 -2
- package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +1 -1
- 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 +2 -2
- 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 +3 -2
- package/dist/types/src/hooks/useForm.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 +28 -27
- package/src/components/Form/Defaults.tsx +61 -2
- package/src/components/Form/Form.tsx +17 -2
- package/src/components/Form/FormActions.tsx +1 -1
- package/src/components/Form/FormContent.tsx +10 -13
- package/src/components/Form/FormContext.tsx +48 -4
- package/src/components/Form/custom/GeoPointInput.tsx +42 -12
- package/src/components/Form/custom/SelectOptionsInput.tsx +7 -9
- package/src/components/Form/factory.tsx +4 -2
- package/src/components/Form/index.ts +3 -0
- package/src/components/ViewEditor/ViewEditor.tsx +2 -2
- package/src/components/deprecated/FormInput.tsx +1 -4
- package/src/hooks/useForm.ts +10 -3
- package/src/util/field.ts +19 -1
- package/src/util/formatting.ts +29 -1
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-form",
|
|
3
|
-
"version": "0.7.5-
|
|
3
|
+
"version": "0.7.5-staging.2ff1350",
|
|
4
4
|
"description": "Data components.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "DXOS.org",
|
|
9
|
+
"type": "module",
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
12
|
"types": "./dist/types/src/index.d.ts",
|
|
@@ -27,48 +28,48 @@
|
|
|
27
28
|
"@effect/schema": "^0.75.5",
|
|
28
29
|
"@radix-ui/react-context": "1.1.1",
|
|
29
30
|
"date-fns": "^3.3.1",
|
|
30
|
-
"effect": "^3.12.1",
|
|
31
31
|
"jsonpath": "^1.1.1",
|
|
32
|
-
"@dxos/async": "0.7.5-
|
|
33
|
-
"@dxos/debug": "0.7.5-
|
|
34
|
-
"@dxos/echo-db": "0.7.5-
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/
|
|
39
|
-
"@dxos/live-object": "0.7.5-
|
|
40
|
-
"@dxos/log": "0.7.5-
|
|
41
|
-
"@dxos/react-client": "0.7.5-
|
|
42
|
-
"@dxos/react-ui-
|
|
43
|
-
"@dxos/react-ui-
|
|
44
|
-
"@dxos/react-ui-types": "0.7.5-
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/
|
|
32
|
+
"@dxos/async": "0.7.5-staging.2ff1350",
|
|
33
|
+
"@dxos/debug": "0.7.5-staging.2ff1350",
|
|
34
|
+
"@dxos/echo-db": "0.7.5-staging.2ff1350",
|
|
35
|
+
"@dxos/echo-schema": "0.7.5-staging.2ff1350",
|
|
36
|
+
"@dxos/effect": "0.7.5-staging.2ff1350",
|
|
37
|
+
"@dxos/invariant": "0.7.5-staging.2ff1350",
|
|
38
|
+
"@dxos/keys": "0.7.5-staging.2ff1350",
|
|
39
|
+
"@dxos/live-object": "0.7.5-staging.2ff1350",
|
|
40
|
+
"@dxos/log": "0.7.5-staging.2ff1350",
|
|
41
|
+
"@dxos/react-client": "0.7.5-staging.2ff1350",
|
|
42
|
+
"@dxos/react-ui-list": "0.7.5-staging.2ff1350",
|
|
43
|
+
"@dxos/react-ui-pickers": "0.7.5-staging.2ff1350",
|
|
44
|
+
"@dxos/react-ui-types": "0.7.5-staging.2ff1350",
|
|
45
|
+
"@dxos/schema": "0.7.5-staging.2ff1350",
|
|
46
|
+
"@dxos/util": "0.7.5-staging.2ff1350"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@phosphor-icons/react": "^2.1.5",
|
|
50
|
-
"@types/jsonpath": "
|
|
50
|
+
"@types/jsonpath": "^0.2.4",
|
|
51
51
|
"@types/react": "~18.2.0",
|
|
52
52
|
"@types/react-dom": "~18.2.0",
|
|
53
|
+
"effect": "^3.12.3",
|
|
53
54
|
"fast-check": "^3.19.0",
|
|
54
55
|
"react": "~18.2.0",
|
|
55
56
|
"react-dom": "~18.2.0",
|
|
56
57
|
"vite": "5.4.7",
|
|
57
|
-
"@dxos/echo-signals": "0.7.5-
|
|
58
|
-
"@dxos/
|
|
59
|
-
"@dxos/react-ui
|
|
60
|
-
"@dxos/react-ui-theme": "0.7.5-
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/
|
|
58
|
+
"@dxos/echo-signals": "0.7.5-staging.2ff1350",
|
|
59
|
+
"@dxos/random": "0.7.5-staging.2ff1350",
|
|
60
|
+
"@dxos/react-ui": "0.7.5-staging.2ff1350",
|
|
61
|
+
"@dxos/react-ui-theme": "0.7.5-staging.2ff1350",
|
|
62
|
+
"@dxos/storybook-utils": "0.7.5-staging.2ff1350",
|
|
63
|
+
"@dxos/react-ui-syntax-highlighter": "0.7.5-staging.2ff1350"
|
|
63
64
|
},
|
|
64
65
|
"peerDependencies": {
|
|
65
66
|
"@effect/schema": "^0.75.5",
|
|
66
67
|
"@phosphor-icons/react": "^2.1.5",
|
|
67
|
-
"effect": "^3.
|
|
68
|
+
"effect": "^3.12.3",
|
|
68
69
|
"react": "~18.2.0",
|
|
69
70
|
"react-dom": "~18.2.0",
|
|
70
|
-
"@dxos/react-ui": "0.7.5-
|
|
71
|
-
"@dxos/react-ui
|
|
71
|
+
"@dxos/react-ui-theme": "0.7.5-staging.2ff1350",
|
|
72
|
+
"@dxos/react-ui": "0.7.5-staging.2ff1350"
|
|
72
73
|
},
|
|
73
74
|
"publishConfig": {
|
|
74
75
|
"access": "public"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React from 'react';
|
|
5
|
+
import React, { useCallback, useEffect, useRef } from 'react';
|
|
6
6
|
|
|
7
7
|
import { Input, Select } from '@dxos/react-ui';
|
|
8
8
|
|
|
@@ -75,6 +75,7 @@ export const NumberInput = ({
|
|
|
75
75
|
|
|
76
76
|
export const BooleanInput = ({ type, label, inputOnly, getStatus, getValue, onValueChange }: InputProps) => {
|
|
77
77
|
const { status, error } = getStatus();
|
|
78
|
+
const checked = Boolean(getValue());
|
|
78
79
|
|
|
79
80
|
return (
|
|
80
81
|
<Input.Root validationValence={status}>
|
|
@@ -83,7 +84,7 @@ export const BooleanInput = ({ type, label, inputOnly, getStatus, getValue, onVa
|
|
|
83
84
|
<Input.Label>{label}</Input.Label>
|
|
84
85
|
</InputHeader>
|
|
85
86
|
)}
|
|
86
|
-
<Input.Switch checked={
|
|
87
|
+
<Input.Switch checked={checked} onCheckedChange={(value) => onValueChange(type, value)} />
|
|
87
88
|
{inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
|
|
88
89
|
</Input.Root>
|
|
89
90
|
);
|
|
@@ -133,3 +134,61 @@ export const SelectInput = ({
|
|
|
133
134
|
</Input.Root>
|
|
134
135
|
);
|
|
135
136
|
};
|
|
137
|
+
|
|
138
|
+
export const MarkdownInput = ({
|
|
139
|
+
type,
|
|
140
|
+
label,
|
|
141
|
+
inputOnly,
|
|
142
|
+
disabled,
|
|
143
|
+
placeholder,
|
|
144
|
+
getStatus,
|
|
145
|
+
getValue,
|
|
146
|
+
onValueChange,
|
|
147
|
+
onBlur,
|
|
148
|
+
}: InputProps) => {
|
|
149
|
+
const { status, error } = getStatus();
|
|
150
|
+
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
|
151
|
+
|
|
152
|
+
// TODO(ZaymonFC): If we start using `Enter` for form submission, we should prevent default behavior here
|
|
153
|
+
// since we're using a textarea element and the user needs to be able to enter new lines.
|
|
154
|
+
|
|
155
|
+
const adjustHeight = useCallback(() => {
|
|
156
|
+
const textarea = textareaRef.current;
|
|
157
|
+
if (textarea) {
|
|
158
|
+
textarea.style.blockSize = 'auto'; // For measurement.
|
|
159
|
+
textarea.style.blockSize = `${textarea.scrollHeight + 2}px`;
|
|
160
|
+
}
|
|
161
|
+
}, []);
|
|
162
|
+
|
|
163
|
+
// Adjust height on initial render.
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
adjustHeight();
|
|
166
|
+
}, [adjustHeight]);
|
|
167
|
+
|
|
168
|
+
// Adjust height when the content changes.
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
adjustHeight();
|
|
171
|
+
}, [getValue(), adjustHeight]);
|
|
172
|
+
|
|
173
|
+
return (
|
|
174
|
+
<Input.Root validationValence={status}>
|
|
175
|
+
{!inputOnly && (
|
|
176
|
+
<InputHeader error={error}>
|
|
177
|
+
<Input.Label>{label}</Input.Label>
|
|
178
|
+
</InputHeader>
|
|
179
|
+
)}
|
|
180
|
+
<Input.TextArea
|
|
181
|
+
ref={textareaRef}
|
|
182
|
+
disabled={disabled}
|
|
183
|
+
placeholder={placeholder}
|
|
184
|
+
value={getValue() ?? ''}
|
|
185
|
+
classNames={'min-bs-auto max-h-40 overflow-auto'}
|
|
186
|
+
onChange={(event) => onValueChange(type, event.target.value)}
|
|
187
|
+
onBlur={onBlur}
|
|
188
|
+
style={{ resize: 'none' }}
|
|
189
|
+
spellCheck={false}
|
|
190
|
+
/>
|
|
191
|
+
{inputOnly && <Input.Validation>{error}</Input.Validation>}
|
|
192
|
+
</Input.Root>
|
|
193
|
+
);
|
|
194
|
+
};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React, { type ReactElement, useMemo } from 'react';
|
|
5
|
+
import React, { type ReactElement, useEffect, useMemo, useRef } from 'react';
|
|
6
6
|
|
|
7
7
|
import { type BaseObject, type S, type PropertyKey } from '@dxos/echo-schema';
|
|
8
8
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
@@ -55,6 +55,7 @@ export const Form = <T extends BaseObject>({
|
|
|
55
55
|
schema,
|
|
56
56
|
values: initialValues,
|
|
57
57
|
path = [],
|
|
58
|
+
autoFocus,
|
|
58
59
|
readonly,
|
|
59
60
|
filter,
|
|
60
61
|
sort,
|
|
@@ -67,18 +68,32 @@ export const Form = <T extends BaseObject>({
|
|
|
67
68
|
lookupComponent,
|
|
68
69
|
Custom,
|
|
69
70
|
}: FormProps<T>) => {
|
|
71
|
+
const formRef = useRef<HTMLDivElement>(null);
|
|
70
72
|
const onValid = useMemo(() => (autoSave ? onSave : undefined), [autoSave, onSave]);
|
|
71
73
|
|
|
74
|
+
// Focus the first input element within this form.
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (autoFocus && formRef.current) {
|
|
77
|
+
const input = formRef.current.querySelector('input');
|
|
78
|
+
if (input) {
|
|
79
|
+
input.focus();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}, [autoFocus]);
|
|
83
|
+
|
|
72
84
|
return (
|
|
73
85
|
<FormProvider
|
|
86
|
+
formRef={formRef}
|
|
74
87
|
schema={schema}
|
|
88
|
+
autoSave={autoSave}
|
|
75
89
|
initialValues={initialValues}
|
|
76
90
|
onValuesChanged={onValuesChanged}
|
|
77
91
|
onValidate={onValidate}
|
|
78
92
|
onValid={onValid}
|
|
79
93
|
onSave={onSave}
|
|
80
94
|
>
|
|
81
|
-
|
|
95
|
+
{/* TODO(burdon): Remove padding. */}
|
|
96
|
+
<div ref={formRef} role='none' className={mx('p-2', classNames)} data-testid={testId}>
|
|
82
97
|
<FormFields
|
|
83
98
|
schema={schema}
|
|
84
99
|
path={path}
|
|
@@ -20,7 +20,7 @@ export const FormActions = ({ onCancel, readonly }: FormActionsProps) => {
|
|
|
20
20
|
const { canSave, handleSave } = useFormContext();
|
|
21
21
|
|
|
22
22
|
return (
|
|
23
|
-
<div role='none' className='flex justify-center'>
|
|
23
|
+
<div role='none' className='mt-2 flex justify-center'>
|
|
24
24
|
<div role='none' className={mx(onCancel && !readonly && 'grid grid-cols-2 gap-2')}>
|
|
25
25
|
{onCancel && !readonly && (
|
|
26
26
|
<IconButton data-testid='cancel-button' icon='ph--x--regular' label={t('button cancel')} onClick={onCancel} />
|
|
@@ -70,18 +70,15 @@ export const FormField = ({ property, path, readonly, inline, lookupComponent, C
|
|
|
70
70
|
const CustomComponent = Custom?.[jsonPath];
|
|
71
71
|
if (CustomComponent) {
|
|
72
72
|
return (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
{...inputProps}
|
|
83
|
-
/>
|
|
84
|
-
</div>
|
|
73
|
+
<CustomComponent
|
|
74
|
+
type={type}
|
|
75
|
+
format={format}
|
|
76
|
+
label={label}
|
|
77
|
+
inputOnly={inline}
|
|
78
|
+
placeholder={placeholder}
|
|
79
|
+
disabled={readonly}
|
|
80
|
+
{...inputProps}
|
|
81
|
+
/>
|
|
85
82
|
);
|
|
86
83
|
}
|
|
87
84
|
|
|
@@ -153,7 +150,7 @@ export const FormFields = ({ schema, path, filter, sort, readonly, lookupCompone
|
|
|
153
150
|
}, [schema, values, filter, sort]);
|
|
154
151
|
|
|
155
152
|
return (
|
|
156
|
-
<div role='form' className={mx('flex flex-col w-full gap-2
|
|
153
|
+
<div role='form' className={mx('flex flex-col w-full gap-2')}>
|
|
157
154
|
{properties
|
|
158
155
|
.map((property) => {
|
|
159
156
|
return (
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React, { createContext, useContext, type FocusEvent, type PropsWithChildren } from 'react';
|
|
5
|
+
import React, { createContext, useContext, useEffect, type FocusEvent, type PropsWithChildren } from 'react';
|
|
6
6
|
|
|
7
7
|
import { raise } from '@dxos/debug';
|
|
8
8
|
import { type BaseObject, getValue } from '@dxos/echo-schema';
|
|
@@ -42,7 +42,51 @@ export const useInputProps = (path: (string | number)[] = []): FormInputStatePro
|
|
|
42
42
|
};
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
export const FormProvider = ({
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
export const FormProvider = ({
|
|
46
|
+
children,
|
|
47
|
+
formRef,
|
|
48
|
+
autoSave,
|
|
49
|
+
...formOptions
|
|
50
|
+
}: PropsWithChildren<
|
|
51
|
+
FormOptions<any> & {
|
|
52
|
+
formRef?: React.RefObject<HTMLDivElement>;
|
|
53
|
+
autoSave?: boolean;
|
|
54
|
+
}
|
|
55
|
+
>) => {
|
|
56
|
+
const form = useForm(formOptions);
|
|
57
|
+
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (!formRef?.current) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
64
|
+
const keyIsEnter = event.key === 'Enter';
|
|
65
|
+
const modifierUsed = event.ctrlKey || event.altKey || event.metaKey || event.shiftKey;
|
|
66
|
+
const inputIsTextarea = (event.target as HTMLElement).tagName.toLowerCase() === 'textarea';
|
|
67
|
+
const inputOptOut = (event.target as HTMLElement).hasAttribute('data-no-submit');
|
|
68
|
+
|
|
69
|
+
// Regular inputs: Submit on Enter (no modifiers).
|
|
70
|
+
const shouldSubmitRegularInput = !inputIsTextarea && keyIsEnter && !modifierUsed;
|
|
71
|
+
|
|
72
|
+
// Textareas: Submit only on Meta+Enter.
|
|
73
|
+
const shouldSubmitTextarea = inputIsTextarea && keyIsEnter && event.metaKey;
|
|
74
|
+
|
|
75
|
+
if ((shouldSubmitRegularInput || shouldSubmitTextarea) && !inputOptOut) {
|
|
76
|
+
if (!autoSave && form.canSave) {
|
|
77
|
+
form.handleSave();
|
|
78
|
+
}
|
|
79
|
+
if (autoSave && form.formIsValid) {
|
|
80
|
+
(event.target as HTMLElement).blur();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const formElement = formRef.current;
|
|
86
|
+
|
|
87
|
+
formElement.addEventListener('keydown', handleKeyDown);
|
|
88
|
+
return () => formElement.removeEventListener('keydown', handleKeyDown);
|
|
89
|
+
}, [formRef, form.canSave, form.formIsValid, form.handleSave, autoSave]);
|
|
90
|
+
|
|
91
|
+
return <FormContext.Provider value={form}>{children}</FormContext.Provider>;
|
|
48
92
|
};
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React from 'react';
|
|
5
|
+
import React, { useCallback, useState, useEffect, useMemo } from 'react';
|
|
6
6
|
|
|
7
|
-
import { type GeoPoint } from '@dxos/echo-schema';
|
|
7
|
+
import { type GeoPoint, GeoLocation } from '@dxos/echo-schema';
|
|
8
8
|
import { Input, useTranslation } from '@dxos/react-ui';
|
|
9
9
|
import { safeParseFloat } from '@dxos/util';
|
|
10
10
|
|
|
@@ -14,28 +14,58 @@ import { InputHeader, type InputProps } from '../Input';
|
|
|
14
14
|
export const GeoPointInput = ({ type, label, disabled, getStatus, getValue, onValueChange, onBlur }: InputProps) => {
|
|
15
15
|
const { t } = useTranslation(translationKey);
|
|
16
16
|
const { status, error } = getStatus();
|
|
17
|
-
const
|
|
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: React.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
|
+
);
|
|
18
46
|
|
|
19
47
|
return (
|
|
20
48
|
<Input.Root validationValence={status}>
|
|
21
49
|
<InputHeader error={error}>
|
|
22
50
|
<Input.Label>{label}</Input.Label>
|
|
23
51
|
</InputHeader>
|
|
24
|
-
<div className='grid grid-cols-2 gap-2'>
|
|
52
|
+
<div role='none' className='grid grid-cols-2 gap-2'>
|
|
25
53
|
<Input.TextInput
|
|
26
|
-
type='
|
|
54
|
+
type='text'
|
|
55
|
+
pattern='^-?[0-9]*\.?[0-9]*$'
|
|
27
56
|
disabled={disabled}
|
|
28
|
-
placeholder={t('placeholder
|
|
29
|
-
value={
|
|
30
|
-
onChange={(
|
|
57
|
+
placeholder={t('placeholder latitude')}
|
|
58
|
+
value={latitudeText}
|
|
59
|
+
onChange={handleCoordinateChange('latitude', setLatitudeText)}
|
|
31
60
|
onBlur={onBlur}
|
|
32
61
|
/>
|
|
33
62
|
<Input.TextInput
|
|
34
|
-
type='
|
|
63
|
+
type='text'
|
|
64
|
+
pattern='^-?[0-9]*\.?[0-9]*$'
|
|
35
65
|
disabled={disabled}
|
|
36
|
-
placeholder={t('placeholder
|
|
37
|
-
value={
|
|
38
|
-
onChange={(
|
|
66
|
+
placeholder={t('placeholder longitude')}
|
|
67
|
+
value={longitudeText}
|
|
68
|
+
onChange={handleCoordinateChange('longitude', setLongitudeText)}
|
|
39
69
|
onBlur={onBlur}
|
|
40
70
|
/>
|
|
41
71
|
</div>
|
|
@@ -8,8 +8,8 @@ import { type SelectOption } from '@dxos/echo-schema';
|
|
|
8
8
|
import { PublicKey } from '@dxos/keys';
|
|
9
9
|
import { type ChromaticPalette, Icon, IconButton, Input, Tag, Toolbar, useTranslation } from '@dxos/react-ui';
|
|
10
10
|
import { List } from '@dxos/react-ui-list';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
11
|
+
import { HuePicker } from '@dxos/react-ui-pickers';
|
|
12
|
+
import { hues, mx } from '@dxos/react-ui-theme';
|
|
13
13
|
|
|
14
14
|
import { translationKey } from '../../../translations';
|
|
15
15
|
import { InputHeader, type InputProps } from '../Input';
|
|
@@ -32,20 +32,17 @@ export const SelectOptionInput = ({ type, label, disabled, getStatus, getValue,
|
|
|
32
32
|
|
|
33
33
|
const randomHue = () => {
|
|
34
34
|
const usedHues = new Set(options?.map((option) => option.color) ?? []);
|
|
35
|
-
const hueKeys = Object.keys(hueTokenThemes) as Array<keyof typeof hueTokenThemes>;
|
|
36
|
-
|
|
37
35
|
// Pick a random unused option from hueTokenThemes.
|
|
38
|
-
const unusedHues =
|
|
36
|
+
const unusedHues = hues.filter((hue) => !usedHues.has(hue));
|
|
39
37
|
if (unusedHues.length > 0) {
|
|
40
38
|
return unusedHues[Math.floor(Math.random() * unusedHues.length)];
|
|
41
39
|
}
|
|
42
40
|
|
|
43
41
|
// Pick a random one that hasn't been used in the last three options
|
|
44
42
|
const lastThree = new Set(options?.slice(-3).map((option) => option.color) ?? []);
|
|
45
|
-
const availableHues =
|
|
43
|
+
const availableHues = hues.filter((hue) => !lastThree.has(hue));
|
|
46
44
|
return (
|
|
47
|
-
availableHues[Math.floor(Math.random() * availableHues.length)] ??
|
|
48
|
-
hueKeys[Math.floor(Math.random() * hueKeys.length)]
|
|
45
|
+
availableHues[Math.floor(Math.random() * availableHues.length)] ?? hues[Math.floor(Math.random() * hues.length)]
|
|
49
46
|
);
|
|
50
47
|
};
|
|
51
48
|
|
|
@@ -154,9 +151,10 @@ export const SelectOptionInput = ({ type, label, disabled, getStatus, getValue,
|
|
|
154
151
|
onChange={handleTitleChange(item.id)}
|
|
155
152
|
onKeyDown={handleKeyDown}
|
|
156
153
|
classNames='flex-1'
|
|
154
|
+
data-no-submit
|
|
157
155
|
/>
|
|
158
156
|
<Toolbar.Root classNames='p-0 m-0 !is-auto'>
|
|
159
|
-
<
|
|
157
|
+
<HuePicker value={item.color} onChange={handleColorChange(item.id)} />
|
|
160
158
|
</Toolbar.Root>
|
|
161
159
|
<IconButton
|
|
162
160
|
icon='ph--trash--fill'
|
|
@@ -5,7 +5,7 @@
|
|
|
5
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
|
|
|
@@ -14,8 +14,10 @@ import { GeoPointInput } from './custom';
|
|
|
14
14
|
*/
|
|
15
15
|
export const getInputComponent = (type: SimpleType, format?: FormatEnum): InputComponent | undefined => {
|
|
16
16
|
switch (format) {
|
|
17
|
-
case FormatEnum.
|
|
17
|
+
case FormatEnum.LatLong:
|
|
18
18
|
return GeoPointInput;
|
|
19
|
+
case FormatEnum.Markdown:
|
|
20
|
+
return MarkdownInput;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
switch (type) {
|
|
@@ -21,7 +21,7 @@ const ViewMetaSchema = S.Struct({
|
|
|
21
21
|
name: S.String.annotations({
|
|
22
22
|
[AST.TitleAnnotationId]: 'View',
|
|
23
23
|
}),
|
|
24
|
-
typename: Format.
|
|
24
|
+
typename: Format.URL.annotations({
|
|
25
25
|
[AST.TitleAnnotationId]: 'Typename',
|
|
26
26
|
}),
|
|
27
27
|
}).pipe(S.mutable);
|
|
@@ -103,7 +103,7 @@ export const ViewEditor = ({
|
|
|
103
103
|
|
|
104
104
|
return (
|
|
105
105
|
<div role='none' className={mx('flex flex-col w-full divide-y divide-separator', classNames)}>
|
|
106
|
-
<Form<ViewMetaType>
|
|
106
|
+
<Form<ViewMetaType> autoSave schema={ViewMetaSchema} values={viewValues} onSave={handleViewUpdate} />
|
|
107
107
|
|
|
108
108
|
<div>
|
|
109
109
|
{/* TODO(burdon): Clean up common form ux. */}
|
|
@@ -29,7 +29,6 @@ export type DeprecatedFormInputProps = {
|
|
|
29
29
|
/**
|
|
30
30
|
* @deprecated
|
|
31
31
|
*/
|
|
32
|
-
// TODO(burdon): Still used in ObservabilitySettings and all settings.
|
|
33
32
|
export const DeprecatedFormInput = ({
|
|
34
33
|
label,
|
|
35
34
|
description,
|
|
@@ -40,7 +39,6 @@ export const DeprecatedFormInput = ({
|
|
|
40
39
|
<div role='none' className='flex w-full gap-4 py-1'>
|
|
41
40
|
<Input.Root>
|
|
42
41
|
<div role='none' className='flex flex-col w-full'>
|
|
43
|
-
{/* TODO(burdon): Consistent height for controls (e.g., Select, Textbox, and Checkbox are all different). */}
|
|
44
42
|
<Input.Label classNames='flex min-bs-[--rail-action] items-center' style={{ fontSize: 'unset' }}>
|
|
45
43
|
{label}
|
|
46
44
|
</Input.Label>
|
|
@@ -61,9 +59,8 @@ export const DeprecatedFormInput = ({
|
|
|
61
59
|
);
|
|
62
60
|
|
|
63
61
|
if (secondary) {
|
|
64
|
-
// console.log(secondary);
|
|
65
62
|
return (
|
|
66
|
-
<div role='none'
|
|
63
|
+
<div role='none'>
|
|
67
64
|
{primary}
|
|
68
65
|
{secondary}
|
|
69
66
|
</div>
|
package/src/hooks/useForm.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
6
6
|
|
|
7
7
|
import { type BaseObject, getValue, setValue } from '@dxos/echo-schema';
|
|
8
|
-
import { type
|
|
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';
|
|
@@ -24,6 +24,7 @@ export type FormHandler<T extends BaseObject> = {
|
|
|
24
24
|
touched: Record<JsonPath, boolean>;
|
|
25
25
|
changed: Record<JsonPath, boolean>;
|
|
26
26
|
canSave: boolean;
|
|
27
|
+
formIsValid: boolean;
|
|
27
28
|
handleSave: () => void;
|
|
28
29
|
|
|
29
30
|
//
|
|
@@ -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,6 +89,8 @@ 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);
|
|
@@ -128,6 +130,10 @@ export const useForm = <T extends BaseObject>({
|
|
|
128
130
|
}
|
|
129
131
|
}, [values, schemaDirty]);
|
|
130
132
|
|
|
133
|
+
const formIsValid = useMemo(() => {
|
|
134
|
+
return Object.keys(errors).length === 0;
|
|
135
|
+
}, [errors]);
|
|
136
|
+
|
|
131
137
|
/**
|
|
132
138
|
* NOTE: We can submit if there is no touched field that has an error.
|
|
133
139
|
* Basically, if there's a validation message visible in the form, submit should be disabled.
|
|
@@ -221,6 +227,7 @@ export const useForm = <T extends BaseObject>({
|
|
|
221
227
|
changed,
|
|
222
228
|
canSave,
|
|
223
229
|
handleSave,
|
|
230
|
+
formIsValid,
|
|
224
231
|
|
|
225
232
|
// Field utils.
|
|
226
233
|
getStatus,
|
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.LatLong: {
|
|
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
|
}
|