@azure-net/kit 1.0.1 → 1.0.3
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.
|
@@ -2,6 +2,8 @@ import type { RequestErrors } from '../../delivery/schema/index.js';
|
|
|
2
2
|
import type { AsyncActionResponse } from '../../index.js';
|
|
3
3
|
export interface FormConfig<FormData> {
|
|
4
4
|
initialData?: FormData;
|
|
5
|
+
onSuccess?: () => Promise<void> | void;
|
|
6
|
+
onError?: () => Promise<void> | void;
|
|
5
7
|
}
|
|
6
8
|
export interface ActiveForm<FormData, Response> {
|
|
7
9
|
data: Partial<FormData>;
|
|
@@ -11,4 +13,4 @@ export interface ActiveForm<FormData, Response> {
|
|
|
11
13
|
pending: boolean;
|
|
12
14
|
dirty: boolean;
|
|
13
15
|
}
|
|
14
|
-
export declare const createActiveForm: <FormData, Response = unknown>(onSubmit: (formData: Partial<FormData>) => Promise<AsyncActionResponse<Response, FormData>>, config
|
|
16
|
+
export declare const createActiveForm: <FormData, Response = unknown>(onSubmit: (formData: Partial<FormData>) => Promise<AsyncActionResponse<Response, FormData>>, config?: FormConfig<FormData>) => ActiveForm<FormData, Response>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ObjectUtil } from 'azure-net-tools';
|
|
2
2
|
export const createActiveForm = (onSubmit, config) => {
|
|
3
|
-
const initial = config
|
|
3
|
+
const initial = config?.initialData ?? {};
|
|
4
4
|
let formData = $state(ObjectUtil.deepClone(initial));
|
|
5
5
|
let formErrors = $state({});
|
|
6
6
|
let pending = $state(false);
|
|
@@ -12,6 +12,12 @@ export const createActiveForm = (onSubmit, config) => {
|
|
|
12
12
|
formErrors = result.error?.fields;
|
|
13
13
|
}
|
|
14
14
|
pending = false;
|
|
15
|
+
if (result.success) {
|
|
16
|
+
await config?.onSuccess?.();
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
await config?.onError?.();
|
|
20
|
+
}
|
|
15
21
|
return result;
|
|
16
22
|
};
|
|
17
23
|
const reset = (toInitial = false) => {
|