@azure-net/kit 1.0.3 → 1.0.4

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.
@@ -5,12 +5,22 @@ export interface FormConfig<FormData> {
5
5
  onSuccess?: () => Promise<void> | void;
6
6
  onError?: () => Promise<void> | void;
7
7
  }
8
- export interface ActiveForm<FormData, Response> {
8
+ export interface ActiveForm<FormData, Response, Custom> {
9
9
  data: Partial<FormData>;
10
10
  errors: RequestErrors<FormData>;
11
- submit: () => Promise<AsyncActionResponse<Response, FormData>>;
12
- reset: () => void;
11
+ submit: () => Promise<AsyncActionResponse<Response, FormData, Custom>>;
12
+ reset: (toInitial?: boolean) => void;
13
13
  pending: boolean;
14
14
  dirty: boolean;
15
15
  }
16
- export declare const createActiveForm: <FormData, Response = unknown>(onSubmit: (formData: Partial<FormData>) => Promise<AsyncActionResponse<Response, FormData>>, config?: FormConfig<FormData>) => ActiveForm<FormData, Response>;
16
+ type ExtractResponse<T> = T extends AsyncActionResponse<infer R, unknown, unknown> ? R : never;
17
+ type ExtractFormData<T> = T extends AsyncActionResponse<unknown, infer D, unknown> ? D : never;
18
+ type ExtractCustom<T> = T extends AsyncActionResponse<unknown, unknown, infer C> ? C : never;
19
+ type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
20
+ type ExtractFromSubmit<T> = {
21
+ response: ExtractResponse<UnwrapPromise<T>>;
22
+ formData: ExtractFormData<UnwrapPromise<T>>;
23
+ custom: ExtractCustom<UnwrapPromise<T>>;
24
+ };
25
+ export declare const createActiveForm: <SubmitReturn extends Promise<AsyncActionResponse<unknown, unknown, unknown>>>(onSubmit: (formData: Partial<ExtractFromSubmit<SubmitReturn>["formData"]>) => SubmitReturn, config?: FormConfig<ExtractFromSubmit<SubmitReturn>["formData"]>) => ActiveForm<ExtractFromSubmit<SubmitReturn>["formData"], ExtractFromSubmit<SubmitReturn>["response"], ExtractFromSubmit<SubmitReturn>["custom"]>;
26
+ export {};
@@ -7,18 +7,22 @@ export const createActiveForm = (onSubmit, config) => {
7
7
  const dirty = $derived(JSON.stringify(formData) !== JSON.stringify(initial));
8
8
  const submit = async () => {
9
9
  pending = true;
10
- const result = await onSubmit(formData);
11
- if (result.error?.fields) {
12
- formErrors = result.error?.fields;
10
+ try {
11
+ const result = await onSubmit(formData);
12
+ if (result.error?.fields) {
13
+ formErrors = result.error.fields;
14
+ }
15
+ if (result.success) {
16
+ await config?.onSuccess?.();
17
+ }
18
+ else {
19
+ await config?.onError?.();
20
+ }
21
+ return result;
13
22
  }
14
- pending = false;
15
- if (result.success) {
16
- await config?.onSuccess?.();
23
+ finally {
24
+ pending = false;
17
25
  }
18
- else {
19
- await config?.onError?.();
20
- }
21
- return result;
22
26
  };
23
27
  const reset = (toInitial = false) => {
24
28
  formData = toInitial ? ObjectUtil.deepClone(initial) : {};
@@ -28,6 +32,9 @@ export const createActiveForm = (onSubmit, config) => {
28
32
  get data() {
29
33
  return formData;
30
34
  },
35
+ set data(value) {
36
+ formData = value;
37
+ },
31
38
  get errors() {
32
39
  return formErrors;
33
40
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",