react-compose-form 0.0.4 → 0.0.6-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 +5 -7
- package/lib/blocks/Form.js +23 -4
- package/lib/blocks/FormControl.d.ts +2 -1
- package/lib/blocks/FormControl.js +7 -3
- package/lib/blocks/FormProvider.d.ts +4 -0
- package/lib/blocks/FormProvider.js +33 -0
- package/lib/blocks/index.d.ts +1 -0
- package/lib/blocks/index.js +1 -0
- package/lib/contexts/FormContext.d.ts +9 -0
- package/lib/contexts/FormContext.js +10 -0
- package/lib/contexts/FormRegistryContext.d.ts +1 -0
- package/lib/contexts/FormRegistryContext.js +6 -0
- package/lib/hooks/index.d.ts +1 -0
- package/lib/hooks/index.js +1 -0
- package/lib/hooks/useForm.d.ts +2 -0
- package/lib/hooks/useForm.js +13 -0
- package/lib/types/FormControl.d.ts +4 -0
- package/lib/types/FormControl.js +1 -0
- package/lib/types/PolymorphicComponentProps.d.ts +7 -0
- package/lib/types/PolymorphicComponentProps.js +1 -0
- package/package.json +1 -1
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,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ElementType } from "react";
|
|
2
2
|
import { UseFormProps, FieldValues, SubmitHandler, SubmitErrorHandler } from "react-hook-form";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}>;
|
|
6
|
-
export type FormProps<T extends FieldValues = FieldValues, C = any, P = unknown> = PropsWithChildren<UseFormProps<T, C> & Omit<P, "onSubmit" | "onInvalid"> & {
|
|
7
|
-
as?: ComponentType<FormComponentProps<P>> | keyof JSX.IntrinsicElements;
|
|
3
|
+
import { PolymorphicComponentProps } from "../types/PolymorphicComponentProps";
|
|
4
|
+
type FormProps<T extends FieldValues = FieldValues, C extends ElementType = "form"> = PolymorphicComponentProps<C, UseFormProps<T, C> & {
|
|
8
5
|
onSubmit?: SubmitHandler<T>;
|
|
9
6
|
onInvalid?: SubmitErrorHandler<T>;
|
|
10
7
|
}>;
|
|
11
|
-
export declare const Form: <T extends FieldValues = FieldValues, C
|
|
8
|
+
export declare const Form: <T extends FieldValues = FieldValues, C extends ElementType = "form">(props: FormProps<T, C>) => import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
export {};
|
package/lib/blocks/Form.js
CHANGED
|
@@ -10,11 +10,13 @@ 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 { useRef, useCallback } from "react";
|
|
13
|
+
import { useRef, useCallback, useEffect, useLayoutEffect } from "react";
|
|
14
14
|
import { useForm, FormProvider } from "react-hook-form";
|
|
15
|
+
import { useFormContext } from "../contexts/FormContext";
|
|
15
16
|
export const Form = (props) => {
|
|
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
|
-
const
|
|
17
|
+
const { id, 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, ["id", "as", "mode", "disabled", "reValidateMode", "defaultValues", "values", "resetOptions", "resolver", "errors", "context", "shouldFocusError", "shouldUnregister", "shouldUseNativeValidation", "progressive", "criteriaMode", "delayError", "formControl", "children", "onSubmit", "onInvalid"]);
|
|
18
|
+
const { registry, registerForm, unregisterForm } = useFormContext();
|
|
19
|
+
const form = useForm({
|
|
18
20
|
mode,
|
|
19
21
|
disabled,
|
|
20
22
|
reValidateMode,
|
|
@@ -32,6 +34,9 @@ export const Form = (props) => {
|
|
|
32
34
|
delayError,
|
|
33
35
|
formControl
|
|
34
36
|
});
|
|
37
|
+
if (id && !registry.has(id)) {
|
|
38
|
+
registry.set(id, form);
|
|
39
|
+
}
|
|
35
40
|
const submitRef = useRef(onSubmit), invalidRef = useRef(onInvalid);
|
|
36
41
|
submitRef.current = onSubmit;
|
|
37
42
|
invalidRef.current = onInvalid;
|
|
@@ -45,5 +50,19 @@ export const Form = (props) => {
|
|
|
45
50
|
return invalidRef.current(errors, event);
|
|
46
51
|
}
|
|
47
52
|
}, []);
|
|
48
|
-
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!id) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
registerForm(id, form);
|
|
58
|
+
}, [registerForm, id, form, form.formState.isSubmitting]);
|
|
59
|
+
useLayoutEffect(() => {
|
|
60
|
+
if (!id) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
return () => {
|
|
64
|
+
unregisterForm(id);
|
|
65
|
+
};
|
|
66
|
+
}, []);
|
|
67
|
+
return (_jsx(FormProvider, Object.assign({}, form, { children: _jsx(Component, Object.assign({}, rest, { id: id, onSubmit: form.handleSubmit(handleSubmit, handleInvalid), children: children })) })));
|
|
49
68
|
};
|
|
@@ -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;
|
|
@@ -13,11 +13,15 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
13
13
|
import { useFormContext, Controller } from "react-hook-form";
|
|
14
14
|
import { FormControlContext } from "../contexts";
|
|
15
15
|
import { useFormGroupName } from "../hooks";
|
|
16
|
-
|
|
17
|
-
const { as: Component = "input", name, disabled = false, required } = props, rest = __rest(props, ["as", "name", "disabled", "required"]);
|
|
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)) }));
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useState, useMemo, useCallback } from "react";
|
|
3
|
+
import { FormContext } from "../contexts/FormContext";
|
|
4
|
+
export const FormProvider = (props) => {
|
|
5
|
+
const { children } = props;
|
|
6
|
+
const [formMap, setFormMap] = useState(() => new Map());
|
|
7
|
+
const handleGetForm = useCallback((formId) => {
|
|
8
|
+
return formMap.get(formId);
|
|
9
|
+
}, [formMap]);
|
|
10
|
+
const handleRegisterForm = useCallback((id, form) => {
|
|
11
|
+
setFormMap((formMap) => {
|
|
12
|
+
const newMap = new Map(formMap);
|
|
13
|
+
newMap.set(id, form);
|
|
14
|
+
return newMap;
|
|
15
|
+
});
|
|
16
|
+
}, []);
|
|
17
|
+
const handleUnregisterForm = useCallback((id) => {
|
|
18
|
+
setFormMap((map) => {
|
|
19
|
+
const formMap = new Map(map);
|
|
20
|
+
formMap.delete(id);
|
|
21
|
+
return formMap;
|
|
22
|
+
});
|
|
23
|
+
}, []);
|
|
24
|
+
const context = useMemo(() => {
|
|
25
|
+
return {
|
|
26
|
+
registry: formMap,
|
|
27
|
+
getForm: handleGetForm,
|
|
28
|
+
registerForm: handleRegisterForm,
|
|
29
|
+
unregisterForm: handleUnregisterForm
|
|
30
|
+
};
|
|
31
|
+
}, [formMap, handleGetForm, handleRegisterForm, handleUnregisterForm]);
|
|
32
|
+
return (_jsx(FormContext, { value: context, children: children }));
|
|
33
|
+
};
|
package/lib/blocks/index.d.ts
CHANGED
package/lib/blocks/index.js
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { UseFormReturn } from "react-hook-form";
|
|
2
|
+
export type FormContextType = {
|
|
3
|
+
registry: Map<string, UseFormReturn>;
|
|
4
|
+
getForm: (id: string) => UseFormReturn | undefined;
|
|
5
|
+
registerForm: (id: string, props: UseFormReturn) => void;
|
|
6
|
+
unregisterForm: (id: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare const FormContext: import("react").Context<FormContextType>;
|
|
9
|
+
export declare const useFormContext: () => FormContextType;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useContext, createContext } from "react";
|
|
2
|
+
export const FormContext = createContext({
|
|
3
|
+
registry: new Map(),
|
|
4
|
+
getForm() {
|
|
5
|
+
return undefined;
|
|
6
|
+
},
|
|
7
|
+
registerForm() { },
|
|
8
|
+
unregisterForm() { }
|
|
9
|
+
});
|
|
10
|
+
export const useFormContext = () => useContext(FormContext);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useFormRegistry: () => void;
|
package/lib/hooks/index.d.ts
CHANGED
package/lib/hooks/index.js
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useFormContext as useHookContext } from "react-hook-form";
|
|
2
|
+
import { useFormContext } from "../contexts/FormContext";
|
|
3
|
+
export const useForm = (formId) => {
|
|
4
|
+
const { registry } = useFormContext();
|
|
5
|
+
if (formId) {
|
|
6
|
+
const form = registry.get(formId);
|
|
7
|
+
if (!form) {
|
|
8
|
+
throw new Error(`Form "${formId}" is not registered yet`);
|
|
9
|
+
}
|
|
10
|
+
return form;
|
|
11
|
+
}
|
|
12
|
+
return useHookContext();
|
|
13
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ElementType, ComponentPropsWithoutRef } from "react";
|
|
2
|
+
type AsProp<T extends ElementType> = {
|
|
3
|
+
as?: T;
|
|
4
|
+
};
|
|
5
|
+
type PropsToOmit<T extends ElementType, P> = keyof (AsProp<T> & P);
|
|
6
|
+
export type PolymorphicComponentProps<T extends ElementType, Props = {}> = Props & AsProp<T> & Omit<ComponentPropsWithoutRef<T>, PropsToOmit<T, Props>>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|