Skip to content

Commit 0293403

Browse files
authored
Add setValueConfig (#1)
* set version 0.0.2 * setValueConfig * changelog
1 parent 7766065 commit 0293403

File tree

7 files changed

+88
-49
lines changed

7 files changed

+88
-49
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# v0.0.2 (not released)
2+
3+
- Add setValueConfig
4+
5+
# v0.0.1
6+
7+
- Support useForm, useFieldArray, Controller

doc/transformation.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type rec useFormReturnOfInputs<'setValueAs> = {
4040
watch: variantOfInputs => watchReturnOfInputs,
4141
formState: formStateOfInputs,
4242
getFieldState: (variantOfInputs, formStateOfInputs) => fieldStateOfInputs,
43-
setValue: (variantOfInputs, ReactHookForm.value) => unit,
43+
setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit,
4444
reset: (~options:defaultValues=?) => unit
4545
}
4646
and controlOfInputs
@@ -57,6 +57,11 @@ and useFormParamsOfInputs<'resolver> = {
5757
defaultValues?: defaultValuesOfInput,
5858
mode?: [#onBlur | #onChange | #onSubmit | #onTouched | #all],
5959
}
60+
and setValueConfigOfInputs = {
61+
shouldValidate: bool,
62+
shouldDirty: bool,
63+
shouldTouch: bool,
64+
}
6065
6166
@module("react-hook-form")
6267
external useFormOfInputs: (

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.1",
3+
"version": "0.0.2",
44
"description": "ReScript PPX which generates the bindings for react-hook-form",
55
"license": "MIT",
66
"author": "Woonki Moon<wk.moon@greenlabs.co.kr>",

src/ppx/signature.ml

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ let map_type_decl
7676
~kind:
7777
(Ptype_record
7878
[
79-
Type.field
80-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
81-
~mut:Immutable (mknoloc "message")
79+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
80+
(mknoloc "message")
8281
(Typ.constr (lid "string") []);
8382
]);
8483
(* @unboxed type watchReturnOfInputs = String(string) | Number(float) *)
@@ -209,9 +208,9 @@ let map_type_decl
209208
^ capitalize record_name)
210209
[]));
211210
]);
212-
(* setValue: (variantOfInputs, ReactHookForm.value) => unit, *)
211+
(* setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit, *)
213212
Type.field ~mut:Immutable (mknoloc "setValue")
214-
(uncurried_core_type_arrow ~arity:2
213+
(uncurried_core_type_arrow ~arity:3
215214
[
216215
Typ.arrow Nolabel
217216
(Typ.constr
@@ -223,7 +222,12 @@ let map_type_decl
223222
(Longident.Ldot
224223
(Lident "ReactHookForm", "value")))
225224
[])
226-
(Typ.constr (lid "unit") []));
225+
(Typ.arrow (Optional "options")
226+
(Typ.constr
227+
(lid @@ "setValueConfigOf"
228+
^ capitalize record_name)
229+
[])
230+
(Typ.constr (lid "unit") [])));
227231
]);
228232
]);
229233
(* type controlOfInputs *)
@@ -243,14 +247,11 @@ let map_type_decl
243247
~kind:
244248
(Ptype_record
245249
[
246-
Type.field
247-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
248-
~mut:Immutable (mknoloc "required")
250+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
251+
(mknoloc "required")
249252
(Typ.constr (lid "bool") []);
250-
Type.field
251-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
252-
~mut:Immutable (mknoloc "setValueAs")
253-
(Typ.var "setValueAs");
253+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
254+
(mknoloc "setValueAs") (Typ.var "setValueAs");
254255
]);
255256
(* type formStateOfInputs = {isDirty: bool, isValid: bool, errors: fieldErrorsOfInputs} *)
256257
Type.mk
@@ -325,19 +326,15 @@ let map_type_decl
325326
~kind:
326327
(Ptype_record
327328
[
328-
Type.field
329-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
330-
~mut:Immutable (mknoloc "resolver")
331-
(Typ.var "resolver");
332-
Type.field
333-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
334-
~mut:Immutable (mknoloc "defaultValues")
329+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
330+
(mknoloc "resolver") (Typ.var "resolver");
331+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
332+
(mknoloc "defaultValues")
335333
(Typ.constr
336334
(lid @@ "defaultValuesOf" ^ capitalize record_name)
337335
[]);
338-
Type.field
339-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
340-
~mut:Immutable (mknoloc "mode")
336+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
337+
(mknoloc "mode")
341338
(Typ.variant
342339
[
343340
Rf.tag (mknoloc "onBlur") true [];
@@ -348,6 +345,22 @@ let map_type_decl
348345
]
349346
Closed None);
350347
]);
348+
Type.mk
349+
(mkloc ("setValueConfigOf" ^ capitalize record_name) ptype_loc)
350+
~priv:Public
351+
~kind:
352+
(Ptype_record
353+
[
354+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
355+
(mknoloc "shouldValidate")
356+
(Typ.constr (lid "bool") []);
357+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
358+
(mknoloc "shouldDirty")
359+
(Typ.constr (lid "bool") []);
360+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
361+
(mknoloc "shouldTouch")
362+
(Typ.constr (lid "bool") []);
363+
]);
351364
]
352365
in
353366

