Skip to content

Commit d49473f

Browse files
authored
Add more variants for watch return type (#3)
* add more variants for watch return type * watch returns option type * change log
1 parent 0293403 commit d49473f

File tree

9 files changed

+2431
-27
lines changed

9 files changed

+2431
-27
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# v0.0.2 (not released)
1+
# v0.0.2
22

33
- Add setValueConfig
4+
- Add more variants for watch return type and watch returns option type
45

56
# v0.0.1
67

doc/transformation.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +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 watchReturnOfInputs = String(string) | Number(float)
34+
type rec watchReturnOfInputs =
35+
| @as(null) Null
36+
| Bool(bool)
37+
| Number(float)
38+
| String(string)
39+
| Object(Js.Dict.t<watchReturnOfInputs>)
40+
| Array(array<watchReturnOfInputs>)
41+
3542
3643
type rec useFormReturnOfInputs<'setValueAs> = {
3744
control: controlOfInputs,
3845
register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps,
3946
handleSubmit: (inputs => unit) => JsxEvent.Form.t => unit,
40-
watch: variantOfInputs => watchReturnOfInputs,
47+
watch: variantOfInputs => option<watchReturnOfInputs>,
4148
formState: formStateOfInputs,
4249
getFieldState: (variantOfInputs, formStateOfInputs) => fieldStateOfInputs,
4350
setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit,

src/ppx/signature.ml

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,60 @@ let map_type_decl
8080
(mknoloc "message")
8181
(Typ.constr (lid "string") []);
8282
]);
83-
(* @unboxed type watchReturnOfInputs = String(string) | Number(float) *)
83+
(* @unboxed
84+
type rec watchReturnOfInputs =
85+
| @as(null) Null
86+
| Bool(bool)
87+
| Number(float)
88+
| String(string)
89+
| Object(Js.Dict.t<watchReturnOfInputs>)
90+
| Array(array<watchReturnOfInputs>)
91+
*)
8492
Type.mk
8593
(mkloc ("watchReturnOf" ^ capitalize record_name) ptype_loc)
8694
~attrs:[ Attr.mk (mknoloc "unboxed") (PStr []) ]
8795
~priv:Public
8896
~kind:
8997
(Ptype_variant
9098
[
91-
Type.constructor (mknoloc "String")
92-
~args:(Pcstr_tuple [ Typ.constr (lid "string") [] ]);
99+
Type.constructor (mknoloc "Null")
100+
~attrs:
101+
[
102+
Attr.mk (mknoloc "as")
103+
(PStr [ Str.eval @@ Exp.ident (lid "null") ]);
104+
];
105+
Type.constructor (mknoloc "Bool")
106+
~args:(Pcstr_tuple [ Typ.constr (lid "bool") [] ]);
93107
Type.constructor (mknoloc "Number")
94108
~args:(Pcstr_tuple [ Typ.constr (lid "float") [] ]);
109+
Type.constructor (mknoloc "String")
110+
~args:(Pcstr_tuple [ Typ.constr (lid "string") [] ]);
111+
Type.constructor (mknoloc "Object")
112+
~args:
113+
(Pcstr_tuple
114+
[
115+
Typ.constr (lid "Js.Dict.t")
116+
[
117+
Typ.constr
118+
(lid
119+
("watchReturnOf"
120+
^ capitalize record_name))
121+
[];
122+
];
123+
]);
124+
Type.constructor (mknoloc "Array")
125+
~args:
126+
(Pcstr_tuple
127+
[
128+
Typ.constr (lid "array")
129+
[
130+
Typ.constr
131+
(lid
132+
("watchReturnOf"
133+
^ capitalize record_name))
134+
[];
135+
];
136+
]);
95137
]);
96138
]
97139
in
@@ -161,18 +203,21 @@ let map_type_decl
161203
(Typ.constr (lid "unit") []);
162204
]);
163205
]);
164-
(* watch: variantOfInputs => watchReturnOfInputs, *)
206+
(* watch: variantOfInputs => option<watchReturnOfInputs>, *)
165207
Type.field ~mut:Immutable (mknoloc "watch")
166208
(uncurried_core_type_arrow ~arity:1
167209
[
168210
Typ.arrow Nolabel
169211
(Typ.constr
170212
(lid @@ "variantOf" ^ capitalize record_name)
171213
[])
172-
(Typ.constr
173-
(lid @@ "watchReturnOf"
174-
^ capitalize record_name)
175-
[]);
214+
(Typ.constr (lid "option")
215+
[
216+
Typ.constr
217+
(lid @@ "watchReturnOf"
218+
^ capitalize record_name)
219+
[];
220+
]);
176221
]);
177222
(* reset: (~options: defaultValuesOfInputs=?) => unit, *)
178223
Type.field ~mut:Immutable (mknoloc "reset")

