Skip to content

Commit 3cb9036

Browse files
authored
Add getValues api (#9)
* set version v0.0.7 * add getValues api
1 parent d575254 commit 3cb9036

File tree

13 files changed

+115
-37
lines changed

13 files changed

+115
-37
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v0.0.7 (unreleased)
2+
3+
- Add `getValues` to `useForm` hook
4+
15
# v0.0.6
26

37
- Remove `-bs-super-errors` flag for the compatibility with ReScript v12

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ let default = () => {
1818
1919
Js.log(watch(Example))
2020
21+
Js.log(getValues(Example))
22+
2123
let exampleFieldState = getFieldState(Example, formState)
2224
Js.log(exampleFieldState)
2325
Js.log(setValue)

doc/examples.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ let make = () => {
1414
1515
Js.log(watch(Example))
1616
17+
Js.log(getValues(Example))
18+
1719
<form onSubmit={handleSubmit(onSubmit)}>
1820
<input {...register(Example)} defaultValue="test" />
1921
<input {...register(ExampleRequired, ~options={required: true})} />
@@ -57,6 +59,8 @@ let make = () => {
5759
5860
Js.log(watch(FirstName))
5961
62+
Js.log(getValues(FirstName))
63+
6064
<form onSubmit={handleSubmit(onSubmit)}>
6165
<input {...register(FirstName)} />
6266
<p>
@@ -138,6 +142,8 @@ let default = () => {
138142
139143
Js.log(watch(MyCheckBox))
140144
145+
Js.log(getValues(MyCheckBox))
146+
141147
<form onSubmit={handleSubmit(onSubmit)}>
142148
<ControllerOfInputs
143149
name={MyCheckBox}

doc/transformation.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ type inputs = {
1919
// generated
2020
type inputsWithId = {
2121
id: string,
22-
example: string,
23-
exampleRequired: string,
24-
cart: array<item>
22+
example?: string,
23+
exampleRequired?: string,
24+
cart?: array<item>
2525
}
2626
type defaultValuesOfInputs = {
2727
example?: string,
@@ -31,19 +31,20 @@ type defaultValuesOfInputs = {
3131
type rec fieldStateOfInputs = {invalid: bool, isDirty: bool, isTouched: bool, error: fieldErrorOfInputs}
3232
and fieldErrorOfInputs = {message?: string}
3333
@unboxed
34-
type rec watchReturnOfInputs =
34+
type rec valuesOfInputs =
3535
| @as(null) Null
3636
| Bool(bool)
3737
| Number(float)
3838
| String(string)
39-
| Object(Js.Dict.t<watchReturnOfInputs>)
40-
| Array(array<watchReturnOfInputs>)
39+
| Object(Js.Dict.t<valuesOfInputs>)
40+
| Array(array<valuesOfInputs>)
4141
4242
type rec useFormReturnOfInputs<'setValueAs> = {
4343
control: controlOfInputs,
4444
register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps,
4545
handleSubmit: (inputs => unit) => JsxEvent.Form.t => unit,
46-
watch: variantOfInputs => option<watchReturnOfInputs>,
46+
watch: variantOfInputs => option<valuesOfInputs>,
47+
getValues: variantOfInputs => option<valuesOfInputs>
4748
formState: formStateOfInputs,
4849
getFieldState: (variantOfInputs, formStateOfInputs) => fieldStateOfInputs,
4950
setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@greenlabs/ppx-rhf",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "ReScript PPX which generates the bindings for react-hook-form",
55
"license": "MIT",
66
"author": "Woonki Moon<[email protected]>",

src/ppx/signature.ml

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@ let map_type_decl
1818
let type_decls =
1919
Sig.type_ Nonrecursive
2020
[
21-
(* type inputsWithId = {id: string, ...} *)
21+
(* type inputsWithId = {id: string, example?: string, ...} *)
2222
Type.mk
2323
(mkloc (record_name ^ "WithId") ptype_loc)
2424
~priv:Public
2525
~kind:
2626
(Ptype_record
2727
(Type.field ~mut:Immutable (mknoloc "id")
2828
(Typ.constr (lid "string") [])
29-
:: lds));
29+
:: (lds
30+
|> List.map (fun ld ->
31+
{
32+
ld with
33+
pld_attributes =
34+
remove_optional_attribute ld.pld_attributes
35+
|> add_optional_attribute;
36+
}))));
3037
]
3138
in
3239
let type_decls1 =
@@ -116,7 +123,7 @@ let map_type_decl
116123
| Array(array<watchReturnOfInputs>)
117124
*)
118125
Type.mk
119-
(mkloc ("watchReturnOf" ^ capitalize record_name) ptype_loc)
126+
(mkloc ("valuesOf" ^ capitalize record_name) ptype_loc)
120127
~attrs:[ Attr.mk (mknoloc "unboxed") (PStr []) ]
121128
~priv:Public
122129
~kind:
@@ -142,8 +149,7 @@ let map_type_decl
142149
[
143150
Typ.constr
144151
(lid
145-
("watchReturnOf"
146-
^ capitalize record_name))
152+
("valuesOf" ^ capitalize record_name))
147153
[];
148154
];
149155
]);
@@ -155,8 +161,7 @@ let map_type_decl
155161
[
156162
Typ.constr
157163
(lid
158-
("watchReturnOf"
159-
^ capitalize record_name))
164+
("valuesOf" ^ capitalize record_name))
160165
[];
161166
];
162167
]);
@@ -170,7 +175,8 @@ let map_type_decl
170175
control: controlOfInputs,
171176
register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps,
172177
handleSubmit: (inputs => unit) => JsxEvent.Form.t => unit,
173-
watch: variantOfInputs => watchReturnOfInputs,
178+
watch: variantOfInputs => valuesOfInputs,
179+
getValues: variantOfInputs => option<valuesOfInputs>,
174180
reset: (~options: defaultValuesOfInputs=?) => unit,
175181
formState: formStateOfInputs,
176182
} *)
@@ -229,7 +235,7 @@ let map_type_decl
229235
(Typ.constr (lid "unit") []);
230236
]);
231237
]);
232-
(* watch: variantOfInputs => option<watchReturnOfInputs>, *)
238+
(* watch: variantOfInputs => option<valuesOfInputs>, *)
233239
Type.field ~mut:Immutable (mknoloc "watch")
234240
(uncurried_core_type_arrow ~arity:1
235241
[
@@ -240,7 +246,23 @@ let map_type_decl
240246
(Typ.constr (lid "option")
241247
[
242248
Typ.constr
243-
(lid @@ "watchReturnOf"
249+
(lid @@ "valuesOf"
250+
^ capitalize record_name)
251+
[];
252+
]);
253+
]);
254+
(* getValues: variantOfInputs => option<valuesOfInputs>, *)
255+
Type.field ~mut:Immutable (mknoloc "getValues")
256+
(uncurried_core_type_arrow ~arity:1
257+
[
258+
Typ.arrow Nolabel
259+
(Typ.constr
260+
(lid @@ "variantOf" ^ capitalize record_name)
261+
[])
262+
(Typ.constr (lid "option")
263+
[
264+
Typ.constr
265+
(lid @@ "valuesOf"
244266
^ capitalize record_name)
245267
[];
246268
]);

src/ppx/structure.ml

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@ let map_type_decl
1818
let type_decls =
1919
Str.type_ Nonrecursive
2020
[
21-
(* type inputsWithId = {id: string, ...} *)
21+
(* type inputsWithId = {id: string, example?: string, ...} *)
2222
Type.mk
2323
(mkloc (record_name ^ "WithId") ptype_loc)
2424
~priv:Public
2525
~kind:
2626
(Ptype_record
2727
(Type.field ~mut:Immutable (mknoloc "id")
2828
(Typ.constr (lid "string") [])
29-
:: lds));
29+
:: (lds
30+
|> List.map (fun ld ->
31+
{
32+
ld with
33+
pld_attributes =
34+
add_optional_attribute ld.pld_attributes;
35+
}))));
3036
]
3137
in
3238
let type_decls1 =
@@ -116,7 +122,7 @@ let map_type_decl
116122
| Array(array<watchReturnOfInputs>)
117123
*)
118124
Type.mk
119-
(mkloc ("watchReturnOf" ^ capitalize record_name) ptype_loc)
125+
(mkloc ("valuesOf" ^ capitalize record_name) ptype_loc)
120126
~attrs:[ Attr.mk (mknoloc "unboxed") (PStr []) ]
121127
~priv:Public
122128
~kind:
@@ -142,8 +148,7 @@ let map_type_decl
142148
[
143149
Typ.constr
144150
(lid
145-
("watchReturnOf"
146-
^ capitalize record_name))
151+
("valuesOf" ^ capitalize record_name))
147152
[];
148153
];
149154
]);
@@ -155,8 +160,7 @@ let map_type_decl
155160
[
156161
Typ.constr
157162
(lid
158-
("watchReturnOf"
159-
^ capitalize record_name))
163+
("valuesOf" ^ capitalize record_name))
160164
[];
161165
];
162166
]);
@@ -170,7 +174,8 @@ let map_type_decl
170174
control: controlOfInputs,
171175
register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps,
172176
handleSubmit: (inputs => unit) => JsxEvent.Form.t => unit,
173-
watch: variantOfInputs => watchReturnOfInputs,
177+
watch: variantOfInputs => valuesOfInputs,
178+
getValues: variantOfInputs => option<valuesOfInputs>,
174179
reset: (~options: defaultValuesOfInputs=?) => unit,
175180
formState: formStateOfInputs,
176181
} *)
@@ -229,7 +234,7 @@ let map_type_decl
229234
(Typ.constr (lid "unit") []);
230235
]);
231236
]);
232-
(* watch: variantOfInputs => option<watchReturnOfInputs>, *)
237+
(* watch: variantOfInputs => option<valuesOfInputs>, *)
233238
Type.field ~mut:Immutable (mknoloc "watch")
234239
(uncurried_core_type_arrow ~arity:1
235240
[
@@ -240,7 +245,23 @@ let map_type_decl
240245
(Typ.constr (lid "option")
241246
[
242247
Typ.constr
243-
(lid @@ "watchReturnOf"
248+
(lid @@ "valuesOf"
249+
^ capitalize record_name)
250+
[];
251+
]);
252+
]);
253+
(* getValues: variantOfInputs => option<valuesOfInputs>, *)
254+
Type.field ~mut:Immutable (mknoloc "getValues")
255+
(uncurried_core_type_arrow ~arity:1
256+
[
257+
Typ.arrow Nolabel
258+
(Typ.constr
259+
(lid @@ "variantOf" ^ capitalize record_name)
260+
[])
261+
(Typ.constr (lid "option")
262+
[
263+
Typ.constr
264+
(lid @@ "valuesOf"
244265
^ capitalize record_name)
245266
[];
246267
]);

src/ppx_rhf.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
opam-version: "2.0"
22
name: "ppx_rhf"
3-
version: "0.0.6"
3+
version: "0.0.7"
44
synopsis: "ReScript PPX for react-hook-form"
55
description: """
66
ReScript PPX which generates the bindings for react-hook-form

test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"eslint-plugin-react-hooks": "^4.6.0",
3333
"eslint-plugin-react-refresh": "^0.4.4",
3434
"react-router-dom": "^6.20.0",
35-
"rescript": "12.0.0-alpha.4",
35+
"rescript": "12.0.0-alpha.5",
3636
"typescript": "^5.2.2",
3737
"vite": "^5.0.0"
3838
}

test/pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)