Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.0.7 (unreleased)

- Add `getValues` to `useForm` hook

# v0.0.6

- Remove `-bs-super-errors` flag for the compatibility with ReScript v12
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ let default = () => {

Js.log(watch(Example))

Js.log(getValues(Example))

let exampleFieldState = getFieldState(Example, formState)
Js.log(exampleFieldState)
Js.log(setValue)
Expand Down
6 changes: 6 additions & 0 deletions doc/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ let make = () => {

Js.log(watch(Example))

Js.log(getValues(Example))

<form onSubmit={handleSubmit(onSubmit)}>
<input {...register(Example)} defaultValue="test" />
<input {...register(ExampleRequired, ~options={required: true})} />
Expand Down Expand Up @@ -57,6 +59,8 @@ let make = () => {

Js.log(watch(FirstName))

Js.log(getValues(FirstName))

<form onSubmit={handleSubmit(onSubmit)}>
<input {...register(FirstName)} />
<p>
Expand Down Expand Up @@ -138,6 +142,8 @@ let default = () => {

Js.log(watch(MyCheckBox))

Js.log(getValues(MyCheckBox))

<form onSubmit={handleSubmit(onSubmit)}>
<ControllerOfInputs
name={MyCheckBox}
Expand Down
15 changes: 8 additions & 7 deletions doc/transformation.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type inputs = {
// generated
type inputsWithId = {
id: string,
example: string,
exampleRequired: string,
cart: array<item>
example?: string,
exampleRequired?: string,
cart?: array<item>
}
type defaultValuesOfInputs = {
example?: string,
Expand All @@ -31,19 +31,20 @@ type defaultValuesOfInputs = {
type rec fieldStateOfInputs = {invalid: bool, isDirty: bool, isTouched: bool, error: fieldErrorOfInputs}
and fieldErrorOfInputs = {message?: string}
@unboxed
type rec watchReturnOfInputs =
type rec valuesOfInputs =
| @as(null) Null
| Bool(bool)
| Number(float)
| String(string)
| Object(Js.Dict.t<watchReturnOfInputs>)
| Array(array<watchReturnOfInputs>)
| Object(Js.Dict.t<valuesOfInputs>)
| Array(array<valuesOfInputs>)

type rec useFormReturnOfInputs<'setValueAs> = {
control: controlOfInputs,
register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps,
handleSubmit: (inputs => unit) => JsxEvent.Form.t => unit,
watch: variantOfInputs => option<watchReturnOfInputs>,
watch: variantOfInputs => option<valuesOfInputs>,
getValues: variantOfInputs => option<valuesOfInputs>
formState: formStateOfInputs,
getFieldState: (variantOfInputs, formStateOfInputs) => fieldStateOfInputs,
setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@greenlabs/ppx-rhf",
"version": "0.0.6",
"version": "0.0.7",
"description": "ReScript PPX which generates the bindings for react-hook-form",
"license": "MIT",
"author": "Woonki Moon<[email protected]>",
Expand Down
42 changes: 32 additions & 10 deletions src/ppx/signature.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ let map_type_decl
let type_decls =
Sig.type_ Nonrecursive
[
(* type inputsWithId = {id: string, ...} *)
(* type inputsWithId = {id: string, example?: string, ...} *)
Type.mk
(mkloc (record_name ^ "WithId") ptype_loc)
~priv:Public
~kind:
(Ptype_record
(Type.field ~mut:Immutable (mknoloc "id")
(Typ.constr (lid "string") [])
:: lds));
:: (lds
|> List.map (fun ld ->
{
ld with
pld_attributes =
remove_optional_attribute ld.pld_attributes
|> add_optional_attribute;
}))));
]
in
let type_decls1 =
Expand Down Expand Up @@ -116,7 +123,7 @@ let map_type_decl
| Array(array<watchReturnOfInputs>)
*)
Type.mk
(mkloc ("watchReturnOf" ^ capitalize record_name) ptype_loc)
(mkloc ("valuesOf" ^ capitalize record_name) ptype_loc)
~attrs:[ Attr.mk (mknoloc "unboxed") (PStr []) ]
~priv:Public
~kind:
Expand All @@ -142,8 +149,7 @@ let map_type_decl
[
Typ.constr
(lid
("watchReturnOf"
^ capitalize record_name))
("valuesOf" ^ capitalize record_name))
[];
];
]);
Expand All @@ -155,8 +161,7 @@ let map_type_decl
[
Typ.constr
(lid
("watchReturnOf"
^ capitalize record_name))
("valuesOf" ^ capitalize record_name))
[];
];
]);
Expand All @@ -170,7 +175,8 @@ let map_type_decl
control: controlOfInputs,
register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps,
handleSubmit: (inputs => unit) => JsxEvent.Form.t => unit,
watch: variantOfInputs => watchReturnOfInputs,
watch: variantOfInputs => valuesOfInputs,
getValues: variantOfInputs => option<valuesOfInputs>,
reset: (~options: defaultValuesOfInputs=?) => unit,
formState: formStateOfInputs,
} *)
Expand Down Expand Up @@ -229,7 +235,7 @@ let map_type_decl
(Typ.constr (lid "unit") []);
]);
]);
(* watch: variantOfInputs => option<watchReturnOfInputs>, *)
(* watch: variantOfInputs => option<valuesOfInputs>, *)
Type.field ~mut:Immutable (mknoloc "watch")
(uncurried_core_type_arrow ~arity:1
[
Expand All @@ -240,7 +246,23 @@ let map_type_decl
(Typ.constr (lid "option")
[
Typ.constr
(lid @@ "watchReturnOf"
(lid @@ "valuesOf"
^ capitalize record_name)
[];
]);
]);
(* getValues: variantOfInputs => option<valuesOfInputs>, *)
Type.field ~mut:Immutable (mknoloc "getValues")
(uncurried_core_type_arrow ~arity:1
[
Typ.arrow Nolabel
(Typ.constr
(lid @@ "variantOf" ^ capitalize record_name)
[])
(Typ.constr (lid "option")
[
Typ.constr
(lid @@ "valuesOf"
^ capitalize record_name)
[];
]);
Expand Down
41 changes: 31 additions & 10 deletions src/ppx/structure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ let map_type_decl
let type_decls =
Str.type_ Nonrecursive
[
(* type inputsWithId = {id: string, ...} *)
(* type inputsWithId = {id: string, example?: string, ...} *)
Type.mk
(mkloc (record_name ^ "WithId") ptype_loc)
~priv:Public
~kind:
(Ptype_record
(Type.field ~mut:Immutable (mknoloc "id")
(Typ.constr (lid "string") [])
:: lds));
:: (lds
|> List.map (fun ld ->
{
ld with
pld_attributes =
add_optional_attribute ld.pld_attributes;
}))));
]
in
let type_decls1 =
Expand Down Expand Up @@ -116,7 +122,7 @@ let map_type_decl
| Array(array<watchReturnOfInputs>)
*)
Type.mk
(mkloc ("watchReturnOf" ^ capitalize record_name) ptype_loc)
(mkloc ("valuesOf" ^ capitalize record_name) ptype_loc)
~attrs:[ Attr.mk (mknoloc "unboxed") (PStr []) ]
~priv:Public
~kind:
Expand All @@ -142,8 +148,7 @@ let map_type_decl
[
Typ.constr
(lid
("watchReturnOf"
^ capitalize record_name))
("valuesOf" ^ capitalize record_name))
[];
];
]);
Expand All @@ -155,8 +160,7 @@ let map_type_decl
[
Typ.constr
(lid
("watchReturnOf"
^ capitalize record_name))
("valuesOf" ^ capitalize record_name))
[];
];
]);
Expand All @@ -170,7 +174,8 @@ let map_type_decl
control: controlOfInputs,
register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps,
handleSubmit: (inputs => unit) => JsxEvent.Form.t => unit,
watch: variantOfInputs => watchReturnOfInputs,
watch: variantOfInputs => valuesOfInputs,
getValues: variantOfInputs => option<valuesOfInputs>,
reset: (~options: defaultValuesOfInputs=?) => unit,
formState: formStateOfInputs,
} *)
Expand Down Expand Up @@ -229,7 +234,7 @@ let map_type_decl
(Typ.constr (lid "unit") []);
]);
]);
(* watch: variantOfInputs => option<watchReturnOfInputs>, *)
(* watch: variantOfInputs => option<valuesOfInputs>, *)
Type.field ~mut:Immutable (mknoloc "watch")
(uncurried_core_type_arrow ~arity:1
[
Expand All @@ -240,7 +245,23 @@ let map_type_decl
(Typ.constr (lid "option")
[
Typ.constr
(lid @@ "watchReturnOf"
(lid @@ "valuesOf"
^ capitalize record_name)
[];
]);
]);
(* getValues: variantOfInputs => option<valuesOfInputs>, *)
Type.field ~mut:Immutable (mknoloc "getValues")
(uncurried_core_type_arrow ~arity:1
[
Typ.arrow Nolabel
(Typ.constr
(lid @@ "variantOf" ^ capitalize record_name)
[])
(Typ.constr (lid "option")
[
Typ.constr
(lid @@ "valuesOf"
^ capitalize record_name)
[];
]);
Expand Down
2 changes: 1 addition & 1 deletion src/ppx_rhf.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
opam-version: "2.0"
name: "ppx_rhf"
version: "0.0.6"
version: "0.0.7"
synopsis: "ReScript PPX for react-hook-form"
description: """
ReScript PPX which generates the bindings for react-hook-form
Expand Down
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.4",
"react-router-dom": "^6.20.0",
"rescript": "12.0.0-alpha.4",
"rescript": "12.0.0-alpha.5",
"typescript": "^5.2.2",
"vite": "^5.0.0"
}
Expand Down
8 changes: 4 additions & 4 deletions test/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion test/src/pages/basic_res/Basic.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ type inputs = {

@react.component
let default = () => {
let {register, handleSubmit, watch, formState, getFieldState, setValue} = useFormOfInputs()
let {
register,
handleSubmit,
watch,
getValues,
formState,
getFieldState,
setValue,
} = useFormOfInputs()
let onSubmit = (data: inputs) => Js.log(data)

switch watch(Example) {
Expand All @@ -19,6 +27,16 @@ let default = () => {
| None => Js.log("undefined")
}

switch getValues(Example) {
| Some(Null) => Js.log("null")
| Some(Bool(v)) => Js.log(v)
| Some(Number(v)) => Js.log(v)
| Some(String(v)) => Js.log(v)
| Some(Object(v)) => Js.log(v)
| Some(Array(v)) => Js.log(v)
| None => Js.log("undefined")
}

let exampleFieldState = getFieldState(Example, formState)
Js.log(exampleFieldState)
Js.log(setValue)
Expand Down
4 changes: 3 additions & 1 deletion test/src/pages/controller_res/Controller.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ module Checkbox = {

@react.component @genType
let default = () => {
let {control, handleSubmit, watch} = useFormOfInputs()
let {control, handleSubmit, watch, getValues} = useFormOfInputs()
let onSubmit = (data: inputs) => Js.log(data)

Js.log(watch(MyCheckBox))

Js.log(getValues(MyCheckBox))

<form onSubmit={handleSubmit(onSubmit)}>
<ControllerOfInputs
name={MyCheckBox}
Expand Down
4 changes: 3 additions & 1 deletion test/src/pages/schema_validation_res/SchemaValidation.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let schema = Zod.z->Zod.object(

@react.component @genType
let default = () => {
let {register, handleSubmit, watch, formState: {errors}} = useFormOfSchema(
let {register, handleSubmit, watch, getValues, formState: {errors}} = useFormOfSchema(
~options={
resolver: Resolver.zodResolver(schema),
defaultValues: {
Expand All @@ -28,6 +28,8 @@ let default = () => {

Js.log(watch(FirstName))

Js.log(getValues(FirstName))

<form onSubmit={handleSubmit(onSubmit)}>
<input {...register(FirstName)} />
<p>
Expand Down
Loading