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 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.5&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,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 = any, P = HTMLAttributes<HTMLFormElement>>(props: FormProps<T, C, P>) => import("react/jsx-runtime").JSX.Element;
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;
@@ -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", className, mode, disabled, reValidateMode, defaultValues, values, resetOptions, resolver, errors, context, shouldFocusError, shouldUnregister, shouldUseNativeValidation, progressive, criteriaMode, delayError, formControl, children, onSubmit, onInvalid } = props, rest = __rest(props, ["as", "className", "mode", "disabled", "reValidateMode", "defaultValues", "values", "resetOptions", "resolver", "errors", "context", "shouldFocusError", "shouldUnregister", "shouldUseNativeValidation", "progressive", "criteriaMode", "delayError", "formControl", "children", "onSubmit", "onInvalid"]);
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, { className: className, onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children })) })));
48
+ return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, Object.assign({}, rest, { onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children })) })));
49
49
  };
@@ -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 { useFormGroupName } from "./FormGroupContext";
6
- import { FormArrayContext } from "./FormArrayContext";
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: fullName, children: _jsx(FormArrayContext.Provider, { value: array, children: _jsx(Component, { children: array.fields.map((field, index) => {
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 { useFormGroupName } from "./FormGroupContext";
15
- import { FormControlContext } from "./FormControlContext";
16
- export const FormControl = (props) => {
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
- return (_jsx(Controller, { control: control, disabled: disabled, name: fullName, rules: {
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)) }));
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { FormGroupContext, useFormGroupName } from "./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);
@@ -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,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,3 +1,2 @@
1
1
  import { UseFieldArrayReturn } from "react-hook-form";
2
2
  export declare const FormArrayContext: import("react").Context<UseFieldArrayReturn>;
3
- export declare const useFormArray: () => UseFieldArrayReturn;
@@ -1,4 +1,4 @@
1
- import { createContext, useContext } from "react";
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);
@@ -6,4 +6,3 @@ export const FormArrayItemContext = createContext({
6
6
  moveDown() { }
7
7
  });
8
8
  export const useFormArrayItemContext = () => useContext(FormArrayItemContext);
9
- ;
@@ -1,4 +1,3 @@
1
1
  export declare const FormGroupContext: import("react").Context<{
2
2
  name?: string;
3
3
  }>;
4
- export declare const useFormGroupName: (name?: string) => string;
@@ -0,0 +1,2 @@
1
+ import { createContext } from "react";
2
+ export const FormGroupContext = createContext({});
@@ -0,0 +1,4 @@
1
+ export * from "./FormArrayContext";
2
+ export * from "./FormArrayItemContext";
3
+ export * from "./FormControlContext";
4
+ export * from "./FormGroupContext";
@@ -0,0 +1,4 @@
1
+ export * from "./FormArrayContext";
2
+ export * from "./FormArrayItemContext";
3
+ export * from "./FormControlContext";
4
+ export * from "./FormGroupContext";
@@ -0,0 +1,2 @@
1
+ export * from "./useFormArray";
2
+ export * from "./useFormGroupName";
@@ -0,0 +1,2 @@
1
+ export * from "./useFormArray";
2
+ export * from "./useFormGroupName";
@@ -0,0 +1,2 @@
1
+ import { UseFieldArrayReturn, FieldValues, ArrayPath } from "react-hook-form";
2
+ export declare const useFormArray: <T extends FieldValues = FieldValues, P extends ArrayPath<T> = ArrayPath<T>, K extends string = "id">() => UseFieldArrayReturn<T, P, K>;
@@ -0,0 +1,3 @@
1
+ import { useContext } from "react";
2
+ import { FormArrayContext } from "../contexts";
3
+ 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
@@ -1 +1,3 @@
1
1
  export * from "./blocks";
2
+ export * from "./contexts";
3
+ export * from "./hooks";
package/lib/index.js CHANGED
@@ -1 +1,3 @@
1
1
  export * from "./blocks";
2
+ export * from "./contexts";
3
+ export * from "./hooks";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-compose-form",
3
- "version": "0.0.3",
3
+ "version": "0.0.5-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",
@@ -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