react-compose-form 0.0.5 → 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 CHANGED
@@ -1,12 +1,12 @@
1
1
  # react-compose-form
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/react-compose-form.svg)](https://www.npmjs.com/package/react-compose-form)
4
- [![Publish](https://github.com/kearisp/react-compose-form/actions/workflows/publish-latest.yml/badge.svg?event=release)](https://github.com/kearisp/react-compose-form/actions/workflows/publish-latest.yml)
4
+ [![Beta](https://github.com/kearisp/react-compose-form/actions/workflows/publish-beta.yml/badge.svg?branch=v0.0.6&event=pull_request)](https://github.com/kearisp/react-compose-form/actions/workflows/publish-beta.yml)
5
5
  [![License](https://img.shields.io/npm/l/react-compose-form)](https://github.com/kearisp/react-compose-form/blob/master/LICENSE)
6
6
 
7
7
  [![npm total downloads](https://img.shields.io/npm/dt/react-compose-form.svg)](https://www.npmjs.com/package/react-compose-form)
8
8
  [![bundle size](https://img.shields.io/bundlephobia/minzip/react-compose-form)](https://bundlephobia.com/package/react-compose-form)
9
- ![Coverage](https://gist.githubusercontent.com/kearisp/f17f46c6332ea3bb043f27b0bddefa9f/raw/coverage-react-compose-form-latest.svg)
9
+ ![Coverage](https://gist.githubusercontent.com/kearisp/f17f46c6332ea3bb043f27b0bddefa9f/raw/coverage-react-compose-form-beta.svg)
10
10
 
11
11
  ```shell
12
12
  npm i react-hook-form react-compose-form
@@ -1,11 +1,9 @@
1
- import { FormEvent, ComponentType, PropsWithChildren, JSX, HTMLAttributes } from "react";
1
+ import { ElementType } from "react";
2
2
  import { UseFormProps, FieldValues, SubmitHandler, SubmitErrorHandler } from "react-hook-form";
3
- export type FormComponentProps<P = unknown> = P & PropsWithChildren<{
4
- onSubmit?: (e: FormEvent) => void;
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 = HTMLFormElement, P = HTMLAttributes<C>>(props: FormProps<T, C, P>) => import("react/jsx-runtime").JSX.Element;
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 {};
@@ -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 formProps = useForm({
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
- return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, Object.assign({}, rest, { onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children })) })));
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
  };
@@ -0,0 +1,4 @@
1
+ import React, { PropsWithChildren } from "react";
2
+ type FormProviderProps = PropsWithChildren;
3
+ export declare const FormProvider: React.FC<FormProviderProps>;
4
+ export {};
@@ -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
+ };
@@ -2,3 +2,4 @@ export * from "./Form";
2
2
  export * from "./FormArray";
3
3
  export * from "./FormControl";
4
4
  export * from "./FormGroup";
5
+ export * from "./FormProvider";
@@ -2,3 +2,4 @@ export * from "./Form";
2
2
  export * from "./FormArray";
3
3
  export * from "./FormControl";
4
4
  export * from "./FormGroup";
5
+ export * from "./FormProvider";
@@ -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;
@@ -0,0 +1,6 @@
1
+ import { useCallback } from "react";
2
+ const formMap = new Map();
3
+ export const useFormRegistry = () => {
4
+ const subscribe = useCallback(() => {
5
+ }, []);
6
+ };
@@ -1,2 +1,3 @@
1
+ export * from "./useForm";
1
2
  export * from "./useFormArray";
2
3
  export * from "./useFormGroupName";
@@ -1,2 +1,3 @@
1
+ export * from "./useForm";
1
2
  export * from "./useFormArray";
2
3
  export * from "./useFormGroupName";
@@ -0,0 +1,2 @@
1
+ import { FieldValues, UseFormReturn } from "react-hook-form";
2
+ export declare const useForm: <V extends FieldValues = FieldValues>(formId?: string) => UseFormReturn<V>;
@@ -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,4 @@
1
+ import { FormState } from "react-hook-form";
2
+ export type FormControl = {
3
+ formState: FormState<any>;
4
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-compose-form",
3
- "version": "0.0.5",
3
+ "version": "0.0.6-beta.0",
4
4
  "description": "Composable form components built on top of React Hook Form",
5
5
  "author": "Kris Papercut <krispcut@gmail.com>",
6
6
  "license": "MIT",