src/ppx/structure.ml

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,60 @@ let map_type_decl
8080
(mknoloc "message")
8181
(Typ.constr (lid "string") []);
8282
]);
83-
(* @unboxed type watchReturnOfInputs = String(string) | Number(float) *)
83+
(* @unboxed
84+
type rec watchReturnOfInputs =
85+
| @as(null) Null
86+
| Bool(bool)
87+
| Number(float)
88+
| String(string)
89+
| Object(Js.Dict.t<watchReturnOfInputs>)
90+
| Array(array<watchReturnOfInputs>)
91+
*)
8492
Type.mk
8593
(mkloc ("watchReturnOf" ^ capitalize record_name) ptype_loc)
8694
~attrs:[ Attr.mk (mknoloc "unboxed") (PStr []) ]
8795
~priv:Public
8896
~kind:
8997
(Ptype_variant
9098
[
91-
Type.constructor (mknoloc "String")
92-
~args:(Pcstr_tuple [ Typ.constr (lid "string") [] ]);
99+
Type.constructor (mknoloc "Null")
100+
~attrs:
101+
[
102+
Attr.mk (mknoloc "as")
103+
(PStr [ Str.eval @@ Exp.ident (lid "null") ]);
104+
];
105+
Type.constructor (mknoloc "Bool")
106+
~args:(Pcstr_tuple [ Typ.constr (lid "bool") [] ]);
93107
Type.constructor (mknoloc "Number")
94108
~args:(Pcstr_tuple [ Typ.constr (lid "float") [] ]);
109+
Type.constructor (mknoloc "String")
110+
~args:(Pcstr_tuple [ Typ.constr (lid "string") [] ]);
111+
Type.constructor (mknoloc "Object")
112+
~args:
113+
(Pcstr_tuple
114+
[
115+
Typ.constr (lid "Js.Dict.t")
116+
[
117+
Typ.constr
118+
(lid
119+
("watchReturnOf"
120+
^ capitalize record_name))
121+
[];
122+
];
123+
]);
124+
Type.constructor (mknoloc "Array")
125+
~args:
126+
(Pcstr_tuple
127+
[
128+
Typ.constr (lid "array")
129+
[
130+
Typ.constr
131+
(lid
132+
("watchReturnOf"
133+
^ capitalize record_name))
134+
[];
135+
];
136+
]);
95137
]);
96138
]
97139
in
@@ -161,18 +203,21 @@ let map_type_decl
161203
(Typ.constr (lid "unit") []);
162204
]);
163205
]);
164-
(* watch: variantOfInputs => watchReturnOfInputs, *)
206+
(* watch: variantOfInputs => option<watchReturnOfInputs>, *)
165207
Type.field ~mut:Immutable (mknoloc "watch")
166208
(uncurried_core_type_arrow ~arity:1
167209
[
168210
Typ.arrow Nolabel
169211
(Typ.constr
170212
(lid @@ "variantOf" ^ capitalize record_name)
171213
[])
172-
(Typ.constr
173-
(lid @@ "watchReturnOf"
174-
^ capitalize record_name)
175-
[]);
214+
(Typ.constr (lid "option")
215+
[
216+
Typ.constr
217+
(lid @@ "watchReturnOf"
218+
^ capitalize record_name)
219+
[];
220+
]);
176221
]);
177222
(* reset: (~options: defaultValuesOfInputs=?) => unit, *)
178223
Type.field ~mut:Immutable (mknoloc "reset")
@@ -797,8 +842,8 @@ let map_type_decl
797842
(
798843
Typ
799844
.constr
800-
(
801-
lid
845+
846+
(lid
802847
"string")
803848
[])
804849
);
@@ -822,8 +867,9 @@ let map_type_decl
822867
~attrs:
823868
[
824869
Attr.mk
825-
(mknoloc
826-
"res.uapp")
870+
871+
(mknoloc
872+
"res.uapp")
827873
(PStr
828874
[]);
829875
]

test/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"private": true,
44
"version": "0.0.0",
55
"type": "module",
6+
"packageManager": "[email protected]",
67
"scripts": {
78
"dev": "vite",
89
"build": "tsc && vite build",
@@ -31,8 +32,8 @@
3132
"eslint-plugin-react-hooks": "^4.6.0",
3233
"eslint-plugin-react-refresh": "^0.4.4",
3334
"react-router-dom": "^6.20.0",
34-
"rescript": "11.0.0-rc.6",
35+
"rescript": "11.1.0-rc.8",
3536
"typescript": "^5.2.2",
3637
"vite": "^5.0.0"
3738
}
38-
}
39+
}

0 commit comments

Comments
 (0)