@ilokesto/form 1.0.1 → 1.0.2
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/core/index.d.ts +1 -1
- package/dist/core/types.d.ts +7 -0
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.js +28 -24
- package/dist/react/types.d.ts +3 -1
- package/dist/react/useFieldState.d.ts +3 -0
- package/package.json +1 -1
package/dist/core/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { CreateForm } from './form/index.js';
|
|
2
|
-
export type { CreateFormOptions, FieldSchemaOptions, FieldPathInput, FieldState, Form, FormArray, FormError, FormState, ResetOptions, SetValueOptions, StandardSchemaV1, ValidationTrigger } from './types.js';
|
|
2
|
+
export type { CreateFormOptions, FieldSchemaOptions, FieldPathValue, FieldPathInput, FieldState, Form, FormArray, FormError, FormState, ResetOptions, SetValueOptions, StandardSchemaV1, ValidationTrigger } from './types.js';
|
package/dist/core/types.d.ts
CHANGED
|
@@ -47,6 +47,12 @@ export type FieldState<TValue = unknown> = {
|
|
|
47
47
|
dirty: boolean;
|
|
48
48
|
modified: boolean;
|
|
49
49
|
};
|
|
50
|
+
export type FieldPathValue<TValues, TName extends FieldPathInput> = TName extends string ? StringFieldValue<TValues, TName> : TName extends FieldPath ? TuplePathValue<TValues, TName> : unknown;
|
|
51
|
+
type StringFieldValue<TValues, TName extends string> = TName extends keyof NonNullable<TValues> ? NonNullable<TValues>[TName] | MissingContainer<TValues> : unknown;
|
|
52
|
+
type TuplePathValue<TValue, TPath extends FieldPath> = TPath extends readonly [] ? TValue : TPath extends readonly [infer THead, ...infer TRest] ? THead extends FieldPathSegment ? TRest extends FieldPath ? SegmentValue<TValue, THead, TRest> : unknown : unknown : unknown;
|
|
53
|
+
type SegmentValue<TValue, THead extends FieldPathSegment, TRest extends FieldPath> = NonNullable<TValue> extends readonly unknown[] ? THead extends number ? TuplePathValue<ArrayElement<NonNullable<TValue>, THead>, TRest> | MissingContainer<TValue> : unknown : THead extends keyof NonNullable<TValue> ? TuplePathValue<NonNullable<TValue>[THead], TRest> | MissingContainer<TValue> : unknown;
|
|
54
|
+
type ArrayElement<TArray, TIndex extends number> = TArray extends readonly unknown[] ? TIndex extends keyof TArray ? TArray[TIndex] : TArray[number] : unknown;
|
|
55
|
+
type MissingContainer<TValue> = Extract<TValue, null | undefined> extends never ? never : undefined;
|
|
50
56
|
export type ArrayKeys = Record<PathKey, string[]>;
|
|
51
57
|
export type FormState<TValues> = {
|
|
52
58
|
defaultValues: TValues;
|
|
@@ -103,3 +109,4 @@ export type SetValueOptions = {
|
|
|
103
109
|
source?: 'user' | 'program';
|
|
104
110
|
validate?: boolean;
|
|
105
111
|
};
|
|
112
|
+
export {};
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { useForm } from './useForm.js';
|
|
2
|
-
export type { ReactFormOptions, RegisterOptions, } from './types.js';
|
|
2
|
+
export type { ReactFormOptions, RegisterOptions, UseFieldStateReturn, } from './types.js';
|
package/dist/react/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { useEffect as
|
|
2
|
-
import { c as
|
|
1
|
+
import { useEffect as F, useSyncExternalStore as R, useMemo as i, useCallback as b, useRef as v } from "react";
|
|
2
|
+
import { c as O, f as g, g as S, b as A, a as W, i as k, d as E } from "../FormStateSummary-gmEQdNXj.js";
|
|
3
3
|
function f(e, n, r, s) {
|
|
4
|
-
const t =
|
|
4
|
+
const t = O(e, n, () => r, s);
|
|
5
5
|
return {
|
|
6
6
|
name: t.name,
|
|
7
7
|
type: t.type,
|
|
@@ -20,30 +20,30 @@ function f(e, n, r, s) {
|
|
|
20
20
|
}
|
|
21
21
|
function N(e, n) {
|
|
22
22
|
const { name: r, schema: s, schemaOptions: t } = n;
|
|
23
|
-
|
|
23
|
+
F(() => {
|
|
24
24
|
if (s)
|
|
25
25
|
return e.registerFieldSchema(r, { schema: s, schemaOptions: t });
|
|
26
26
|
}, [e, r, s, t]);
|
|
27
27
|
}
|
|
28
28
|
function V(e, n) {
|
|
29
29
|
const r = n.map((s) => `${g(s.name)}:${s.schema ? "schema" : "none"}`).join("|");
|
|
30
|
-
|
|
30
|
+
F(() => {
|
|
31
31
|
const s = n.flatMap((t) => t.schema ? [e.registerFieldSchema(t.name, { schema: t.schema, schemaOptions: t.schemaOptions })] : []);
|
|
32
32
|
return () => {
|
|
33
33
|
s.forEach((t) => t());
|
|
34
34
|
};
|
|
35
35
|
}, [e, n, r]);
|
|
36
36
|
}
|
|
37
|
-
function
|
|
38
|
-
return
|
|
37
|
+
function m(e) {
|
|
38
|
+
return R(
|
|
39
39
|
(n) => e.subscribe(n),
|
|
40
40
|
() => e.getState(),
|
|
41
41
|
() => e.getState()
|
|
42
42
|
);
|
|
43
43
|
}
|
|
44
|
-
function
|
|
44
|
+
function x(e, n) {
|
|
45
45
|
N(e, n);
|
|
46
|
-
const r =
|
|
46
|
+
const r = m(e), s = S(e, r, n.name), t = g(n.name), a = i(() => f(e, n, s, t), [e, n, s, t]), c = b((u) => {
|
|
47
47
|
e.setValue(n.name, u, { source: "program" });
|
|
48
48
|
}, [e, n.name]);
|
|
49
49
|
return i(() => ({
|
|
@@ -55,33 +55,37 @@ function W(e, n) {
|
|
|
55
55
|
touched: s.touched
|
|
56
56
|
}), [s, a, c]);
|
|
57
57
|
}
|
|
58
|
-
function
|
|
59
|
-
const
|
|
60
|
-
return
|
|
58
|
+
function B(e, n) {
|
|
59
|
+
const r = m(e);
|
|
60
|
+
return S(e, r, n);
|
|
61
61
|
}
|
|
62
|
-
function
|
|
63
|
-
const
|
|
62
|
+
function C(e) {
|
|
63
|
+
const n = m(e);
|
|
64
|
+
return i(() => A(n), [n]);
|
|
65
|
+
}
|
|
66
|
+
function D(e, n, ...r) {
|
|
67
|
+
const s = Array.isArray(n) || r.length > 0, t = M(n, r);
|
|
64
68
|
V(e, t);
|
|
65
|
-
const a =
|
|
69
|
+
const a = m(e), c = i(
|
|
66
70
|
() => t.map((u) => {
|
|
67
|
-
const
|
|
68
|
-
return f(e, u,
|
|
71
|
+
const l = S(e, a, u.name), d = g(u.name);
|
|
72
|
+
return f(e, u, l, d);
|
|
69
73
|
}),
|
|
70
74
|
[e, t, a]
|
|
71
75
|
);
|
|
72
76
|
return s ? c : c[0];
|
|
73
77
|
}
|
|
74
|
-
function
|
|
78
|
+
function M(e, n) {
|
|
75
79
|
return Array.isArray(e) ? e : n.length > 0 ? [e, ...n] : [e];
|
|
76
80
|
}
|
|
77
|
-
function
|
|
78
|
-
const n =
|
|
79
|
-
|
|
81
|
+
function $(e) {
|
|
82
|
+
const n = v(void 0), r = v(void 0), s = k(e), t = s ? e : n.current ??= W(e), a = s ? void 0 : e.values, c = s ? void 0 : e.resetOptions;
|
|
83
|
+
F(() => {
|
|
80
84
|
a === void 0 || r.current === a || (r.current = a, t.reset(a, c));
|
|
81
85
|
}, [t, c, a]);
|
|
82
|
-
const u = ((o, ...
|
|
83
|
-
return i(() => ({ form: t, useRegister: u, useField:
|
|
86
|
+
const u = ((o, ...h) => D(t, o, ...h)), l = ((o) => x(t, o)), d = ((o) => B(t, o)), p = () => C(t), y = ((o, h) => E(t, o, h));
|
|
87
|
+
return i(() => ({ form: t, useRegister: u, useField: l, useFieldState: d, useFormState: p, handleSubmit: y }), [t]);
|
|
84
88
|
}
|
|
85
89
|
export {
|
|
86
|
-
|
|
90
|
+
$ as useForm
|
|
87
91
|
};
|
package/dist/react/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ChangeEventHandler, FocusEventHandler, InputHTMLAttributes, SelectHTMLAttributes, TextareaHTMLAttributes, FormEvent } from 'react';
|
|
2
|
-
import type { CreateFormOptions, Form, FormError, FormState, ResetOptions } from '../core/index.js';
|
|
2
|
+
import type { CreateFormOptions, FieldPathInput, FieldPathValue, FieldState, Form, FormError, FormState, ResetOptions } from '../core/index.js';
|
|
3
3
|
import type { RegisterOptions, SubmitHandler, SubmitInvalidHandler, SubmitValidHandler } from '../adapters/dom.js';
|
|
4
4
|
export type { RegisterOptions } from '../adapters/dom.js';
|
|
5
5
|
export type ReactFormOptions<TValues> = CreateFormOptions<TValues> & {
|
|
@@ -45,6 +45,7 @@ export type UseFieldReturn<TProps extends RegisterProps = InputRegisterProps> =
|
|
|
45
45
|
dirty: boolean;
|
|
46
46
|
touched: boolean;
|
|
47
47
|
};
|
|
48
|
+
export type UseFieldStateReturn<TValues, TName extends FieldPathInput> = Readonly<FieldState<FieldPathValue<TValues, TName>>>;
|
|
48
49
|
export type UseFormStateReturn<TValues> = {
|
|
49
50
|
state: Readonly<FormState<TValues>>;
|
|
50
51
|
errors: Record<string, FormError[]>;
|
|
@@ -66,6 +67,7 @@ export type ReactForm<TValues> = {
|
|
|
66
67
|
useRegister<TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(options: TOptions): RegisterPropsList<TElement, TOptions>;
|
|
67
68
|
useRegister<TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(...options: TOptions): RegisterPropsList<TElement, TOptions>;
|
|
68
69
|
useField<TElement extends RegisterElement = HTMLInputElement>(options: RegisterOptions): UseFieldReturn<RegisterPropsForElement<TElement>>;
|
|
70
|
+
useFieldState<const TName extends FieldPathInput>(name: TName): UseFieldStateReturn<TValues, TName>;
|
|
69
71
|
useFormState(): UseFormStateReturn<TValues>;
|
|
70
72
|
handleSubmit<TResult>(onValid: SubmitValidHandler<TValues, TResult>, onInvalid?: SubmitInvalidHandler): SubmitHandler<FormEvent<HTMLFormElement>, TResult>;
|
|
71
73
|
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { FieldPathInput, Form } from '../core/index.js';
|
|
2
|
+
import type { UseFieldStateReturn } from './types.js';
|
|
3
|
+
export declare function useFieldStateWithForm<TValues, const TName extends FieldPathInput>(form: Form<TValues>, name: TName): UseFieldStateReturn<TValues, TName>;
|