Skip to content

Commit

Permalink
Add setValueConfig (#1)
Browse files Browse the repository at this point in the history
* set version 0.0.2

* setValueConfig

* changelog
  • Loading branch information
mununki authored Dec 11, 2023
1 parent 7766065 commit 0293403
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 49 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# v0.0.2 (not released)

- Add setValueConfig

# v0.0.1

- Support useForm, useFieldArray, Controller
7 changes: 6 additions & 1 deletion doc/transformation.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type rec useFormReturnOfInputs<'setValueAs> = {
watch: variantOfInputs => watchReturnOfInputs,
formState: formStateOfInputs,
getFieldState: (variantOfInputs, formStateOfInputs) => fieldStateOfInputs,
setValue: (variantOfInputs, ReactHookForm.value) => unit,
setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit,
reset: (~options:defaultValues=?) => unit
}
and controlOfInputs
Expand All @@ -57,6 +57,11 @@ and useFormParamsOfInputs<'resolver> = {
defaultValues?: defaultValuesOfInput,
mode?: [#onBlur | #onChange | #onSubmit | #onTouched | #all],
}
and setValueConfigOfInputs = {
shouldValidate: bool,
shouldDirty: bool,
shouldTouch: bool,
}
@module("react-hook-form")
external useFormOfInputs: (
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.1",
"version": "0.0.2",
"description": "ReScript PPX which generates the bindings for react-hook-form",
"license": "MIT",
"author": "Woonki Moon<wk.moon@greenlabs.co.kr>",
Expand Down
59 changes: 36 additions & 23 deletions src/ppx/signature.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ let map_type_decl
~kind:
(Ptype_record
[
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "message")
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "message")
(Typ.constr (lid "string") []);
]);
(* @unboxed type watchReturnOfInputs = String(string) | Number(float) *)
Expand Down Expand Up @@ -209,9 +208,9 @@ let map_type_decl
^ capitalize record_name)
[]));
]);
(* setValue: (variantOfInputs, ReactHookForm.value) => unit, *)
(* setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit, *)
Type.field ~mut:Immutable (mknoloc "setValue")
(uncurried_core_type_arrow ~arity:2
(uncurried_core_type_arrow ~arity:3
[
Typ.arrow Nolabel
(Typ.constr
Expand All @@ -223,7 +222,12 @@ let map_type_decl
(Longident.Ldot
(Lident "ReactHookForm", "value")))
[])
(Typ.constr (lid "unit") []));
(Typ.arrow (Optional "options")
(Typ.constr
(lid @@ "setValueConfigOf"
^ capitalize record_name)
[])
(Typ.constr (lid "unit") [])));
]);
]);
(* type controlOfInputs *)
Expand All @@ -243,14 +247,11 @@ let map_type_decl
~kind:
(Ptype_record
[
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "required")
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "required")
(Typ.constr (lid "bool") []);
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "setValueAs")
(Typ.var "setValueAs");
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "setValueAs") (Typ.var "setValueAs");
]);
(* type formStateOfInputs = {isDirty: bool, isValid: bool, errors: fieldErrorsOfInputs} *)
Type.mk
Expand Down Expand Up @@ -325,19 +326,15 @@ let map_type_decl
~kind:
(Ptype_record
[
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "resolver")
(Typ.var "resolver");
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "defaultValues")
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "resolver") (Typ.var "resolver");
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "defaultValues")
(Typ.constr
(lid @@ "defaultValuesOf" ^ capitalize record_name)
[]);
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "mode")
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "mode")
(Typ.variant
[
Rf.tag (mknoloc "onBlur") true [];
Expand All @@ -348,6 +345,22 @@ let map_type_decl
]
Closed None);
]);
Type.mk
(mkloc ("setValueConfigOf" ^ capitalize record_name) ptype_loc)
~priv:Public
~kind:
(Ptype_record
[
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "shouldValidate")
(Typ.constr (lid "bool") []);
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "shouldDirty")
(Typ.constr (lid "bool") []);
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "shouldTouch")
(Typ.constr (lid "bool") []);
]);
]
in

Expand Down
59 changes: 36 additions & 23 deletions src/ppx/structure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ let map_type_decl
~kind:
(Ptype_record
[
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "message")
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "message")
(Typ.constr (lid "string") []);
]);
(* @unboxed type watchReturnOfInputs = String(string) | Number(float) *)
Expand Down Expand Up @@ -209,9 +208,9 @@ let map_type_decl
^ capitalize record_name)
[]));
]);
(* setValue: (variantOfInputs, ReactHookForm.value) => unit, *)
(* setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit, *)
Type.field ~mut:Immutable (mknoloc "setValue")
(uncurried_core_type_arrow ~arity:2
(uncurried_core_type_arrow ~arity:3
[
Typ.arrow Nolabel
(Typ.constr
Expand All @@ -223,7 +222,12 @@ let map_type_decl
(Longident.Ldot
(Lident "ReactHookForm", "value")))
[])
(Typ.constr (lid "unit") []));
(Typ.arrow (Optional "options")
(Typ.constr
(lid @@ "setValueConfigOf"
^ capitalize record_name)
[])
(Typ.constr (lid "unit") [])));
]);
]);
(* type controlOfInputs *)
Expand All @@ -243,14 +247,11 @@ let map_type_decl
~kind:
(Ptype_record
[
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "required")
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "required")
(Typ.constr (lid "bool") []);
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "setValueAs")
(Typ.var "setValueAs");
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "setValueAs") (Typ.var "setValueAs");
]);
(* type formStateOfInputs = {isDirty: bool, isValid: bool, errors: fieldErrorsOfInputs} *)
Type.mk
Expand Down Expand Up @@ -325,19 +326,15 @@ let map_type_decl
~kind:
(Ptype_record
[
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "resolver")
(Typ.var "resolver");
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "defaultValues")
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "resolver") (Typ.var "resolver");
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "defaultValues")
(Typ.constr
(lid @@ "defaultValuesOf" ^ capitalize record_name)
[]);
Type.field
~attrs:[ Attr.mk (mknoloc "res.optional") (PStr []) ]
~mut:Immutable (mknoloc "mode")
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "mode")
(Typ.variant
[
Rf.tag (mknoloc "onBlur") true [];
Expand All @@ -348,6 +345,22 @@ let map_type_decl
]
Closed None);
]);
Type.mk
(mkloc ("setValueConfigOf" ^ capitalize record_name) ptype_loc)
~priv:Public
~kind:
(Ptype_record
[
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "shouldValidate")
(Typ.constr (lid "bool") []);
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "shouldDirty")
(Typ.constr (lid "bool") []);
Type.field ~attrs:[ attr_optional ] ~mut:Immutable
(mknoloc "shouldTouch")
(Typ.constr (lid "bool") []);
]);
]
in

Expand Down
1 change: 1 addition & 0 deletions src/ppx/utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let attr_named_arg = Attr.mk (mknoloc "res.namedArgLoc") (PStr [])
let attr_template = Attr.mk (mknoloc "res.template") (PStr [])
let attr_uapp = Attr.mk (mknoloc "res.uapp") (PStr [])
let attr_brace = Attr.mk (mknoloc "res.braces") (PStr [])
let attr_optional = Attr.mk (mknoloc "res.optional") (PStr [])
let has_attribute { attr_name = { Location.txt } } = txt = attribute_name

let remove_optional_attribute (attrs : attributes) : attributes =
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.1"
version: "0.0.2"
synopsis: "ReScript PPX for react-hook-form"
description: """
ReScript PPX which generates the bindings for react-hook-form
Expand Down

0 comments on commit 0293403

Please sign in to comment.