src/ppx/structure.ml

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ let map_type_decl
7676
~kind:
7777
(Ptype_record
7878
[
79-
Type.field
80-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
81-
~mut:Immutable (mknoloc "message")
79+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
80+
(mknoloc "message")
8281
(Typ.constr (lid "string") []);
8382
]);
8483
(* @unboxed type watchReturnOfInputs = String(string) | Number(float) *)
@@ -209,9 +208,9 @@ let map_type_decl
209208
^ capitalize record_name)
210209
[]));
211210
]);
212-
(* setValue: (variantOfInputs, ReactHookForm.value) => unit, *)
211+
(* setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit, *)
213212
Type.field ~mut:Immutable (mknoloc "setValue")
214-
(uncurried_core_type_arrow ~arity:2
213+
(uncurried_core_type_arrow ~arity:3
215214
[
216215
Typ.arrow Nolabel
217216
(Typ.constr
@@ -223,7 +222,12 @@ let map_type_decl
223222
(Longident.Ldot
224223
(Lident "ReactHookForm", "value")))
225224
[])
226-
(Typ.constr (lid "unit") []));
225+
(Typ.arrow (Optional "options")
226+
(Typ.constr
227+
(lid @@ "setValueConfigOf"
228+
^ capitalize record_name)
229+
[])
230+
(Typ.constr (lid "unit") [])));
227231
]);
228232
]);
229233
(* type controlOfInputs *)
@@ -243,14 +247,11 @@ let map_type_decl
243247
~kind:
244248
(Ptype_record
245249
[
246-
Type.field
247-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
248-
~mut:Immutable (mknoloc "required")
250+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
251+
(mknoloc "required")
249252
(Typ.constr (lid "bool") []);
250-
Type.field
251-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
252-
~mut:Immutable (mknoloc "setValueAs")
253-
(Typ.var "setValueAs");
253+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
254+
(mknoloc "setValueAs") (Typ.var "setValueAs");
254255
]);
255256
(* type formStateOfInputs = {isDirty: bool, isValid: bool, errors: fieldErrorsOfInputs} *)
256257
Type.mk
@@ -325,19 +326,15 @@ let map_type_decl
325326
~kind:
326327
(Ptype_record
327328
[
328-
Type.field
329-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
330-
~mut:Immutable (mknoloc "resolver")
331-
(Typ.var "resolver");
332-
Type.field
333-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
334-
~mut:Immutable (mknoloc "defaultValues")
329+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
330+
(mknoloc "resolver") (Typ.var "resolver");
331+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
332+
(mknoloc "defaultValues")
335333
(Typ.constr
336334
(lid @@ "defaultValuesOf" ^ capitalize record_name)
337335
[]);
338-
Type.field
339-
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
340-
~mut:Immutable (mknoloc "mode")
336+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
337+
(mknoloc "mode")
341338
(Typ.variant
342339
[
343340
Rf.tag (mknoloc "onBlur") true [];
@@ -348,6 +345,22 @@ let map_type_decl
348345
]
349346
Closed None);
350347
]);
348+
Type.mk
349+
(mkloc ("setValueConfigOf" ^ capitalize record_name) ptype_loc)
350+
~priv:Public
351+
~kind:
352+
(Ptype_record
353+
[
354+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
355+
(mknoloc "shouldValidate")
356+
(Typ.constr (lid "bool") []);
357+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
358+
(mknoloc "shouldDirty")
359+
(Typ.constr (lid "bool") []);
360+
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
361+
(mknoloc "shouldTouch")
362+
(Typ.constr (lid "bool") []);
363+
]);
351364
]
352365
in
353366

src/ppx/utils.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ let attr_named_arg = Attr.mk (mknoloc "res.namedArgLoc") (PStr [])
2525
let attr_template = Attr.mk (mknoloc "res.template") (PStr [])
2626
let attr_uapp = Attr.mk (mknoloc "res.uapp") (PStr [])
2727
let attr_brace = Attr.mk (mknoloc "res.braces") (PStr [])
28+
let attr_optional = Attr.mk (mknoloc "res.optional") (PStr [])
2829
let has_attribute { attr_name = { Location.txt } } = txt = attribute_name
2930

3031
let remove_optional_attribute (attrs : attributes) : attributes =

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.1"
3+
version: "0.0.2"
44
synopsis: "ReScript PPX for react-hook-form"
55
description: """
66
ReScript PPX which generates the bindings for react-hook-form

0 commit comments

Comments
 (0)