react-compose-form 0.0.1 → 0.0.2
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 +2 -2
- package/lib/blocks/Form.d.ts +14 -0
- package/lib/blocks/Form.js +31 -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 +6 -0
- package/lib/blocks/index.js +6 -0
- package/package.json +1 -1
- 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
|
@@ -7,7 +7,7 @@ npm i react-hook-form react-compose-form
|
|
|
7
7
|
## Usage
|
|
8
8
|
|
|
9
9
|
```tsx
|
|
10
|
-
import {Form, FormControl, FormFieldProps} from "
|
|
10
|
+
import {Form, FormControl, FormFieldProps} from "react-compose-form";
|
|
11
11
|
|
|
12
12
|
type InputProps = {
|
|
13
13
|
label: string;
|
|
@@ -31,7 +31,7 @@ const InputField = ({label, ...props}: FormFieldProps<InputProps>) => {
|
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
```tsx
|
|
34
|
-
import {Form, FormControl, FormArray, useFormArray} from "
|
|
34
|
+
import {Form, FormControl, FormArray, useFormArray} from "react-compose-form";
|
|
35
35
|
|
|
36
36
|
|
|
37
37
|
const ExampleArray = ({ children }: PropsWithChildren) => {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FormEvent, ElementType, ComponentType, PropsWithChildren } from "react";
|
|
2
|
+
import { UseFormProps, FieldValues, SubmitHandler, SubmitErrorHandler } from "react-hook-form";
|
|
3
|
+
type FormComponentProps = {
|
|
4
|
+
className?: string;
|
|
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>;
|
|
9
|
+
className?: string;
|
|
10
|
+
onSubmit?: SubmitHandler<T>;
|
|
11
|
+
onInvalid?: SubmitErrorHandler<T>;
|
|
12
|
+
}>;
|
|
13
|
+
export declare const Form: <T extends FieldValues = FieldValues, C = any>(props: FormProps<T, C>) => import("react/jsx-runtime").JSX.Element;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
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, children, onSubmit, onInvalid } = props, rest = __rest(props, ["as", "className", "children", "onSubmit", "onInvalid"]);
|
|
17
|
+
const formProps = useForm(rest), submitRef = useRef(onSubmit), invalidRef = useRef(onInvalid);
|
|
18
|
+
submitRef.current = onSubmit;
|
|
19
|
+
invalidRef.current = onInvalid;
|
|
20
|
+
const handleSubmit = useCallback((data, event) => {
|
|
21
|
+
if (submitRef.current) {
|
|
22
|
+
submitRef.current(data, event);
|
|
23
|
+
}
|
|
24
|
+
}, []);
|
|
25
|
+
const handleInvalid = useCallback((errors, event) => {
|
|
26
|
+
if (invalidRef.current) {
|
|
27
|
+
invalidRef.current(errors, event);
|
|
28
|
+
}
|
|
29
|
+
}, []);
|
|
30
|
+
return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, { className: className, onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children }) })));
|
|
31
|
+
};
|
|
@@ -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
package/lib/blocks/index.js
CHANGED
package/package.json
CHANGED
|
@@ -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
|