@dxos/react-ui-form 0.8.4-main.b97322e → 0.8.4-main.bc674ce
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 +2220 -2058
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +2220 -2058
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/ControlSection/ControlSection.d.ts +4 -4
- package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +1 -1
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +84 -5
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +6 -5
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +82 -5
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form-old.stories.d.ts +131 -0
- package/dist/types/src/components/Form/Form-old.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/Form.d.ts +81 -25
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +127 -55
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormField.d.ts +37 -0
- package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts +78 -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 +19 -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 +14 -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 +82 -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/ObjectPicker/ObjectPicker.d.ts +46 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +85 -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/ViewEditor/ViewEditor.d.ts +21 -9
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +83 -8
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +1 -1
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +4 -3
- 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 +6 -0
- package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +19 -10
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/util/echo.d.ts +6 -0
- package/dist/types/src/util/echo.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 +2 -3
- package/dist/types/src/util/refs.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +51 -40
- package/src/components/ControlSection/ControlSection.stories.tsx +11 -14
- package/src/components/ControlSection/ControlSection.tsx +30 -16
- package/src/components/FieldEditor/FieldEditor.stories.tsx +52 -31
- package/src/components/FieldEditor/FieldEditor.test.tsx +3 -2
- package/src/components/FieldEditor/FieldEditor.tsx +86 -81
- package/src/components/Form/Form-old.stories.tsx +314 -0
- package/src/components/Form/Form.stories.tsx +155 -219
- package/src/components/Form/Form.tsx +320 -96
- package/src/components/Form/FormField.tsx +286 -0
- package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
- package/src/components/Form/FormFieldComponent.tsx +174 -0
- package/src/components/Form/FormFieldSet.tsx +125 -0
- package/src/components/Form/README.md +3 -0
- package/src/components/Form/fields/ArrayField.tsx +151 -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 +181 -0
- package/src/components/Form/fields/SelectField.tsx +65 -0
- package/src/components/Form/{custom/SelectOptionsInput.stories.tsx → fields/SelectOptionField.stories.tsx} +25 -22
- package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +47 -42
- 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/ObjectPicker/ObjectPicker.stories.tsx +214 -0
- package/src/components/ObjectPicker/ObjectPicker.tsx +175 -0
- package/src/components/ObjectPicker/index.ts +5 -0
- package/src/components/ViewEditor/ViewEditor.stories.tsx +137 -61
- package/src/components/ViewEditor/ViewEditor.test.tsx +19 -17
- package/src/components/ViewEditor/ViewEditor.tsx +324 -203
- package/src/components/index.ts +1 -2
- package/src/components/testing.tsx +19 -11
- package/src/hooks/index.ts +2 -3
- package/src/hooks/useFormHandler.ts +362 -0
- package/src/hooks/useKeyHandler.tsx +37 -0
- package/src/translations.ts +21 -12
- package/src/util/echo.ts +25 -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 +12 -18
- 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/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/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,332 @@
|
|
|
1
1
|
//
|
|
2
|
-
// Copyright
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
import { type
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
import {
|
|
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 type * 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 { IconButton, type IconButtonProps, ScrollArea, type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
13
|
+
import { mx } from '@dxos/ui-theme';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
type FormHandler,
|
|
17
|
+
type FormHandlerProps,
|
|
18
|
+
type FormUpdateMeta,
|
|
19
|
+
useFormHandler,
|
|
20
|
+
useKeyHandler,
|
|
21
|
+
} from '../../hooks';
|
|
22
|
+
import { translationKey } from '../../translations';
|
|
23
|
+
import { setValueEchoAware } from '../../util';
|
|
24
|
+
|
|
25
|
+
import { FormFieldLabel, type FormFieldLabelProps, type FormFieldStateProps } from './FormFieldComponent';
|
|
26
|
+
import {
|
|
27
|
+
FormFieldSet as NaturalFormFieldSet,
|
|
28
|
+
type FormFieldSetProps as NaturalFormFieldSetProps,
|
|
29
|
+
} from './FormFieldSet';
|
|
30
|
+
|
|
31
|
+
// New features/polish
|
|
32
|
+
// [x] Unify readonly/inline modes
|
|
33
|
+
// [ ] Don't call save/autoSave if value hasn't changed
|
|
34
|
+
// [ ] Fix onCancel (restore values)
|
|
35
|
+
// [ ] Fix useSchema Type.Obj.Any cast
|
|
36
|
+
// [ ] Remove @dxos/echo-db deps
|
|
37
|
+
// [ ] TableCellEditor (handleEnter/ModalController).
|
|
38
|
+
// [ ] Use FormFieldWrapper uniformly
|
|
39
|
+
// [ ] Inline tables for object arrays
|
|
40
|
+
// [ ] Defer query until popover
|
|
41
|
+
// [ ] Omit id from sub properties.
|
|
42
|
+
// [x] Refs
|
|
43
|
+
// [x] Single-select (fix popover)
|
|
44
|
+
// [x] Multi-select (array)
|
|
45
|
+
// [ ] auto save doesn't work for combobox + select due to only firing on blur (workaround is to use onValuesChanged)
|
|
46
|
+
|
|
47
|
+
// TODO(burdon): Move to @dxos/schema (re-export here).
|
|
48
|
+
export type ExcludeId<S extends Schema.Schema.AnyNoContext> = Omit<Schema.Schema.Type<S>, 'id'>;
|
|
49
|
+
|
|
50
|
+
// TODO(burdon): Move to @dxos/schema (re-export here).
|
|
51
|
+
export const omitId = <S extends Schema.Schema.AnyNoContext>(schema: S): Schema.Schema<ExcludeId<S>, ExcludeId<S>> =>
|
|
52
|
+
schema.pipe(Schema.omit('id')) as any;
|
|
53
|
+
|
|
54
|
+
//
|
|
55
|
+
// Context
|
|
56
|
+
//
|
|
57
|
+
|
|
58
|
+
type FormContextValue<T extends AnyProperties = any> = {
|
|
59
|
+
/**
|
|
60
|
+
* Form handler.
|
|
61
|
+
*/
|
|
62
|
+
form: FormHandler<T>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Show debug info.
|
|
66
|
+
*/
|
|
67
|
+
debug?: boolean;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Testing.
|
|
71
|
+
*/
|
|
72
|
+
testId?: string;
|
|
73
|
+
} & Pick<NaturalFormFieldSetProps<T>, 'readonly' | 'layout' | 'fieldMap' | 'fieldProvider'>;
|
|
74
|
+
|
|
75
|
+
const [FormContextProvider, useFormContext] = createContext<FormContextValue>('Form');
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Get the current form values.
|
|
79
|
+
*/
|
|
80
|
+
const useFormValues: {
|
|
81
|
+
<T extends AnyProperties>(componentName: string, path?: (string | number)[]): T;
|
|
82
|
+
<T extends AnyProperties>(
|
|
83
|
+
componentName: string,
|
|
84
|
+
path: (string | number)[] | undefined,
|
|
85
|
+
defaultValue: () => T,
|
|
86
|
+
): T | undefined;
|
|
87
|
+
} = (componentName: string, path: (string | number)[] = [], defaultValue?: () => any) => {
|
|
88
|
+
const jsonPath = createJsonPath(path);
|
|
89
|
+
const {
|
|
90
|
+
form: { values },
|
|
91
|
+
} = useFormContext(componentName);
|
|
92
|
+
|
|
93
|
+
const value = getValue$(values, jsonPath);
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (!value && defaultValue) {
|
|
97
|
+
setValueEchoAware(values, jsonPath, defaultValue());
|
|
98
|
+
}
|
|
99
|
+
}, [value, defaultValue]);
|
|
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
|
+
type FormViewportProps = ThemedClassName<PropsWithChildren<{}>>;
|
|
168
|
+
|
|
169
|
+
const FormViewport = ({ classNames, children }: FormViewportProps) => {
|
|
170
|
+
return (
|
|
171
|
+
<ScrollArea.Root>
|
|
172
|
+
<ScrollArea.Viewport classNames={['plb-cardSpacingBlock', classNames]}>{children}</ScrollArea.Viewport>
|
|
173
|
+
<ScrollArea.Scrollbar orientation='vertical'>
|
|
174
|
+
<ScrollArea.Thumb />
|
|
175
|
+
</ScrollArea.Scrollbar>
|
|
176
|
+
</ScrollArea.Root>
|
|
177
|
+
);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
FormViewport.displayName = 'Form.Viewport';
|
|
181
|
+
|
|
182
|
+
//
|
|
183
|
+
// Content
|
|
184
|
+
//
|
|
185
|
+
|
|
186
|
+
type FormContentProps = ThemedClassName<PropsWithChildren<{}>>;
|
|
187
|
+
|
|
188
|
+
// TOOD(burdon): Figure out nesting (indent and testId).
|
|
189
|
+
const FormContent = ({ classNames, children }: FormContentProps) => {
|
|
190
|
+
const { form, testId } = useFormContext(FormContent.displayName);
|
|
191
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
192
|
+
useKeyHandler(ref.current, form);
|
|
72
193
|
|
|
73
194
|
return (
|
|
74
|
-
<
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
onValuesChanged={onValuesChanged}
|
|
80
|
-
onValidate={onValidate}
|
|
81
|
-
onValid={handleValid}
|
|
82
|
-
onSave={onSave}
|
|
195
|
+
<div
|
|
196
|
+
ref={ref}
|
|
197
|
+
role='form'
|
|
198
|
+
className={mx('flex flex-col is-full pli-cardSpacingInline density-fine', classNames)}
|
|
199
|
+
data-testid={testId}
|
|
83
200
|
>
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
201
|
+
{children}
|
|
202
|
+
</div>
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
FormContent.displayName = 'Form.Content';
|
|
207
|
+
|
|
208
|
+
//
|
|
209
|
+
// FieldSet
|
|
210
|
+
//
|
|
211
|
+
|
|
212
|
+
type FormFieldSetProps = ThemedClassName<{}>;
|
|
213
|
+
|
|
214
|
+
const FormFieldSet = ({ classNames }: FormFieldSetProps) => {
|
|
215
|
+
const { form, ...props } = useFormContext(FormFieldSet.displayName);
|
|
216
|
+
|
|
217
|
+
return <NaturalFormFieldSet classNames={classNames} schema={form.schema} {...props} />;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
FormFieldSet.displayName = 'Form.FieldSet';
|
|
221
|
+
|
|
222
|
+
//
|
|
223
|
+
// Actions
|
|
224
|
+
//
|
|
225
|
+
|
|
226
|
+
type FormActionsProps = ThemedClassName<{}>;
|
|
227
|
+
|
|
228
|
+
const FormActions = ({ classNames }: FormActionsProps) => {
|
|
229
|
+
const { t } = useTranslation(translationKey);
|
|
230
|
+
const {
|
|
231
|
+
form: { canSave, onSave, onCancel },
|
|
232
|
+
readonly,
|
|
233
|
+
layout,
|
|
234
|
+
} = useFormContext(FormActions.displayName);
|
|
235
|
+
|
|
236
|
+
if (readonly || layout === 'static') {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// TODO(burdon): Currently onCancel is a no-op; implement "revert values".
|
|
241
|
+
// Deprecate FormSubmit ans use FormActions without Cancel button if no callback is supplied.
|
|
242
|
+
|
|
243
|
+
return (
|
|
244
|
+
<div role='none' className={mx('grid grid-flow-col auto-cols-fr gap-2 pbs-cardSpacingBlock', classNames)}>
|
|
245
|
+
{onCancel && (
|
|
246
|
+
<IconButton
|
|
247
|
+
icon='ph--x--regular'
|
|
248
|
+
iconEnd
|
|
249
|
+
label={t('cancel button label')}
|
|
250
|
+
onClick={onCancel}
|
|
251
|
+
data-testid='cancel-button'
|
|
252
|
+
/>
|
|
253
|
+
)}
|
|
254
|
+
{onSave && (
|
|
255
|
+
<IconButton
|
|
256
|
+
type='submit'
|
|
257
|
+
variant='primary'
|
|
258
|
+
disabled={!canSave}
|
|
259
|
+
icon='ph--check--regular'
|
|
260
|
+
iconEnd
|
|
261
|
+
label={t('save button label')}
|
|
262
|
+
onClick={onSave}
|
|
263
|
+
data-testid='save-button'
|
|
101
264
|
/>
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
)}
|
|
105
|
-
</div>
|
|
106
|
-
</FormProvider>
|
|
265
|
+
)}
|
|
266
|
+
</div>
|
|
107
267
|
);
|
|
108
268
|
};
|
|
269
|
+
|
|
270
|
+
FormActions.displayName = 'Form.Actions';
|
|
271
|
+
|
|
272
|
+
//
|
|
273
|
+
// Submit
|
|
274
|
+
//
|
|
275
|
+
|
|
276
|
+
type FormSubmitProps = ThemedClassName<Partial<Pick<IconButtonProps, 'icon' | 'label'>>>;
|
|
277
|
+
|
|
278
|
+
const FormSubmit = ({ classNames, label, icon }: FormSubmitProps) => {
|
|
279
|
+
const { t } = useTranslation(translationKey);
|
|
280
|
+
const {
|
|
281
|
+
form: { canSave, onSave },
|
|
282
|
+
readonly,
|
|
283
|
+
layout,
|
|
284
|
+
} = useFormContext(FormSubmit.displayName);
|
|
285
|
+
|
|
286
|
+
if (readonly || layout === 'static') {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return (
|
|
291
|
+
<div role='none' className={mx('flex is-full pbs-cardSpacingBlock', classNames)}>
|
|
292
|
+
<IconButton
|
|
293
|
+
classNames='is-full'
|
|
294
|
+
type='submit'
|
|
295
|
+
variant='primary'
|
|
296
|
+
disabled={!canSave}
|
|
297
|
+
icon={icon ?? 'ph--check--regular'}
|
|
298
|
+
label={label ?? t('save button label')}
|
|
299
|
+
onClick={onSave}
|
|
300
|
+
data-testid='save-button'
|
|
301
|
+
/>
|
|
302
|
+
</div>
|
|
303
|
+
);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
FormSubmit.displayName = 'Form.Submit';
|
|
307
|
+
|
|
308
|
+
//
|
|
309
|
+
// Form
|
|
310
|
+
// https://www.radix-ui.com/primitives/docs/guides/composition
|
|
311
|
+
//
|
|
312
|
+
|
|
313
|
+
export const Form = {
|
|
314
|
+
Root: FormRoot,
|
|
315
|
+
Viewport: FormViewport,
|
|
316
|
+
Content: FormContent,
|
|
317
|
+
FieldSet: FormFieldSet,
|
|
318
|
+
Actions: FormActions,
|
|
319
|
+
Submit: FormSubmit,
|
|
320
|
+
Label: FormFieldLabel,
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
export { useFormContext, useFormValues, useFormFieldState };
|
|
324
|
+
|
|
325
|
+
export type {
|
|
326
|
+
FormRootProps,
|
|
327
|
+
FormViewportProps,
|
|
328
|
+
FormContentProps,
|
|
329
|
+
FormFieldSetProps,
|
|
330
|
+
FormActionsProps,
|
|
331
|
+
FormFieldLabelProps as LabelProps,
|
|
332
|
+
};
|