@aienpah/nanoform 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 @@
1
+ just read me
package/dist/index.cjs ADDED
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ VR: () => rules_default,
24
+ useForm: () => useForm_default,
25
+ validateForm: () => validateForm
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/useForm.ts
30
+ var import_react = require("react");
31
+ function useForm(initialValues, validate) {
32
+ const valuesRef = (0, import_react.useRef)({ ...initialValues });
33
+ const formRef = (0, import_react.useRef)(null);
34
+ const [errors, setErrors] = (0, import_react.useState)();
35
+ const [isSubmitting, setIsSubmitting] = (0, import_react.useState)(false);
36
+ function handleChange(e) {
37
+ const { name, value } = e.target;
38
+ valuesRef.current[name] = value;
39
+ }
40
+ function handleSubmit(onSubmit) {
41
+ return async (e) => {
42
+ e.preventDefault();
43
+ const currentValues = { ...valuesRef.current };
44
+ if (validate) {
45
+ const validateErrors = validate(currentValues);
46
+ setErrors(validateErrors);
47
+ if (Object.keys(validateErrors).length > 0) return;
48
+ }
49
+ try {
50
+ setIsSubmitting(true);
51
+ await onSubmit(currentValues);
52
+ } finally {
53
+ setIsSubmitting(false);
54
+ reset();
55
+ }
56
+ };
57
+ }
58
+ function reset() {
59
+ var _a;
60
+ valuesRef.current = { ...initialValues };
61
+ setErrors(void 0);
62
+ (_a = formRef.current) == null ? void 0 : _a.reset();
63
+ }
64
+ return {
65
+ formRef,
66
+ values: initialValues,
67
+ errors,
68
+ isSubmitting,
69
+ handleChange,
70
+ handleSubmit
71
+ };
72
+ }
73
+ var useForm_default = useForm;
74
+
75
+ // src/validation/validateForm.ts
76
+ function validateForm(schema, values) {
77
+ return Object.keys(schema).reduce(
78
+ (errors, key) => {
79
+ const rules = schema[key];
80
+ const value = values[key];
81
+ const error = rules.map((rule) => rule(value)).find(Boolean);
82
+ if (error) {
83
+ errors[key] = error;
84
+ }
85
+ return errors;
86
+ },
87
+ {}
88
+ );
89
+ }
90
+
91
+ // src/validation/rules.ts
92
+ var VALIDATION_RULES = {
93
+ required: (message) => (val) => !val ? message || "\u0627\u06CC\u0646 \u0641\u06CC\u0644\u062F \u0636\u0631\u0648\u0631\u06CC \u0645\u06CC\u200C\u0628\u0627\u0634\u062F." : null,
94
+ phoneNumber: (message) => (val) => !/^[0-9]{10,15}$/.test(val) ? message || "\u0634\u0645\u0627\u0631\u0647\u200C\u062A\u0644\u0641\u0646 \u0645\u0639\u062A\u0628\u0631 \u0646\u0645\u06CC\u200C\u0628\u0627\u0634\u062F." : null,
95
+ email: (message) => (val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val) || !val ? null : message || "\u0622\u062F\u0631\u0633 \u0627\u06CC\u0645\u06CC\u0644 \u0645\u0639\u062A\u0628\u0631 \u0646\u0645\u06CC\u200C\u0628\u0627\u0634\u062F."
96
+ };
97
+ var rules_default = VALIDATION_RULES;
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ VR,
101
+ useForm,
102
+ validateForm
103
+ });
@@ -0,0 +1,27 @@
1
+ import * as react from 'react';
2
+ import { ChangeEvent, FormEvent } from 'react';
3
+
4
+ type TErrors<T> = Partial<Record<keyof T, string>>;
5
+ declare function useForm<T extends Record<string, string>>(initialValues: T, validate?: (values: T) => TErrors<T>): {
6
+ formRef: react.RefObject<HTMLFormElement | null>;
7
+ values: T;
8
+ errors: Partial<Record<keyof T, string>> | undefined;
9
+ isSubmitting: boolean;
10
+ handleChange: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
11
+ handleSubmit: (onSubmit: (values: T) => Promise<void> | void) => (e: FormEvent) => Promise<void>;
12
+ };
13
+
14
+ type TValidationRule = (val: string) => string | null;
15
+ type TValidationSchema<T> = {
16
+ [K in keyof T]: TValidationRule[];
17
+ };
18
+
19
+ declare function validateForm<T extends Record<string, string>>(schema: TValidationSchema<T>, values: T): Partial<Record<keyof T, string>>;
20
+
21
+ declare const VALIDATION_RULES: {
22
+ required: (message?: string) => TValidationRule;
23
+ phoneNumber: (message?: string) => TValidationRule;
24
+ email: (message?: string) => TValidationRule;
25
+ };
26
+
27
+ export { type TValidationSchema, VALIDATION_RULES as VR, useForm, validateForm };
@@ -0,0 +1,27 @@
1
+ import * as react from 'react';
2
+ import { ChangeEvent, FormEvent } from 'react';
3
+
4
+ type TErrors<T> = Partial<Record<keyof T, string>>;
5
+ declare function useForm<T extends Record<string, string>>(initialValues: T, validate?: (values: T) => TErrors<T>): {
6
+ formRef: react.RefObject<HTMLFormElement | null>;
7
+ values: T;
8
+ errors: Partial<Record<keyof T, string>> | undefined;
9
+ isSubmitting: boolean;
10
+ handleChange: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
11
+ handleSubmit: (onSubmit: (values: T) => Promise<void> | void) => (e: FormEvent) => Promise<void>;
12
+ };
13
+
14
+ type TValidationRule = (val: string) => string | null;
15
+ type TValidationSchema<T> = {
16
+ [K in keyof T]: TValidationRule[];
17
+ };
18
+
19
+ declare function validateForm<T extends Record<string, string>>(schema: TValidationSchema<T>, values: T): Partial<Record<keyof T, string>>;
20
+
21
+ declare const VALIDATION_RULES: {
22
+ required: (message?: string) => TValidationRule;
23
+ phoneNumber: (message?: string) => TValidationRule;
24
+ email: (message?: string) => TValidationRule;
25
+ };
26
+
27
+ export { type TValidationSchema, VALIDATION_RULES as VR, useForm, validateForm };
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
1
+ // src/useForm.ts
2
+ import { useRef, useState } from "react";
3
+ function useForm(initialValues, validate) {
4
+ const valuesRef = useRef({ ...initialValues });
5
+ const formRef = useRef(null);
6
+ const [errors, setErrors] = useState();
7
+ const [isSubmitting, setIsSubmitting] = useState(false);
8
+ function handleChange(e) {
9
+ const { name, value } = e.target;
10
+ valuesRef.current[name] = value;
11
+ }
12
+ function handleSubmit(onSubmit) {
13
+ return async (e) => {
14
+ e.preventDefault();
15
+ const currentValues = { ...valuesRef.current };
16
+ if (validate) {
17
+ const validateErrors = validate(currentValues);
18
+ setErrors(validateErrors);
19
+ if (Object.keys(validateErrors).length > 0) return;
20
+ }
21
+ try {
22
+ setIsSubmitting(true);
23
+ await onSubmit(currentValues);
24
+ } finally {
25
+ setIsSubmitting(false);
26
+ reset();
27
+ }
28
+ };
29
+ }
30
+ function reset() {
31
+ var _a;
32
+ valuesRef.current = { ...initialValues };
33
+ setErrors(void 0);
34
+ (_a = formRef.current) == null ? void 0 : _a.reset();
35
+ }
36
+ return {
37
+ formRef,
38
+ values: initialValues,
39
+ errors,
40
+ isSubmitting,
41
+ handleChange,
42
+ handleSubmit
43
+ };
44
+ }
45
+ var useForm_default = useForm;
46
+
47
+ // src/validation/validateForm.ts
48
+ function validateForm(schema, values) {
49
+ return Object.keys(schema).reduce(
50
+ (errors, key) => {
51
+ const rules = schema[key];
52
+ const value = values[key];
53
+ const error = rules.map((rule) => rule(value)).find(Boolean);
54
+ if (error) {
55
+ errors[key] = error;
56
+ }
57
+ return errors;
58
+ },
59
+ {}
60
+ );
61
+ }
62
+
63
+ // src/validation/rules.ts
64
+ var VALIDATION_RULES = {
65
+ required: (message) => (val) => !val ? message || "\u0627\u06CC\u0646 \u0641\u06CC\u0644\u062F \u0636\u0631\u0648\u0631\u06CC \u0645\u06CC\u200C\u0628\u0627\u0634\u062F." : null,
66
+ phoneNumber: (message) => (val) => !/^[0-9]{10,15}$/.test(val) ? message || "\u0634\u0645\u0627\u0631\u0647\u200C\u062A\u0644\u0641\u0646 \u0645\u0639\u062A\u0628\u0631 \u0646\u0645\u06CC\u200C\u0628\u0627\u0634\u062F." : null,
67
+ email: (message) => (val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val) || !val ? null : message || "\u0622\u062F\u0631\u0633 \u0627\u06CC\u0645\u06CC\u0644 \u0645\u0639\u062A\u0628\u0631 \u0646\u0645\u06CC\u200C\u0628\u0627\u0634\u062F."
68
+ };
69
+ var rules_default = VALIDATION_RULES;
70
+ export {
71
+ rules_default as VR,
72
+ useForm_default as useForm,
73
+ validateForm
74
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@aienpah/nanoform",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "build": "tsup src/index.ts --dts --format esm,cjs"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "type": "module",
13
+ "main": "dist/index.cjs",
14
+ "module": "dist/index.js",
15
+ "types": "dist/index.d.ts",
16
+ "sideEffects": false,
17
+ "keywords": [],
18
+ "author": "Aien Pahlevan",
19
+ "license": "MIT",
20
+ "peerDependencies": {
21
+ "react": ">=18"
22
+ },
23
+ "devDependencies": {
24
+ "@types/react": "^19.2.14",
25
+ "tsup": "^8.5.1",
26
+ "typescript": "^5.9.3"
27
+ }
28
+ }