solid-hook-form 1.0.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 ADDED
@@ -0,0 +1,13 @@
1
+ # solid-hook-form
2
+
3
+ ```tsx
4
+ import { useForm } from "solid-hook-form";
5
+
6
+ const { errors, isValid, register } = useForm({
7
+ defaultValues: {
8
+ email: "",
9
+ },
10
+ });
11
+
12
+ register("email", { required: true });
13
+ ```
package/dist/main.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { useForm } from './use_form';
package/dist/main.js ADDED
@@ -0,0 +1,213 @@
1
+ const _ = (t, s) => t === s, q = {
2
+ equals: _
3
+ };
4
+ let G = j;
5
+ const g = 1, w = 2;
6
+ var b = null;
7
+ let V = null, P = null, i = null, o = null, h = null, A = 0;
8
+ function x(t, s) {
9
+ s = s ? Object.assign({}, q, s) : q;
10
+ const e = {
11
+ value: t,
12
+ observers: null,
13
+ observerSlots: null,
14
+ comparator: s.equals || void 0
15
+ }, r = (u) => (typeof u == "function" && (u = u(e.value)), D(e, u));
16
+ return [z.bind(e), r];
17
+ }
18
+ function Q(t) {
19
+ if (i === null) return t();
20
+ const s = i;
21
+ i = null;
22
+ try {
23
+ return t();
24
+ } finally {
25
+ i = s;
26
+ }
27
+ }
28
+ function z() {
29
+ if (this.sources && this.state)
30
+ if (this.state === g) F(this);
31
+ else {
32
+ const t = o;
33
+ o = null, O(() => S(this)), o = t;
34
+ }
35
+ if (i) {
36
+ const t = this.observers ? this.observers.length : 0;
37
+ i.sources ? (i.sources.push(this), i.sourceSlots.push(t)) : (i.sources = [this], i.sourceSlots = [t]), this.observers ? (this.observers.push(i), this.observerSlots.push(i.sources.length - 1)) : (this.observers = [i], this.observerSlots = [i.sources.length - 1]);
38
+ }
39
+ return this.value;
40
+ }
41
+ function D(t, s, e) {
42
+ let r = t.value;
43
+ return (!t.comparator || !t.comparator(r, s)) && (t.value = s, t.observers && t.observers.length && O(() => {
44
+ for (let u = 0; u < t.observers.length; u += 1) {
45
+ const l = t.observers[u], f = V && V.running;
46
+ f && V.disposed.has(l), (f ? !l.tState : !l.state) && (l.pure ? o.push(l) : h.push(l), l.observers && T(l)), f || (l.state = g);
47
+ }
48
+ if (o.length > 1e6)
49
+ throw o = [], new Error();
50
+ })), s;
51
+ }
52
+ function F(t) {
53
+ if (!t.fn) return;
54
+ E(t);
55
+ const s = A;
56
+ B(
57
+ t,
58
+ t.value,
59
+ s
60
+ );
61
+ }
62
+ function B(t, s, e) {
63
+ let r;
64
+ const u = b, l = i;
65
+ i = b = t;
66
+ try {
67
+ r = t.fn(s);
68
+ } catch (f) {
69
+ return t.pure && (t.state = g, t.owned && t.owned.forEach(E), t.owned = null), t.updatedAt = e + 1, N(f);
70
+ } finally {
71
+ i = l, b = u;
72
+ }
73
+ (!t.updatedAt || t.updatedAt <= e) && (t.updatedAt != null && "observers" in t ? D(t, r) : t.value = r, t.updatedAt = e);
74
+ }
75
+ function U(t) {
76
+ if (t.state === 0) return;
77
+ if (t.state === w) return S(t);
78
+ if (t.suspense && Q(t.suspense.inFallback)) return t.suspense.effects.push(t);
79
+ const s = [t];
80
+ for (; (t = t.owner) && (!t.updatedAt || t.updatedAt < A); )
81
+ t.state && s.push(t);
82
+ for (let e = s.length - 1; e >= 0; e--)
83
+ if (t = s[e], t.state === g)
84
+ F(t);
85
+ else if (t.state === w) {
86
+ const r = o;
87
+ o = null, O(() => S(t, s[0])), o = r;
88
+ }
89
+ }
90
+ function O(t, s) {
91
+ if (o) return t();
92
+ let e = !1;
93
+ o = [], h ? e = !0 : h = [], A++;
94
+ try {
95
+ const r = t();
96
+ return H(e), r;
97
+ } catch (r) {
98
+ e || (h = null), o = null, N(r);
99
+ }
100
+ }
101
+ function H(t) {
102
+ if (o && (j(o), o = null), t) return;
103
+ const s = h;
104
+ h = null, s.length && O(() => G(s));
105
+ }
106
+ function j(t) {
107
+ for (let s = 0; s < t.length; s++) U(t[s]);
108
+ }
109
+ function S(t, s) {
110
+ t.state = 0;
111
+ for (let e = 0; e < t.sources.length; e += 1) {
112
+ const r = t.sources[e];
113
+ if (r.sources) {
114
+ const u = r.state;
115
+ u === g ? r !== s && (!r.updatedAt || r.updatedAt < A) && U(r) : u === w && S(r, s);
116
+ }
117
+ }
118
+ }
119
+ function T(t) {
120
+ for (let s = 0; s < t.observers.length; s += 1) {
121
+ const e = t.observers[s];
122
+ e.state || (e.state = w, e.pure ? o.push(e) : h.push(e), e.observers && T(e));
123
+ }
124
+ }
125
+ function E(t) {
126
+ let s;
127
+ if (t.sources)
128
+ for (; t.sources.length; ) {
129
+ const e = t.sources.pop(), r = t.sourceSlots.pop(), u = e.observers;
130
+ if (u && u.length) {
131
+ const l = u.pop(), f = e.observerSlots.pop();
132
+ r < u.length && (l.sourceSlots[f] = r, u[r] = l, e.observerSlots[r] = f);
133
+ }
134
+ }
135
+ if (t.tOwned) {
136
+ for (s = t.tOwned.length - 1; s >= 0; s--) E(t.tOwned[s]);
137
+ delete t.tOwned;
138
+ }
139
+ if (t.owned) {
140
+ for (s = t.owned.length - 1; s >= 0; s--) E(t.owned[s]);
141
+ t.owned = null;
142
+ }
143
+ if (t.cleanups) {
144
+ for (s = t.cleanups.length - 1; s >= 0; s--) t.cleanups[s]();
145
+ t.cleanups = null;
146
+ }
147
+ t.state = 0;
148
+ }
149
+ function J(t) {
150
+ return t instanceof Error ? t : new Error(typeof t == "string" ? t : "Unknown error", {
151
+ cause: t
152
+ });
153
+ }
154
+ function N(t, s = b) {
155
+ throw J(t);
156
+ }
157
+ const X = ({
158
+ defaultValues: t,
159
+ mode: s = "onInput"
160
+ }) => {
161
+ const e = t, r = {}, u = {}, [l, f] = x({}), [k, C] = x(!0), y = (n) => {
162
+ const a = r[n], c = u[n];
163
+ c != null && c.required && !(a != null && a.value) ? f((p) => {
164
+ const v = { message: "Required" };
165
+ return { ...p, [n]: v };
166
+ }) : f((p) => {
167
+ const v = { ...p };
168
+ return delete v[n], v;
169
+ }), C(!Object.keys(l()).length);
170
+ }, L = () => {
171
+ Object.keys(e).forEach((n) => {
172
+ y(n);
173
+ });
174
+ }, R = (n, a) => (a && (u[n] = { required: a.required }), {
175
+ name: n,
176
+ value: e[n],
177
+ onInput(c) {
178
+ if (s === "onInput") {
179
+ const p = c.target.value;
180
+ e[n] = p, y(n);
181
+ }
182
+ },
183
+ onChange(c) {
184
+ if (s === "onChange") {
185
+ const p = c.target.value;
186
+ e[n] = p, y(n);
187
+ }
188
+ },
189
+ ref: (c) => {
190
+ r[n] || (r[n] = c, c && (c.value = e[n]));
191
+ }
192
+ }), I = (n) => n ? e[n] : e;
193
+ return {
194
+ errors: l,
195
+ isValid: k,
196
+ register: R,
197
+ getValues: I,
198
+ setValue: (n, a) => {
199
+ e[n] = a;
200
+ const c = r[n];
201
+ c && (c.value = a);
202
+ },
203
+ onSubmit: (n) => (a) => {
204
+ a.preventDefault(), L(), k() && n(I());
205
+ },
206
+ reset: (n) => {
207
+ Object.assign(e, n || t), f({}), C(!0);
208
+ }
209
+ };
210
+ };
211
+ export {
212
+ X as useForm
213
+ };
@@ -0,0 +1 @@
1
+ (function(g,v){typeof exports=="object"&&typeof module<"u"?v(exports):typeof define=="function"&&define.amd?define(["exports"],v):(g=typeof globalThis<"u"?globalThis:g||self,v(g["solid-hook-form"]={}))})(this,function(g){"use strict";const q={equals:(t,e)=>t===e};let L=j;const b=1,w=2;var S=null;let d=null,P=null,i=null,o=null,h=null,E=0;function C(t,e){e=e?Object.assign({},q,e):q;const s={value:t,observers:null,observerSlots:null,comparator:e.equals||void 0},r=u=>(typeof u=="function"&&(u=u(s.value)),F(s,u));return[_.bind(s),r]}function R(t){if(i===null)return t();const e=i;i=null;try{return t()}finally{i=e}}function _(){if(this.sources&&this.state)if(this.state===b)I(this);else{const t=o;o=null,y(()=>A(this)),o=t}if(i){const t=this.observers?this.observers.length:0;i.sources?(i.sources.push(this),i.sourceSlots.push(t)):(i.sources=[this],i.sourceSlots=[t]),this.observers?(this.observers.push(i),this.observerSlots.push(i.sources.length-1)):(this.observers=[i],this.observerSlots=[i.sources.length-1])}return this.value}function F(t,e,s){let r=t.value;return(!t.comparator||!t.comparator(r,e))&&(t.value=e,t.observers&&t.observers.length&&y(()=>{for(let u=0;u<t.observers.length;u+=1){const l=t.observers[u],f=d&&d.running;f&&d.disposed.has(l),(f?!l.tState:!l.state)&&(l.pure?o.push(l):h.push(l),l.observers&&x(l)),f||(l.state=b)}if(o.length>1e6)throw o=[],new Error})),e}function I(t){if(!t.fn)return;O(t);const e=E;G(t,t.value,e)}function G(t,e,s){let r;const u=S,l=i;i=S=t;try{r=t.fn(e)}catch(f){return t.pure&&(t.state=b,t.owned&&t.owned.forEach(O),t.owned=null),t.updatedAt=s+1,D(f)}finally{i=l,S=u}(!t.updatedAt||t.updatedAt<=s)&&(t.updatedAt!=null&&"observers"in t?F(t,r):t.value=r,t.updatedAt=s)}function T(t){if(t.state===0)return;if(t.state===w)return A(t);if(t.suspense&&R(t.suspense.inFallback))return t.suspense.effects.push(t);const e=[t];for(;(t=t.owner)&&(!t.updatedAt||t.updatedAt<E);)t.state&&e.push(t);for(let s=e.length-1;s>=0;s--)if(t=e[s],t.state===b)I(t);else if(t.state===w){const r=o;o=null,y(()=>A(t,e[0])),o=r}}function y(t,e){if(o)return t();let s=!1;o=[],h?s=!0:h=[],E++;try{const r=t();return M(s),r}catch(r){s||(h=null),o=null,D(r)}}function M(t){if(o&&(j(o),o=null),t)return;const e=h;h=null,e.length&&y(()=>L(e))}function j(t){for(let e=0;e<t.length;e++)T(t[e])}function A(t,e){t.state=0;for(let s=0;s<t.sources.length;s+=1){const r=t.sources[s];if(r.sources){const u=r.state;u===b?r!==e&&(!r.updatedAt||r.updatedAt<E)&&T(r):u===w&&A(r,e)}}}function x(t){for(let e=0;e<t.observers.length;e+=1){const s=t.observers[e];s.state||(s.state=w,s.pure?o.push(s):h.push(s),s.observers&&x(s))}}function O(t){let e;if(t.sources)for(;t.sources.length;){const s=t.sources.pop(),r=t.sourceSlots.pop(),u=s.observers;if(u&&u.length){const l=u.pop(),f=s.observerSlots.pop();r<u.length&&(l.sourceSlots[f]=r,u[r]=l,s.observerSlots[r]=f)}}if(t.tOwned){for(e=t.tOwned.length-1;e>=0;e--)O(t.tOwned[e]);delete t.tOwned}if(t.owned){for(e=t.owned.length-1;e>=0;e--)O(t.owned[e]);t.owned=null}if(t.cleanups){for(e=t.cleanups.length-1;e>=0;e--)t.cleanups[e]();t.cleanups=null}t.state=0}function Q(t){return t instanceof Error?t:new Error(typeof t=="string"?t:"Unknown error",{cause:t})}function D(t,e=S){throw Q(t)}const z=({defaultValues:t,mode:e="onInput"})=>{const s=t,r={},u={},[l,f]=C({}),[U,m]=C(!0),k=n=>{const a=r[n],c=u[n];c!=null&&c.required&&!(a!=null&&a.value)?f(p=>{const V={message:"Required"};return{...p,[n]:V}}):f(p=>{const V={...p};return delete V[n],V}),m(!Object.keys(l()).length)},B=()=>{Object.keys(s).forEach(n=>{k(n)})},H=(n,a)=>(a&&(u[n]={required:a.required}),{name:n,value:s[n],onInput(c){if(e==="onInput"){const p=c.target.value;s[n]=p,k(n)}},onChange(c){if(e==="onChange"){const p=c.target.value;s[n]=p,k(n)}},ref:c=>{r[n]||(r[n]=c,c&&(c.value=s[n]))}}),N=n=>n?s[n]:s;return{errors:l,isValid:U,register:H,getValues:N,setValue:(n,a)=>{s[n]=a;const c=r[n];c&&(c.value=a)},onSubmit:n=>a=>{a.preventDefault(),B(),U()&&n(N())},reset:n=>{Object.assign(s,n||t),f({}),m(!0)}}};g.useForm=z,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Type which can be used to index an array or tuple type.
3
+ */
4
+ export type ArrayKey = number;
5
+ /**
6
+ * Type which given a tuple type returns its own keys, i.e. only its indices.
7
+ * @typeParam T - tuple type
8
+ * @example
9
+ * ```
10
+ * TupleKeys<[number, string]> = '0' | '1'
11
+ * ```
12
+ */
13
+ export type TupleKeys<T extends ReadonlyArray<any>> = Exclude<keyof T, keyof any[]>;
14
+ /**
15
+ * Type to query whether an array type T is a tuple type.
16
+ * @typeParam T - type which may be an array or tuple
17
+ * @example
18
+ * ```
19
+ * IsTuple<[number]> = true
20
+ * IsTuple<number[]> = false
21
+ * ```
22
+ */
23
+ export type IsTuple<T extends ReadonlyArray<any>> = number extends T["length"] ? false : true;
@@ -0,0 +1,19 @@
1
+ import { FormValues } from './form';
2
+ import { BrowserNativeObject, IsAny, Merge } from './utils';
3
+ export type FieldError = {
4
+ message?: Message;
5
+ };
6
+ export type Message = string;
7
+ export type GlobalError = Partial<{
8
+ type: string | number;
9
+ message: Message;
10
+ }>;
11
+ export type DeepRequired<T> = T extends BrowserNativeObject | Blob ? T : {
12
+ [K in keyof T]-?: NonNullable<DeepRequired<T[K]>>;
13
+ };
14
+ export type FieldErrorsImpl<T extends FormValues = FormValues> = {
15
+ [K in keyof T]?: T[K] extends BrowserNativeObject | Blob ? FieldError : K extends "root" | `root.${string}` ? GlobalError : T[K] extends object ? Merge<FieldError, FieldErrorsImpl<T[K]>> : FieldError;
16
+ };
17
+ export type FieldErrors<T extends FormValues = FormValues> = Partial<FormValues extends IsAny<FormValues> ? any : FieldErrorsImpl<DeepRequired<T>>> & {
18
+ root?: Record<string, GlobalError> & GlobalError;
19
+ };
@@ -0,0 +1 @@
1
+ export type FormValues = Record<string, any>;
@@ -0,0 +1,23 @@
1
+ import { ArrayKey, IsTuple, TupleKeys } from './array';
2
+ import { BrowserNativeObject, IsEqual, Primitive } from './utils';
3
+ /**
4
+ * Helper function to break apart T1 and check if any are equal to T2
5
+ *
6
+ * See {@link IsEqual}
7
+ */
8
+ type AnyIsEqual<T1, T2> = T1 extends T2 ? (IsEqual<T1, T2> extends true ? true : never) : never;
9
+ /**
10
+ * Helper type for recursively constructing paths through a type.
11
+ * This actually constructs the strings and recurses into nested
12
+ * object types.
13
+ *
14
+ * See {@link Path}
15
+ */
16
+ type PathImpl<K extends string | number, V, TraversedTypes> = V extends Primitive | BrowserNativeObject ? `${K}` : true extends AnyIsEqual<TraversedTypes, V> ? `${K}` : `${K}` | `${K}.${PathInternal<V, TraversedTypes | V>}`;
17
+ type PathInternal<T, TraversedTypes = T> = T extends ReadonlyArray<infer V> ? IsTuple<T> extends true ? {
18
+ [K in TupleKeys<T>]-?: PathImpl<K & string, T[K], TraversedTypes>;
19
+ }[TupleKeys<T>] : PathImpl<ArrayKey, V, TraversedTypes> : {
20
+ [K in keyof T]-?: PathImpl<K & string, T[K], TraversedTypes>;
21
+ }[keyof T];
22
+ export type Path<T> = T extends any ? PathInternal<T> : never;
23
+ export {};
@@ -0,0 +1,30 @@
1
+ export type Primitive = null | undefined | string | number | boolean | symbol | bigint;
2
+ export type BrowserNativeObject = Date | FileList | File;
3
+ /**
4
+ * Checks whether T1 can be exactly (mutually) assigned to T2
5
+ * @typeParam T1 - type to check
6
+ * @typeParam T2 - type to check against
7
+ * ```
8
+ * IsEqual<string, string> = true
9
+ * IsEqual<'foo', 'foo'> = true
10
+ * IsEqual<string, number> = false
11
+ * IsEqual<string, number> = false
12
+ * IsEqual<string, 'foo'> = false
13
+ * IsEqual<'foo', string> = false
14
+ * IsEqual<'foo' | 'bar', 'foo'> = boolean // 'foo' is assignable, but 'bar' is not (true | false) -> boolean
15
+ * ```
16
+ */
17
+ export type IsEqual<T1, T2> = T1 extends T2 ? (<G>() => G extends T1 ? 1 : 2) extends <G>() => G extends T2 ? 1 : 2 ? true : false : false;
18
+ /**
19
+ * Checks whether the type is any
20
+ * See {@link https://stackoverflow.com/a/49928360/3406963}
21
+ * @typeParam T - type which may be any
22
+ * ```
23
+ * IsAny<any> = true
24
+ * IsAny<string> = false
25
+ * ```
26
+ */
27
+ export type IsAny<T> = 0 extends 1 & T ? true : false;
28
+ export type Merge<A, B> = {
29
+ [K in keyof A | keyof B]?: K extends keyof A & keyof B ? [A[K], B[K]] extends [object, object] ? Merge<A[K], B[K]> : A[K] | B[K] : K extends keyof A ? A[K] : K extends keyof B ? B[K] : never;
30
+ };
@@ -0,0 +1,27 @@
1
+ import { FormValues } from './types/form';
2
+ import { Path } from './types/path';
3
+ import { FieldErrors } from './types/errors';
4
+ type UseFormArg<T extends FormValues> = {
5
+ defaultValues: T;
6
+ mode?: "onInput" | "onChange" | "onSubmit";
7
+ };
8
+ type SubmitCallback<T extends FormValues> = (values: T) => void;
9
+ type Rules = {
10
+ required: boolean;
11
+ };
12
+ export declare const useForm: <T extends FormValues>({ defaultValues, mode, }: UseFormArg<T>) => {
13
+ errors: import('solid-js').Accessor<FieldErrors<T>>;
14
+ isValid: import('solid-js').Accessor<boolean>;
15
+ register: (name: Path<T>, options?: Rules) => {
16
+ name: Path<T>;
17
+ value: any;
18
+ onInput(event: InputEvent): void;
19
+ onChange(event: Event): void;
20
+ ref: (ref: HTMLInputElement | null) => void;
21
+ };
22
+ getValues: (name?: Path<T>) => any;
23
+ setValue: (name: Path<T>, value: any) => void;
24
+ onSubmit: (submit: SubmitCallback<T>) => (event: SubmitEvent) => void;
25
+ reset: (newDefaultValues?: Partial<T>) => void;
26
+ };
27
+ export {};
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "solid-hook-form",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "./dist/main.umd.cjs",
6
+ "module": "./dist/main.js",
7
+ "types": "./dist/main.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/main.js",
11
+ "require": "./dist/main.umd.cjs"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md"
17
+ ],
18
+ "peerDependencies": {
19
+ "solid-js": "^1.9.5"
20
+ },
21
+ "devDependencies": {
22
+ "react-hook-form": "^7.55.0",
23
+ "typescript": "~5.7.2",
24
+ "vite": "^6.0.11",
25
+ "vite-plugin-dts": "^4.5.0",
26
+ "vitest": "^3.0.5"
27
+ },
28
+ "scripts": {
29
+ "build": "tsc && vite build",
30
+ "test": "vitest run"
31
+ },
32
+ "author": "thorn_pear",
33
+ "license": "ISC",
34
+ "description": "Performant, flexible and extensible forms library for Solid",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/tatsmaki/solid-hook-form.git"
38
+ },
39
+ "keywords": [
40
+ "solid",
41
+ "hooks",
42
+ "form",
43
+ "forms",
44
+ "form-validation",
45
+ "validation",
46
+ "typescript",
47
+ "solid-hooks"
48
+ ]
49
+ }