@molecule/app-angular 3.3.1 → 3.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/README.md +3880 -1264
- package/dist/hooks/external-store.d.ts +32 -0
- package/dist/hooks/external-store.d.ts.map +1 -0
- package/dist/hooks/external-store.js +62 -0
- package/dist/hooks/external-store.js.map +1 -0
- package/dist/hooks/index.d.ts +24 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +24 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/use-ai-models.d.ts +46 -0
- package/dist/hooks/use-ai-models.d.ts.map +1 -0
- package/dist/hooks/use-ai-models.js +142 -0
- package/dist/hooks/use-ai-models.js.map +1 -0
- package/dist/hooks/use-async.d.ts +125 -0
- package/dist/hooks/use-async.d.ts.map +1 -0
- package/dist/hooks/use-async.js +189 -0
- package/dist/hooks/use-async.js.map +1 -0
- package/dist/hooks/use-auth-flows.d.ts +207 -0
- package/dist/hooks/use-auth-flows.d.ts.map +1 -0
- package/dist/hooks/use-auth-flows.js +460 -0
- package/dist/hooks/use-auth-flows.js.map +1 -0
- package/dist/hooks/use-auth.d.ts +80 -0
- package/dist/hooks/use-auth.d.ts.map +1 -0
- package/dist/hooks/use-auth.js +89 -0
- package/dist/hooks/use-auth.js.map +1 -0
- package/dist/hooks/use-forms.d.ts +177 -0
- package/dist/hooks/use-forms.d.ts.map +1 -0
- package/dist/hooks/use-forms.js +185 -0
- package/dist/hooks/use-forms.js.map +1 -0
- package/dist/hooks/use-http.d.ts +155 -0
- package/dist/hooks/use-http.d.ts.map +1 -0
- package/dist/hooks/use-http.js +212 -0
- package/dist/hooks/use-http.js.map +1 -0
- package/dist/hooks/use-ide.d.ts +208 -0
- package/dist/hooks/use-ide.d.ts.map +1 -0
- package/dist/hooks/use-ide.js +217 -0
- package/dist/hooks/use-ide.js.map +1 -0
- package/dist/hooks/use-logger.d.ts +40 -0
- package/dist/hooks/use-logger.d.ts.map +1 -0
- package/dist/hooks/use-logger.js +56 -0
- package/dist/hooks/use-logger.js.map +1 -0
- package/dist/hooks/use-native.d.ts +213 -0
- package/dist/hooks/use-native.d.ts.map +1 -0
- package/dist/hooks/use-native.js +158 -0
- package/dist/hooks/use-native.js.map +1 -0
- package/dist/hooks/use-profile-form.d.ts +113 -0
- package/dist/hooks/use-profile-form.d.ts.map +1 -0
- package/dist/hooks/use-profile-form.js +253 -0
- package/dist/hooks/use-profile-form.js.map +1 -0
- package/dist/hooks/use-router.d.ts +212 -0
- package/dist/hooks/use-router.d.ts.map +1 -0
- package/dist/hooks/use-router.js +251 -0
- package/dist/hooks/use-router.js.map +1 -0
- package/dist/hooks/use-storage.d.ts +128 -0
- package/dist/hooks/use-storage.d.ts.map +1 -0
- package/dist/hooks/use-storage.js +217 -0
- package/dist/hooks/use-storage.js.map +1 -0
- package/dist/hooks/use-store.d.ts +74 -0
- package/dist/hooks/use-store.d.ts.map +1 -0
- package/dist/hooks/use-store.js +103 -0
- package/dist/hooks/use-store.js.map +1 -0
- package/dist/hooks/use-theme.d.ts +59 -0
- package/dist/hooks/use-theme.d.ts.map +1 -0
- package/dist/hooks/use-theme.js +117 -0
- package/dist/hooks/use-theme.js.map +1 -0
- package/dist/hooks/use-translation.d.ts +86 -0
- package/dist/hooks/use-translation.d.ts.map +1 -0
- package/dist/hooks/use-translation.js +146 -0
- package/dist/hooks/use-translation.js.map +1 -0
- package/dist/hooks/use-verify-payment-return.d.ts +86 -0
- package/dist/hooks/use-verify-payment-return.d.ts.map +1 -0
- package/dist/hooks/use-verify-payment-return.js +167 -0
- package/dist/hooks/use-verify-payment-return.js.map +1 -0
- package/dist/hooks/use-version.d.ts +49 -0
- package/dist/hooks/use-version.d.ts.map +1 -0
- package/dist/hooks/use-version.js +70 -0
- package/dist/hooks/use-version.js.map +1 -0
- package/dist/i18n-reactive.d.ts +2 -5
- package/dist/i18n-reactive.d.ts.map +1 -1
- package/dist/i18n-reactive.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/inject.d.ts +24 -0
- package/dist/inject.d.ts.map +1 -0
- package/dist/inject.js +33 -0
- package/dist/inject.js.map +1 -0
- package/dist/providers.d.ts +24 -0
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +44 -3
- package/dist/providers.js.map +1 -1
- package/dist/session-ready.d.ts +41 -0
- package/dist/session-ready.d.ts.map +1 -0
- package/dist/session-ready.js +79 -0
- package/dist/session-ready.js.map +1 -0
- package/dist/site.d.ts +51 -0
- package/dist/site.d.ts.map +1 -0
- package/dist/site.js +69 -0
- package/dist/site.js.map +1 -0
- package/dist/tokens.d.ts +15 -0
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +12 -0
- package/dist/tokens.js.map +1 -1
- package/dist/types.d.ts +14 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for form handling, mirroring `useForm`, `useWatch` and
|
|
3
|
+
* `useFieldState` from `@molecule/app-react`.
|
|
4
|
+
*
|
|
5
|
+
* Like React — whose component instance survives an argument change — the
|
|
6
|
+
* controller survives this site's re-creation (an argument change to the
|
|
7
|
+
* hook), unless the `resetKey` changed, which starts a fresh form. See
|
|
8
|
+
* `site.ts`.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { type Signal } from '@angular/core';
|
|
13
|
+
import type { FieldRegistration, FieldState, FormController, FormOptions, FormResetOptions, FormState, FormValuesConstraint, RegisterOptions } from '@molecule/app-forms';
|
|
14
|
+
/**
|
|
15
|
+
* Options for {@link useForm}.
|
|
16
|
+
*
|
|
17
|
+
* Every `FormOptions` field is supported. `createForm` is only an override —
|
|
18
|
+
* omit it and the hook creates the controller through the bonded forms
|
|
19
|
+
* provider (`createForm` from `@molecule/app-forms`), which uses the built-in
|
|
20
|
+
* native provider until the app bonds its own.
|
|
21
|
+
*
|
|
22
|
+
* @template T - The form's values shape.
|
|
23
|
+
*/
|
|
24
|
+
export interface UseFormOptions<T extends FormValuesConstraint<T>> extends FormOptions<T> {
|
|
25
|
+
/** External values preserve dirty fields by default. Set keepDirtyValues: false to replace them explicitly. */
|
|
26
|
+
resetOptions?: FormResetOptions;
|
|
27
|
+
/** New record identity. Serialize composite keys; fresh objects/arrays must not reset drafts on each render. */
|
|
28
|
+
resetKey?: string | number | boolean | bigint | null;
|
|
29
|
+
/**
|
|
30
|
+
* Explicit form factory for this one form. Defaults to the bonded forms
|
|
31
|
+
* provider; pass it only to pin a specific provider instance.
|
|
32
|
+
*
|
|
33
|
+
* @param options - The form options.
|
|
34
|
+
* @returns The controller.
|
|
35
|
+
*/
|
|
36
|
+
createForm?: (options: FormOptions<T>) => FormController<T>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A set-state function over a form value: a value or an updater receiving the
|
|
40
|
+
* previous value.
|
|
41
|
+
*
|
|
42
|
+
* @template T - The value's shape.
|
|
43
|
+
*/
|
|
44
|
+
export type FormSetState<T> = (value: T | ((previous: T) => T)) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Result of {@link useForm}: the state fields as signals and every method as a
|
|
47
|
+
* plain function.
|
|
48
|
+
*
|
|
49
|
+
* @template T - The form's values shape.
|
|
50
|
+
*/
|
|
51
|
+
export interface UseFormResult<T extends FormValuesConstraint<T>> {
|
|
52
|
+
/** The whole-form state. */
|
|
53
|
+
readonly formState: Signal<FormState<T>>;
|
|
54
|
+
/** Whether the form has no errors. */
|
|
55
|
+
readonly isValid: Signal<boolean>;
|
|
56
|
+
/** Whether any field differs from its baseline. */
|
|
57
|
+
readonly isDirty: Signal<boolean>;
|
|
58
|
+
/** Whether a submit is running. */
|
|
59
|
+
readonly isSubmitting: Signal<boolean>;
|
|
60
|
+
/**
|
|
61
|
+
* Registers a field and returns its bindings.
|
|
62
|
+
*
|
|
63
|
+
* @param name - The field's name.
|
|
64
|
+
* @param options - Registration options.
|
|
65
|
+
* @returns The field registration.
|
|
66
|
+
*/
|
|
67
|
+
register: (name: keyof T, options?: Omit<RegisterOptions, 'name'> & {
|
|
68
|
+
name?: string;
|
|
69
|
+
}) => FieldRegistration;
|
|
70
|
+
/**
|
|
71
|
+
* Reads one field's value.
|
|
72
|
+
*
|
|
73
|
+
* @param name - The field's name.
|
|
74
|
+
* @returns The value.
|
|
75
|
+
*/
|
|
76
|
+
getValue: <K extends keyof T>(name: K) => T[K];
|
|
77
|
+
/**
|
|
78
|
+
* Writes one field's value (marks it touched).
|
|
79
|
+
*
|
|
80
|
+
* @param name - The field's name.
|
|
81
|
+
* @param value - The new value.
|
|
82
|
+
*/
|
|
83
|
+
setValue: <K extends keyof T>(name: K, value: T[K]) => void;
|
|
84
|
+
/**
|
|
85
|
+
* Reads one field's error.
|
|
86
|
+
*
|
|
87
|
+
* @param name - The field's name.
|
|
88
|
+
* @returns The error message, or `undefined`.
|
|
89
|
+
*/
|
|
90
|
+
getError: (name: keyof T) => string | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* Sets or clears one field's error.
|
|
93
|
+
*
|
|
94
|
+
* @param name - The field's name.
|
|
95
|
+
* @param error - The message, or `undefined` to clear.
|
|
96
|
+
*/
|
|
97
|
+
setError: (name: keyof T, error: string | undefined) => void;
|
|
98
|
+
/** Clears every field error. */
|
|
99
|
+
clearErrors: () => void;
|
|
100
|
+
/**
|
|
101
|
+
* Builds the submit runner over the controller's validation.
|
|
102
|
+
*
|
|
103
|
+
* @param onSubmit - Called with the values once validation passes.
|
|
104
|
+
* @returns The runner to call on submit.
|
|
105
|
+
*/
|
|
106
|
+
handleSubmit: (onSubmit: (values: T) => void | Promise<void>) => (event?: {
|
|
107
|
+
preventDefault?: () => void;
|
|
108
|
+
}) => Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* Resets to the baseline (or new values, which become the baseline).
|
|
111
|
+
*
|
|
112
|
+
* @param values - Optional new baseline.
|
|
113
|
+
*/
|
|
114
|
+
reset: (values?: Partial<T>) => void;
|
|
115
|
+
/**
|
|
116
|
+
* Applies outside values.
|
|
117
|
+
*
|
|
118
|
+
* @param values - The values to apply.
|
|
119
|
+
* @param resetOptions - Whether edits survive.
|
|
120
|
+
*/
|
|
121
|
+
updateValues: (values: Partial<T>, resetOptions?: FormResetOptions) => void;
|
|
122
|
+
/**
|
|
123
|
+
* Whole-form state tuple: edits mark only changed fields dirty; fills
|
|
124
|
+
* preserve them.
|
|
125
|
+
*
|
|
126
|
+
* @returns `[values, edit, fill]`.
|
|
127
|
+
*/
|
|
128
|
+
asState(): readonly [T, FormSetState<T>, FormSetState<Partial<T>>];
|
|
129
|
+
/**
|
|
130
|
+
* One field as [value, edit, fill]. Fill preserves edits made before data
|
|
131
|
+
* arrives.
|
|
132
|
+
*
|
|
133
|
+
* @param name - The field's name.
|
|
134
|
+
* @returns `[value, edit, fill]`.
|
|
135
|
+
*/
|
|
136
|
+
asState<K extends keyof T>(name: K): readonly [T[K], FormSetState<T[K]>, FormSetState<T[K]>];
|
|
137
|
+
/**
|
|
138
|
+
* Runs the whole validation.
|
|
139
|
+
*
|
|
140
|
+
* @returns Whether the form is valid.
|
|
141
|
+
*/
|
|
142
|
+
validate: () => Promise<boolean>;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Form state management. Call it in an injection context (a component field
|
|
146
|
+
* initializer or constructor).
|
|
147
|
+
*
|
|
148
|
+
* Creates its controller through the bonded forms provider, so an app wires a
|
|
149
|
+
* form library once (`setProvider` from `@molecule/app-forms`) and every form
|
|
150
|
+
* picks it up. Passing `createForm` overrides that for one form.
|
|
151
|
+
*
|
|
152
|
+
* When `values` changes by content, it becomes the new baseline;
|
|
153
|
+
* `resetOptions.keepDirtyValues` retains edited fields while pristine fields
|
|
154
|
+
* take the loaded values. A changed `resetKey` starts a fresh form; new
|
|
155
|
+
* `defaultValues` without one leave the form alone.
|
|
156
|
+
*
|
|
157
|
+
* @param options - Form options.
|
|
158
|
+
* @returns Form state as signals and the form methods.
|
|
159
|
+
*/
|
|
160
|
+
export declare function useForm<T extends FormValuesConstraint<T> = Record<string, unknown>>(options?: UseFormOptions<T>): UseFormResult<T>;
|
|
161
|
+
/**
|
|
162
|
+
* Watches one field's value. Call it in an injection context.
|
|
163
|
+
*
|
|
164
|
+
* @param form - Form controller.
|
|
165
|
+
* @param name - Field name to watch.
|
|
166
|
+
* @returns The current field value as a signal.
|
|
167
|
+
*/
|
|
168
|
+
export declare function useWatch<T extends FormValuesConstraint<T>, K extends keyof T>(form: FormController<T>, name: K): Signal<T[K]>;
|
|
169
|
+
/**
|
|
170
|
+
* Reads one field's state. Call it in an injection context.
|
|
171
|
+
*
|
|
172
|
+
* @param form - Form controller.
|
|
173
|
+
* @param name - Field name.
|
|
174
|
+
* @returns The field state as a signal.
|
|
175
|
+
*/
|
|
176
|
+
export declare function useFieldState<T extends FormValuesConstraint<T>>(form: FormController<T>, name: keyof T): Signal<FieldState<T[keyof T]>>;
|
|
177
|
+
//# sourceMappingURL=use-forms.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-forms.d.ts","sourceRoot":"","sources":["../../src/hooks/use-forms.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAA4C,KAAK,MAAM,EAAE,MAAM,eAAe,CAAA;AAErF,OAAO,KAAK,EACV,iBAAiB,EACjB,UAAU,EACV,cAAc,EACd,WAAW,EACX,gBAAgB,EAChB,SAAS,EACT,oBAAoB,EACpB,eAAe,EAChB,MAAM,qBAAqB,CAAA;AAU5B;;;;;;;;;GASG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,oBAAoB,CAAC,CAAC,CAAC,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IACvF,+GAA+G;IAC/G,YAAY,CAAC,EAAE,gBAAgB,CAAA;IAC/B,gHAAgH;IAChH,QAAQ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O,CAAC,CAAC,SAAS,oBAAoB,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjF,OAAO,GAAE,cAAc,CAAC,CAAC,CAAM,GAC9B,aAAa,CAAC,CAAC,CAAC,CAgJlB;AAKD;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,oBAAoB,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAC3E,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,EACvB,IAAI,EAAE,CAAC,GACN,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAEd;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,oBAAoB,CAAC,CAAC,CAAC,EAC7D,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,EACvB,IAAI,EAAE,MAAM,CAAC,GACZ,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAEhC"}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for form handling, mirroring `useForm`, `useWatch` and
|
|
3
|
+
* `useFieldState` from `@molecule/app-react`.
|
|
4
|
+
*
|
|
5
|
+
* Like React — whose component instance survives an argument change — the
|
|
6
|
+
* controller survives this site's re-creation (an argument change to the
|
|
7
|
+
* hook), unless the `resetKey` changed, which starts a fresh form. See
|
|
8
|
+
* `site.ts`.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { computed, DestroyRef, ElementRef, inject } from '@angular/core';
|
|
13
|
+
import { createForm as createBondedForm, formValuesEqual, snapshotFormValue, } from '@molecule/app-forms';
|
|
14
|
+
import { adoptLane, captureSite, parkLane } from '../site.js';
|
|
15
|
+
import { useExternalStore } from './external-store.js';
|
|
16
|
+
/**
|
|
17
|
+
* Adapts controller notifications to the external-store snapshot contract.
|
|
18
|
+
* Providers may return fresh objects from their getters; reads between
|
|
19
|
+
* notifications must still return the same snapshot.
|
|
20
|
+
*
|
|
21
|
+
* @param form - Controller that owns the subscription.
|
|
22
|
+
* @param read - Reads the form state or a selected field.
|
|
23
|
+
* @returns The current subscribed snapshot as a signal.
|
|
24
|
+
*/
|
|
25
|
+
function useFormSnapshot(form, read) {
|
|
26
|
+
let snapshot = read();
|
|
27
|
+
return useExternalStore(() => snapshot, (onChange) => {
|
|
28
|
+
const refresh = () => {
|
|
29
|
+
snapshot = read();
|
|
30
|
+
onChange();
|
|
31
|
+
};
|
|
32
|
+
const unsubscribe = form.subscribe(refresh);
|
|
33
|
+
// A controller can change after render but before React subscribes.
|
|
34
|
+
refresh();
|
|
35
|
+
return unsubscribe;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Form state management. Call it in an injection context (a component field
|
|
40
|
+
* initializer or constructor).
|
|
41
|
+
*
|
|
42
|
+
* Creates its controller through the bonded forms provider, so an app wires a
|
|
43
|
+
* form library once (`setProvider` from `@molecule/app-forms`) and every form
|
|
44
|
+
* picks it up. Passing `createForm` overrides that for one form.
|
|
45
|
+
*
|
|
46
|
+
* When `values` changes by content, it becomes the new baseline;
|
|
47
|
+
* `resetOptions.keepDirtyValues` retains edited fields while pristine fields
|
|
48
|
+
* take the loaded values. A changed `resetKey` starts a fresh form; new
|
|
49
|
+
* `defaultValues` without one leave the form alone.
|
|
50
|
+
*
|
|
51
|
+
* @param options - Form options.
|
|
52
|
+
* @returns Form state as signals and the form methods.
|
|
53
|
+
*/
|
|
54
|
+
export function useForm(options = {}) {
|
|
55
|
+
const { createForm = createBondedForm, resetKey, ...formOptions } = options;
|
|
56
|
+
const destroyRef = inject(DestroyRef);
|
|
57
|
+
const host = inject(ElementRef).nativeElement;
|
|
58
|
+
let mounted = false;
|
|
59
|
+
// This site's lane: the controller survives an argument change unless the
|
|
60
|
+
// resetKey changed, exactly like React's formRef/formKey pair.
|
|
61
|
+
const adopted = adoptLane('useForm');
|
|
62
|
+
const keep = adopted !== undefined && Object.is(adopted.resetKey, resetKey);
|
|
63
|
+
const lane = keep
|
|
64
|
+
? adopted
|
|
65
|
+
: {
|
|
66
|
+
controller: createForm(formOptions),
|
|
67
|
+
resetKey,
|
|
68
|
+
lastExternalValues: null,
|
|
69
|
+
mounted: false,
|
|
70
|
+
};
|
|
71
|
+
const form = lane.controller;
|
|
72
|
+
// The site's container, recorded once the host is attached (see captureSite).
|
|
73
|
+
let site = null;
|
|
74
|
+
captureSite(host, (captured) => {
|
|
75
|
+
site = captured;
|
|
76
|
+
});
|
|
77
|
+
destroyRef.onDestroy(() => {
|
|
78
|
+
mounted = false;
|
|
79
|
+
lane.mounted = false;
|
|
80
|
+
parkLane('useForm', lane, site);
|
|
81
|
+
});
|
|
82
|
+
mounted = true;
|
|
83
|
+
lane.mounted = true;
|
|
84
|
+
// External content has its own history. A reset may change the controller
|
|
85
|
+
// baseline without making an unchanged record external again.
|
|
86
|
+
const values = options.values;
|
|
87
|
+
const keepDirtyValues = options.resetOptions?.keepDirtyValues !== false;
|
|
88
|
+
if (values === undefined) {
|
|
89
|
+
lane.lastExternalValues = null;
|
|
90
|
+
}
|
|
91
|
+
else if (lane.lastExternalValues === null ||
|
|
92
|
+
lane.lastExternalValues.controller !== form ||
|
|
93
|
+
!formValuesEqual(lane.lastExternalValues.values, values)) {
|
|
94
|
+
lane.lastExternalValues = { controller: form, values: snapshotFormValue(values) };
|
|
95
|
+
form.updateValues(values, { keepDirtyValues });
|
|
96
|
+
}
|
|
97
|
+
const readState = () => form.getState();
|
|
98
|
+
const formState = useFormSnapshot(form, readState);
|
|
99
|
+
const register = (name, registerOptions) => form.register({ ...registerOptions, name: name });
|
|
100
|
+
const getValue = (name) => form.getValue(name);
|
|
101
|
+
const setValue = (name, value) => form.setValue(name, value);
|
|
102
|
+
const getError = (name) => form.getError(name);
|
|
103
|
+
const setError = (name, error) => {
|
|
104
|
+
if (error !== undefined) {
|
|
105
|
+
form.setError(name, error);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
form.clearError(name);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const clearErrors = () => form.clearErrors();
|
|
112
|
+
const reset = (resetValues) => form.reset(resetValues);
|
|
113
|
+
const updateValues = (newValues, resetOpts) => form.updateValues(newValues, resetOpts);
|
|
114
|
+
// The implementation is widened to `unknown` and the object literal's cast
|
|
115
|
+
// restores the overloads, like the Vue and Svelte bindings: `edit`/`fill`
|
|
116
|
+
// accept a superset of the tuple's declared call shapes.
|
|
117
|
+
const asState = (name) => {
|
|
118
|
+
const read = () => name === undefined ? form.getState().values : form.getValue(name);
|
|
119
|
+
const edit = (value) => {
|
|
120
|
+
const next = typeof value === 'function'
|
|
121
|
+
? value(read())
|
|
122
|
+
: value;
|
|
123
|
+
if (name !== undefined) {
|
|
124
|
+
form.setValue(name, next);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const nextValues = next;
|
|
128
|
+
for (const field of Object.keys(Object.assign({}, form.getState().values, nextValues))) {
|
|
129
|
+
if (!formValuesEqual(form.getValue(field), nextValues[field]))
|
|
130
|
+
form.setValue(field, nextValues[field]);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
const fill = (value) => {
|
|
134
|
+
const next = typeof value === 'function'
|
|
135
|
+
? value(read())
|
|
136
|
+
: value;
|
|
137
|
+
form.updateValues(name === undefined ? next : { [name]: next }, { keepDirtyValues: true });
|
|
138
|
+
};
|
|
139
|
+
const visible = name === undefined ? form.getState().values : form.getValue(name);
|
|
140
|
+
return [visible, edit, fill];
|
|
141
|
+
};
|
|
142
|
+
const validate = () => form.validate();
|
|
143
|
+
const handleSubmit = (onSubmit) => form.handleSubmit((values) => {
|
|
144
|
+
if (mounted && lane.controller === form)
|
|
145
|
+
return onSubmit(values);
|
|
146
|
+
});
|
|
147
|
+
return {
|
|
148
|
+
formState,
|
|
149
|
+
isValid: computed(() => formState().isValid),
|
|
150
|
+
isDirty: computed(() => formState().isDirty),
|
|
151
|
+
isSubmitting: computed(() => formState().isSubmitting),
|
|
152
|
+
register,
|
|
153
|
+
getValue,
|
|
154
|
+
setValue,
|
|
155
|
+
getError,
|
|
156
|
+
setError,
|
|
157
|
+
clearErrors,
|
|
158
|
+
handleSubmit,
|
|
159
|
+
reset,
|
|
160
|
+
updateValues,
|
|
161
|
+
asState: asState,
|
|
162
|
+
validate,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Watches one field's value. Call it in an injection context.
|
|
167
|
+
*
|
|
168
|
+
* @param form - Form controller.
|
|
169
|
+
* @param name - Field name to watch.
|
|
170
|
+
* @returns The current field value as a signal.
|
|
171
|
+
*/
|
|
172
|
+
export function useWatch(form, name) {
|
|
173
|
+
return useFormSnapshot(form, () => form.getValue(name));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Reads one field's state. Call it in an injection context.
|
|
177
|
+
*
|
|
178
|
+
* @param form - Form controller.
|
|
179
|
+
* @param name - Field name.
|
|
180
|
+
* @returns The field state as a signal.
|
|
181
|
+
*/
|
|
182
|
+
export function useFieldState(form, name) {
|
|
183
|
+
return useFormSnapshot(form, () => form.getFieldState(name));
|
|
184
|
+
}
|
|
185
|
+
//# sourceMappingURL=use-forms.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-forms.js","sourceRoot":"","sources":["../../src/hooks/use-forms.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAe,MAAM,eAAe,CAAA;AAYrF,OAAO,EACL,UAAU,IAAI,gBAAgB,EAC9B,eAAe,EACf,iBAAiB,GAClB,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM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C,aAAa,CAAC,IAAI,CAAC,CAAC,CAAA;AAC9D,CAAC"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for HTTP requests, mirroring `useHttpClient`, `useHttp`,
|
|
3
|
+
* `useGet`, `usePost`, `usePut`, `usePatch` and `useDelete` from
|
|
4
|
+
* `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* Like React — whose component instance survives an argument change — the
|
|
7
|
+
* request state survives this site's re-creation (an argument change to the
|
|
8
|
+
* hook): the destroyed instance parks it at the site and the next instance
|
|
9
|
+
* adopts it. See `site.ts`.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import { type Signal } from '@angular/core';
|
|
14
|
+
import type { HttpClient, RequestConfig } from '@molecule/app-http';
|
|
15
|
+
/**
|
|
16
|
+
* The HTTP client registered by `provideMolecule({ http })` or `provideHttp`.
|
|
17
|
+
* Call it in an injection context.
|
|
18
|
+
*
|
|
19
|
+
* @returns The HTTP client.
|
|
20
|
+
* @throws {Error} When no HTTP client is registered.
|
|
21
|
+
*/
|
|
22
|
+
export declare function useHttpClient(): HttpClient;
|
|
23
|
+
/**
|
|
24
|
+
* State for async HTTP operations.
|
|
25
|
+
*
|
|
26
|
+
* @template T - The response data's shape.
|
|
27
|
+
*/
|
|
28
|
+
export interface UseHttpState<T> {
|
|
29
|
+
/** The latest successful response's data, `null` before one and after a reset. */
|
|
30
|
+
data: T | null;
|
|
31
|
+
/** Whether a request is in flight. */
|
|
32
|
+
loading: boolean;
|
|
33
|
+
/** The failure of the newest owned request. */
|
|
34
|
+
error: Error | null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Result of {@link useHttp}: every state field as a signal (the same fields
|
|
38
|
+
* React's result carries as plain values) and the two controls as plain
|
|
39
|
+
* functions.
|
|
40
|
+
*
|
|
41
|
+
* @template T - The response data's shape.
|
|
42
|
+
*/
|
|
43
|
+
export interface UseHttpResult<T> {
|
|
44
|
+
/** The latest successful response's data, `null` before one and after a reset. */
|
|
45
|
+
readonly data: Signal<T | null>;
|
|
46
|
+
/** Whether a request is in flight. */
|
|
47
|
+
readonly loading: Signal<boolean>;
|
|
48
|
+
/** The failure of the newest owned request. */
|
|
49
|
+
readonly error: Signal<Error | null>;
|
|
50
|
+
/**
|
|
51
|
+
* Runs the request and resolves with its own data, or `null` on failure,
|
|
52
|
+
* even when a newer request or `reset` has superseded its displayed state.
|
|
53
|
+
*
|
|
54
|
+
* This function is IDENTITY-STABLE for as long as `method`, `url` and the
|
|
55
|
+
* HTTP client do not change, so it is safe as an effect dependency. Its
|
|
56
|
+
* request options are read when it is CALLED.
|
|
57
|
+
*
|
|
58
|
+
* @returns The response data, or `null` on failure.
|
|
59
|
+
*/
|
|
60
|
+
execute: () => Promise<T | null>;
|
|
61
|
+
/** Supersedes every pending request and clears the state. */
|
|
62
|
+
reset: () => void;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Options for {@link useHttp} and the verb hooks.
|
|
66
|
+
*
|
|
67
|
+
* Every request option — headers, params, body `data`, `signal`, `onSuccess`,
|
|
68
|
+
* `onError` — is read at the moment `execute` runs, not when the hook mounted,
|
|
69
|
+
* so an inline object or callback needs no memoizing. Only `immediate` is read
|
|
70
|
+
* on mount.
|
|
71
|
+
*
|
|
72
|
+
* @template T - The response data's shape.
|
|
73
|
+
*/
|
|
74
|
+
export interface UseHttpOptions<T> extends RequestConfig {
|
|
75
|
+
/**
|
|
76
|
+
* Whether to execute the request immediately on mount.
|
|
77
|
+
*/
|
|
78
|
+
immediate?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Callback for each request that succeeds while mounted, including a request
|
|
81
|
+
* superseded by another `execute` or `reset`. Read `data` from the hook when
|
|
82
|
+
* only the newest request's result should drive the UI.
|
|
83
|
+
*
|
|
84
|
+
* @param data - The response data.
|
|
85
|
+
*/
|
|
86
|
+
onSuccess?: (data: T) => void;
|
|
87
|
+
/**
|
|
88
|
+
* Callback for each request that fails while mounted, including a request
|
|
89
|
+
* superseded by another `execute` or `reset`.
|
|
90
|
+
*
|
|
91
|
+
* @param error - The failure.
|
|
92
|
+
*/
|
|
93
|
+
onError?: (error: Error) => void;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* HTTP requests with state management. Call it in an injection context (a
|
|
97
|
+
* component field initializer or constructor).
|
|
98
|
+
*
|
|
99
|
+
* `execute` keeps one identity while `method`, `url` and the HTTP client stay
|
|
100
|
+
* the same — across the hook's own loading/data updates and inline options or
|
|
101
|
+
* callbacks. The current headers, body, `signal` and callbacks are read when
|
|
102
|
+
* `execute` is called. Changing `method`, `url` or the client selects a
|
|
103
|
+
* different request.
|
|
104
|
+
*
|
|
105
|
+
* The newest request owns the state: an older request that answers after a
|
|
106
|
+
* newer one (or after `reset`) cannot repopulate the displayed state, though
|
|
107
|
+
* its own callbacks still report its outcome.
|
|
108
|
+
*
|
|
109
|
+
* @param method - HTTP method.
|
|
110
|
+
* @param url - Request URL.
|
|
111
|
+
* @param options - Request options, read at call time.
|
|
112
|
+
* @returns The request state as signals and the execute/reset functions.
|
|
113
|
+
*/
|
|
114
|
+
export declare function useHttp<T>(method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', url: string, options?: UseHttpOptions<T>): UseHttpResult<T>;
|
|
115
|
+
/**
|
|
116
|
+
* GET requests.
|
|
117
|
+
*
|
|
118
|
+
* @param url - Request URL to fetch from.
|
|
119
|
+
* @param options - HTTP request options including callbacks and request config.
|
|
120
|
+
* @returns Request state (data, loading, error) and controls (execute, reset).
|
|
121
|
+
*/
|
|
122
|
+
export declare function useGet<T>(url: string, options?: UseHttpOptions<T>): UseHttpResult<T>;
|
|
123
|
+
/**
|
|
124
|
+
* POST requests, with the body in `options.data`.
|
|
125
|
+
*
|
|
126
|
+
* @param url - Request URL to post to.
|
|
127
|
+
* @param options - HTTP request options including body data and callbacks.
|
|
128
|
+
* @returns Request state (data, loading, error) and controls (execute, reset).
|
|
129
|
+
*/
|
|
130
|
+
export declare function usePost<T>(url: string, options?: UseHttpOptions<T>): UseHttpResult<T>;
|
|
131
|
+
/**
|
|
132
|
+
* PUT requests, with the replacement body.
|
|
133
|
+
*
|
|
134
|
+
* @param url - Request URL to send the PUT request to.
|
|
135
|
+
* @param options - HTTP request options including body data and callbacks.
|
|
136
|
+
* @returns Request state (data, loading, error) and controls (execute, reset).
|
|
137
|
+
*/
|
|
138
|
+
export declare function usePut<T>(url: string, options?: UseHttpOptions<T>): UseHttpResult<T>;
|
|
139
|
+
/**
|
|
140
|
+
* PATCH requests, with the partial body.
|
|
141
|
+
*
|
|
142
|
+
* @param url - Request URL to send the PATCH request to.
|
|
143
|
+
* @param options - HTTP request options including partial body data and callbacks.
|
|
144
|
+
* @returns Request state (data, loading, error) and controls (execute, reset).
|
|
145
|
+
*/
|
|
146
|
+
export declare function usePatch<T>(url: string, options?: UseHttpOptions<T>): UseHttpResult<T>;
|
|
147
|
+
/**
|
|
148
|
+
* DELETE requests; a body travels in `options.data`.
|
|
149
|
+
*
|
|
150
|
+
* @param url - Request URL of the resource to delete.
|
|
151
|
+
* @param options - HTTP request options including callbacks.
|
|
152
|
+
* @returns Request state (data, loading, error) and controls (execute, reset).
|
|
153
|
+
*/
|
|
154
|
+
export declare function useDelete<T>(url: string, options?: UseHttpOptions<T>): UseHttpResult<T>;
|
|
155
|
+
//# sourceMappingURL=use-http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-http.d.ts","sourceRoot":"","sources":["../../src/hooks/use-http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAIL,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAOnE;;;;;;GAMG;AACH,wBAAgB,aAAa,IAAI,UAAU,CAM1C;AAED;;;;GAIG;AACH,MA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