react-compose-form 0.0.1 → 0.0.2

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
@@ -7,7 +7,7 @@ npm i react-hook-form react-compose-form
7
7
  ## Usage
8
8
 
9
9
  ```tsx
10
- import {Form, FormControl, FormFieldProps} from "@kearisp/react-admin";
10
+ import {Form, FormControl, FormFieldProps} from "react-compose-form";
11
11
 
12
12
  type InputProps = {
13
13
  label: string;
@@ -31,7 +31,7 @@ const InputField = ({label, ...props}: FormFieldProps<InputProps>) => {
31
31
  ```
32
32
 
33
33
  ```tsx
34
- import {Form, FormControl, FormArray, useFormArray} from "@kearisp/react-admin";
34
+ import {Form, FormControl, FormArray, useFormArray} from "react-compose-form";
35
35
 
36
36
 
37
37
  const ExampleArray = ({ children }: PropsWithChildren) => {
@@ -0,0 +1,14 @@
1
+ import { FormEvent, ElementType, ComponentType, PropsWithChildren } from "react";
2
+ import { UseFormProps, FieldValues, SubmitHandler, SubmitErrorHandler } from "react-hook-form";
3
+ type FormComponentProps = {
4
+ className?: string;
5
+ onSubmit?: (e: FormEvent) => void;
6
+ };
7
+ export type FormProps<T extends FieldValues = FieldValues, C = any> = PropsWithChildren<UseFormProps<T, C> & {
8
+ as?: ElementType<FormComponentProps> | ComponentType<FormComponentProps>;
9
+ className?: string;
10
+ onSubmit?: SubmitHandler<T>;
11
+ onInvalid?: SubmitErrorHandler<T>;
12
+ }>;
13
+ export declare const Form: <T extends FieldValues = FieldValues, C = any>(props: FormProps<T, C>) => import("react/jsx-runtime").JSX.Element;
14
+ export {};
@@ -0,0 +1,31 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { jsx as _jsx } from "react/jsx-runtime";
13
+ import { useRef, useCallback } from "react";
14
+ import { useForm, FormProvider } from "react-hook-form";
15
+ export const Form = (props) => {
16
+ const { as: Component = "form", className, children, onSubmit, onInvalid } = props, rest = __rest(props, ["as", "className", "children", "onSubmit", "onInvalid"]);
17
+ const formProps = useForm(rest), submitRef = useRef(onSubmit), invalidRef = useRef(onInvalid);
18
+ submitRef.current = onSubmit;
19
+ invalidRef.current = onInvalid;
20
+ const handleSubmit = useCallback((data, event) => {
21
+ if (submitRef.current) {
22
+ submitRef.current(data, event);
23
+ }
24
+ }, []);
25
+ const handleInvalid = useCallback((errors, event) => {
26
+ if (invalidRef.current) {
27
+ invalidRef.current(errors, event);
28
+ }
29
+ }, []);
30
+ return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, { className: className, onSubmit: formProps.handleSubmit(handleSubmit, handleInvalid), children: children }) })));
31
+ };
@@ -0,0 +1,6 @@
1
+ import React, { PropsWithChildren, ComponentType, ElementType } from "react";
2
+ export type FormArrayProps = PropsWithChildren<{
3
+ as?: ComponentType<PropsWithChildren> | ElementType<PropsWithChildren>;
4
+ name: string;
5
+ }>;
6
+ export declare const FormArray: React.FC<FormArrayProps>;
@@ -0,0 +1,10 @@
1
+ import { ComponentType } from "react";
2
+ import { ControllerRenderProps, Message, ValidationRule, ControllerFieldState } from "react-hook-form";
3
+ export type FormFieldProps<P = unknown> = P & Partial<ControllerRenderProps & ControllerFieldState>;
4
+ export type FormControlProps<P = unknown> = Omit<P, "as" | "name" | "disabled" | "required" | keyof ControllerFieldState | keyof ControllerRenderProps> & {
5
+ as?: ComponentType<FormFieldProps<P>>;
6
+ name: string;
7
+ disabled?: boolean;
8
+ required?: Message | ValidationRule<boolean>;
9
+ };
10
+ export declare const FormControl: <P = unknown>(props: FormControlProps<P>) => import("react/jsx-runtime").JSX.Element;
@@ -10,15 +10,16 @@ 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 { useFormContext, Controller, } from "react-hook-form";
13
+ import { useFormContext, Controller } from "react-hook-form";
14
14
  import { useFormGroupName } from "./FormGroupContext";
15
+ import { FormControlContext } from "./FormControlContext";
15
16
  export const FormControl = (props) => {
16
17
  const { as: Component = "input", name, disabled = false, required } = props, rest = __rest(props, ["as", "name", "disabled", "required"]);
17
18
  const { control } = useFormContext();
18
19
  const fullName = useFormGroupName(name);
19
20
  return (_jsx(Controller, { control: control, disabled: disabled, name: fullName, rules: {
20
21
  required
21
- }, render: ({ field }) => {
22
- return (_jsx(Component, Object.assign({}, rest, field)));
22
+ }, render: ({ field, fieldState }) => {
23
+ return (_jsx(FormControlContext, { value: fieldState, children: _jsx(Component, Object.assign({}, rest, field)) }));
23
24
  } }));
24
25
  };
@@ -0,0 +1,3 @@
1
+ import { ControllerFieldState } from "react-hook-form";
2
+ export declare const FormControlContext: import("react").Context<ControllerFieldState>;
3
+ export declare const useFormControl: () => ControllerFieldState;
@@ -0,0 +1,8 @@
1
+ import { useContext, createContext } from "react";
2
+ export const FormControlContext = createContext({
3
+ invalid: false,
4
+ isDirty: false,
5
+ isTouched: false,
6
+ isValidating: false
7
+ });
8
+ export const useFormControl = () => useContext(FormControlContext);
@@ -1,5 +1,5 @@
1
1
  import { useMemo, useContext, createContext } from "react";
2
- import { joinDotNotation } from "../../utils";
2
+ import { joinDotNotation } from "../utils";
3
3
  export const FormGroupContext = createContext({});
4
4
  export const useFormGroupName = (name = "") => {
5
5
  const { name: parentName = "" } = useContext(FormGroupContext);
@@ -1 +1,7 @@
1
1
  export * from "./Form";
2
+ export * from "./FormArray";
3
+ export * from "./FormArrayContext";
4
+ export * from "./FormControl";
5
+ export * from "./FormControlContext";
6
+ export * from "./FormGroup";
7
+ export * from "./FormGroupContext";
@@ -1 +1,7 @@
1
1
  export * from "./Form";
2
+ export * from "./FormArray";
3
+ export * from "./FormArrayContext";
4
+ export * from "./FormControl";
5
+ export * from "./FormControlContext";
6
+ export * from "./FormGroup";
7
+ export * from "./FormGroupContext";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-compose-form",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
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,16 +0,0 @@
1
- import { FormEvent, ComponentType, PropsWithChildren } from "react";
2
- import { Resolver, FieldValues, Mode, DefaultValues } from "react-hook-form";
3
- export type FormProps<T extends FieldValues = FieldValues> = PropsWithChildren<{
4
- as?: ComponentType<{
5
- className?: string;
6
- onSubmit: (e: FormEvent) => void;
7
- }>;
8
- className?: string;
9
- mode?: Mode;
10
- resolver?: Resolver<T>;
11
- values?: T;
12
- defaultValues?: DefaultValues<T>;
13
- onSubmit?: (data: T) => void;
14
- onError?: (err: unknown) => void;
15
- }>;
16
- export declare const Form: <T extends FieldValues = FieldValues>(props: FormProps<T>) => import("react/jsx-runtime").JSX.Element;
@@ -1,18 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { useCallback } from "react";
3
- import { useForm, FormProvider } from "react-hook-form";
4
- export const Form = (props) => {
5
- const { as: Component = "form", className, mode, resolver, values, defaultValues, children, onSubmit, onError } = props;
6
- const formProps = useForm({
7
- mode,
8
- resolver,
9
- values,
10
- defaultValues
11
- });
12
- const handleSubmit = useCallback((data) => {
13
- if (onSubmit) {
14
- onSubmit(data);
15
- }
16
- }, [onSubmit]);
17
- return (_jsx(FormProvider, Object.assign({}, formProps, { children: _jsx(Component, { className: className, onSubmit: formProps.handleSubmit(handleSubmit, onError), children: children }) })));
18
- };
@@ -1,6 +0,0 @@
1
- import React, { PropsWithChildren, ComponentType } from "react";
2
- export type FormArrayProps = PropsWithChildren<{
3
- as?: ComponentType<PropsWithChildren>;
4
- name: string;
5
- }>;
6
- export declare const FormArray: React.FC<FormArrayProps>;
@@ -1,10 +0,0 @@
1
- import { ComponentType } from "react";
2
- import { ControllerRenderProps, Message, ValidationRule } from "react-hook-form";
3
- export type FormFieldProps<P = unknown> = P & ControllerRenderProps;
4
- export type FormControlProps<P = unknown> = P & {
5
- as?: ComponentType<FormFieldProps<P>>;
6
- name: string;
7
- disabled?: boolean;
8
- required?: Message | ValidationRule<boolean>;
9
- };
10
- export declare const FormControl: <P = unknown>(props: FormControlProps<P>) => import("react/jsx-runtime").JSX.Element;
@@ -1,5 +0,0 @@
1
- export * from "./Form";
2
- export * from "./FormArray";
3
- export * from "./FormArrayContext";
4
- export * from "./FormControl";
5
- export * from "./FormGroup";
@@ -1,5 +0,0 @@
1
- export * from "./Form";
2
- export * from "./FormArray";
3
- export * from "./FormArrayContext";
4
- export * from "./FormControl";
5
- export * from "./FormGroup";
File without changes
File without changes