react-compose-form 0.0.3 → 0.0.5-beta.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 +2 -2
- package/lib/blocks/Form.d.ts +2 -4
- package/lib/blocks/Form.js +4 -4
- package/lib/blocks/FormArray.js +3 -4
- package/lib/blocks/FormControl.d.ts +2 -1
- package/lib/blocks/FormControl.js +9 -5
- package/lib/blocks/FormGroup.js +2 -1
- package/lib/blocks/index.d.ts +0 -4
- package/lib/blocks/index.js +0 -4
- package/lib/{blocks → contexts}/FormArrayContext.d.ts +0 -1
- package/lib/{blocks → contexts}/FormArrayContext.js +1 -2
- package/lib/{blocks → contexts}/FormArrayItemContext.js +0 -1
- package/lib/{blocks → contexts}/FormGroupContext.d.ts +0 -1
- package/lib/contexts/FormGroupContext.js +2 -0
- package/lib/contexts/index.d.ts +4 -0
- package/lib/contexts/index.js +4 -0
- package/lib/hooks/index.d.ts +2 -0
- package/lib/hooks/index.js +2 -0
- package/lib/hooks/useFormArray.d.ts +2 -0
- package/lib/hooks/useFormArray.js +3 -0
- package/lib/hooks/useFormGroupName.d.ts +1 -0
- package/lib/hooks/useFormGroupName.js +9 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/package.json +1 -1
- package/lib/blocks/FormGroupContext.js +0 -9
- /package/lib/{blocks → contexts}/FormArrayItemContext.d.ts +0 -0
- /package/lib/{blocks → contexts}/FormControlContext.d.ts +0 -0
- /package/lib/{blocks → contexts}/FormControlContext.js +0 -0
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# react-compose-form
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/react-compose-form)
|
|
4
|
-
[](https://github.com/kearisp/react-compose-form/actions/workflows/publish-beta.yml)
|
|
5
5
|
[](https://github.com/kearisp/react-compose-form/blob/master/LICENSE)
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/react-compose-form)
|
|
8
8
|
[](https://bundlephobia.com/package/react-compose-form)
|
|
9
|
-

|
|
10
10
|
|
|
11
11
|
```shell
|
|
12
12
|
npm i react-hook-form react-compose-form
|
package/lib/blocks/Form.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { FormEvent, ComponentType, PropsWithChildren, JSX, HTMLAttributes } from "react";
|
|
2
2
|
import { UseFormProps, FieldValues, SubmitHandler, SubmitErrorHandler } from "react-hook-form";
|
|
3
3
|
export type FormComponentProps<P = unknown> = P & PropsWithChildren<{
|
|
4
|
-
className?: string;
|
|
5
4
|
onSubmit?: (e: FormEvent) => void;
|
|
6
5
|
}>;
|
|
7
|
-
export type FormProps<T extends FieldValues = FieldValues, C = any, P = unknown> = PropsWithChildren<UseFormProps<T, C> & P & {
|
|
6
|
+
export type FormProps<T extends FieldValues = FieldValues, C = any, P = unknown> = PropsWithChildren<UseFormProps<T, C> & Omit<P, "onSubmit" | "onInvalid"> & {
|
|
8
7
|
as?: ComponentType<FormComponentProps<P>> | keyof JSX.IntrinsicElements;
|
|
9
|
-
className?: string;
|
|
10
8
|
onSubmit?: SubmitHandler<T>;
|
|
11
9
|
onInvalid?: SubmitErrorHandler<T>;
|
|
12
10
|
}>;
|
|
13
|
-
export declare const Form: <T extends FieldValues = FieldValues, C =
|
|
11
|
+
export declare const Form: <T extends FieldValues = FieldValues, C = HTMLFormElement, P = HTMLAttributes<C>>(props: FormProps<T, C, P>) => import("react/jsx-runtime").JSX.Element;
|
package/lib/blocks/Form.js
CHANGED
|
@@ -13,7 +13,7 @@ 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",
|
|
16
|
+
const { as: Component = "form", mode, disabled, reValidateMode, defaultValues, values, resetOptions, resolver, errors, context, shouldFocusError, shouldUnregister, shouldUseNativeValidation, progressive, criteriaMode, delayError, formControl, children, onSubmit, onInvalid } = props, rest = __rest(props, ["as", "mode", "disabled", "reValidateMode", "defaultValues", "values", "resetOptions", "resolver", "errors", "context", "shouldFocusError", "shouldUnregister", "shouldUseNativeValidation", "progressive", "criteriaMode", "delayError", "formControl", "children", "onSubmit", "onInvalid"]);
|
|
17
17
|
const formProps = useForm({
|
|
18
18
|
mode,
|
|
19
19
|
disabled,
|
|
@@ -37,13 +37,13 @@ export const Form = (props) => {
|
|
|
37
37
|
invalidRef.current = onInvalid;
|
|
38
38
|
const handleSubmit = useCallback((data, event) => {
|
|
39
39
|
if (submitRef.current) {
|
|
40
|
-
submitRef.current(data, event);
|
|
40
|
+
return submitRef.current(data, event);
|
|
41
41
|
}
|
|
42
42
|
}, []);
|
|
43
43
|
const handleInvalid = useCallback((errors, event) => {
|
|
44
44
|
if (invalidRef.current) {
|
|
45
|
-
invalidRef.current(errors, event);
|
|
45
|
+
return invalidRef.current(errors, event);
|
|
46
46
|
}
|
|
47
47
|
}, []);
|
|
48
|
-
return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, Object.assign({}, rest, {
|
|
48
|
+
return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, Object.assign({}, rest, { onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children })) })));
|
|
49
49
|
};
|
package/lib/blocks/FormArray.js
CHANGED
|
@@ -2,16 +2,15 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import React, { isValidElement, cloneElement, Children } from "react";
|
|
3
3
|
import { useFieldArray } from "react-hook-form";
|
|
4
4
|
import { FormGroup } from "./FormGroup";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { FormArrayItemContext } from "./FormArrayItemContext";
|
|
5
|
+
import { FormArrayContext, FormArrayItemContext } from "../contexts";
|
|
6
|
+
import { useFormGroupName } from "../hooks";
|
|
8
7
|
export const FormArray = (props) => {
|
|
9
8
|
const { as: Component = React.Fragment, name, children } = props;
|
|
10
9
|
const fullName = useFormGroupName(name);
|
|
11
10
|
const array = useFieldArray({
|
|
12
11
|
name: fullName
|
|
13
12
|
});
|
|
14
|
-
return (_jsx(FormGroup, { name:
|
|
13
|
+
return (_jsx(FormGroup, { name: name, children: _jsx(FormArrayContext.Provider, { value: array, children: _jsx(Component, { children: array.fields.map((field, index) => {
|
|
15
14
|
return (_jsx(FormGroup, { name: index.toString(), children: _jsx(FormArrayItemContext.Provider, { value: {
|
|
16
15
|
index,
|
|
17
16
|
remove: () => array.remove(index),
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { ComponentType } from "react";
|
|
2
|
-
import { ControllerRenderProps, Message, ValidationRule, ControllerFieldState } from "react-hook-form";
|
|
2
|
+
import { Control, ControllerRenderProps, Message, ValidationRule, ControllerFieldState } from "react-hook-form";
|
|
3
3
|
export type FormFieldProps<P = unknown> = P & Partial<ControllerRenderProps & ControllerFieldState>;
|
|
4
4
|
export type FormControlProps<P = unknown> = Omit<P, "as" | "name" | "disabled" | "required" | keyof ControllerFieldState | keyof ControllerRenderProps> & {
|
|
5
5
|
as?: ComponentType<FormFieldProps<P>>;
|
|
6
6
|
name: string;
|
|
7
7
|
disabled?: boolean;
|
|
8
8
|
required?: Message | ValidationRule<boolean>;
|
|
9
|
+
control?: Control;
|
|
9
10
|
};
|
|
10
11
|
export declare const FormControl: <P = unknown>(props: FormControlProps<P>) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -11,13 +11,17 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
11
11
|
};
|
|
12
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
13
|
import { useFormContext, Controller } from "react-hook-form";
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
const { as: Component = "input", name, disabled = false, required } = props, rest = __rest(props, ["as", "name", "disabled", "required"]);
|
|
14
|
+
import { FormControlContext } from "../contexts";
|
|
15
|
+
import { useFormGroupName } from "../hooks";
|
|
16
|
+
const ControllerWithContext = (props) => {
|
|
18
17
|
const { control } = useFormContext();
|
|
18
|
+
return (_jsx(Controller, Object.assign({}, props, { control: control })));
|
|
19
|
+
};
|
|
20
|
+
export const FormControl = (props) => {
|
|
21
|
+
const { as: Component = "input", name, disabled = false, required, control } = props, rest = __rest(props, ["as", "name", "disabled", "required", "control"]);
|
|
19
22
|
const fullName = useFormGroupName(name);
|
|
20
|
-
|
|
23
|
+
const C = control ? Controller : ControllerWithContext;
|
|
24
|
+
return (_jsx(C, { control: control, disabled: disabled, name: fullName, rules: {
|
|
21
25
|
required
|
|
22
26
|
}, render: ({ field, fieldState }) => {
|
|
23
27
|
return (_jsx(FormControlContext, { value: fieldState, children: _jsx(Component, Object.assign({}, rest, field)) }));
|
package/lib/blocks/FormGroup.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { FormGroupContext
|
|
2
|
+
import { FormGroupContext } from "../contexts";
|
|
3
|
+
import { useFormGroupName } from "../hooks";
|
|
3
4
|
export const FormGroup = (props) => {
|
|
4
5
|
const { name, children } = props;
|
|
5
6
|
const fullName = useFormGroupName(name);
|
package/lib/blocks/index.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
export * from "./Form";
|
|
2
2
|
export * from "./FormArray";
|
|
3
|
-
export * from "./FormArrayContext";
|
|
4
3
|
export * from "./FormControl";
|
|
5
|
-
export * from "./FormControlContext";
|
|
6
4
|
export * from "./FormGroup";
|
|
7
|
-
export * from "./FormGroupContext";
|
|
8
|
-
export * from "./FormArrayItemContext";
|
package/lib/blocks/index.js
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
export * from "./Form";
|
|
2
2
|
export * from "./FormArray";
|
|
3
|
-
export * from "./FormArrayContext";
|
|
4
3
|
export * from "./FormControl";
|
|
5
|
-
export * from "./FormControlContext";
|
|
6
4
|
export * from "./FormGroup";
|
|
7
|
-
export * from "./FormGroupContext";
|
|
8
|
-
export * from "./FormArrayItemContext";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext
|
|
1
|
+
import { createContext } from "react";
|
|
2
2
|
export const FormArrayContext = createContext({
|
|
3
3
|
fields: [],
|
|
4
4
|
swap() { },
|
|
@@ -10,4 +10,3 @@ export const FormArrayContext = createContext({
|
|
|
10
10
|
update() { },
|
|
11
11
|
replace() { }
|
|
12
12
|
});
|
|
13
|
-
export const useFormArray = () => useContext(FormArrayContext);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useFormGroupName: (name: string) => string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useMemo, useContext } from "react";
|
|
2
|
+
import { FormGroupContext } from "../contexts";
|
|
3
|
+
import { joinDotNotation } from "../utils";
|
|
4
|
+
export const useFormGroupName = (name) => {
|
|
5
|
+
const { name: parent = "" } = useContext(FormGroupContext);
|
|
6
|
+
return useMemo(() => {
|
|
7
|
+
return joinDotNotation(parent, name);
|
|
8
|
+
}, [parent, name]);
|
|
9
|
+
};
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +0,0 @@
|
|
|
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
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|