@dxos/react-ui-form 0.8.4-main.c4373fc → 0.8.4-main.d05539e30a
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 +2747 -2324
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/translations.mjs +82 -0
- package/dist/lib/browser/translations.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +2747 -2324
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/translations.mjs +84 -0
- package/dist/lib/node-esm/translations.mjs.map +7 -0
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +8 -7
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +68 -60
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +109 -26
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +114 -137
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormField.d.ts +43 -0
- package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts +79 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts +9 -0
- package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldSet.d.ts +15 -0
- package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/ArrayField.d.ts +18 -0
- package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/BooleanField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/GeoPointField.d.ts +5 -0
- package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/MarkdownField.d.ts +11 -0
- package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts +84 -0
- package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/RefField.d.ts +22 -0
- package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectField.d.ts +10 -0
- package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +84 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
- package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/index.d.ts +12 -0
- package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
- package/dist/types/src/components/Form/index.d.ts +2 -4
- package/dist/types/src/components/Form/index.d.ts.map +1 -1
- package/dist/types/src/components/ObjectForm/ObjectForm.d.ts +9 -0
- package/dist/types/src/components/ObjectForm/ObjectForm.d.ts.map +1 -0
- package/dist/types/src/components/ObjectForm/index.d.ts +2 -0
- package/dist/types/src/components/ObjectForm/index.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +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 -59
- 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 +127 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts +2 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/index.d.ts +2 -0
- package/dist/types/src/components/ObjectProperties/index.d.ts.map +1 -0
- package/dist/types/src/components/Settings/Settings.d.ts +51 -0
- package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
- package/dist/types/src/components/Settings/Settings.stories.d.ts +84 -0
- package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsFieldSet.d.ts +43 -0
- package/dist/types/src/components/Settings/SettingsFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsItem.d.ts +16 -0
- package/dist/types/src/components/Settings/SettingsItem.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsSelectField.d.ts +18 -0
- package/dist/types/src/components/Settings/SettingsSelectField.d.ts.map +1 -0
- package/dist/types/src/components/Settings/index.d.ts +4 -0
- package/dist/types/src/components/Settings/index.d.ts.map +1 -0
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +15 -16
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +70 -64
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +3 -1
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +4 -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 +65 -60
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/util/field-type.d.ts +19 -0
- package/dist/types/src/util/field-type.d.ts.map +1 -0
- package/dist/types/src/util/field.d.ts +3 -3
- package/dist/types/src/util/field.d.ts.map +1 -1
- package/dist/types/src/util/formatting.d.ts +2 -2
- package/dist/types/src/util/formatting.d.ts.map +1 -1
- package/dist/types/src/util/index.d.ts +2 -1
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/properties.d.ts +13 -0
- package/dist/types/src/util/properties.d.ts.map +1 -0
- package/dist/types/src/util/properties.test.d.ts +2 -0
- package/dist/types/src/util/properties.test.d.ts.map +1 -0
- package/dist/types/src/util/refs.d.ts +2 -2
- package/dist/types/src/util/refs.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +57 -51
- package/src/components/FieldEditor/FieldEditor.stories.tsx +44 -25
- package/src/components/FieldEditor/FieldEditor.test.tsx +0 -1
- package/src/components/FieldEditor/FieldEditor.tsx +76 -79
- package/src/components/Form/AUDIT.md +17 -0
- package/src/components/Form/Form.stories.tsx +139 -251
- package/src/components/Form/Form.tsx +375 -106
- package/src/components/Form/FormField.tsx +306 -0
- package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
- package/src/components/Form/FormFieldComponent.tsx +174 -0
- package/src/components/Form/FormFieldSet.tsx +144 -0
- package/src/components/Form/README.md +3 -0
- package/src/components/Form/fields/ArrayField.tsx +155 -0
- package/src/components/Form/fields/BooleanField.tsx +22 -0
- package/src/components/Form/fields/GeoPointField.tsx +121 -0
- package/src/components/Form/fields/MarkdownField.stories.tsx +206 -0
- package/src/components/Form/fields/MarkdownField.tsx +168 -0
- package/src/components/Form/fields/NumberField.tsx +39 -0
- package/src/components/Form/fields/RefField.tsx +229 -0
- package/src/components/Form/fields/SelectField.tsx +65 -0
- package/src/components/Form/{custom/SelectOptionsInput.stories.tsx → fields/SelectOptionField.stories.tsx} +23 -18
- package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +30 -24
- 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} +16 -9
- package/src/components/Form/fields/index.ts +15 -0
- package/src/components/Form/index.ts +3 -4
- package/src/components/ObjectForm/ObjectForm.tsx +114 -0
- package/src/components/{ControlSection → ObjectForm}/index.ts +1 -1
- package/src/components/ObjectPicker/ObjectPicker.stories.tsx +50 -45
- package/src/components/ObjectPicker/ObjectPicker.tsx +127 -86
- package/src/components/ObjectProperties/ObjectProperties.stories.tsx +362 -0
- package/src/components/ObjectProperties/ObjectProperties.test.tsx +101 -0
- package/src/components/ObjectProperties/ObjectProperties.tsx +148 -0
- package/src/components/ObjectProperties/index.ts +5 -0
- package/src/components/Settings/AUDIT.md +51 -0
- package/src/components/Settings/Settings.stories.tsx +104 -0
- package/src/components/Settings/Settings.tsx +80 -0
- package/src/components/Settings/SettingsFieldSet.tsx +257 -0
- package/src/components/Settings/SettingsItem.tsx +58 -0
- package/src/components/Settings/SettingsSelectField.tsx +59 -0
- package/src/components/Settings/index.ts +7 -0
- package/src/components/ViewEditor/ViewEditor.stories.tsx +73 -54
- package/src/components/ViewEditor/ViewEditor.test.tsx +8 -7
- package/src/components/ViewEditor/ViewEditor.tsx +304 -269
- package/src/components/index.ts +3 -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 +70 -65
- 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 +63 -0
- package/src/util/properties.ts +29 -0
- package/src/util/refs.ts +16 -18
- 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 -84
- 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 -21
- 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 -79
- 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 -28
- 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 -322
- package/src/components/Form/FormContext.tsx +0 -110
- package/src/components/Form/Input.tsx +0 -54
- package/src/components/Form/RefField.tsx +0 -269
- 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 -295
- package/src/hooks/useQueryRefOptions.ts +0 -59
- package/src/hooks/useRefQueryLookupHandler.ts +0 -39
- package/src/util/schema.ts +0 -12
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import React, {
|
|
6
|
-
type FocusEvent,
|
|
7
|
-
type PropsWithChildren,
|
|
8
|
-
type RefObject,
|
|
9
|
-
createContext,
|
|
10
|
-
useCallback,
|
|
11
|
-
useContext,
|
|
12
|
-
useEffect,
|
|
13
|
-
useMemo,
|
|
14
|
-
} from 'react';
|
|
15
|
-
|
|
16
|
-
import { raise } from '@dxos/debug';
|
|
17
|
-
import { type BaseObject, getValue } from '@dxos/echo/internal';
|
|
18
|
-
import { type SimpleType, createJsonPath } from '@dxos/effect';
|
|
19
|
-
|
|
20
|
-
import { type FormHandler, type FormOptions, useForm } from '../../hooks';
|
|
21
|
-
|
|
22
|
-
type FormContextValue<T extends BaseObject> = FormHandler<T>;
|
|
23
|
-
|
|
24
|
-
const FormContext = createContext<FormContextValue<any> | undefined>(undefined);
|
|
25
|
-
|
|
26
|
-
export const useFormContext = <T extends BaseObject>(): FormContextValue<T> => {
|
|
27
|
-
return useContext(FormContext) ?? raise(new Error('Missing FormContext'));
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export const useFormValues = (path: (string | number)[] = []): any => {
|
|
31
|
-
const { values: formValues } = useFormContext();
|
|
32
|
-
const jsonPath = createJsonPath(path);
|
|
33
|
-
return getValue(formValues, jsonPath) as BaseObject;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export type FormInputStateProps = {
|
|
37
|
-
getStatus: () => { status?: 'error'; error?: string };
|
|
38
|
-
getValue: <V>() => V | undefined;
|
|
39
|
-
onValueChange: (type: SimpleType, value: any) => void;
|
|
40
|
-
onBlur: (event: FocusEvent<HTMLElement>) => void;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const useInputProps = (path: (string | number)[] = []): FormInputStateProps => {
|
|
44
|
-
const { getStatus, getValue: getFormValue, onValueChange, onTouched } = useFormContext();
|
|
45
|
-
|
|
46
|
-
const stablePath = useMemo(() => path, [Array.isArray(path) ? path.join('.') : path]);
|
|
47
|
-
return useMemo(
|
|
48
|
-
() => ({
|
|
49
|
-
getStatus: () => getStatus(stablePath),
|
|
50
|
-
getValue: () => getFormValue(stablePath),
|
|
51
|
-
onValueChange: (type: SimpleType, value: any) => onValueChange(stablePath, type, value),
|
|
52
|
-
onBlur: () => onTouched(stablePath),
|
|
53
|
-
}),
|
|
54
|
-
[getStatus, getFormValue, onValueChange, onTouched, stablePath],
|
|
55
|
-
);
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export const FormProvider = ({
|
|
59
|
-
children,
|
|
60
|
-
formRef,
|
|
61
|
-
autoSave,
|
|
62
|
-
...formOptions
|
|
63
|
-
}: PropsWithChildren<
|
|
64
|
-
FormOptions<any> & {
|
|
65
|
-
formRef?: RefObject<HTMLDivElement | null>;
|
|
66
|
-
autoSave?: boolean;
|
|
67
|
-
}
|
|
68
|
-
>) => {
|
|
69
|
-
const form = useForm(formOptions);
|
|
70
|
-
|
|
71
|
-
const handleKeyDown = useCallback(
|
|
72
|
-
(event: KeyboardEvent) => {
|
|
73
|
-
const keyIsEnter = event.key === 'Enter';
|
|
74
|
-
const modifierUsed = event.ctrlKey || event.altKey || event.metaKey || event.shiftKey;
|
|
75
|
-
const inputIsTextarea = (event.target as HTMLElement).tagName.toLowerCase() === 'textarea';
|
|
76
|
-
const inputOptOut =
|
|
77
|
-
(event.target as HTMLElement).hasAttribute('data-no-submit') ||
|
|
78
|
-
(event.target as HTMLElement).closest('[data-no-submit]') !== null;
|
|
79
|
-
|
|
80
|
-
// Regular inputs: Submit on Enter (no modifiers).
|
|
81
|
-
const shouldSubmitRegularInput = !inputIsTextarea && keyIsEnter && !modifierUsed;
|
|
82
|
-
|
|
83
|
-
// Textareas: Submit only on Meta+Enter.
|
|
84
|
-
const shouldSubmitTextarea = inputIsTextarea && keyIsEnter && event.metaKey;
|
|
85
|
-
|
|
86
|
-
if ((shouldSubmitRegularInput || shouldSubmitTextarea) && !inputOptOut) {
|
|
87
|
-
if (!autoSave && form.canSave) {
|
|
88
|
-
form.handleSave();
|
|
89
|
-
}
|
|
90
|
-
if (autoSave && form.formIsValid) {
|
|
91
|
-
(event.target as HTMLElement).blur();
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
[form.canSave, form.formIsValid, form.handleSave, autoSave],
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
useEffect(() => {
|
|
99
|
-
if (!formRef?.current) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const formElement = formRef.current;
|
|
104
|
-
|
|
105
|
-
formElement.addEventListener('keydown', handleKeyDown);
|
|
106
|
-
return () => formElement.removeEventListener('keydown', handleKeyDown);
|
|
107
|
-
}, [formRef, form.canSave, form.formIsValid, form.handleSave, autoSave]);
|
|
108
|
-
|
|
109
|
-
return <FormContext.Provider value={form}>{children}</FormContext.Provider>;
|
|
110
|
-
};
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import React, { type FC } from 'react';
|
|
6
|
-
|
|
7
|
-
import { type FormatEnum } from '@dxos/echo/internal';
|
|
8
|
-
import { type SimpleType } from '@dxos/effect';
|
|
9
|
-
import { Icon, Input, Tooltip } from '@dxos/react-ui';
|
|
10
|
-
import { labelSpacing } from '@dxos/react-ui-stack';
|
|
11
|
-
import { errorText, mx } from '@dxos/react-ui-theme';
|
|
12
|
-
|
|
13
|
-
import { type FormInputStateProps } from './FormContext';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Props passed to input components.
|
|
17
|
-
*/
|
|
18
|
-
export type InputProps = {
|
|
19
|
-
type: SimpleType;
|
|
20
|
-
format?: FormatEnum;
|
|
21
|
-
label: string;
|
|
22
|
-
placeholder?: string;
|
|
23
|
-
inputOnly?: boolean;
|
|
24
|
-
/**
|
|
25
|
-
* Specifies the readonly variant: either disabled inputs, elements indicating they are usually editable but currently are not; or `static`, a field’s representation as regular content without signifiers that it is ever editable.
|
|
26
|
-
*/
|
|
27
|
-
readonly?: 'disabled-input' | 'static' | false;
|
|
28
|
-
} & FormInputStateProps;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Form input component.
|
|
32
|
-
*/
|
|
33
|
-
export type InputComponent = FC<InputProps>;
|
|
34
|
-
|
|
35
|
-
export type InputHeaderProps = {
|
|
36
|
-
error?: string;
|
|
37
|
-
label: string;
|
|
38
|
-
readonly?: boolean;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export const InputHeader = ({ error, label, readonly }: InputHeaderProps) => {
|
|
42
|
-
const Label = readonly ? 'span' : Input.Label;
|
|
43
|
-
const labelProps = readonly ? {} : { classNames: '!mlb-0' };
|
|
44
|
-
return (
|
|
45
|
-
<div role='none' className={mx('flex justify-between items-center', labelSpacing)}>
|
|
46
|
-
<Label {...labelProps}>{label}</Label>
|
|
47
|
-
{error && (
|
|
48
|
-
<Tooltip.Trigger asChild content={error} side='bottom'>
|
|
49
|
-
<Icon icon='ph--warning--regular' size={4} classNames={errorText} />
|
|
50
|
-
</Tooltip.Trigger>
|
|
51
|
-
)}
|
|
52
|
-
</div>
|
|
53
|
-
);
|
|
54
|
-
};
|
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import type * as Schema from 'effect/Schema';
|
|
6
|
-
import type * as SchemaAST from 'effect/SchemaAST';
|
|
7
|
-
import React, { useCallback, useMemo } from 'react';
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
type EchoSchema,
|
|
11
|
-
Expando,
|
|
12
|
-
Ref,
|
|
13
|
-
ReferenceAnnotationId,
|
|
14
|
-
type ReferenceAnnotationValue,
|
|
15
|
-
getTypeAnnotation,
|
|
16
|
-
} from '@dxos/echo/internal';
|
|
17
|
-
import { findAnnotation } from '@dxos/effect';
|
|
18
|
-
import { DXN } from '@dxos/keys';
|
|
19
|
-
import { type DxTagPickerItemClick } from '@dxos/lit-ui';
|
|
20
|
-
import { DxAnchor, DxTagPickerItem } from '@dxos/lit-ui/react';
|
|
21
|
-
import { Button, Icon, Input, useTranslation } from '@dxos/react-ui';
|
|
22
|
-
import { descriptionText, mx } from '@dxos/react-ui-theme';
|
|
23
|
-
import { isNonNullable } from '@dxos/util';
|
|
24
|
-
|
|
25
|
-
import { type QueryRefOptions, useQueryRefOptions } from '../../hooks';
|
|
26
|
-
import { translationKey } from '../../translations';
|
|
27
|
-
import { ObjectPicker } from '../ObjectPicker';
|
|
28
|
-
|
|
29
|
-
import { TextInput } from './Defaults';
|
|
30
|
-
import { InputHeader, type InputProps } from './Input';
|
|
31
|
-
|
|
32
|
-
export type RefFieldProps = InputProps & {
|
|
33
|
-
ast?: SchemaAST.AST;
|
|
34
|
-
array?: boolean;
|
|
35
|
-
createOptionLabel?: [string, { ns: string }];
|
|
36
|
-
createOptionIcon?: string;
|
|
37
|
-
onCreate?: (values: any) => void;
|
|
38
|
-
createSchema?: Schema.Schema.AnyNoContext;
|
|
39
|
-
createInitialValuePath?: string;
|
|
40
|
-
onQueryRefOptions?: QueryRefOptions;
|
|
41
|
-
schema?: EchoSchema;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
// TODO(thure): Is this a standard that should be better canonized?
|
|
45
|
-
const isRefSnapShot = (val: any): val is { '/': string } => {
|
|
46
|
-
return typeof val === 'object' && typeof (val as any)?.['/'] === 'string';
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export const RefField = ({
|
|
50
|
-
type,
|
|
51
|
-
label,
|
|
52
|
-
readonly,
|
|
53
|
-
placeholder,
|
|
54
|
-
inputOnly,
|
|
55
|
-
array,
|
|
56
|
-
ast,
|
|
57
|
-
getValue,
|
|
58
|
-
createOptionLabel,
|
|
59
|
-
createOptionIcon,
|
|
60
|
-
onBlur,
|
|
61
|
-
onCreate,
|
|
62
|
-
createSchema,
|
|
63
|
-
createInitialValuePath,
|
|
64
|
-
onQueryRefOptions,
|
|
65
|
-
onValueChange,
|
|
66
|
-
...restInputProps
|
|
67
|
-
}: RefFieldProps) => {
|
|
68
|
-
const { t } = useTranslation(translationKey);
|
|
69
|
-
const refTypeInfo = useMemo(
|
|
70
|
-
() => (ast ? findAnnotation<ReferenceAnnotationValue>(ast, ReferenceAnnotationId) : undefined),
|
|
71
|
-
[ast],
|
|
72
|
-
);
|
|
73
|
-
const { options: availableOptions, loading: _loading } = useQueryRefOptions({ refTypeInfo, onQueryRefOptions });
|
|
74
|
-
|
|
75
|
-
if ((refTypeInfo && refTypeInfo?.typename === getTypeAnnotation(Expando)?.typename) || !onQueryRefOptions) {
|
|
76
|
-
// If ref type is expando, fall back to taking a DXN in string format.
|
|
77
|
-
return (
|
|
78
|
-
<RefFieldFallback
|
|
79
|
-
{...{ type, label, placeholder, readonly, inputOnly, getValue, onBlur, onValueChange, ...restInputProps }}
|
|
80
|
-
/>
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const handleGetValue = useCallback(() => {
|
|
85
|
-
const formValue = getValue();
|
|
86
|
-
|
|
87
|
-
const unknownToRefOption = (val: unknown) => {
|
|
88
|
-
const isRef = Ref.isRef(val);
|
|
89
|
-
if (isRef || isRefSnapShot(val)) {
|
|
90
|
-
const dxnString = isRef ? val.dxn.toString() : val['/'];
|
|
91
|
-
const matchingOption = availableOptions.find((option) => option.id === dxnString);
|
|
92
|
-
if (matchingOption) {
|
|
93
|
-
return matchingOption;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return undefined;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
if (array && Array.isArray(formValue)) {
|
|
100
|
-
return formValue.map(unknownToRefOption).filter(isNonNullable) ?? [];
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const option = unknownToRefOption(formValue);
|
|
104
|
-
if (option) {
|
|
105
|
-
return [option];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return [];
|
|
109
|
-
}, [getValue, availableOptions, array]);
|
|
110
|
-
|
|
111
|
-
const handleUpdate = useCallback(
|
|
112
|
-
(ids: string[]) => {
|
|
113
|
-
if (ids.length === 0) {
|
|
114
|
-
onValueChange('object', undefined);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const refs = ids
|
|
119
|
-
.map((id) => {
|
|
120
|
-
const item = availableOptions.find((option) => option.id === id);
|
|
121
|
-
if (item) {
|
|
122
|
-
const dxn = DXN.parse(item.id);
|
|
123
|
-
return Ref.fromDXN(dxn);
|
|
124
|
-
}
|
|
125
|
-
return null;
|
|
126
|
-
})
|
|
127
|
-
.filter(isNonNullable);
|
|
128
|
-
|
|
129
|
-
if (array) {
|
|
130
|
-
onValueChange('object', refs);
|
|
131
|
-
} else {
|
|
132
|
-
onValueChange('object', refs[0]);
|
|
133
|
-
}
|
|
134
|
-
},
|
|
135
|
-
[availableOptions, array, onValueChange],
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
if (!refTypeInfo) {
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const { status, error } = restInputProps.getStatus();
|
|
143
|
-
|
|
144
|
-
const items = handleGetValue();
|
|
145
|
-
const selectedIds = useMemo(() => items.map((i: any) => i.id), [items]);
|
|
146
|
-
const toggleSelect = useCallback(
|
|
147
|
-
(id: string) => {
|
|
148
|
-
if (array) {
|
|
149
|
-
const nextIds = selectedIds.includes(id) ? selectedIds.filter((x) => x !== id) : [...selectedIds, id];
|
|
150
|
-
handleUpdate(nextIds);
|
|
151
|
-
} else {
|
|
152
|
-
if (selectedIds[0] === id) {
|
|
153
|
-
handleUpdate([]);
|
|
154
|
-
} else {
|
|
155
|
-
handleUpdate([id]);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
[array, selectedIds, handleUpdate],
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
// NOTE(thure): I left both predicates in-place in case we decide to add variants which do render readonly but empty values.
|
|
163
|
-
return readonly && items.length < 1 ? null : (
|
|
164
|
-
<Input.Root validationValence={status}>
|
|
165
|
-
{!inputOnly && <InputHeader error={error} label={label} />}
|
|
166
|
-
<div data-no-submit>
|
|
167
|
-
{readonly ? (
|
|
168
|
-
items.length < 1 ? (
|
|
169
|
-
<p className={mx(descriptionText, 'mbe-2')}>{t('empty readonly ref field label')}</p>
|
|
170
|
-
) : (
|
|
171
|
-
items.map((item) => (
|
|
172
|
-
<DxAnchor key={item.id} refid={item.id} rootclassname='mie-1'>
|
|
173
|
-
{item.label}
|
|
174
|
-
</DxAnchor>
|
|
175
|
-
))
|
|
176
|
-
)
|
|
177
|
-
) : (
|
|
178
|
-
<ObjectPicker.Root>
|
|
179
|
-
<ObjectPicker.Trigger asChild>
|
|
180
|
-
<Button variant='ghost' classNames='is-full text-start gap-2'>
|
|
181
|
-
<div role='none' className='grow'>
|
|
182
|
-
{items?.length ? (
|
|
183
|
-
items?.map((item) => (
|
|
184
|
-
<DxTagPickerItem
|
|
185
|
-
key={item.id}
|
|
186
|
-
itemId={item.id}
|
|
187
|
-
label={item.label}
|
|
188
|
-
{...(item.hue ? { hue: item.hue } : {})}
|
|
189
|
-
removeLabel={t('remove item label')}
|
|
190
|
-
onItemClick={(event: DxTagPickerItemClick) => {
|
|
191
|
-
if (event.action === 'remove') {
|
|
192
|
-
toggleSelect(item.id);
|
|
193
|
-
}
|
|
194
|
-
}}
|
|
195
|
-
/>
|
|
196
|
-
))
|
|
197
|
-
) : (
|
|
198
|
-
<span className='text-description'>
|
|
199
|
-
{placeholder ?? t('ref field placeholder', { count: array ? 99 : 1 })}
|
|
200
|
-
</span>
|
|
201
|
-
)}
|
|
202
|
-
</div>
|
|
203
|
-
<Icon size={3} icon='ph--caret-down--bold' />
|
|
204
|
-
</Button>
|
|
205
|
-
</ObjectPicker.Trigger>
|
|
206
|
-
<ObjectPicker.Content
|
|
207
|
-
options={availableOptions}
|
|
208
|
-
selectedIds={selectedIds}
|
|
209
|
-
onSelect={toggleSelect}
|
|
210
|
-
createSchema={createSchema}
|
|
211
|
-
createOptionLabel={createOptionLabel}
|
|
212
|
-
createOptionIcon={createOptionIcon}
|
|
213
|
-
createInitialValuePath={createInitialValuePath}
|
|
214
|
-
onCreate={onCreate}
|
|
215
|
-
/>
|
|
216
|
-
</ObjectPicker.Root>
|
|
217
|
-
)}
|
|
218
|
-
</div>
|
|
219
|
-
{inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
|
|
220
|
-
</Input.Root>
|
|
221
|
-
);
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
const RefFieldFallback = ({
|
|
225
|
-
type,
|
|
226
|
-
label,
|
|
227
|
-
readonly,
|
|
228
|
-
placeholder,
|
|
229
|
-
inputOnly,
|
|
230
|
-
getValue,
|
|
231
|
-
onValueChange,
|
|
232
|
-
...restInputProps
|
|
233
|
-
}: InputProps) => {
|
|
234
|
-
const handleOnValueChange = (_type: any, dxnString: string) => {
|
|
235
|
-
const dxn = DXN.tryParse(dxnString);
|
|
236
|
-
if (dxn) {
|
|
237
|
-
onValueChange?.('object', Ref.fromDXN(dxn));
|
|
238
|
-
} else if (dxnString === '') {
|
|
239
|
-
onValueChange?.('object', undefined);
|
|
240
|
-
} else {
|
|
241
|
-
onValueChange?.('string', dxnString);
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
const handleGetValue = () => {
|
|
246
|
-
const formValue = getValue();
|
|
247
|
-
if (typeof formValue === 'string') {
|
|
248
|
-
return formValue;
|
|
249
|
-
}
|
|
250
|
-
if (Ref.isRef(formValue)) {
|
|
251
|
-
return formValue.dxn.toString();
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
return undefined;
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
return (
|
|
258
|
-
<TextInput
|
|
259
|
-
type={type}
|
|
260
|
-
label={label}
|
|
261
|
-
readonly={readonly}
|
|
262
|
-
placeholder={placeholder}
|
|
263
|
-
inputOnly={inputOnly}
|
|
264
|
-
getValue={handleGetValue as <V>() => V | undefined}
|
|
265
|
-
onValueChange={handleOnValueChange}
|
|
266
|
-
{...restInputProps}
|
|
267
|
-
/>
|
|
268
|
-
);
|
|
269
|
-
};
|
|
@@ -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
|
-
};
|