@schema-engine/forms-react 0.0.0-stage → 0.5.0
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 +21 -0
- package/README.md +87 -2
- package/dist/chunk-5XCNXEJB.js +19 -0
- package/dist/chunk-5XCNXEJB.js.map +7 -0
- package/dist/chunk-6RI4LMM6.js +19 -0
- package/dist/chunk-6RI4LMM6.js.map +7 -0
- package/dist/chunk-7QNWPTT7.js +20 -0
- package/dist/chunk-7QNWPTT7.js.map +7 -0
- package/dist/chunk-7RUFT6EJ.js +39 -0
- package/dist/chunk-7RUFT6EJ.js.map +7 -0
- package/dist/chunk-AALIXTCD.js +56 -0
- package/dist/chunk-AALIXTCD.js.map +7 -0
- package/dist/chunk-CPJMN2IH.js +128 -0
- package/dist/chunk-CPJMN2IH.js.map +7 -0
- package/dist/chunk-DTJ64NZQ.js +142 -0
- package/dist/chunk-DTJ64NZQ.js.map +7 -0
- package/dist/chunk-EYDB6NR6.js +9 -0
- package/dist/chunk-EYDB6NR6.js.map +7 -0
- package/dist/chunk-GH2QO3WC.js +11 -0
- package/dist/chunk-GH2QO3WC.js.map +7 -0
- package/dist/chunk-H25SUEJE.js +8 -0
- package/dist/chunk-H25SUEJE.js.map +7 -0
- package/dist/chunk-HT43MPKW.js +0 -0
- package/dist/chunk-HT43MPKW.js.map +7 -0
- package/dist/chunk-IHX6VMAQ.js +0 -0
- package/dist/chunk-IHX6VMAQ.js.map +7 -0
- package/dist/chunk-ISDPSORR.js +265 -0
- package/dist/chunk-ISDPSORR.js.map +7 -0
- package/dist/chunk-IXRV3YCF.js +36 -0
- package/dist/chunk-IXRV3YCF.js.map +7 -0
- package/dist/chunk-K3LLWGXU.js +30 -0
- package/dist/chunk-K3LLWGXU.js.map +7 -0
- package/dist/chunk-K66J2V73.js +108 -0
- package/dist/chunk-K66J2V73.js.map +7 -0
- package/dist/chunk-KG6Q7MNI.js +51 -0
- package/dist/chunk-KG6Q7MNI.js.map +7 -0
- package/dist/chunk-MBN4WIAC.js +41 -0
- package/dist/chunk-MBN4WIAC.js.map +7 -0
- package/dist/chunk-NDFV5AQW.js +39 -0
- package/dist/chunk-NDFV5AQW.js.map +7 -0
- package/dist/chunk-QUZN42PL.js +128 -0
- package/dist/chunk-QUZN42PL.js.map +7 -0
- package/dist/chunk-SED35QJC.js +78 -0
- package/dist/chunk-SED35QJC.js.map +7 -0
- package/dist/chunk-ULKJHK3P.js +6 -0
- package/dist/chunk-ULKJHK3P.js.map +7 -0
- package/dist/chunk-USRF3EEE.js +122 -0
- package/dist/chunk-USRF3EEE.js.map +7 -0
- package/dist/chunk-WFRWVH4X.js +44 -0
- package/dist/chunk-WFRWVH4X.js.map +7 -0
- package/dist/chunk-WOTW7A2I.js +7 -0
- package/dist/chunk-WOTW7A2I.js.map +7 -0
- package/dist/chunk-WRJWSEL7.js +98 -0
- package/dist/chunk-WRJWSEL7.js.map +7 -0
- package/dist/chunk-XWHP2AGG.js +7 -0
- package/dist/chunk-XWHP2AGG.js.map +7 -0
- package/dist/chunk-ZWEYALBG.js +25 -0
- package/dist/chunk-ZWEYALBG.js.map +7 -0
- package/dist/components/form-debugger/form-debugger.css +374 -0
- package/dist/components/form-debugger/form-debugger.css.map +7 -0
- package/dist/components/form-debugger/form-debugger.d.ts +23 -0
- package/dist/components/form-debugger/form-debugger.js +17 -0
- package/dist/components/form-debugger/form-debugger.js.map +7 -0
- package/dist/components/form-debugger/index.css +374 -0
- package/dist/components/form-debugger/index.css.map +7 -0
- package/dist/components/form-debugger/index.d.ts +2 -0
- package/dist/components/form-debugger/index.js +17 -0
- package/dist/components/form-debugger/index.js.map +7 -0
- package/dist/components/form-engine-provider.d.ts +7 -0
- package/dist/components/form-engine-provider.js +7 -0
- package/dist/components/form-engine-provider.js.map +7 -0
- package/dist/components/form-provider.d.ts +23 -0
- package/dist/components/form-provider.js +13 -0
- package/dist/components/form-provider.js.map +7 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +43 -0
- package/dist/components/index.js.map +7 -0
- package/dist/components/internals/element-error.d.ts +9 -0
- package/dist/components/internals/element-error.js +6 -0
- package/dist/components/internals/element-error.js.map +7 -0
- package/dist/components/internals/field-renderer.d.ts +26 -0
- package/dist/components/internals/field-renderer.js +24 -0
- package/dist/components/internals/field-renderer.js.map +7 -0
- package/dist/components/internals/form-elements.d.ts +20 -0
- package/dist/components/internals/form-elements.js +25 -0
- package/dist/components/internals/form-elements.js.map +7 -0
- package/dist/components/internals/form-renderer.d.ts +28 -0
- package/dist/components/internals/form-renderer.js +31 -0
- package/dist/components/internals/form-renderer.js.map +7 -0
- package/dist/components/internals/recipe-renderer.d.ts +10 -0
- package/dist/components/internals/recipe-renderer.js +17 -0
- package/dist/components/internals/recipe-renderer.js.map +7 -0
- package/dist/components/provider.d.ts +18 -0
- package/dist/components/provider.js +8 -0
- package/dist/components/provider.js.map +7 -0
- package/dist/contexts/field-constraint-context.d.ts +15 -0
- package/dist/contexts/field-constraint-context.js +6 -0
- package/dist/contexts/field-constraint-context.js.map +7 -0
- package/dist/contexts/index.d.ts +2 -0
- package/dist/contexts/index.js +6 -0
- package/dist/contexts/index.js.map +7 -0
- package/dist/contexts/view-context.d.ts +14 -0
- package/dist/contexts/view-context.js +16 -0
- package/dist/contexts/view-context.js.map +7 -0
- package/dist/global.css +8 -0
- package/dist/hooks/field-text.d.ts +6 -0
- package/dist/hooks/field-text.js +6 -0
- package/dist/hooks/field-text.js.map +7 -0
- package/dist/hooks/form-state/context.d.ts +27 -0
- package/dist/hooks/form-state/context.js +10 -0
- package/dist/hooks/form-state/context.js.map +7 -0
- package/dist/hooks/form-state/field-registry.d.ts +60 -0
- package/dist/hooks/form-state/field-registry.js +9 -0
- package/dist/hooks/form-state/field-registry.js.map +7 -0
- package/dist/hooks/form-state/index.d.ts +8 -0
- package/dist/hooks/form-state/index.js +27 -0
- package/dist/hooks/form-state/index.js.map +7 -0
- package/dist/hooks/form-state/native-control.d.ts +39 -0
- package/dist/hooks/form-state/native-control.js +17 -0
- package/dist/hooks/form-state/native-control.js.map +7 -0
- package/dist/hooks/form-state/types.d.ts +191 -0
- package/dist/hooks/form-state/types.js +0 -0
- package/dist/hooks/form-state/types.js.map +7 -0
- package/dist/hooks/form-state/use-field-array.d.ts +12 -0
- package/dist/hooks/form-state/use-field-array.js +7 -0
- package/dist/hooks/form-state/use-field-array.js.map +7 -0
- package/dist/hooks/form-state/use-form-context.d.ts +10 -0
- package/dist/hooks/form-state/use-form-context.js +7 -0
- package/dist/hooks/form-state/use-form-context.js.map +7 -0
- package/dist/hooks/form-state/use-form-field.d.ts +24 -0
- package/dist/hooks/form-state/use-form-field.js +11 -0
- package/dist/hooks/form-state/use-form-field.js.map +7 -0
- package/dist/hooks/form-state/use-watch.d.ts +12 -0
- package/dist/hooks/form-state/use-watch.js +7 -0
- package/dist/hooks/form-state/use-watch.js.map +7 -0
- package/dist/hooks/index.d.ts +7 -0
- package/dist/hooks/index.js +78 -0
- package/dist/hooks/index.js.map +7 -0
- package/dist/hooks/is-element-required.d.ts +5 -0
- package/dist/hooks/is-element-required.js +6 -0
- package/dist/hooks/is-element-required.js.map +7 -0
- package/dist/hooks/use-debounce.d.ts +14 -0
- package/dist/hooks/use-debounce.js +8 -0
- package/dist/hooks/use-debounce.js.map +7 -0
- package/dist/hooks/use-field.d.ts +44 -0
- package/dist/hooks/use-field.js +16 -0
- package/dist/hooks/use-field.js.map +7 -0
- package/dist/hooks/use-form-context.d.ts +63 -0
- package/dist/hooks/use-form-context.js +18 -0
- package/dist/hooks/use-form-context.js.map +7 -0
- package/dist/hooks/use-mobile.d.ts +9 -0
- package/dist/hooks/use-mobile.js +6 -0
- package/dist/hooks/use-mobile.js.map +7 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +7 -0
- package/dist/lib/logger.d.ts +2 -0
- package/dist/lib/object-identity.d.ts +6 -0
- package/dist/primitives.d.ts +28 -0
- package/dist/primitives.js +37 -0
- package/dist/primitives.js.map +7 -0
- package/package.json +139 -3
- package/skill/SKILL.md +79 -0
- package/skill/reference/forms-react-widgets.md +222 -0
- package/skill/reference/forms-react.md +92 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { NativeValidity } from '@schema-engine/forms/state-container';
|
|
2
|
+
/** The elements the browser validates and holds a value for. */
|
|
3
|
+
export type NativeControl = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
4
|
+
export declare function isNativeControl(element: HTMLElement): element is NativeControl;
|
|
5
|
+
/**
|
|
6
|
+
* What the control holds, as the container's value for it.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* `current` is the container's value for the path, which a choice that does not decide the whole value keeps:
|
|
10
|
+
* a checkbox with a `value` attribute adds or removes its own value in a list, and a radio that is not the
|
|
11
|
+
* checked one leaves the value as it is.
|
|
12
|
+
*/
|
|
13
|
+
export declare function readControl(control: NativeControl, current: unknown): unknown;
|
|
14
|
+
/**
|
|
15
|
+
* Whether reading the control would already give `value`.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* It compares what the control shows with what the container holds and never rewrites text the user is
|
|
19
|
+
* typing: a number input with `badInput` reports an empty value, so it matches an empty container value and
|
|
20
|
+
* keeps its text, and a control that echoes its own change matches too, so the cursor and an IME
|
|
21
|
+
* composition are left alone. A file input can only be cleared, so a value that is not empty matches.
|
|
22
|
+
*/
|
|
23
|
+
export declare function controlMatches(control: NativeControl, value: unknown): boolean;
|
|
24
|
+
/** Makes the control show `value`: the counterpart of {@link readControl}, written outside React. */
|
|
25
|
+
export declare function writeControl(control: NativeControl, value: unknown): void;
|
|
26
|
+
/** Whether two values read from a control are the same, an array by its items. */
|
|
27
|
+
export declare function sameValue(left: unknown, right: unknown): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* What the browser says about the control's value, as the container's native fact, or `null` when it is valid.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* The same fact `rui-form` of Radiant UI reports for the same control state, so a form validates alike in both
|
|
33
|
+
* renderers: the first flag set, in the order of {@link FLAGS}, is the `type`, `params` carry the constraint the flag names,
|
|
34
|
+
* and a control that is invalid with no flag set, or whose script called `setCustomValidity`, is a
|
|
35
|
+
* `customError` whose `message` is the script's text. A control the browser does not validate (disabled,
|
|
36
|
+
* read-only, hidden) reports valid. The browser's own message travels only as a fallback, since the form's
|
|
37
|
+
* formatter words the issue. Reads properties and never calls `checkValidity()`, which fires `invalid`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function readNativeFact(control: NativeControl): NativeValidity | null;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
controlMatches,
|
|
3
|
+
isNativeControl,
|
|
4
|
+
readControl,
|
|
5
|
+
readNativeFact,
|
|
6
|
+
sameValue,
|
|
7
|
+
writeControl
|
|
8
|
+
} from "../../chunk-CPJMN2IH.js";
|
|
9
|
+
import "../../chunk-H25SUEJE.js";
|
|
10
|
+
export {
|
|
11
|
+
controlMatches,
|
|
12
|
+
isNativeControl,
|
|
13
|
+
readControl,
|
|
14
|
+
readNativeFact,
|
|
15
|
+
sameValue,
|
|
16
|
+
writeControl
|
|
17
|
+
};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import type { EngineOutputMaps, DeepReadonly, ElementConfig, FormActionConfig, DefaultValuesOf, FormActionTrigger, FormSubmissionState as SubmissionState, FormSubmitHelpers, FormValues, NormalizedFormConfig, SubmissionActionResult } from '@schema-engine/forms';
|
|
2
|
+
import type { RequiredFlagSource } from '../is-element-required.js';
|
|
3
|
+
import type { FieldArrayValue, FormIssueFormatter, FormStateError, FormStateIssue, FormSubmissionIssue, FormValidateResult } from '@schema-engine/forms/state-container';
|
|
4
|
+
import type { ChangeEvent, CSSProperties, ReactNode, RefCallback } from 'react';
|
|
5
|
+
export type { FormSubmitHelpers, FormValues };
|
|
6
|
+
/**
|
|
7
|
+
* What `useFormContext` returns: the commands over the form and a lazily tracked `formState`.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* `formState` is read through getters, and a component re-renders only for the keys it reads: a submit
|
|
11
|
+
* button that reads `submission` does not re-render while the user types. `errors` is flat by dotted path
|
|
12
|
+
* (`contacts.0.email`). An error is its text, which the form's formatter makes or else the issue's own
|
|
13
|
+
* `message`, and it is absent when there is neither.
|
|
14
|
+
*/
|
|
15
|
+
export interface FormContextValue {
|
|
16
|
+
/** The values the form holds now. */
|
|
17
|
+
getValues: () => FormValues;
|
|
18
|
+
/** Writes the branch at `name` as a change. */
|
|
19
|
+
setValue: (name: string, value: unknown) => void;
|
|
20
|
+
/** Validates `name` (a path or several; the whole form when omitted) and says how that ended: `passed`, `invalid` or `overtaken` by an edit or a reset, which is no verdict. */
|
|
21
|
+
validate: (name?: string | readonly string[]) => Promise<FormValidateResult>;
|
|
22
|
+
/** Restores the defaults, or replaces them with `values` first, and clears the errors, marks and the submission state: a fresh form. */
|
|
23
|
+
reset: (values?: Record<string, unknown>) => void;
|
|
24
|
+
/** Focuses the control a field registered with the `ref` of `useFormField`. */
|
|
25
|
+
setFocus: (name: string) => void;
|
|
26
|
+
/**
|
|
27
|
+
* The text of an issue, an issue of a field or why a submission failed, as the form's formatter says.
|
|
28
|
+
*
|
|
29
|
+
* @remarks It answers with the issue's own `message` when there is no formatter or it says nothing, and with `undefined` when there is neither. A submission failure takes `FORM_ROOT` as its path.
|
|
30
|
+
*/
|
|
31
|
+
formatIssue: (issue: FormStateIssue | FormSubmissionIssue, field: {
|
|
32
|
+
readonly path: string;
|
|
33
|
+
}) => string | undefined;
|
|
34
|
+
/** Hides the outcome of the last submission: the status goes back to `idle` and its failures are dropped. */
|
|
35
|
+
clearSubmission: () => void;
|
|
36
|
+
formState: {
|
|
37
|
+
errors: Record<string, {
|
|
38
|
+
message?: string;
|
|
39
|
+
}>;
|
|
40
|
+
/** Whether no error from any source is recorded. Cheap, and no proof that the values are valid. */
|
|
41
|
+
isValid: boolean;
|
|
42
|
+
/** The one submission state: the status (`validating` while it is checked, `submitting` once it passed and its actions and `onSubmit` run), the failures as issues worded by `formatIssue`, and the submissions that ended, whether they passed or failed validation. */
|
|
43
|
+
submission: SubmissionState;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The state of the field at a path, as `useFormField` reads it.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* `error` and `warning` are texts, from the form's formatter or else the issue's own `message`. A field can be
|
|
51
|
+
* `invalid` with no `error` to show, when nothing says what is wrong in words, so a widget marks itself invalid
|
|
52
|
+
* from `invalid` and not from the text. `issue` is the engine's error, with its `type`, `ruleId`, `params` and
|
|
53
|
+
* every issue in `all`, for a widget that words it itself. It holds no value: `useWatch` reads values.
|
|
54
|
+
*/
|
|
55
|
+
export interface FormFieldStatus {
|
|
56
|
+
error?: string;
|
|
57
|
+
warning?: string;
|
|
58
|
+
issue: FormStateError | undefined;
|
|
59
|
+
/** Whether the field has a blocking error, whatever its text. */
|
|
60
|
+
invalid: boolean;
|
|
61
|
+
/** Whether the field, a field around it or a field below it has been blurred. */
|
|
62
|
+
isTouched: boolean;
|
|
63
|
+
/** Whether the field or a field below it differs from the defaults. */
|
|
64
|
+
isDirty: boolean;
|
|
65
|
+
/** Whether a check that asked for exactly this field is still running. */
|
|
66
|
+
isValidating: boolean;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The parts of an element config that `useFormField` reads.
|
|
70
|
+
*
|
|
71
|
+
* @remarks A registration passes its own config as it is: the hook asks for no more than this, so the config of any element type fits.
|
|
72
|
+
*/
|
|
73
|
+
export type FormFieldConfig = Pick<ElementConfig, 'name' | 'id' | 'disabled' | 'readonly' | 'htmlAttributes' | 'grid' | 'style' | 'constraints'> & RequiredFlagSource;
|
|
74
|
+
/** The props a field puts on its control, over the config's `htmlAttributes`. */
|
|
75
|
+
export interface FormControlProps {
|
|
76
|
+
id: string | undefined;
|
|
77
|
+
name: string;
|
|
78
|
+
required: boolean;
|
|
79
|
+
disabled: boolean;
|
|
80
|
+
readOnly: boolean | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Registers the control with the form: `setFocus` and a failed submit focus it, and when it is an input, select or textarea the form reports what the browser says about its value.
|
|
83
|
+
*
|
|
84
|
+
* @remarks A control that is not native can take it too, for focus alone.
|
|
85
|
+
*/
|
|
86
|
+
ref: RefCallback<HTMLElement | null>;
|
|
87
|
+
'aria-invalid': boolean;
|
|
88
|
+
'aria-describedby': string | undefined;
|
|
89
|
+
}
|
|
90
|
+
/** What an uncontrolled field puts on its native control: {@link FormControlProps} and the handlers that carry the user's changes into the form. */
|
|
91
|
+
export interface NativeControlProps extends FormControlProps {
|
|
92
|
+
onChange: (event: ChangeEvent<HTMLElement>) => void;
|
|
93
|
+
onBlur: () => void;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* What the parts of a field that come from its element config are, in either mode.
|
|
97
|
+
*
|
|
98
|
+
* @remarks `required` is the constraint-evaluated one, so a field that a constraint makes required is.
|
|
99
|
+
*/
|
|
100
|
+
interface FormFieldElement extends FormFieldStatus {
|
|
101
|
+
required: boolean;
|
|
102
|
+
disabled: boolean;
|
|
103
|
+
labelProps: {
|
|
104
|
+
htmlFor: string | undefined;
|
|
105
|
+
};
|
|
106
|
+
/** Props for the element that shows the error; `null` when there is no text to show. */
|
|
107
|
+
errorProps: {
|
|
108
|
+
id: string;
|
|
109
|
+
role: 'alert';
|
|
110
|
+
} | null;
|
|
111
|
+
containerStyles: CSSProperties;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* What `useFormField` binds a native control to: the field's status and what its config adds.
|
|
115
|
+
*
|
|
116
|
+
* @remarks
|
|
117
|
+
* The control keeps its own value and the field never re-renders for a keystroke. Spread `fieldProps` on
|
|
118
|
+
* the control and give it `defaultValue` (`defaultChecked` for a checkbox, `defaultChecked={value === option}`
|
|
119
|
+
* for a radio). `defaultValue` is frozen when the field mounts, and while the form hydrates it is the
|
|
120
|
+
* server's value, so the markup matches.
|
|
121
|
+
*/
|
|
122
|
+
export interface FormField extends FormFieldElement {
|
|
123
|
+
/** The value the control starts with, as the container held it when the field mounted. */
|
|
124
|
+
defaultValue: unknown;
|
|
125
|
+
/** Props for the control: its id, name, state attributes and the config's `htmlAttributes`, and the handlers that report its changes. */
|
|
126
|
+
fieldProps: NativeControlProps;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* What `useFormField` binds a component to when it takes only `value` and `onChange` props.
|
|
130
|
+
*
|
|
131
|
+
* @remarks
|
|
132
|
+
* The field re-renders whenever its value changes, since the component shows it. `onChange` writes a value
|
|
133
|
+
* and `onBlur` marks the field blurred. `fieldProps` has no change handler, and its `ref` still serves focus
|
|
134
|
+
* and, when it lands on a native control, the facts the browser reports.
|
|
135
|
+
*/
|
|
136
|
+
export interface ControlledFormField extends FormFieldElement {
|
|
137
|
+
value: unknown;
|
|
138
|
+
onChange: (value: unknown) => void;
|
|
139
|
+
onBlur: () => void;
|
|
140
|
+
fieldProps: FormControlProps;
|
|
141
|
+
}
|
|
142
|
+
/** How a field is bound: left out it is uncontrolled, and `controlled: true` is for a component that takes `value` and `onChange`. */
|
|
143
|
+
export interface FormFieldOptions {
|
|
144
|
+
controlled?: boolean;
|
|
145
|
+
}
|
|
146
|
+
export type { FieldArrayValue };
|
|
147
|
+
/**
|
|
148
|
+
* An action of a normalized form config, as the registrations of the engine type it: frozen, like the config.
|
|
149
|
+
*/
|
|
150
|
+
export type FormProviderAction<TOutputs extends EngineOutputMaps = EngineOutputMaps> = DeepReadonly<FormActionConfig<TOutputs['actions']>>;
|
|
151
|
+
/**
|
|
152
|
+
* Props for the FormProvider component
|
|
153
|
+
*
|
|
154
|
+
* @template TValues - The values the form submits: the type its config carries (`engine.defineForm` of a literal, or `engine.validateForm(json, { values })`), else {@link FormValues}. It types `onSubmit`, `onAction` and `defaultValues`, never `onChange`, whose values are live and may be excluded or unfinished.
|
|
155
|
+
*/
|
|
156
|
+
export interface FormProviderProps<TOutputs extends EngineOutputMaps = EngineOutputMaps, TValues extends FormValues = FormValues> {
|
|
157
|
+
/**
|
|
158
|
+
* The form, normalized by the engine (`engine.validateForm`).
|
|
159
|
+
*
|
|
160
|
+
* @remarks Its `validationMode`, `reValidateMode` and `payload` go to the form's container as they are.
|
|
161
|
+
*/
|
|
162
|
+
config: NormalizedFormConfig<TOutputs['elements'], TOutputs['recipes'], TOutputs['actions'], TValues>;
|
|
163
|
+
/** Optional default values for form fields */
|
|
164
|
+
defaultValues?: DefaultValuesOf<NoInfer<TValues>>;
|
|
165
|
+
/** React children to render inside the form context */
|
|
166
|
+
children: ReactNode;
|
|
167
|
+
/**
|
|
168
|
+
* Callback function executed on successful form submission, with the values that passed validation.
|
|
169
|
+
*
|
|
170
|
+
* @remarks `setServerErrors` shows the server's answer to this submission on the fields it names.
|
|
171
|
+
*/
|
|
172
|
+
onSubmit?: (data: TValues, helpers: FormSubmitHelpers) => void | Promise<void>;
|
|
173
|
+
/** Callback function executed whenever form data changes */
|
|
174
|
+
onChange?: (data: FormValues) => void;
|
|
175
|
+
/** Callback function executed when a form action is triggered */
|
|
176
|
+
onAction?: (action: FormProviderAction<TOutputs>, data: TValues) => void | Promise<void>;
|
|
177
|
+
/** Results of each action trigger, including submission failures. */
|
|
178
|
+
onActionResults?: (results: readonly SubmissionActionResult[], trigger: FormActionTrigger) => void | Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Takes the page to the `redirectUrl` a submit action asked for, once the whole submission succeeded.
|
|
181
|
+
*
|
|
182
|
+
* @remarks Defaults to `location.assign` in a browser and does nothing on a server. Pass your router's navigate to keep the visit client-side.
|
|
183
|
+
*/
|
|
184
|
+
navigate?: (url: string) => void;
|
|
185
|
+
/**
|
|
186
|
+
* Turns an issue into the text a field or a submission failure shows, for this form.
|
|
187
|
+
*
|
|
188
|
+
* @remarks Overrides the engine's `formatIssue` (`createFormEngine({ formatIssue })`). It is the only per-form override.
|
|
189
|
+
*/
|
|
190
|
+
formatIssue?: FormIssueFormatter;
|
|
191
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type FieldArrayValue } from '@schema-engine/forms/state-container';
|
|
2
|
+
/**
|
|
3
|
+
* The rows of the array at `name` and the commands that change them.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Rows are the container's, so a row keeps its id, errors and touched state through every operation (dirty is
|
|
7
|
+
* the comparison with the defaults, index by index), and a falsy row (`''`, `0`, `false`) is a row. An operation
|
|
8
|
+
* that adds rows (`append`, `prepend`, `insert`) focuses the first control of the first row it added, once that
|
|
9
|
+
* row has rendered; an operation that changes no row focuses nothing. The same object comes back until the rows
|
|
10
|
+
* change.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useFieldArray(name: string): FieldArrayValue;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FormContextValue } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* The commands over the form and its state, read lazily.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* `formState` re-renders a component only for the keys it reads: one that reads `submission` does not
|
|
7
|
+
* render while the user types, and reading `isValid` costs nothing, since it reports the errors recorded
|
|
8
|
+
* and never validates. The returned object keeps its identity while the form's formatter does.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useFormContext(): FormContextValue;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ControlledFormField, FormField, FormFieldConfig, FormFieldStatus } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Binds a widget to a field of the form.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Given the config of an element it is everything a registry widget needs: the field's state (`error`,
|
|
7
|
+
* `warning`, `issue`, `invalid` and the flags), its `required` evaluated against the element's constraints,
|
|
8
|
+
* and the props of its control, label and error (`fieldProps`, `labelProps`, `errorProps`, `containerStyles`).
|
|
9
|
+
* Given a path (a field, an array, or `FORM_ROOT` for the form itself) it is the state and formatted texts
|
|
10
|
+
* of that path alone; `useWatch` reads values.
|
|
11
|
+
*
|
|
12
|
+
* By default the control keeps its own value (`defaultValue`, `fieldProps.onChange`) and the field
|
|
13
|
+
* re-renders only when its error, warning or flags change, never for a keystroke. A component that takes only
|
|
14
|
+
* `value` and `onChange` props passes `{ controlled: true }` and re-renders with its value. In both modes
|
|
15
|
+
* `fieldProps.ref` also reports what the browser says about a native control (`validity`) as the native
|
|
16
|
+
* facts of the container, and focuses it for `setFocus` and a failed submit.
|
|
17
|
+
*/
|
|
18
|
+
export declare function useFormField(path: string): FormFieldStatus;
|
|
19
|
+
export declare function useFormField(config: FormFieldConfig, options: {
|
|
20
|
+
controlled: true;
|
|
21
|
+
}): ControlledFormField;
|
|
22
|
+
export declare function useFormField(config: FormFieldConfig, options?: {
|
|
23
|
+
controlled?: false;
|
|
24
|
+
}): FormField;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useFormField
|
|
3
|
+
} from "../../chunk-DTJ64NZQ.js";
|
|
4
|
+
import "../../chunk-EYDB6NR6.js";
|
|
5
|
+
import "../../chunk-XWHP2AGG.js";
|
|
6
|
+
import "../../chunk-AALIXTCD.js";
|
|
7
|
+
import "../../chunk-ZWEYALBG.js";
|
|
8
|
+
import "../../chunk-GH2QO3WC.js";
|
|
9
|
+
export {
|
|
10
|
+
useFormField
|
|
11
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FormValues } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Watches the form's values and re-renders when what it watches changes.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* With no argument it is the whole value tree, the same object while no value changed. With a path it is the
|
|
7
|
+
* value there, `unknown` as the container holds it, which the caller narrows. With a list it is the values at
|
|
8
|
+
* those paths, in the order given, the same array while none changed.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useWatch(): FormValues;
|
|
11
|
+
export declare function useWatch(path: string): unknown;
|
|
12
|
+
export declare function useWatch(paths: readonly string[]): readonly unknown[];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './use-field.js';
|
|
2
|
+
export * from './field-text.js';
|
|
3
|
+
export * from './use-form-context.js';
|
|
4
|
+
export * from './use-debounce.js';
|
|
5
|
+
export * from './use-mobile.js';
|
|
6
|
+
export { useViewContext, useOptionalViewContext, useActiveStep, useFormNavigation, useViewConfig, } from '../contexts/view-context.js';
|
|
7
|
+
export * from './form-state';
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import "../chunk-HT43MPKW.js";
|
|
2
|
+
import {
|
|
3
|
+
useDebounce,
|
|
4
|
+
useDebouncedValue
|
|
5
|
+
} from "../chunk-7RUFT6EJ.js";
|
|
6
|
+
import {
|
|
7
|
+
combineHandlers,
|
|
8
|
+
useFieldAppearanceAttributes,
|
|
9
|
+
useFieldClassName,
|
|
10
|
+
useFieldHost
|
|
11
|
+
} from "../chunk-WFRWVH4X.js";
|
|
12
|
+
import {
|
|
13
|
+
useIsMobile
|
|
14
|
+
} from "../chunk-7QNWPTT7.js";
|
|
15
|
+
import "../chunk-WOTW7A2I.js";
|
|
16
|
+
import {
|
|
17
|
+
FORM_ROOT
|
|
18
|
+
} from "../chunk-ULKJHK3P.js";
|
|
19
|
+
import {
|
|
20
|
+
useWatch
|
|
21
|
+
} from "../chunk-NDFV5AQW.js";
|
|
22
|
+
import {
|
|
23
|
+
useFieldArray
|
|
24
|
+
} from "../chunk-MBN4WIAC.js";
|
|
25
|
+
import {
|
|
26
|
+
useFormContext
|
|
27
|
+
} from "../chunk-K66J2V73.js";
|
|
28
|
+
import {
|
|
29
|
+
useFormField
|
|
30
|
+
} from "../chunk-DTJ64NZQ.js";
|
|
31
|
+
import "../chunk-EYDB6NR6.js";
|
|
32
|
+
import {
|
|
33
|
+
useActiveStep,
|
|
34
|
+
useFormNavigation,
|
|
35
|
+
useOptionalViewContext,
|
|
36
|
+
useViewConfig,
|
|
37
|
+
useViewContext
|
|
38
|
+
} from "../chunk-IXRV3YCF.js";
|
|
39
|
+
import {
|
|
40
|
+
fieldText
|
|
41
|
+
} from "../chunk-H25SUEJE.js";
|
|
42
|
+
import "../chunk-XWHP2AGG.js";
|
|
43
|
+
import {
|
|
44
|
+
useChildGridProps,
|
|
45
|
+
useFormEngine,
|
|
46
|
+
useFormInstanceActions,
|
|
47
|
+
useFormRuntime,
|
|
48
|
+
useLayoutThemeId,
|
|
49
|
+
useOptionalFormEngine
|
|
50
|
+
} from "../chunk-AALIXTCD.js";
|
|
51
|
+
import "../chunk-ZWEYALBG.js";
|
|
52
|
+
import "../chunk-GH2QO3WC.js";
|
|
53
|
+
export {
|
|
54
|
+
FORM_ROOT,
|
|
55
|
+
combineHandlers,
|
|
56
|
+
fieldText,
|
|
57
|
+
useActiveStep,
|
|
58
|
+
useChildGridProps,
|
|
59
|
+
useDebounce,
|
|
60
|
+
useDebouncedValue,
|
|
61
|
+
useFieldAppearanceAttributes,
|
|
62
|
+
useFieldArray,
|
|
63
|
+
useFieldClassName,
|
|
64
|
+
useFieldHost,
|
|
65
|
+
useFormContext,
|
|
66
|
+
useFormEngine,
|
|
67
|
+
useFormField,
|
|
68
|
+
useFormInstanceActions,
|
|
69
|
+
useFormNavigation,
|
|
70
|
+
useFormRuntime,
|
|
71
|
+
useIsMobile,
|
|
72
|
+
useLayoutThemeId,
|
|
73
|
+
useOptionalFormEngine,
|
|
74
|
+
useOptionalViewContext,
|
|
75
|
+
useViewConfig,
|
|
76
|
+
useViewContext,
|
|
77
|
+
useWatch
|
|
78
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ValidationRegistry, getValidationRulesForElement } from '@schema-engine/forms';
|
|
2
|
+
/** The parts of an element its required flag is derived from. */
|
|
3
|
+
export type RequiredFlagSource = Parameters<typeof getValidationRulesForElement>[0];
|
|
4
|
+
/** Base required flag before constraints: authored rules plus the type's default rules. */
|
|
5
|
+
export declare function isElementRequired(element: RequiredFlagSource, registry: ValidationRegistry): boolean;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hook that debounces function calls
|
|
3
|
+
* @param callback - The function to debounce
|
|
4
|
+
* @param delay - The delay in milliseconds
|
|
5
|
+
* @returns A debounced version of the callback
|
|
6
|
+
*/
|
|
7
|
+
export declare function useDebounce<T extends (...args: any[]) => void>(callback: T, delay: number): (...args: Parameters<T>) => void;
|
|
8
|
+
/**
|
|
9
|
+
* A hook that debounces a value
|
|
10
|
+
* @param value - The value to debounce
|
|
11
|
+
* @param delay - The delay in milliseconds
|
|
12
|
+
* @returns The debounced value
|
|
13
|
+
*/
|
|
14
|
+
export declare function useDebouncedValue<T>(value: T, delay: number): T;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import { type ElementGridConfig } from '@schema-engine/layout';
|
|
3
|
+
export interface FieldHostProps {
|
|
4
|
+
className: string;
|
|
5
|
+
'data-se-theme': string;
|
|
6
|
+
style: React.CSSProperties;
|
|
7
|
+
[appearanceAttribute: `data-se-${string}`]: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* `data-se-<axis>` attributes for the element `FormElement` is rendering.
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* Spread onto the field's host element. `FieldGroupRoot` and {@link useFieldHost} already do; call it
|
|
14
|
+
* for a host built by hand. Empty outside a `FormElement` or for an element with no appearance.
|
|
15
|
+
*/
|
|
16
|
+
export declare function useFieldAppearanceAttributes(): Record<string, string>;
|
|
17
|
+
/**
|
|
18
|
+
* The authored `className` of the element `FormElement` is rendering, for its field host.
|
|
19
|
+
*
|
|
20
|
+
* @remarks `FieldGroupRoot` and {@link useFieldHost} already apply it. The host is the element that
|
|
21
|
+
* carries the field's `style` and `data-se-*` attributes, in both renderers.
|
|
22
|
+
*/
|
|
23
|
+
export declare function useFieldClassName(): string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Host attributes that put an element on the form grid.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* Any element can sit on the grid, not only inputs. `useFormField`
|
|
29
|
+
* returns the grid variables as `containerStyles` for fields bound to a config; a content or decoration element that
|
|
30
|
+
* renders its own container calls this directly and spreads the result:
|
|
31
|
+
*
|
|
32
|
+
* ```tsx
|
|
33
|
+
* <div {...useFieldHost(config.grid, config.style)}>…</div>
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* Without it an element ignores its `grid` config entirely — placement and
|
|
37
|
+
* `marginBlockStart`/`marginBlockEnd` alike — because the generated stylesheet
|
|
38
|
+
* matches on `.se-grid-item` scoped by the owning engine's theme id.
|
|
39
|
+
*/
|
|
40
|
+
export declare function useFieldHost(grid?: ElementGridConfig, style?: React.CSSProperties): FieldHostProps;
|
|
41
|
+
/**
|
|
42
|
+
* Combines handlers from props with controller handlers
|
|
43
|
+
*/
|
|
44
|
+
export declare function combineHandlers<T>(controllerHandler: (value: T) => void, propsHandler?: (value: T) => void): (value: T) => void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
combineHandlers,
|
|
3
|
+
useFieldAppearanceAttributes,
|
|
4
|
+
useFieldClassName,
|
|
5
|
+
useFieldHost
|
|
6
|
+
} from "../chunk-WFRWVH4X.js";
|
|
7
|
+
import "../chunk-WOTW7A2I.js";
|
|
8
|
+
import "../chunk-AALIXTCD.js";
|
|
9
|
+
import "../chunk-ZWEYALBG.js";
|
|
10
|
+
import "../chunk-GH2QO3WC.js";
|
|
11
|
+
export {
|
|
12
|
+
combineHandlers,
|
|
13
|
+
useFieldAppearanceAttributes,
|
|
14
|
+
useFieldClassName,
|
|
15
|
+
useFieldHost
|
|
16
|
+
};
|