react-compose-form 0.0.2 → 0.0.3

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,4 +1,12 @@
1
- # kp-admin
1
+ # react-compose-form
2
+
3
+ [![npm version](https://img.shields.io/npm/v/react-compose-form.svg)](https://www.npmjs.com/package/react-compose-form)
4
+ [![Publish](https://github.com/kearisp/react-compose-form/actions/workflows/publish-latest.yml/badge.svg?event=release)](https://github.com/kearisp/react-compose-form/actions/workflows/publish-latest.yml)
5
+ [![License](https://img.shields.io/npm/l/react-compose-form)](https://github.com/kearisp/react-compose-form/blob/master/LICENSE)
6
+
7
+ [![npm total downloads](https://img.shields.io/npm/dt/react-compose-form.svg)](https://www.npmjs.com/package/react-compose-form)
8
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/react-compose-form)](https://bundlephobia.com/package/react-compose-form)
9
+ ![Coverage](https://gist.githubusercontent.com/kearisp/f17f46c6332ea3bb043f27b0bddefa9f/raw/coverage-react-compose-form-latest.svg)
2
10
 
3
11
  ```shell
4
12
  npm i react-hook-form react-compose-form
@@ -1,14 +1,13 @@
1
- import { FormEvent, ElementType, ComponentType, PropsWithChildren } from "react";
1
+ import { FormEvent, ComponentType, PropsWithChildren, JSX, HTMLAttributes } from "react";
2
2
  import { UseFormProps, FieldValues, SubmitHandler, SubmitErrorHandler } from "react-hook-form";
3
- type FormComponentProps = {
3
+ export type FormComponentProps<P = unknown> = P & PropsWithChildren<{
4
4
  className?: string;
5
5
  onSubmit?: (e: FormEvent) => void;
6
- };
7
- export type FormProps<T extends FieldValues = FieldValues, C = any> = PropsWithChildren<UseFormProps<T, C> & {
8
- as?: ElementType<FormComponentProps> | ComponentType<FormComponentProps>;
6
+ }>;
7
+ export type FormProps<T extends FieldValues = FieldValues, C = any, P = unknown> = PropsWithChildren<UseFormProps<T, C> & P & {
8
+ as?: ComponentType<FormComponentProps<P>> | keyof JSX.IntrinsicElements;
9
9
  className?: string;
10
10
  onSubmit?: SubmitHandler<T>;
11
11
  onInvalid?: SubmitErrorHandler<T>;
12
12
  }>;
13
- export declare const Form: <T extends FieldValues = FieldValues, C = any>(props: FormProps<T, C>) => import("react/jsx-runtime").JSX.Element;
14
- export {};
13
+ export declare const Form: <T extends FieldValues = FieldValues, C = any, P = HTMLAttributes<HTMLFormElement>>(props: FormProps<T, C, P>) => import("react/jsx-runtime").JSX.Element;
@@ -13,8 +13,26 @@ import { jsx as _jsx } from "react/jsx-runtime";
13
13
  import { useRef, useCallback } from "react";
14
14
  import { useForm, FormProvider } from "react-hook-form";
15
15
  export const Form = (props) => {
16
- const { as: Component = "form", className, children, onSubmit, onInvalid } = props, rest = __rest(props, ["as", "className", "children", "onSubmit", "onInvalid"]);
17
- const formProps = useForm(rest), submitRef = useRef(onSubmit), invalidRef = useRef(onInvalid);
16
+ const { as: Component = "form", className, mode, disabled, reValidateMode, defaultValues, values, resetOptions, resolver, errors, context, shouldFocusError, shouldUnregister, shouldUseNativeValidation, progressive, criteriaMode, delayError, formControl, children, onSubmit, onInvalid } = props, rest = __rest(props, ["as", "className", "mode", "disabled", "reValidateMode", "defaultValues", "values", "resetOptions", "resolver", "errors", "context", "shouldFocusError", "shouldUnregister", "shouldUseNativeValidation", "progressive", "criteriaMode", "delayError", "formControl", "children", "onSubmit", "onInvalid"]);
17
+ const formProps = useForm({
18
+ mode,
19
+ disabled,
20
+ reValidateMode,
21
+ defaultValues,
22
+ values,
23
+ resetOptions,
24
+ resolver,
25
+ errors,
26
+ context,
27
+ shouldFocusError,
28
+ shouldUnregister,
29
+ shouldUseNativeValidation,
30
+ progressive,
31
+ criteriaMode,
32
+ delayError,
33
+ formControl
34
+ });
35
+ const submitRef = useRef(onSubmit), invalidRef = useRef(onInvalid);
18
36
  submitRef.current = onSubmit;
19
37
  invalidRef.current = onInvalid;
20
38
  const handleSubmit = useCallback((data, event) => {
@@ -27,5 +45,5 @@ export const Form = (props) => {
27
45
  invalidRef.current(errors, event);
28
46
  }
29
47
  }, []);
30
- return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, { className: className, onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children }) })));
48
+ return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, Object.assign({}, rest, { className: className, onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children })) })));
31
49
  };
@@ -5,3 +5,4 @@ export * from "./FormControl";
5
5
  export * from "./FormControlContext";
6
6
  export * from "./FormGroup";
7
7
  export * from "./FormGroupContext";
8
+ export * from "./FormArrayItemContext";
@@ -5,3 +5,4 @@ export * from "./FormControl";
5
5
  export * from "./FormControlContext";
6
6
  export * from "./FormGroup";
7
7
  export * from "./FormGroupContext";
8
+ export * from "./FormArrayItemContext";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { joinDotNotation } from "./joinDotNotation";
3
+ describe("joinDotNotation", () => {
4
+ it("joins simple names with dots", () => {
5
+ expect(joinDotNotation("user", "name")).toBe("user.name");
6
+ expect(joinDotNotation("profile", "contacts", "email")).toBe("profile.contacts.email");
7
+ });
8
+ it("splits dotted segments and flattens", () => {
9
+ expect(joinDotNotation("user.info", "first")).toBe("user.info.first");
10
+ expect(joinDotNotation("a.b", "c.d", "e")).toBe("a.b.c.d.e");
11
+ });
12
+ it("respects escaped dots (\\.) and does not split them", () => {
13
+ expect(joinDotNotation("user\\.info")).toBe("user\\.info");
14
+ expect(joinDotNotation("user\\.info", "first")).toBe("user\\.info.first");
15
+ expect(joinDotNotation("one\\.two\\.three", "x")).toBe("one\\.two\\.three.x");
16
+ });
17
+ it("ignores empty strings and falsy names", () => {
18
+ expect(joinDotNotation("", "a", "", "b")).toBe("a.b");
19
+ expect(joinDotNotation("")).toBe("");
20
+ });
21
+ it("handles no arguments", () => {
22
+ expect(joinDotNotation()).toBe("");
23
+ });
24
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-compose-form",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Composable form components built on top of React Hook Form",
5
5
  "author": "Kris Papercut <krispcut@gmail.com>",
6
6
  "license": "MIT",
@@ -26,14 +26,25 @@
26
26
  "start": "npm run watch",
27
27
  "watch": "tsc --watch",
28
28
  "build": "tsc --build",
29
- "test": "echo 'error'"
29
+ "test": "vitest run --coverage",
30
+ "make-coverage-badge": "make-coverage-badge",
31
+ "test:watch": "vitest"
30
32
  },
31
33
  "peerDependencies": {
32
- "react": "x.x.x",
34
+ "react": ">=16.8.0",
33
35
  "react-hook-form": "^7.x.x"
34
36
  },
35
37
  "devDependencies": {
38
+ "@testing-library/jest-dom": "^6.4.2",
39
+ "@testing-library/react": "^16.0.0",
40
+ "@testing-library/user-event": "^14.5.2",
36
41
  "@types/react": "^19.2.2",
37
- "typescript": "^5.9.3"
42
+ "@types/react-dom": "^19.2.2",
43
+ "@vitest/coverage-v8": "^4.0.5",
44
+ "jsdom": "^25.0.1",
45
+ "make-coverage-badge": "^1.2.0",
46
+ "react": "^19.2.0",
47
+ "typescript": "^5.9.3",
48
+ "vitest": "^4.0.5"
38
49
  }
39
50
  }