diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ffcd8e..f465a99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index c2826d4..c26eb95 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/doc/examples.md b/doc/examples.md index e5314ce..6f05745 100644 --- a/doc/examples.md +++ b/doc/examples.md @@ -14,6 +14,8 @@ let make = () => { Js.log(watch(Example)) + Js.log(getValues(Example)) +
@@ -57,6 +59,8 @@ let make = () => { Js.log(watch(FirstName)) + Js.log(getValues(FirstName)) +

@@ -138,6 +142,8 @@ let default = () => { Js.log(watch(MyCheckBox)) + Js.log(getValues(MyCheckBox)) + + example?: string, + exampleRequired?: string, + cart?: array } type defaultValuesOfInputs = { example?: string, @@ -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) - | Array(array) + | Object(Js.Dict.t) + | Array(array) type rec useFormReturnOfInputs<'setValueAs> = { control: controlOfInputs, register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps, handleSubmit: (inputs => unit) => JsxEvent.Form.t => unit, - watch: variantOfInputs => option, + watch: variantOfInputs => option, + getValues: variantOfInputs => option formState: formStateOfInputs, getFieldState: (variantOfInputs, formStateOfInputs) => fieldStateOfInputs, setValue: (variantOfInputs, ReactHookForm.value, ~options: setValueConfigOfInputs=?) => unit, diff --git a/package.json b/package.json index 99b43e0..3d61207 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/ppx/signature.ml b/src/ppx/signature.ml index 16151e9..9d16f84 100644 --- a/src/ppx/signature.ml +++ b/src/ppx/signature.ml @@ -18,7 +18,7 @@ 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 @@ -26,7 +26,14 @@ let map_type_decl (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 = @@ -116,7 +123,7 @@ let map_type_decl | Array(array) *) 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: @@ -142,8 +149,7 @@ let map_type_decl [ Typ.constr (lid - ("watchReturnOf" - ^ capitalize record_name)) + ("valuesOf" ^ capitalize record_name)) []; ]; ]); @@ -155,8 +161,7 @@ let map_type_decl [ Typ.constr (lid - ("watchReturnOf" - ^ capitalize record_name)) + ("valuesOf" ^ capitalize record_name)) []; ]; ]); @@ -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, reset: (~options: defaultValuesOfInputs=?) => unit, formState: formStateOfInputs, } *) @@ -229,7 +235,7 @@ let map_type_decl (Typ.constr (lid "unit") []); ]); ]); - (* watch: variantOfInputs => option, *) + (* watch: variantOfInputs => option, *) Type.field ~mut:Immutable (mknoloc "watch") (uncurried_core_type_arrow ~arity:1 [ @@ -240,7 +246,23 @@ let map_type_decl (Typ.constr (lid "option") [ Typ.constr - (lid @@ "watchReturnOf" + (lid @@ "valuesOf" + ^ capitalize record_name) + []; + ]); + ]); + (* getValues: variantOfInputs => option, *) + 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) []; ]); diff --git a/src/ppx/structure.ml b/src/ppx/structure.ml index ca46090..86ad3a8 100644 --- a/src/ppx/structure.ml +++ b/src/ppx/structure.ml @@ -18,7 +18,7 @@ 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 @@ -26,7 +26,13 @@ let map_type_decl (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 = @@ -116,7 +122,7 @@ let map_type_decl | Array(array) *) 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: @@ -142,8 +148,7 @@ let map_type_decl [ Typ.constr (lid - ("watchReturnOf" - ^ capitalize record_name)) + ("valuesOf" ^ capitalize record_name)) []; ]; ]); @@ -155,8 +160,7 @@ let map_type_decl [ Typ.constr (lid - ("watchReturnOf" - ^ capitalize record_name)) + ("valuesOf" ^ capitalize record_name)) []; ]; ]); @@ -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, reset: (~options: defaultValuesOfInputs=?) => unit, formState: formStateOfInputs, } *) @@ -229,7 +234,7 @@ let map_type_decl (Typ.constr (lid "unit") []); ]); ]); - (* watch: variantOfInputs => option, *) + (* watch: variantOfInputs => option, *) Type.field ~mut:Immutable (mknoloc "watch") (uncurried_core_type_arrow ~arity:1 [ @@ -240,7 +245,23 @@ let map_type_decl (Typ.constr (lid "option") [ Typ.constr - (lid @@ "watchReturnOf" + (lid @@ "valuesOf" + ^ capitalize record_name) + []; + ]); + ]); + (* getValues: variantOfInputs => option, *) + 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) []; ]); diff --git a/src/ppx_rhf.opam b/src/ppx_rhf.opam index 7f492ad..2b4a874 100644 --- a/src/ppx_rhf.opam +++ b/src/ppx_rhf.opam @@ -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 diff --git a/test/package.json b/test/package.json index 5ce9e74..bf48269 100644 --- a/test/package.json +++ b/test/package.json @@ -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" } diff --git a/test/pnpm-lock.yaml b/test/pnpm-lock.yaml index 873b12c..066be78 100644 --- a/test/pnpm-lock.yaml +++ b/test/pnpm-lock.yaml @@ -56,8 +56,8 @@ devDependencies: specifier: ^6.20.0 version: 6.22.3(react-dom@18.2.0)(react@18.2.0) rescript: - specifier: 12.0.0-alpha.4 - version: 12.0.0-alpha.4 + specifier: 12.0.0-alpha.5 + version: 12.0.0-alpha.5 typescript: specifier: ^5.2.2 version: 5.4.5 @@ -2044,8 +2044,8 @@ packages: resolution: {integrity: sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==} dev: false - /rescript@12.0.0-alpha.4: - resolution: {integrity: sha512-w9uIv8C8+kBWbcb1GVn1rOdLEj2YX1TIRvUkR6DSvVO8oSdJ0GAUABN7y7WU/+zCkjNwsVrBlO0hG7NTJqXHUA==} + /rescript@12.0.0-alpha.5: + resolution: {integrity: sha512-s2GGg9fkoiDBiP0uuJ1RRG5NBq+SkGqxMEmt32q9tTJoueL7bfU/9Jj3UgSwFFNdehuns1taAOvP2DV4IE+R9A==} engines: {node: '>=18'} hasBin: true requiresBuild: true diff --git a/test/src/pages/basic_res/Basic.res b/test/src/pages/basic_res/Basic.res index e455ff7..09baba1 100644 --- a/test/src/pages/basic_res/Basic.res +++ b/test/src/pages/basic_res/Basic.res @@ -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) { @@ -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) diff --git a/test/src/pages/controller_res/Controller.res b/test/src/pages/controller_res/Controller.res index 7fc0271..3142014 100644 --- a/test/src/pages/controller_res/Controller.res +++ b/test/src/pages/controller_res/Controller.res @@ -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)) + 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: { @@ -28,6 +28,8 @@ let default = () => { Js.log(watch(FirstName)) + Js.log(getValues(FirstName)) +