react-compose-form 0.0.1 → 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 +11 -3
- package/lib/blocks/Form.d.ts +13 -0
- package/lib/blocks/Form.js +49 -0
- package/lib/blocks/FormArray.d.ts +6 -0
- package/lib/blocks/FormControl.d.ts +10 -0
- package/lib/blocks/{Form/FormControl.js → FormControl.js} +4 -3
- package/lib/blocks/FormControlContext.d.ts +3 -0
- package/lib/blocks/FormControlContext.js +8 -0
- package/lib/blocks/{Form/FormGroupContext.js → FormGroupContext.js} +1 -1
- package/lib/blocks/index.d.ts +7 -0
- package/lib/blocks/index.js +7 -0
- package/lib/utils/joinDotNotation.test.d.ts +1 -0
- package/lib/utils/joinDotNotation.test.js +24 -0
- package/package.json +15 -4
- package/lib/blocks/Form/Form.d.ts +0 -16
- package/lib/blocks/Form/Form.js +0 -18
- package/lib/blocks/Form/FormArray.d.ts +0 -6
- package/lib/blocks/Form/FormControl.d.ts +0 -10
- package/lib/blocks/Form/index.d.ts +0 -5
- package/lib/blocks/Form/index.js +0 -5
- /package/lib/blocks/{Form/FormArray.js → FormArray.js} +0 -0
- /package/lib/blocks/{Form/FormArrayContext.d.ts → FormArrayContext.d.ts} +0 -0
- /package/lib/blocks/{Form/FormArrayContext.js → FormArrayContext.js} +0 -0
- /package/lib/blocks/{Form/FormArrayItemContext.d.ts → FormArrayItemContext.d.ts} +0 -0
- /package/lib/blocks/{Form/FormArrayItemContext.js → FormArrayItemContext.js} +0 -0
- /package/lib/blocks/{Form/FormGroup.d.ts → FormGroup.d.ts} +0 -0
- /package/lib/blocks/{Form/FormGroup.js → FormGroup.js} +0 -0
- /package/lib/blocks/{Form/FormGroupContext.d.ts → FormGroupContext.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-compose-form
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/react-compose-form)
|
|
4
|
+
[](https://github.com/kearisp/react-compose-form/actions/workflows/publish-latest.yml)
|
|
5
|
+
[](https://github.com/kearisp/react-compose-form/blob/master/LICENSE)
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/react-compose-form)
|
|
8
|
+
[](https://bundlephobia.com/package/react-compose-form)
|
|
9
|
+

|
|
2
10
|
|
|
3
11
|
```shell
|
|
4
12
|
npm i react-hook-form react-compose-form
|
|
@@ -7,7 +15,7 @@ npm i react-hook-form react-compose-form
|
|
|
7
15
|
## Usage
|
|
8
16
|
|
|
9
17
|
```tsx
|
|
10
|
-
import {Form, FormControl, FormFieldProps} from "
|
|
18
|
+
import {Form, FormControl, FormFieldProps} from "react-compose-form";
|
|
11
19
|
|
|
12
20
|
type InputProps = {
|
|
13
21
|
label: string;
|
|
@@ -31,7 +39,7 @@ const InputField = ({label, ...props}: FormFieldProps<InputProps>) => {
|
|
|
31
39
|
```
|
|
32
40
|
|
|
33
41
|
```tsx
|
|
34
|
-
import {Form, FormControl, FormArray, useFormArray} from "
|
|
42
|
+
import {Form, FormControl, FormArray, useFormArray} from "react-compose-form";
|
|
35
43
|
|
|
36
44
|
|
|
37
45
|
const ExampleArray = ({ children }: PropsWithChildren) => {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FormEvent, ComponentType, PropsWithChildren, JSX, HTMLAttributes } from "react";
|
|
2
|
+
import { UseFormProps, FieldValues, SubmitHandler, SubmitErrorHandler } from "react-hook-form";
|
|
3
|
+
export type FormComponentProps<P = unknown> = P & PropsWithChildren<{
|
|
4
|
+
className?: string;
|
|
5
|
+
onSubmit?: (e: FormEvent) => void;
|
|
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
|
+
className?: string;
|
|
10
|
+
onSubmit?: SubmitHandler<T>;
|
|
11
|
+
onInvalid?: SubmitErrorHandler<T>;
|
|
12
|
+
}>;
|
|
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;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
import { useRef, useCallback } from "react";
|
|
14
|
+
import { useForm, FormProvider } from "react-hook-form";
|
|
15
|
+
export const Form = (props) => {
|
|
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);
|
|
36
|
+
submitRef.current = onSubmit;
|
|
37
|
+
invalidRef.current = onInvalid;
|
|
38
|
+
const handleSubmit = useCallback((data, event) => {
|
|
39
|
+
if (submitRef.current) {
|
|
40
|
+
submitRef.current(data, event);
|
|
41
|
+
}
|
|
42
|
+
}, []);
|
|
43
|
+
const handleInvalid = useCallback((errors, event) => {
|
|
44
|
+
if (invalidRef.current) {
|
|
45
|
+
invalidRef.current(errors, event);
|
|
46
|
+
}
|
|
47
|
+
}, []);
|
|
48
|
+
return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, Object.assign({}, rest, { className: className, onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children })) })));
|
|
49
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React, { PropsWithChildren, ComponentType, ElementType } from "react";
|
|
2
|
+
export type FormArrayProps = PropsWithChildren<{
|
|
3
|
+
as?: ComponentType<PropsWithChildren> | ElementType<PropsWithChildren>;
|
|
4
|
+
name: string;
|
|
5
|
+
}>;
|
|
6
|
+
export declare const FormArray: React.FC<FormArrayProps>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ComponentType } from "react";
|
|
2
|
+
import { ControllerRenderProps, Message, ValidationRule, ControllerFieldState } from "react-hook-form";
|
|
3
|
+
export type FormFieldProps<P = unknown> = P & Partial<ControllerRenderProps & ControllerFieldState>;
|
|
4
|
+
export type FormControlProps<P = unknown> = Omit<P, "as" | "name" | "disabled" | "required" | keyof ControllerFieldState | keyof ControllerRenderProps> & {
|
|
5
|
+
as?: ComponentType<FormFieldProps<P>>;
|
|
6
|
+
name: string;
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
required?: Message | ValidationRule<boolean>;
|
|
9
|
+
};
|
|
10
|
+
export declare const FormControl: <P = unknown>(props: FormControlProps<P>) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -10,15 +10,16 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
-
import { useFormContext, Controller
|
|
13
|
+
import { useFormContext, Controller } from "react-hook-form";
|
|
14
14
|
import { useFormGroupName } from "./FormGroupContext";
|
|
15
|
+
import { FormControlContext } from "./FormControlContext";
|
|
15
16
|
export const FormControl = (props) => {
|
|
16
17
|
const { as: Component = "input", name, disabled = false, required } = props, rest = __rest(props, ["as", "name", "disabled", "required"]);
|
|
17
18
|
const { control } = useFormContext();
|
|
18
19
|
const fullName = useFormGroupName(name);
|
|
19
20
|
return (_jsx(Controller, { control: control, disabled: disabled, name: fullName, rules: {
|
|
20
21
|
required
|
|
21
|
-
}, render: ({ field }) => {
|
|
22
|
-
return (_jsx(Component, Object.assign({}, rest, field)));
|
|
22
|
+
}, render: ({ field, fieldState }) => {
|
|
23
|
+
return (_jsx(FormControlContext, { value: fieldState, children: _jsx(Component, Object.assign({}, rest, field)) }));
|
|
23
24
|
} }));
|
|
24
25
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useMemo, useContext, createContext } from "react";
|
|
2
|
-
import { joinDotNotation } from "
|
|
2
|
+
import { joinDotNotation } from "../utils";
|
|
3
3
|
export const FormGroupContext = createContext({});
|
|
4
4
|
export const useFormGroupName = (name = "") => {
|
|
5
5
|
const { name: parentName = "" } = useContext(FormGroupContext);
|
package/lib/blocks/index.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
1
|
export * from "./Form";
|
|
2
|
+
export * from "./FormArray";
|
|
3
|
+
export * from "./FormArrayContext";
|
|
4
|
+
export * from "./FormControl";
|
|
5
|
+
export * from "./FormControlContext";
|
|
6
|
+
export * from "./FormGroup";
|
|
7
|
+
export * from "./FormGroupContext";
|
|
8
|
+
export * from "./FormArrayItemContext";
|
package/lib/blocks/index.js
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
1
|
export * from "./Form";
|
|
2
|
+
export * from "./FormArray";
|
|
3
|
+
export * from "./FormArrayContext";
|
|
4
|
+
export * from "./FormControl";
|
|
5
|
+
export * from "./FormControlContext";
|
|
6
|
+
export * from "./FormGroup";
|
|
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.
|
|
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": "
|
|
29
|
+
"test": "vitest run --coverage",
|
|
30
|
+
"make-coverage-badge": "make-coverage-badge",
|
|
31
|
+
"test:watch": "vitest"
|
|
30
32
|
},
|
|
31
33
|
"peerDependencies": {
|
|
32
|
-
"react": "
|
|
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
|
-
"
|
|
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
|
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { FormEvent, ComponentType, PropsWithChildren } from "react";
|
|
2
|
-
import { Resolver, FieldValues, Mode, DefaultValues } from "react-hook-form";
|
|
3
|
-
export type FormProps<T extends FieldValues = FieldValues> = PropsWithChildren<{
|
|
4
|
-
as?: ComponentType<{
|
|
5
|
-
className?: string;
|
|
6
|
-
onSubmit: (e: FormEvent) => void;
|
|
7
|
-
}>;
|
|
8
|
-
className?: string;
|
|
9
|
-
mode?: Mode;
|
|
10
|
-
resolver?: Resolver<T>;
|
|
11
|
-
values?: T;
|
|
12
|
-
defaultValues?: DefaultValues<T>;
|
|
13
|
-
onSubmit?: (data: T) => void;
|
|
14
|
-
onError?: (err: unknown) => void;
|
|
15
|
-
}>;
|
|
16
|
-
export declare const Form: <T extends FieldValues = FieldValues>(props: FormProps<T>) => import("react/jsx-runtime").JSX.Element;
|
package/lib/blocks/Form/Form.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useCallback } from "react";
|
|
3
|
-
import { useForm, FormProvider } from "react-hook-form";
|
|
4
|
-
export const Form = (props) => {
|
|
5
|
-
const { as: Component = "form", className, mode, resolver, values, defaultValues, children, onSubmit, onError } = props;
|
|
6
|
-
const formProps = useForm({
|
|
7
|
-
mode,
|
|
8
|
-
resolver,
|
|
9
|
-
values,
|
|
10
|
-
defaultValues
|
|
11
|
-
});
|
|
12
|
-
const handleSubmit = useCallback((data) => {
|
|
13
|
-
if (onSubmit) {
|
|
14
|
-
onSubmit(data);
|
|
15
|
-
}
|
|
16
|
-
}, [onSubmit]);
|
|
17
|
-
return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, { className: className, onSubmit: formProps.handleSubmit(handleSubmit, onError), children: children }) })));
|
|
18
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ComponentType } from "react";
|
|
2
|
-
import { ControllerRenderProps, Message, ValidationRule } from "react-hook-form";
|
|
3
|
-
export type FormFieldProps<P = unknown> = P & ControllerRenderProps;
|
|
4
|
-
export type FormControlProps<P = unknown> = P & {
|
|
5
|
-
as?: ComponentType<FormFieldProps<P>>;
|
|
6
|
-
name: string;
|
|
7
|
-
disabled?: boolean;
|
|
8
|
-
required?: Message | ValidationRule<boolean>;
|
|
9
|
-
};
|
|
10
|
-
export declare const FormControl: <P = unknown>(props: FormControlProps<P>) => import("react/jsx-runtime").JSX.Element;
|
package/lib/blocks/Form/index.js
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|