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 +2 -2
- package/lib/blocks/FormControl.d.ts +2 -1
- package/lib/blocks/FormControl.js +7 -3
- package/lib/blocks/FormSubmit.d.ts +8 -0
- package/lib/blocks/FormSubmit.js +25 -0
- package/lib/types/PolymorphicComponentProps.d.ts +7 -0
- package/lib/types/PolymorphicComponentProps.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# react-compose-form
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/react-compose-form)
|
|
4
|
-
[](https://github.com/kearisp/react-compose-form/actions/workflows/publish-beta.yml)
|
|
5
5
|
[](https://github.com/kearisp/react-compose-form/blob/master/LICENSE)
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/react-compose-form)
|
|
8
8
|
[](https://bundlephobia.com/package/react-compose-form)
|
|
9
|
-

|
|
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
|
-
|
|
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
|
-
|
|
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 {};
|