-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormHandler.tsx
58 lines (53 loc) · 1.46 KB
/
FormHandler.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { ReactElement, ReactNode, useMemo } from "react";
import { FastContext } from "react-fast-context";
import { ContextMainInterface } from "../contexts/ContextMain";
import {
UseFormHandlerArg,
UseFormHandlerReturn,
} from "../hooks/useFormHandler";
import { BaseValues } from "../types/BaseValues";
export interface FormHandlerProps<Values extends BaseValues>
extends UseFormHandlerArg<Values> {
children: ReactNode;
}
/**
* @private
*/
export interface CreateFormHandlerDependencies<
Values extends BaseValues,
ExtraContext extends Record<string, unknown>
> {
ContextMain: FastContext<ContextMainInterface<Values, ExtraContext>>;
useFormHandler: (
arg: UseFormHandlerArg<Values>
) => UseFormHandlerReturn<Values, ExtraContext>;
}
/**
* @private
*/
export const createFormHandler = <
Values extends BaseValues,
ExtraContext extends Record<string, unknown>
>({
ContextMain,
useFormHandler,
}: CreateFormHandlerDependencies<Values, ExtraContext>) => {
/**
* Get the form handler using the hook `useFormHandler` and render the context provider.
*
* @param {FormHandlerProps<Values>} props
* @returns {ReactElement}
*/
const FormHandler = ({
children,
...rest
}: FormHandlerProps<Values>): ReactElement => {
const handler = useFormHandler(rest);
return (
<ContextMain.Provider value={handler}>
{useMemo(() => children, [children])}
</ContextMain.Provider>
);
};
return FormHandler;
};