react-compose-form 0.0.4 → 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 +2 -2
- package/lib/blocks/FormControl.d.ts +2 -1
- package/lib/blocks/FormControl.js +7 -3
- 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)) }));
|