@neutro/form 0.0.5 → 0.1.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/dist/adapters/angular.cjs +24 -6
- package/dist/adapters/angular.d.cts +163 -19
- package/dist/adapters/angular.d.ts +163 -19
- package/dist/adapters/angular.js +25 -7
- package/dist/adapters/react.cjs +5 -1
- package/dist/adapters/react.d.cts +86 -5
- package/dist/adapters/react.d.ts +86 -5
- package/dist/adapters/react.js +6 -2
- package/dist/adapters/solid.cjs +12 -3
- package/dist/adapters/solid.d.cts +161 -16
- package/dist/adapters/solid.d.ts +161 -16
- package/dist/adapters/solid.js +12 -3
- package/dist/adapters/svelte.cjs +18 -9
- package/dist/adapters/svelte.d.cts +161 -17
- package/dist/adapters/svelte.d.ts +161 -17
- package/dist/adapters/svelte.js +18 -9
- package/dist/adapters/vue.cjs +15 -4
- package/dist/adapters/vue.d.cts +161 -17
- package/dist/adapters/vue.d.ts +161 -17
- package/dist/adapters/vue.js +23 -5
- package/dist/chunk-FDAQJJJ7.js +657 -0
- package/dist/core.cjs +510 -425
- package/dist/core.d.cts +94 -7
- package/dist/core.d.ts +94 -7
- package/dist/core.js +24 -569
- package/dist/devtools.cjs +244 -0
- package/dist/devtools.d.cts +141 -0
- package/dist/devtools.d.ts +141 -0
- package/dist/devtools.js +217 -0
- package/dist/testing.cjs +667 -0
- package/dist/testing.d.cts +254 -0
- package/dist/testing.d.ts +254 -0
- package/dist/testing.js +39 -0
- package/package.json +25 -7
package/dist/adapters/vue.d.ts
CHANGED
|
@@ -1,22 +1,166 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
|
-
import { MaybeRef } from 'vue';
|
|
2
|
+
import { DeepReadonly, ShallowRef, MaybeRef } from 'vue';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @neutro/form-core
|
|
6
|
+
* High-Performance, Zero-Dependency, Framework-Agnostic Reactive Form Engine.
|
|
7
|
+
*/
|
|
8
|
+
type Primitive = string | number | boolean | null | undefined | Date | File;
|
|
9
|
+
type Prev = [never, 0, 1, 2, 3, 4, 5, ...any[]];
|
|
10
|
+
type PathImpl<T, K extends keyof T, Depth extends number = 5> = [Depth] extends [never] ? never : K extends string ? T[K] extends Primitive ? K : T[K] extends Array<infer U> ? K | `${K}.${number}` | (U extends object ? `${K}.${number}.${PathImpl<U, keyof U, Prev[Depth]>}` : never) : NonNullable<T[K]> extends object ? K | `${K}.${PathImpl<NonNullable<T[K]>, keyof NonNullable<T[K]>, Prev[Depth]>}` : K : never;
|
|
11
|
+
type Path<T> = PathImpl<T, keyof T> & string;
|
|
12
|
+
type _GetPathValue<T, P extends string> = P extends `${infer K}.${infer Rest}` ? K extends keyof T ? _GetPathValue<NonNullable<T[K]>, Rest> : T extends readonly any[] ? _GetPathValue<NonNullable<T[number]>, Rest> : unknown : P extends keyof T ? T[P] : T extends readonly any[] ? T[number] : unknown;
|
|
13
|
+
type GetPathValue<T, P extends string> = _GetPathValue<T, P>;
|
|
14
|
+
interface FormState<T> {
|
|
15
|
+
values: T;
|
|
16
|
+
errors: Record<string, string>;
|
|
17
|
+
touched: Record<string, boolean>;
|
|
18
|
+
dirty: Record<string, boolean>;
|
|
19
|
+
isSubmitting: boolean;
|
|
20
|
+
isValidating: boolean;
|
|
21
|
+
}
|
|
22
|
+
type FormSubscriber<T> = (state: FormState<T>) => void;
|
|
23
|
+
type PathSubscriber<V = any> = (value: V, fieldState: {
|
|
24
|
+
error?: string;
|
|
25
|
+
touched?: boolean;
|
|
26
|
+
dirty?: boolean;
|
|
27
|
+
}) => void;
|
|
28
|
+
type ValidationMode = 'onChange' | 'onBlur' | 'onTouched' | 'onSubmitOnly';
|
|
29
|
+
type FormAction = {
|
|
30
|
+
type: 'SET';
|
|
31
|
+
path: string;
|
|
32
|
+
value: unknown;
|
|
33
|
+
options?: {
|
|
34
|
+
touch?: boolean;
|
|
35
|
+
validate?: boolean;
|
|
36
|
+
};
|
|
37
|
+
} | {
|
|
38
|
+
type: 'VALIDATE';
|
|
39
|
+
paths?: string[];
|
|
40
|
+
} | {
|
|
41
|
+
type: 'SUBMIT';
|
|
42
|
+
} | {
|
|
43
|
+
type: 'RESET';
|
|
44
|
+
newValues?: unknown;
|
|
45
|
+
} | {
|
|
46
|
+
type: 'SET_ERRORS';
|
|
47
|
+
errors: Record<string, string>;
|
|
48
|
+
} | {
|
|
49
|
+
type: 'CONNECT';
|
|
50
|
+
path: string;
|
|
51
|
+
} | {
|
|
52
|
+
type: 'DISCONNECT';
|
|
53
|
+
path: string;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'BLUR';
|
|
56
|
+
path: string;
|
|
57
|
+
} | {
|
|
58
|
+
type: 'BATCH_START';
|
|
59
|
+
} | {
|
|
60
|
+
type: 'BATCH_END';
|
|
61
|
+
} | {
|
|
62
|
+
type: 'ARRAY_APPEND';
|
|
63
|
+
path: string;
|
|
64
|
+
item: unknown;
|
|
65
|
+
} | {
|
|
66
|
+
type: 'ARRAY_INSERT';
|
|
67
|
+
path: string;
|
|
68
|
+
index: number;
|
|
69
|
+
item: unknown;
|
|
70
|
+
} | {
|
|
71
|
+
type: 'ARRAY_REMOVE';
|
|
72
|
+
path: string;
|
|
73
|
+
index: number;
|
|
74
|
+
} | {
|
|
75
|
+
type: 'ARRAY_MOVE';
|
|
76
|
+
path: string;
|
|
77
|
+
from: number;
|
|
78
|
+
to: number;
|
|
79
|
+
} | {
|
|
80
|
+
type: 'ARRAY_SWAP';
|
|
81
|
+
path: string;
|
|
82
|
+
i: number;
|
|
83
|
+
j: number;
|
|
84
|
+
} | {
|
|
85
|
+
type: 'CLEAR_ERRORS';
|
|
18
86
|
};
|
|
19
|
-
|
|
87
|
+
interface AriaPropsOptions {
|
|
88
|
+
required?: boolean;
|
|
89
|
+
errorId?: string;
|
|
90
|
+
}
|
|
91
|
+
interface AriaProps {
|
|
92
|
+
'aria-invalid': 'true' | 'false';
|
|
93
|
+
'aria-describedby': string | undefined;
|
|
94
|
+
'aria-required': true | undefined;
|
|
95
|
+
}
|
|
96
|
+
interface ConnectOptions {
|
|
97
|
+
persist?: boolean;
|
|
98
|
+
format?: (val: string) => string;
|
|
99
|
+
validateOn?: ValidationMode;
|
|
100
|
+
}
|
|
101
|
+
interface FormInstance<T extends object> {
|
|
102
|
+
subscribe: (fn: FormSubscriber<T>) => () => void;
|
|
103
|
+
subscribeToPath<P extends Path<T>>(path: P, fn: PathSubscriber<GetPathValue<T, P>>): () => void;
|
|
104
|
+
subscribeToPath(path: string, fn: PathSubscriber): () => void;
|
|
105
|
+
get<P extends Path<T>>(path: P): GetPathValue<T, P>;
|
|
106
|
+
get(path: string | string[]): any;
|
|
107
|
+
set: (path: Path<T> | string | string[], val: any, options?: {
|
|
108
|
+
touch?: boolean;
|
|
109
|
+
validate?: boolean;
|
|
110
|
+
}) => void;
|
|
111
|
+
validate: (scopePaths?: Path<T>[] | string[] | string[][]) => Promise<boolean>;
|
|
112
|
+
connect: (path: Path<T> | string, el: HTMLElement, options?: ConnectOptions) => () => void;
|
|
113
|
+
submit: (onValid: (payload: Partial<T>) => void | Promise<void>) => Promise<boolean>;
|
|
114
|
+
handleSubmit: (onValid: (payload: Partial<T>) => void | Promise<void>, onInvalid?: (errors: Record<string, string>) => void) => (e?: Event) => void;
|
|
115
|
+
getState: () => FormState<T>;
|
|
116
|
+
getPayload: () => Partial<T>;
|
|
117
|
+
getAriaProps: (path: Path<T> | string, options?: AriaPropsOptions) => AriaProps;
|
|
118
|
+
batch: (fn: () => void) => void;
|
|
119
|
+
arrayAppend: (path: Path<T> | string | string[], item: any) => void;
|
|
120
|
+
arrayInsert: (path: Path<T> | string | string[], index: number, item: any) => void;
|
|
121
|
+
arrayRemove: (path: Path<T> | string | string[], index: number) => void;
|
|
122
|
+
arrayMove: (path: Path<T> | string | string[], fromIndex: number, toIndex: number) => void;
|
|
123
|
+
arraySwap: (path: Path<T> | string | string[], indexA: number, indexB: number) => void;
|
|
124
|
+
reset: (newValues?: T) => void;
|
|
125
|
+
getConnectedCount: () => number;
|
|
126
|
+
destroy: () => void;
|
|
127
|
+
setErrors: (errors: Record<Path<T> | (string & {}), string>) => void;
|
|
128
|
+
clearErrors: () => void;
|
|
129
|
+
/**
|
|
130
|
+
* Returns the effective ValidationMode for a field. Useful for debugging
|
|
131
|
+
* validation timing; framework adapters should rely on this only in custom
|
|
132
|
+
* event handlers, not in render logic.
|
|
133
|
+
*/
|
|
134
|
+
getFieldMode: (path: string) => ValidationMode;
|
|
135
|
+
_subscribeToActions: (fn: (action: FormAction, state: FormState<T>) => void) => () => void;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
interface VueFormReturn<T extends object> {
|
|
139
|
+
state: DeepReadonly<ShallowRef<FormState<T>>>;
|
|
140
|
+
get: FormInstance<T>['get'];
|
|
141
|
+
set: FormInstance<T>['set'];
|
|
142
|
+
connect: FormInstance<T>['connect'];
|
|
143
|
+
submit: FormInstance<T>['submit'];
|
|
144
|
+
handleSubmit: FormInstance<T>['handleSubmit'];
|
|
145
|
+
reset: FormInstance<T>['reset'];
|
|
146
|
+
batch: FormInstance<T>['batch'];
|
|
147
|
+
validate: FormInstance<T>['validate'];
|
|
148
|
+
subscribeToPath: FormInstance<T>['subscribeToPath'];
|
|
149
|
+
getPayload: FormInstance<T>['getPayload'];
|
|
150
|
+
getAriaProps: FormInstance<T>['getAriaProps'];
|
|
151
|
+
getFieldMode: FormInstance<T>['getFieldMode'];
|
|
152
|
+
getConnectedCount: FormInstance<T>['getConnectedCount'];
|
|
153
|
+
destroy: FormInstance<T>['destroy'];
|
|
154
|
+
arrayAppend: FormInstance<T>['arrayAppend'];
|
|
155
|
+
arrayInsert: FormInstance<T>['arrayInsert'];
|
|
156
|
+
arrayRemove: FormInstance<T>['arrayRemove'];
|
|
157
|
+
arrayMove: FormInstance<T>['arrayMove'];
|
|
158
|
+
arraySwap: FormInstance<T>['arraySwap'];
|
|
159
|
+
setErrors: FormInstance<T>['setErrors'];
|
|
160
|
+
clearErrors: FormInstance<T>['clearErrors'];
|
|
161
|
+
}
|
|
162
|
+
declare function useVueForm<T extends object>(form: FormInstance<T>): VueFormReturn<T>;
|
|
163
|
+
declare function useVueFormPath<T extends object>(form: FormInstance<T>, path: MaybeRef<string>): {
|
|
20
164
|
value: Readonly<vue.Ref<Readonly<unknown>, Readonly<unknown>>>;
|
|
21
165
|
fieldState: Readonly<vue.Ref<{
|
|
22
166
|
readonly error?: string | undefined;
|
|
@@ -29,4 +173,4 @@ declare function useVueFormPath(form: any, path: MaybeRef<string>): {
|
|
|
29
173
|
} | null>>;
|
|
30
174
|
};
|
|
31
175
|
|
|
32
|
-
export { useVueForm, useVueFormPath };
|
|
176
|
+
export { type VueFormReturn, useVueForm, useVueFormPath };
|
package/dist/adapters/vue.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
// ../adapters/vue/dist/index.js
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
getCurrentInstance,
|
|
4
|
+
onUnmounted,
|
|
5
|
+
readonly,
|
|
6
|
+
shallowRef,
|
|
7
|
+
unref,
|
|
8
|
+
watch
|
|
9
|
+
} from "vue";
|
|
3
10
|
function useVueForm(form) {
|
|
4
11
|
const state = shallowRef(form.getState());
|
|
5
12
|
const unsubscribe = form.subscribe((s) => {
|
|
6
13
|
state.value = s;
|
|
7
14
|
});
|
|
8
|
-
onUnmounted(unsubscribe);
|
|
15
|
+
if (getCurrentInstance()) onUnmounted(unsubscribe);
|
|
9
16
|
return {
|
|
10
17
|
state: readonly(state),
|
|
11
18
|
get: form.get,
|
|
@@ -15,16 +22,27 @@ function useVueForm(form) {
|
|
|
15
22
|
handleSubmit: form.handleSubmit,
|
|
16
23
|
reset: form.reset,
|
|
17
24
|
batch: form.batch,
|
|
25
|
+
validate: form.validate,
|
|
26
|
+
subscribeToPath: form.subscribeToPath,
|
|
27
|
+
getPayload: form.getPayload,
|
|
28
|
+
getAriaProps: form.getAriaProps,
|
|
29
|
+
getFieldMode: form.getFieldMode,
|
|
30
|
+
getConnectedCount: form.getConnectedCount,
|
|
31
|
+
destroy: form.destroy,
|
|
18
32
|
arrayAppend: form.arrayAppend,
|
|
19
33
|
arrayInsert: form.arrayInsert,
|
|
20
34
|
arrayRemove: form.arrayRemove,
|
|
21
35
|
arrayMove: form.arrayMove,
|
|
22
|
-
arraySwap: form.arraySwap
|
|
36
|
+
arraySwap: form.arraySwap,
|
|
37
|
+
setErrors: form.setErrors,
|
|
38
|
+
clearErrors: form.clearErrors
|
|
23
39
|
};
|
|
24
40
|
}
|
|
25
41
|
function useVueFormPath(form, path) {
|
|
26
42
|
const value = shallowRef(form.get(unref(path)));
|
|
27
|
-
const fieldState = shallowRef(
|
|
43
|
+
const fieldState = shallowRef(
|
|
44
|
+
null
|
|
45
|
+
);
|
|
28
46
|
let unsubscribe = form.subscribeToPath(unref(path), (v, fs) => {
|
|
29
47
|
value.value = v;
|
|
30
48
|
fieldState.value = fs;
|
|
@@ -41,7 +59,7 @@ function useVueFormPath(form, path) {
|
|
|
41
59
|
});
|
|
42
60
|
}
|
|
43
61
|
);
|
|
44
|
-
onUnmounted(() => unsubscribe());
|
|
62
|
+
if (getCurrentInstance()) onUnmounted(() => unsubscribe());
|
|
45
63
|
return { value: readonly(value), fieldState: readonly(fieldState) };
|
|
46
64
|
}
|
|
47
65
|
export {
|