@aienpah/nanoform 1.0.0 → 1.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/README.md CHANGED
@@ -1 +1,5 @@
1
- just read me
1
+ nanoform
2
+
3
+ A lightweight React form hook focused on performance.
4
+
5
+ nanoform stores field values in refs to avoid unnecessary re-renders while still supporting validation, async submissions, and form resets.
package/dist/index.cjs CHANGED
@@ -29,17 +29,32 @@ module.exports = __toCommonJS(index_exports);
29
29
  // src/useForm.ts
30
30
  var import_react = require("react");
31
31
  function useForm(initialValues, validate) {
32
+ const lastInitialValuesRef = (0, import_react.useRef)(initialValues);
32
33
  const valuesRef = (0, import_react.useRef)({ ...initialValues });
33
34
  const formRef = (0, import_react.useRef)(null);
34
35
  const [errors, setErrors] = (0, import_react.useState)();
36
+ const [values, setValues] = (0, import_react.useState)(initialValues);
35
37
  const [isSubmitting, setIsSubmitting] = (0, import_react.useState)(false);
36
- function handleChange(e) {
37
- const { name, value } = e.target;
38
+ (0, import_react.useEffect)(() => {
39
+ const updateValues = () => {
40
+ lastInitialValuesRef.current = initialValues;
41
+ valuesRef.current = initialValues;
42
+ setValues(initialValues);
43
+ };
44
+ if (JSON.stringify(lastInitialValuesRef.current) !== JSON.stringify(initialValues)) {
45
+ updateValues();
46
+ }
47
+ }, [initialValues]);
48
+ function handleChange(event) {
49
+ const { name, value } = event.target;
50
+ valuesRef.current[name] = value;
51
+ }
52
+ function handleSelectOption(name, value) {
38
53
  valuesRef.current[name] = value;
39
54
  }
40
55
  function handleSubmit(onSubmit) {
41
- return async (e) => {
42
- e.preventDefault();
56
+ return async (event) => {
57
+ event.preventDefault();
43
58
  const currentValues = { ...valuesRef.current };
44
59
  if (validate) {
45
60
  const validateErrors = validate(currentValues);
@@ -51,7 +66,6 @@ function useForm(initialValues, validate) {
51
66
  await onSubmit(currentValues);
52
67
  } finally {
53
68
  setIsSubmitting(false);
54
- reset();
55
69
  }
56
70
  };
57
71
  }
@@ -62,12 +76,14 @@ function useForm(initialValues, validate) {
62
76
  (_a = formRef.current) == null ? void 0 : _a.reset();
63
77
  }
64
78
  return {
79
+ values,
65
80
  formRef,
66
- values: initialValues,
67
81
  errors,
68
82
  isSubmitting,
69
83
  handleChange,
70
- handleSubmit
84
+ handleSubmit,
85
+ reset,
86
+ handleSelectOption
71
87
  };
72
88
  }
73
89
  var useForm_default = useForm;
package/dist/index.d.cts CHANGED
@@ -1,14 +1,16 @@
1
1
  import * as react from 'react';
2
- import { ChangeEvent, FormEvent } from 'react';
2
+ import { ChangeEvent, SubmitEvent } from 'react';
3
3
 
4
4
  type TErrors<T> = Partial<Record<keyof T, string>>;
5
5
  declare function useForm<T extends Record<string, string>>(initialValues: T, validate?: (values: T) => TErrors<T>): {
6
- formRef: react.RefObject<HTMLFormElement | null>;
7
6
  values: T;
7
+ formRef: react.RefObject<HTMLFormElement | null>;
8
8
  errors: Partial<Record<keyof T, string>> | undefined;
9
9
  isSubmitting: boolean;
10
- handleChange: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
11
- handleSubmit: (onSubmit: (values: T) => Promise<void> | void) => (e: FormEvent) => Promise<void>;
10
+ handleChange: (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
11
+ handleSubmit: (onSubmit: (values: T) => Promise<void> | void) => (event: SubmitEvent) => Promise<void>;
12
+ reset: () => void;
13
+ handleSelectOption: (name: string, value: string) => void;
12
14
  };
13
15
 
14
16
  type TValidationRule = (val: string) => string | null;
package/dist/index.d.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  import * as react from 'react';
2
- import { ChangeEvent, FormEvent } from 'react';
2
+ import { ChangeEvent, SubmitEvent } from 'react';
3
3
 
4
4
  type TErrors<T> = Partial<Record<keyof T, string>>;
5
5
  declare function useForm<T extends Record<string, string>>(initialValues: T, validate?: (values: T) => TErrors<T>): {
6
- formRef: react.RefObject<HTMLFormElement | null>;
7
6
  values: T;
7
+ formRef: react.RefObject<HTMLFormElement | null>;
8
8
  errors: Partial<Record<keyof T, string>> | undefined;
9
9
  isSubmitting: boolean;
10
- handleChange: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
11
- handleSubmit: (onSubmit: (values: T) => Promise<void> | void) => (e: FormEvent) => Promise<void>;
10
+ handleChange: (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
11
+ handleSubmit: (onSubmit: (values: T) => Promise<void> | void) => (event: SubmitEvent) => Promise<void>;
12
+ reset: () => void;
13
+ handleSelectOption: (name: string, value: string) => void;
12
14
  };
13
15
 
14
16
  type TValidationRule = (val: string) => string | null;
package/dist/index.js CHANGED
@@ -1,17 +1,32 @@
1
1
  // src/useForm.ts
2
- import { useRef, useState } from "react";
2
+ import { useEffect, useRef, useState } from "react";
3
3
  function useForm(initialValues, validate) {
4
+ const lastInitialValuesRef = useRef(initialValues);
4
5
  const valuesRef = useRef({ ...initialValues });
5
6
  const formRef = useRef(null);
6
7
  const [errors, setErrors] = useState();
8
+ const [values, setValues] = useState(initialValues);
7
9
  const [isSubmitting, setIsSubmitting] = useState(false);
8
- function handleChange(e) {
9
- const { name, value } = e.target;
10
+ useEffect(() => {
11
+ const updateValues = () => {
12
+ lastInitialValuesRef.current = initialValues;
13
+ valuesRef.current = initialValues;
14
+ setValues(initialValues);
15
+ };
16
+ if (JSON.stringify(lastInitialValuesRef.current) !== JSON.stringify(initialValues)) {
17
+ updateValues();
18
+ }
19
+ }, [initialValues]);
20
+ function handleChange(event) {
21
+ const { name, value } = event.target;
22
+ valuesRef.current[name] = value;
23
+ }
24
+ function handleSelectOption(name, value) {
10
25
  valuesRef.current[name] = value;
11
26
  }
12
27
  function handleSubmit(onSubmit) {
13
- return async (e) => {
14
- e.preventDefault();
28
+ return async (event) => {
29
+ event.preventDefault();
15
30
  const currentValues = { ...valuesRef.current };
16
31
  if (validate) {
17
32
  const validateErrors = validate(currentValues);
@@ -23,7 +38,6 @@ function useForm(initialValues, validate) {
23
38
  await onSubmit(currentValues);
24
39
  } finally {
25
40
  setIsSubmitting(false);
26
- reset();
27
41
  }
28
42
  };
29
43
  }
@@ -34,12 +48,14 @@ function useForm(initialValues, validate) {
34
48
  (_a = formRef.current) == null ? void 0 : _a.reset();
35
49
  }
36
50
  return {
51
+ values,
37
52
  formRef,
38
- values: initialValues,
39
53
  errors,
40
54
  isSubmitting,
41
55
  handleChange,
42
- handleSubmit
56
+ handleSubmit,
57
+ reset,
58
+ handleSelectOption
43
59
  };
44
60
  }
45
61
  var useForm_default = useForm;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@aienpah/nanoform",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1",
6
+ "test": "vitest",
7
7
  "build": "tsup src/index.ts --dts --format esm,cjs"
8
8
  },
9
9
  "files": [
@@ -21,8 +21,12 @@
21
21
  "react": ">=18"
22
22
  },
23
23
  "devDependencies": {
24
+ "@testing-library/jest-dom": "^6.9.1",
25
+ "@testing-library/react": "^16.3.2",
24
26
  "@types/react": "^19.2.14",
27
+ "jsdom": "^29.1.1",
25
28
  "tsup": "^8.5.1",
26
- "typescript": "^5.9.3"
29
+ "typescript": "^5.9.3",
30
+ "vitest": "^4.1.8"
27
31
  }
28
- }
32
+ }