react-compose-form 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Kris Papercut
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # kp-admin
2
+
3
+ ```shell
4
+ npm i react-hook-form react-compose-form
5
+ ```
6
+
7
+ ## Usage
8
+
9
+ ```tsx
10
+ import {Form, FormControl, FormFieldProps} from "@kearisp/react-admin";
11
+
12
+ type InputProps = {
13
+ label: string;
14
+ };
15
+
16
+ const InputField = ({label, ...props}: FormFieldProps<InputProps>) => {
17
+ return (
18
+ <div>
19
+ <label>{label}</label>
20
+ <input {...props} />
21
+ </div>
22
+ );
23
+ };
24
+
25
+ <Form>
26
+ <FormControl
27
+ as={InputField}
28
+ label="First name"
29
+ name="first_name" />
30
+ </Form>
31
+ ```
32
+
33
+ ```tsx
34
+ import {Form, FormControl, FormArray, useFormArray} from "@kearisp/react-admin";
35
+
36
+
37
+ const ExampleArray = ({ children }: PropsWithChildren) => {
38
+ const {
39
+ append
40
+ } = useFormArray();
41
+
42
+ return (
43
+ <div className="array-container">
44
+ {children}
45
+
46
+ <button type="button" onClick={() => append({first_name: "", last_name: ""})}>
47
+ Add Item
48
+ </button>
49
+ </div>
50
+ );
51
+ };
52
+
53
+ <Form>
54
+ <FormArray as={ExampleArray} name="users">
55
+ <div className="row">
56
+ <FormControl name="first_name" />
57
+ <FormControl name="last_name" />
58
+ </div>
59
+ </FormArray>
60
+ </Form>
61
+ ```
@@ -0,0 +1,16 @@
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;
@@ -0,0 +1,18 @@
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
+ };
@@ -0,0 +1,6 @@
1
+ import React, { PropsWithChildren, ComponentType } from "react";
2
+ export type FormArrayProps = PropsWithChildren<{
3
+ as?: ComponentType<PropsWithChildren>;
4
+ name: string;
5
+ }>;
6
+ export declare const FormArray: React.FC<FormArrayProps>;
@@ -0,0 +1,37 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import React, { isValidElement, cloneElement, Children } from "react";
3
+ import { useFieldArray } from "react-hook-form";
4
+ import { FormGroup } from "./FormGroup";
5
+ import { useFormGroupName } from "./FormGroupContext";
6
+ import { FormArrayContext } from "./FormArrayContext";
7
+ import { FormArrayItemContext } from "./FormArrayItemContext";
8
+ export const FormArray = (props) => {
9
+ const { as: Component = React.Fragment, name, children } = props;
10
+ const fullName = useFormGroupName(name);
11
+ const array = useFieldArray({
12
+ name: fullName
13
+ });
14
+ return (_jsx(FormGroup, { name: fullName, children: _jsx(FormArrayContext.Provider, { value: array, children: _jsx(Component, { children: array.fields.map((field, index) => {
15
+ return (_jsx(FormGroup, { name: index.toString(), children: _jsx(FormArrayItemContext.Provider, { value: {
16
+ index,
17
+ remove: () => array.remove(index),
18
+ moveUp: () => {
19
+ if (index - 1 >= 0) {
20
+ array.swap(index, index - 1);
21
+ }
22
+ },
23
+ moveDown: () => {
24
+ if (index + 1 < array.fields.length) {
25
+ array.swap(index, index + 1);
26
+ }
27
+ }
28
+ }, children: Children.map(children, (child, index) => {
29
+ if (!isValidElement(child)) {
30
+ return null;
31
+ }
32
+ return cloneElement(child, {
33
+ key: index.toString()
34
+ });
35
+ }) }) }, field.id));
36
+ }) }) }) }));
37
+ };
@@ -0,0 +1,3 @@
1
+ import { UseFieldArrayReturn } from "react-hook-form";
2
+ export declare const FormArrayContext: import("react").Context<UseFieldArrayReturn>;
3
+ export declare const useFormArray: () => UseFieldArrayReturn;
@@ -0,0 +1,13 @@
1
+ import { createContext, useContext } from "react";
2
+ export const FormArrayContext = createContext({
3
+ fields: [],
4
+ swap() { },
5
+ remove() { },
6
+ append() { },
7
+ move() { },
8
+ insert() { },
9
+ prepend() { },
10
+ update() { },
11
+ replace() { }
12
+ });
13
+ export const useFormArray = () => useContext(FormArrayContext);
@@ -0,0 +1,12 @@
1
+ export declare const FormArrayItemContext: import("react").Context<{
2
+ index: number;
3
+ remove: () => void;
4
+ moveUp: () => void;
5
+ moveDown: () => void;
6
+ }>;
7
+ export declare const useFormArrayItemContext: () => {
8
+ index: number;
9
+ remove: () => void;
10
+ moveUp: () => void;
11
+ moveDown: () => void;
12
+ };
@@ -0,0 +1,9 @@
1
+ import { createContext, useContext } from "react";
2
+ export const FormArrayItemContext = createContext({
3
+ index: 0,
4
+ remove() { },
5
+ moveUp() { },
6
+ moveDown() { }
7
+ });
8
+ export const useFormArrayItemContext = () => useContext(FormArrayItemContext);
9
+ ;
@@ -0,0 +1,10 @@
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;
@@ -0,0 +1,24 @@
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 { useFormContext, Controller, } from "react-hook-form";
14
+ import { useFormGroupName } from "./FormGroupContext";
15
+ export const FormControl = (props) => {
16
+ const { as: Component = "input", name, disabled = false, required } = props, rest = __rest(props, ["as", "name", "disabled", "required"]);
17
+ const { control } = useFormContext();
18
+ const fullName = useFormGroupName(name);
19
+ return (_jsx(Controller, { control: control, disabled: disabled, name: fullName, rules: {
20
+ required
21
+ }, render: ({ field }) => {
22
+ return (_jsx(Component, Object.assign({}, rest, field)));
23
+ } }));
24
+ };
@@ -0,0 +1,5 @@
1
+ import React, { PropsWithChildren } from "react";
2
+ export type FormGroupProps = PropsWithChildren<{
3
+ name: string;
4
+ }>;
5
+ export declare const FormGroup: React.FC<FormGroupProps>;
@@ -0,0 +1,9 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { FormGroupContext, useFormGroupName } from "./FormGroupContext";
3
+ export const FormGroup = (props) => {
4
+ const { name, children } = props;
5
+ const fullName = useFormGroupName(name);
6
+ return (_jsx(FormGroupContext.Provider, { value: {
7
+ name: fullName
8
+ }, children: children }));
9
+ };
@@ -0,0 +1,4 @@
1
+ export declare const FormGroupContext: import("react").Context<{
2
+ name?: string;
3
+ }>;
4
+ export declare const useFormGroupName: (name?: string) => string;
@@ -0,0 +1,9 @@
1
+ import { useMemo, useContext, createContext } from "react";
2
+ import { joinDotNotation } from "../../utils";
3
+ export const FormGroupContext = createContext({});
4
+ export const useFormGroupName = (name = "") => {
5
+ const { name: parentName = "" } = useContext(FormGroupContext);
6
+ return useMemo(() => {
7
+ return joinDotNotation(parentName, name);
8
+ }, [parentName, name]);
9
+ };
@@ -0,0 +1,5 @@
1
+ export * from "./Form";
2
+ export * from "./FormArray";
3
+ export * from "./FormArrayContext";
4
+ export * from "./FormControl";
5
+ export * from "./FormGroup";
@@ -0,0 +1,5 @@
1
+ export * from "./Form";
2
+ export * from "./FormArray";
3
+ export * from "./FormArrayContext";
4
+ export * from "./FormControl";
5
+ export * from "./FormGroup";
@@ -0,0 +1 @@
1
+ export * from "./Form";
@@ -0,0 +1 @@
1
+ export * from "./Form";
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./blocks";
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./blocks";
@@ -0,0 +1 @@
1
+ export * from "./joinDotNotation";
@@ -0,0 +1 @@
1
+ export * from "./joinDotNotation";
@@ -0,0 +1 @@
1
+ export declare const joinDotNotation: (...names: string[]) => string;
@@ -0,0 +1,8 @@
1
+ export const joinDotNotation = (...names) => {
2
+ return names.reduce((res, name) => {
3
+ return [
4
+ ...res,
5
+ ...name ? name.split(/(?<!\\)\./) : []
6
+ ];
7
+ }, []).join(".");
8
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "react-compose-form",
3
+ "version": "0.0.1",
4
+ "description": "Composable form components built on top of React Hook Form",
5
+ "author": "Kris Papercut <krispcut@gmail.com>",
6
+ "license": "MIT",
7
+ "main": "./lib/index.js",
8
+ "types": "./lib/index.d.ts",
9
+ "keywords": [
10
+ "react",
11
+ "form",
12
+ "hooks",
13
+ "forms",
14
+ "dashboard"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/kearisp/react-compose-form.git"
19
+ },
20
+ "homepage": "https://github.com/kearisp/react-compose-form/blob/master/README.md",
21
+ "bugs": {
22
+ "url": "https://github.com/kearisp/react-compose-form/issues"
23
+ },
24
+ "scripts": {
25
+ "prepublishOnly": "npm run build",
26
+ "start": "npm run watch",
27
+ "watch": "tsc --watch",
28
+ "build": "tsc --build",
29
+ "test": "echo 'error'"
30
+ },
31
+ "peerDependencies": {
32
+ "react": "x.x.x",
33
+ "react-hook-form": "^7.x.x"
34
+ },
35
+ "devDependencies": {
36
+ "@types/react": "^19.2.2",
37
+ "typescript": "^5.9.3"
38
+ }
39
+ }