@bbodek/hooks 0.0.1
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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react"),d=typeof window<"u"?n.useLayoutEffect:n.useEffect,y=({initialValues:c,validate:o})=>{const[s,a]=n.useState(c),[f,u]=n.useState({});return{values:s,setValues:t=>{const e=typeof t=="function"?t(s):t;if(a(e),o){const r=o(e);u(r)}},errors:f,handleChange:t=>{const{name:e,value:r}=t.target;if(e in c&&(a({...s,[e]:r}),o)){const i=o({...s,[e]:r});u(i)}},setErrors:u}};exports.useForm=y;exports.useIsomorphicLayoutEffect=d;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useLayoutEffect as d, useEffect as p, useState as u } from "react";
|
|
2
|
+
const y = typeof window < "u" ? d : p, g = ({
|
|
3
|
+
initialValues: c,
|
|
4
|
+
validate: e
|
|
5
|
+
}) => {
|
|
6
|
+
const [s, f] = u(c), [a, r] = u({});
|
|
7
|
+
return {
|
|
8
|
+
values: s,
|
|
9
|
+
setValues: (o) => {
|
|
10
|
+
const t = typeof o == "function" ? o(s) : o;
|
|
11
|
+
if (f(t), e) {
|
|
12
|
+
const n = e(t);
|
|
13
|
+
r(n);
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
errors: a,
|
|
17
|
+
handleChange: (o) => {
|
|
18
|
+
const { name: t, value: n } = o.target;
|
|
19
|
+
if (t in c && (f({ ...s, [t]: n }), e)) {
|
|
20
|
+
const i = e({
|
|
21
|
+
...s,
|
|
22
|
+
[t]: n
|
|
23
|
+
});
|
|
24
|
+
r(i);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
setErrors: r
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export {
|
|
31
|
+
g as useForm,
|
|
32
|
+
y as useIsomorphicLayoutEffect
|
|
33
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ChangeEvent, Dispatch, SetStateAction } from "react";
|
|
2
|
+
export type ValueKey<T> = keyof T & string;
|
|
3
|
+
export type UseFormErrors = {
|
|
4
|
+
[key: string]: string;
|
|
5
|
+
};
|
|
6
|
+
export interface Props<T> {
|
|
7
|
+
initialValues: T;
|
|
8
|
+
validate?: (fields: T) => UseFormErrors;
|
|
9
|
+
}
|
|
10
|
+
export interface UseFormHandleChangeParams {
|
|
11
|
+
event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>;
|
|
12
|
+
}
|
|
13
|
+
export interface UseFormReturnType<T> {
|
|
14
|
+
values: T;
|
|
15
|
+
setValues: Dispatch<SetStateAction<T>>;
|
|
16
|
+
errors: UseFormErrors;
|
|
17
|
+
handleChange: (event: UseFormHandleChangeParams["event"]) => void;
|
|
18
|
+
setErrors: Dispatch<SetStateAction<UseFormErrors>>;
|
|
19
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bbodek/hooks",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"main": "dist/dotoli-hooks.cjs.js",
|
|
9
|
+
"module": "dist/dotoli-hooks.es.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"require": "./dist/dotoli-hooks.cjs.js",
|
|
14
|
+
"import": "./dist/dotoli-hooks.es.js",
|
|
15
|
+
"types": "./dist/index.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "vite",
|
|
20
|
+
"build": "vite build && tsc --project tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|
|
21
|
+
"lint": "eslint .",
|
|
22
|
+
"lint:fix": "eslint . --ext ts,tsx --report-unused-disable-directives --fix",
|
|
23
|
+
"preview": "vite preview",
|
|
24
|
+
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css,html}\""
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"react": "^18.3.1",
|
|
28
|
+
"react-dom": "^18.3.1"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@dotoli/eslint-config": "workspace:*",
|
|
32
|
+
"@dotoli/prettier-config": "workspace:*",
|
|
33
|
+
"@dotoli/typescript-config": "workspace:*",
|
|
34
|
+
"@eslint/js": "^9.13.0",
|
|
35
|
+
"@types/node": "^22.9.0",
|
|
36
|
+
"@types/react": "^18.3.12",
|
|
37
|
+
"@types/react-dom": "^18.3.1",
|
|
38
|
+
"@typescript-eslint/parser": "^7.1.0",
|
|
39
|
+
"@vitejs/plugin-react-swc": "^3.5.0",
|
|
40
|
+
"eslint": "^9.13.0",
|
|
41
|
+
"eslint-config-prettier": "^9.1.0",
|
|
42
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
43
|
+
"eslint-plugin-react": "^7.37.2",
|
|
44
|
+
"eslint-plugin-react-hooks": "^5.0.0",
|
|
45
|
+
"eslint-plugin-react-refresh": "^0.4.14",
|
|
46
|
+
"globals": "^15.11.0",
|
|
47
|
+
"typescript": "~5.6.2",
|
|
48
|
+
"typescript-eslint": "^8.11.0",
|
|
49
|
+
"vite": "^5.4.10",
|
|
50
|
+
"vite-tsconfig-paths": "^5.1.1"
|
|
51
|
+
},
|
|
52
|
+
"prettier": "@dotoli/prettier-config"
|
|
53
|
+
}
|