react-compose-form 0.0.4 → 0.0.5-beta.1

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,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
- export const FormControl = (props) => {
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
- 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)) }));
@@ -0,0 +1,8 @@
1
+ import { ElementType } from "react";
2
+ import { Control } from "react-hook-form";
3
+ import { PolymorphicComponentProps } from "../types/PolymorphicComponentProps";
4
+ type FormSubmitProps<T extends ElementType = "button"> = PolymorphicComponentProps<T, {
5
+ control?: Control;
6
+ }>;
7
+ export declare const FormSubmit: <T extends ElementType = "button">(props: FormSubmitProps<T>) => import("react/jsx-runtime").JSX.Element;
8
+ export {};
@@ -0,0 +1,25 @@
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 { useFormContext } from "react-hook-form";
14
+ export const FormSubmit = (props) => {
15
+ const Component = props.control ? SubmitWrapper : SubmitWrapperWithContext;
16
+ return (_jsx(Component, Object.assign({}, props, { control: props.control })));
17
+ };
18
+ const SubmitWrapperWithContext = (props) => {
19
+ const { control } = useFormContext();
20
+ return (_jsx(SubmitWrapper, Object.assign({}, props, { control: control })));
21
+ };
22
+ const SubmitWrapper = (props) => {
23
+ const { as: Component = "button", control } = props, rest = __rest(props, ["as", "control"]);
24
+ return (_jsx(Component, Object.assign({}, rest)));
25
+ };
@@ -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 {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-compose-form",
3
- "version": "0.0.4",
3
+ "version": "0.0.5-beta.1",
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",