@dxos/react-ui-form 0.8.4-main.f9ba587 → 0.8.4-main.fcfe5033a5
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/README.md +0 -1
- package/dist/lib/browser/index.mjs +2758 -2113
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +2758 -2113
- 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 +9 -9
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +85 -7
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +86 -25
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +129 -56
- 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 +77 -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 +18 -0
- package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/ArrayField.d.ts +18 -0
- package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/BooleanField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/GeoPointField.d.ts +5 -0
- package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/MarkdownField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/RefField.d.ts +16 -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 +83 -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 +54 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +86 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/index.d.ts +2 -0
- package/dist/types/src/components/ObjectPicker/index.d.ts.map +1 -0
- package/dist/types/src/components/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 +83 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.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 +43 -0
- package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
- package/dist/types/src/components/Settings/Settings.stories.d.ts +83 -0
- package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsFieldSet.d.ts +38 -0
- package/dist/types/src/components/Settings/SettingsFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsItem.d.ts +17 -0
- package/dist/types/src/components/Settings/SettingsItem.d.ts.map +1 -0
- package/dist/types/src/components/Settings/index.d.ts +3 -0
- package/dist/types/src/components/Settings/index.d.ts.map +1 -0
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +22 -10
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +86 -9
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +4 -2
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +4 -4
- 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/translations.d.ts +64 -54
- 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 +7 -0
- package/dist/types/src/util/properties.d.ts.map +1 -0
- package/dist/types/src/util/refs.d.ts +3 -3
- package/dist/types/src/util/refs.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +46 -43
- package/src/components/FieldEditor/FieldEditor.stories.tsx +51 -31
- package/src/components/FieldEditor/FieldEditor.test.tsx +13 -13
- package/src/components/FieldEditor/FieldEditor.tsx +88 -94
- package/src/components/Form/AUDIT.md +17 -0
- package/src/components/Form/Form.stories.tsx +144 -221
- package/src/components/Form/Form.tsx +359 -95
- package/src/components/Form/FormField.tsx +304 -0
- package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
- package/src/components/Form/FormFieldComponent.tsx +171 -0
- package/src/components/Form/FormFieldSet.tsx +144 -0
- package/src/components/Form/README.md +3 -0
- package/src/components/Form/fields/ArrayField.tsx +154 -0
- package/src/components/Form/fields/BooleanField.tsx +22 -0
- package/src/components/Form/fields/GeoPointField.tsx +120 -0
- package/src/components/Form/fields/MarkdownField.tsx +78 -0
- package/src/components/Form/fields/NumberField.tsx +39 -0
- package/src/components/Form/fields/RefField.tsx +198 -0
- package/src/components/Form/fields/SelectField.tsx +65 -0
- package/src/components/Form/fields/SelectOptionField.stories.tsx +65 -0
- package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +49 -45
- 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 -10
- package/src/components/Form/fields/index.ts +15 -0
- package/src/components/Form/index.ts +3 -4
- package/src/components/ObjectForm/ObjectForm.tsx +113 -0
- package/src/components/{ControlSection → ObjectForm}/index.ts +1 -1
- package/src/components/ObjectPicker/ObjectPicker.stories.tsx +209 -0
- package/src/components/ObjectPicker/ObjectPicker.tsx +186 -0
- package/src/components/ObjectPicker/index.ts +5 -0
- package/src/components/ObjectProperties/ObjectProperties.stories.tsx +63 -0
- package/src/components/ObjectProperties/ObjectProperties.tsx +129 -0
- package/src/components/ObjectProperties/index.ts +5 -0
- package/src/components/Settings/Settings.stories.tsx +103 -0
- package/src/components/Settings/Settings.tsx +81 -0
- package/src/components/Settings/SettingsFieldSet.tsx +211 -0
- package/src/components/Settings/SettingsItem.tsx +62 -0
- package/src/components/Settings/index.ts +6 -0
- package/src/components/ViewEditor/ViewEditor.stories.tsx +144 -61
- package/src/components/ViewEditor/ViewEditor.test.tsx +22 -22
- package/src/components/ViewEditor/ViewEditor.tsx +353 -218
- package/src/components/index.ts +4 -3
- package/src/components/testing.tsx +28 -12
- package/src/hooks/index.ts +2 -3
- package/src/hooks/useFormHandler.ts +380 -0
- package/src/hooks/useKeyHandler.tsx +37 -0
- package/src/translations.ts +67 -58
- package/src/util/field-type.ts +57 -0
- package/src/util/field.ts +33 -43
- package/src/util/formatting.ts +22 -22
- package/src/util/index.ts +2 -1
- package/src/util/properties.ts +18 -0
- package/src/util/refs.ts +17 -19
- 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 -8
- 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 -57
- package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
- package/dist/types/src/components/Form/FormContext.d.ts +0 -23
- package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
- package/dist/types/src/components/Form/Input.d.ts +0 -26
- package/dist/types/src/components/Form/Input.d.ts.map +0 -1
- package/dist/types/src/components/Form/RefField.d.ts +0 -12
- package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -7
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
- package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/index.d.ts +0 -4
- package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
- package/dist/types/src/components/Form/factory.d.ts +0 -8
- package/dist/types/src/components/Form/factory.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/Form.d.ts +0 -15
- package/dist/types/src/components/deprecated/Form.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/Form.stories.d.ts +0 -13
- package/dist/types/src/components/deprecated/Form.stories.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/FormInput.d.ts +0 -16
- package/dist/types/src/components/deprecated/FormInput.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/index.d.ts +0 -2
- package/dist/types/src/components/deprecated/index.d.ts.map +0 -1
- package/dist/types/src/hooks/useForm.d.ts +0 -67
- package/dist/types/src/hooks/useForm.d.ts.map +0 -1
- package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -23
- 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 -41
- package/src/components/ControlSection/ControlSection.tsx +0 -139
- package/src/components/Form/ArrayField.tsx +0 -124
- package/src/components/Form/Defaults.tsx +0 -203
- package/src/components/Form/FormActions.tsx +0 -53
- package/src/components/Form/FormContent.tsx +0 -240
- package/src/components/Form/FormContext.tsx +0 -110
- package/src/components/Form/Input.tsx +0 -51
- package/src/components/Form/RefField.tsx +0 -213
- package/src/components/Form/custom/GeoPointInput.tsx +0 -72
- package/src/components/Form/custom/SelectOptionsInput.stories.tsx +0 -61
- package/src/components/Form/custom/index.ts +0 -7
- package/src/components/Form/factory.tsx +0 -31
- package/src/components/deprecated/Form.stories.tsx +0 -62
- package/src/components/deprecated/Form.tsx +0 -119
- package/src/components/deprecated/FormInput.tsx +0 -71
- package/src/components/deprecated/index.ts +0 -5
- package/src/hooks/useForm.ts +0 -291
- package/src/hooks/useQueryRefOptions.ts +0 -53
- package/src/hooks/useRefQueryLookupHandler.ts +0 -39
- package/src/util/schema.ts +0 -14
|
@@ -1,108 +1,372 @@
|
|
|
1
1
|
//
|
|
2
|
-
// Copyright
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
import { type
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { createContext } from '@radix-ui/react-context';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
8
|
+
import React, { type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
|
|
9
|
+
|
|
10
|
+
import { type AnyProperties } from '@dxos/echo/internal';
|
|
11
|
+
import { createJsonPath, getValue as getValue$ } from '@dxos/effect';
|
|
12
|
+
import {
|
|
13
|
+
IconButton,
|
|
14
|
+
type IconButtonProps,
|
|
15
|
+
ScrollArea,
|
|
16
|
+
type ThemedClassName,
|
|
17
|
+
useMergeRefs,
|
|
18
|
+
useTranslation,
|
|
19
|
+
} from '@dxos/react-ui';
|
|
20
|
+
import { composable, composableProps, mx, withColumn } from '@dxos/ui-theme';
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
type FormHandler,
|
|
24
|
+
type FormHandlerProps,
|
|
25
|
+
type FormUpdateMeta,
|
|
26
|
+
useFormHandler,
|
|
27
|
+
useKeyHandler,
|
|
28
|
+
} from '../../hooks';
|
|
29
|
+
import { translationKey } from '../../translations';
|
|
30
|
+
import { FormFieldLabel, type FormFieldLabelProps, type FormFieldStateProps } from './FormFieldComponent';
|
|
31
|
+
import {
|
|
32
|
+
FormFieldSet as NaturalFormFieldSet,
|
|
33
|
+
type FormFieldSetProps as NaturalFormFieldSetProps,
|
|
34
|
+
} from './FormFieldSet';
|
|
35
|
+
|
|
36
|
+
// TODO(burdon): Move styles to form.ts (as with ui-theme).
|
|
37
|
+
|
|
38
|
+
// TODO(burdon): Reconcile with @dxos/echo.
|
|
39
|
+
export type ExcludeId<S extends Schema.Schema.AnyNoContext> = Omit<Schema.Schema.Type<S>, 'id'>;
|
|
40
|
+
|
|
41
|
+
// TODO(burdon): Move to @dxos/schema (re-export here).
|
|
42
|
+
export const omitId = <S extends Schema.Schema.AnyNoContext>(schema: S): Schema.Schema<ExcludeId<S>, ExcludeId<S>> =>
|
|
43
|
+
schema.pipe(Schema.omit('id')) as any;
|
|
44
|
+
|
|
45
|
+
//
|
|
46
|
+
// Context
|
|
47
|
+
//
|
|
48
|
+
|
|
49
|
+
type FormContextValue<T extends AnyProperties = any> = {
|
|
50
|
+
/**
|
|
51
|
+
* Form handler.
|
|
52
|
+
*/
|
|
53
|
+
form: FormHandler<T>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Show debug info.
|
|
57
|
+
*/
|
|
58
|
+
debug?: boolean;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Testing.
|
|
62
|
+
*/
|
|
63
|
+
testId?: string;
|
|
64
|
+
} & Pick<
|
|
65
|
+
NaturalFormFieldSetProps<T>,
|
|
66
|
+
'readonly' | 'layout' | 'fieldMap' | 'fieldProvider' | 'projection' | 'createTypename' | 'createFieldMap'
|
|
67
|
+
>;
|
|
68
|
+
|
|
69
|
+
const [FormContextProvider, useFormContext] = createContext<FormContextValue>('Form');
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Get the current form values.
|
|
73
|
+
*/
|
|
74
|
+
const useFormValues: {
|
|
75
|
+
<T extends AnyProperties>(componentName: string, path?: (string | number)[]): T;
|
|
76
|
+
<T extends AnyProperties>(
|
|
77
|
+
componentName: string,
|
|
78
|
+
path: (string | number)[] | undefined,
|
|
79
|
+
defaultValue: () => T,
|
|
80
|
+
): T | undefined;
|
|
81
|
+
} = (componentName: string, path?: (string | number)[], defaultValue?: () => any) => {
|
|
82
|
+
const stablePath = useMemo(() => path ?? [], [path ? path.join('.') : undefined]);
|
|
83
|
+
const jsonPath = createJsonPath(stablePath);
|
|
84
|
+
const {
|
|
85
|
+
form: { values, onValueChange },
|
|
86
|
+
} = useFormContext(componentName);
|
|
87
|
+
|
|
88
|
+
const value = getValue$(values, jsonPath);
|
|
89
|
+
|
|
90
|
+
// Apply default value once when the field has no value. lastAppliedPathRef prevents
|
|
91
|
+
// re-applying on every render (e.g. when defaultValue() returns null) and ensures
|
|
92
|
+
// we apply per path when the hook is used for different fields.
|
|
93
|
+
const lastAppliedPathRef = useRef<string | null>(null);
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (value == null && defaultValue && lastAppliedPathRef.current !== jsonPath) {
|
|
96
|
+
lastAppliedPathRef.current = jsonPath;
|
|
97
|
+
onValueChange(stablePath, SchemaAST.stringKeyword, defaultValue());
|
|
98
|
+
}
|
|
99
|
+
}, [value, defaultValue, onValueChange, stablePath, jsonPath]);
|
|
100
|
+
|
|
101
|
+
return value;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Get the state props for the given field.
|
|
106
|
+
*/
|
|
107
|
+
const useFormFieldState = (componentName: string, path: (string | number)[] = []): FormFieldStateProps => {
|
|
108
|
+
const stablePath = useMemo(() => path, [Array.isArray(path) ? path.join('.') : path]);
|
|
109
|
+
const {
|
|
110
|
+
form: { getStatus, getValue, onBlur, onValueChange },
|
|
111
|
+
} = useFormContext(componentName);
|
|
112
|
+
|
|
113
|
+
return useMemo(
|
|
114
|
+
() => ({
|
|
115
|
+
getStatus: () => getStatus(stablePath),
|
|
116
|
+
getValue: () => getValue(stablePath),
|
|
117
|
+
onBlur: () => onBlur(stablePath),
|
|
118
|
+
onValueChange: (ast: SchemaAST.AST, value: any) => onValueChange(stablePath, ast, value),
|
|
119
|
+
}),
|
|
120
|
+
[getStatus, getValue, onBlur, onValueChange, stablePath],
|
|
121
|
+
);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
//
|
|
125
|
+
// Root
|
|
126
|
+
//
|
|
127
|
+
|
|
128
|
+
type FormRootProps<T extends AnyProperties = AnyProperties> = PropsWithChildren<
|
|
129
|
+
{
|
|
130
|
+
/**
|
|
131
|
+
* Called when the form is submitted and passes validation.
|
|
132
|
+
*/
|
|
133
|
+
onSave?: (values: T, meta: FormUpdateMeta<T>) => void;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Called when the form is canceled to abandon/undo any pending changes.
|
|
137
|
+
*/
|
|
138
|
+
onCancel?: () => void;
|
|
139
|
+
} & Omit<FormContextValue<T>, 'form'> &
|
|
140
|
+
Pick<FormHandlerProps<T>, 'schema' | 'autoSave' | 'values' | 'defaultValues' | 'onValidate' | 'onValuesChanged'> &
|
|
141
|
+
Omit<NaturalFormFieldSetProps<T>, 'schema' | 'path'>
|
|
142
|
+
>;
|
|
143
|
+
|
|
144
|
+
const FormRoot = <T extends AnyProperties = AnyProperties>({
|
|
145
|
+
children,
|
|
52
146
|
schema,
|
|
53
|
-
|
|
54
|
-
onValidate,
|
|
147
|
+
values,
|
|
55
148
|
onSave,
|
|
149
|
+
onCancel,
|
|
56
150
|
...props
|
|
57
|
-
}:
|
|
58
|
-
const
|
|
151
|
+
}: FormRootProps<T>) => {
|
|
152
|
+
const form = useFormHandler({ schema, values, onSave, onCancel, ...props });
|
|
59
153
|
|
|
60
|
-
|
|
61
|
-
|
|
154
|
+
return (
|
|
155
|
+
<FormContextProvider form={form} {...props}>
|
|
156
|
+
{children}
|
|
157
|
+
</FormContextProvider>
|
|
158
|
+
);
|
|
159
|
+
};
|
|
62
160
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
161
|
+
FormRoot.displayName = 'Form.Root';
|
|
162
|
+
|
|
163
|
+
//
|
|
164
|
+
// Viewport
|
|
165
|
+
//
|
|
166
|
+
|
|
167
|
+
const FORM_VIEWPORT_NAME = 'Form.Viewport';
|
|
168
|
+
|
|
169
|
+
type FormViewportProps = {};
|
|
170
|
+
|
|
171
|
+
const FormViewport = composable<HTMLDivElement>(({ children, ...props }, forwardedRef) => {
|
|
172
|
+
return (
|
|
173
|
+
<ScrollArea.Root {...composableProps(props)} orientation='vertical' centered padding thin ref={forwardedRef}>
|
|
174
|
+
<ScrollArea.Viewport>{children}</ScrollArea.Viewport>
|
|
175
|
+
</ScrollArea.Root>
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
FormViewport.displayName = FORM_VIEWPORT_NAME;
|
|
180
|
+
|
|
181
|
+
//
|
|
182
|
+
// Content
|
|
183
|
+
//
|
|
184
|
+
|
|
185
|
+
const FORM_CONTENT_NAME = 'Form.Content';
|
|
186
|
+
|
|
187
|
+
type FormContentProps = ThemedClassName<PropsWithChildren<{}>>;
|
|
188
|
+
|
|
189
|
+
const FormContent = composable<HTMLDivElement, FormContentProps>(({ children, ...props }, forwardedRef) => {
|
|
190
|
+
const { form, testId } = useFormContext(FORM_CONTENT_NAME);
|
|
191
|
+
const localRef = useRef<HTMLDivElement>(null);
|
|
192
|
+
const mergedRef = useMergeRefs([forwardedRef, localRef]);
|
|
193
|
+
useKeyHandler(localRef, form);
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<div
|
|
197
|
+
{...composableProps(props, {
|
|
198
|
+
role: 'form',
|
|
199
|
+
classNames: mx(withColumn.center(), 'flex flex-col w-full pb-form-gap'),
|
|
200
|
+
})}
|
|
201
|
+
data-testid={testId}
|
|
202
|
+
ref={mergedRef}
|
|
203
|
+
>
|
|
204
|
+
{children}
|
|
205
|
+
</div>
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
FormContent.displayName = FORM_CONTENT_NAME;
|
|
210
|
+
|
|
211
|
+
//
|
|
212
|
+
// FieldSet
|
|
213
|
+
//
|
|
214
|
+
|
|
215
|
+
const FORM_FIELDSET_NAME = 'Form.FieldSet';
|
|
216
|
+
|
|
217
|
+
type FormFieldSetProps = ThemedClassName<NaturalFormFieldSetProps<any>>;
|
|
218
|
+
|
|
219
|
+
const FormFieldSet = (props: FormFieldSetProps) => {
|
|
220
|
+
const { form, ...contextProps } = useFormContext(FORM_FIELDSET_NAME);
|
|
221
|
+
|
|
222
|
+
return <NaturalFormFieldSet schema={form.schema} {...contextProps} {...props} />;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
FormFieldSet.displayName = FORM_FIELDSET_NAME;
|
|
226
|
+
|
|
227
|
+
//
|
|
228
|
+
// Actions
|
|
229
|
+
//
|
|
230
|
+
|
|
231
|
+
const FORM_ACTIONS_NAME = 'Form.Actions';
|
|
232
|
+
|
|
233
|
+
type FormActionsProps = ThemedClassName<{}>;
|
|
234
|
+
|
|
235
|
+
const FormActions = ({ classNames }: FormActionsProps) => {
|
|
236
|
+
const { t } = useTranslation(translationKey);
|
|
237
|
+
const {
|
|
238
|
+
form: { canSave, onSave, onCancel },
|
|
239
|
+
readonly,
|
|
240
|
+
layout,
|
|
241
|
+
} = useFormContext(FORM_ACTIONS_NAME);
|
|
242
|
+
|
|
243
|
+
if (readonly || layout === 'static') {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// TODO(burdon): Currently onCancel is a no-op; implement "revert values".
|
|
248
|
+
// Deprecate FormSubmit ans use FormActions without Cancel button if no callback is supplied.
|
|
72
249
|
|
|
73
250
|
return (
|
|
74
|
-
<
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
autoSave={autoSave}
|
|
78
|
-
initialValues={initialValues}
|
|
79
|
-
onValuesChanged={onValuesChanged}
|
|
80
|
-
onValidate={onValidate}
|
|
81
|
-
onValid={handleValid}
|
|
82
|
-
onSave={onSave}
|
|
251
|
+
<div
|
|
252
|
+
role='none'
|
|
253
|
+
className={mx(withColumn.center(), 'grid grid-flow-col gap-form-gap auto-cols-fr py-form-padding', classNames)}
|
|
83
254
|
>
|
|
84
|
-
|
|
85
|
-
<
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
: outerSpacing === 'scroll-fields'
|
|
92
|
-
? 'pli-cardSpacingInline pbe-cardSpacingBlock'
|
|
93
|
-
: outerSpacing
|
|
94
|
-
? cardSpacing
|
|
95
|
-
: false,
|
|
96
|
-
outerSpacing === 'scroll-fields' && cardDialogOverflow,
|
|
97
|
-
classNames,
|
|
98
|
-
]}
|
|
99
|
-
readonly={readonly}
|
|
100
|
-
schema={schema}
|
|
255
|
+
{onCancel && (
|
|
256
|
+
<IconButton
|
|
257
|
+
icon='ph--x--regular'
|
|
258
|
+
iconEnd
|
|
259
|
+
label={t('cancel-button.label')}
|
|
260
|
+
onClick={onCancel}
|
|
261
|
+
data-testid='cancel-button'
|
|
101
262
|
/>
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
263
|
+
)}
|
|
264
|
+
{onSave && (
|
|
265
|
+
<IconButton
|
|
266
|
+
type='submit'
|
|
267
|
+
variant='primary'
|
|
268
|
+
disabled={!canSave}
|
|
269
|
+
icon='ph--check--regular'
|
|
270
|
+
iconEnd
|
|
271
|
+
label={t('save-button.label')}
|
|
272
|
+
onClick={onSave}
|
|
273
|
+
data-testid='save-button'
|
|
274
|
+
/>
|
|
275
|
+
)}
|
|
276
|
+
</div>
|
|
277
|
+
);
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
FormActions.displayName = FORM_ACTIONS_NAME;
|
|
281
|
+
|
|
282
|
+
//
|
|
283
|
+
// Section
|
|
284
|
+
//
|
|
285
|
+
|
|
286
|
+
const FORM_SECTION_NAME = 'Form.Section';
|
|
287
|
+
|
|
288
|
+
type FormSectionProps = ThemedClassName<{ label: string; description?: string }>;
|
|
289
|
+
|
|
290
|
+
const FormSection = composable<HTMLDivElement, FormSectionProps>(
|
|
291
|
+
({ children, label, description, ...props }, forwardedRef) => {
|
|
292
|
+
return (
|
|
293
|
+
<div
|
|
294
|
+
{...composableProps(props, { classNames: 'flex flex-col pt-form-section-gap first:pt-0' })}
|
|
295
|
+
ref={forwardedRef}
|
|
296
|
+
>
|
|
297
|
+
<h2 className='text-lg'>{label}</h2>
|
|
298
|
+
{description && <p className='text-description'>{description}</p>}
|
|
299
|
+
{children}
|
|
105
300
|
</div>
|
|
106
|
-
|
|
301
|
+
);
|
|
302
|
+
},
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
FormSection.displayName = FORM_SECTION_NAME;
|
|
306
|
+
|
|
307
|
+
//
|
|
308
|
+
// Submit
|
|
309
|
+
//
|
|
310
|
+
|
|
311
|
+
const FORM_SUBMIT_NAME = 'Form.Submit';
|
|
312
|
+
|
|
313
|
+
type FormSubmitProps = ThemedClassName<Partial<Pick<IconButtonProps, 'icon' | 'label' | 'disabled'>>>;
|
|
314
|
+
|
|
315
|
+
const FormSubmit = ({ classNames, label, icon, disabled }: FormSubmitProps) => {
|
|
316
|
+
const { t } = useTranslation(translationKey);
|
|
317
|
+
const {
|
|
318
|
+
form: { canSave, onSave },
|
|
319
|
+
readonly,
|
|
320
|
+
layout,
|
|
321
|
+
} = useFormContext(FORM_SUBMIT_NAME);
|
|
322
|
+
|
|
323
|
+
if (readonly || layout === 'static') {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return (
|
|
328
|
+
<div role='none' className={mx('flex w-full pt-form-padding', classNames)}>
|
|
329
|
+
<IconButton
|
|
330
|
+
classNames='w-full'
|
|
331
|
+
type='submit'
|
|
332
|
+
variant='primary'
|
|
333
|
+
disabled={disabled ?? !canSave}
|
|
334
|
+
icon={icon ?? 'ph--check--regular'}
|
|
335
|
+
label={label ?? t('save-button.label')}
|
|
336
|
+
onClick={onSave}
|
|
337
|
+
data-testid='save-button'
|
|
338
|
+
/>
|
|
339
|
+
</div>
|
|
107
340
|
);
|
|
108
341
|
};
|
|
342
|
+
|
|
343
|
+
FormSubmit.displayName = FORM_SUBMIT_NAME;
|
|
344
|
+
|
|
345
|
+
//
|
|
346
|
+
// Form
|
|
347
|
+
// https://www.radix-ui.com/primitives/docs/guides/composition
|
|
348
|
+
//
|
|
349
|
+
|
|
350
|
+
export const Form = {
|
|
351
|
+
Root: FormRoot,
|
|
352
|
+
Viewport: FormViewport,
|
|
353
|
+
Content: FormContent,
|
|
354
|
+
Section: FormSection,
|
|
355
|
+
FieldSet: FormFieldSet,
|
|
356
|
+
Label: FormFieldLabel,
|
|
357
|
+
Actions: FormActions,
|
|
358
|
+
Submit: FormSubmit,
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
export { useFormContext, useFormValues, useFormFieldState };
|
|
362
|
+
|
|
363
|
+
export type {
|
|
364
|
+
FormRootProps,
|
|
365
|
+
FormViewportProps,
|
|
366
|
+
FormContentProps,
|
|
367
|
+
FormSectionProps,
|
|
368
|
+
FormFieldSetProps,
|
|
369
|
+
FormFieldLabelProps,
|
|
370
|
+
FormActionsProps,
|
|
371
|
+
FormSubmitProps,
|
|
372
|
+
};
|