diff --git a/example/Example.res b/example/Example.res index 540daad..03ab270 100644 --- a/example/Example.res +++ b/example/Example.res @@ -12,24 +12,12 @@ module Form = { } = Hooks.Form.use(. ~config=Hooks.Form.config( ~mode=#onSubmit, - ~defaultValues=Js.Json.object_( - Js.Dict.fromArray([ - (Values.firstName, Js.Json.string("")), - (Values.lastName, Js.Json.string("")), - (Values.acceptTerms, Js.Json.boolean(false)), - ( - Values.hobbies, - Js.Json.array([ - Js.Json.object_( - Js.Dict.fromArray([ - ("id", Js.Json.string(Uuid.v4())), - (Values.Hobby.name, Js.Json.string("")), - ]), - ), - ]), - ), - ]), - ), + ~defaultValues=Values.encoder({ + "firstName": "", + "lastName": "", + "acceptTerms": false, + "hobbies": [{"id": Uuid.v4(), "name": ""}], + }), (), ), (), @@ -93,7 +81,7 @@ module Form = {
@@ -119,7 +107,8 @@ module Form = { - @@ -130,16 +119,11 @@ module Form = { type_="button" onClick={_event => { if hobbiesAreShown { - switch getValues(. None)->ReCode.Decode.decodeJson(Values.decoder) { - | Ok({hobbies}) => - setValue(. - "hobbies", - Js.Json.stringifyAny(hobbies)->Belt.Option.mapWithDefault( - Js.Json.array([]), - Js.Json.parseExn, - ), - ) - | Error(error) => Js.log(error) + switch getValues(. None)->ReCode.Decode.decodeJson( + ReCode.Decode.field("hobbies", ReCode.Decode.raw), + ) { + | Ok(hobbies) => setValue(. "hobbies", hobbies) + | Error(_) => ignore() } } diff --git a/example/Values.res b/example/Values.res index b046d5f..fcb570a 100644 --- a/example/Values.res +++ b/example/Values.res @@ -1,14 +1,24 @@ -open ReCode.Decode -open ReCode.DecodeExtra - module Hobby = { type t = {name: string} + let id = "id" + let name = "name" let make = name => {name: name} - let decoder = pure(make)->Object.required(name, string->String.required) + let decoder = { + open ReCode.Decode + open ReCode.DecodeExtra + + pure(make)->Object.required(name, string->String.required) + } + + let encoder = hobby => { + open ReCode.Encode + + object([(id, string(hobby["id"])), (name, string(hobby["name"]))]) + } } type t = {firstName: string, lastName: string, acceptTerms: bool, hobbies: array} @@ -26,9 +36,24 @@ let make = (firstName, lastName, acceptTerms, hobbies) => { hobbies: hobbies, } -let decoder = +let decoder = { + open ReCode.Decode + open ReCode.DecodeExtra + pure(make) ->Object.required(firstName, string) ->Object.required(lastName, string) ->Object.required(acceptTerms, bool) ->Object.required(hobbies, array(Hobby.decoder)) +} + +let encoder = values => { + open ReCode.Encode + + object([ + (firstName, string(values["firstName"])), + (lastName, string(values["lastName"])), + (acceptTerms, bool(values["acceptTerms"])), + (hobbies, array(Hobby.encoder, values["hobbies"])), + ]) +} diff --git a/package.json b/package.json index 81e673d..feca8d0 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "peerDependencies": { "@hookform/error-message": "^2.0.0", "@rescript/react": "^0.10.2", - "re-code": "https://github.com/scoville/re-code", "react": "^17.0.2", "react-dom": "^17.0.2", "react-hook-form": "^7.1.1" diff --git a/yarn.lock b/yarn.lock index 2c2d843..276ed53 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4254,7 +4254,7 @@ range-parser@~1.2.1: "re-code@https://github.com/scoville/re-code": version "1.0.0" - resolved "https://github.com/scoville/re-code#01211c0a04531acad7894156ae94c3d16af7c392" + resolved "https://github.com/scoville/re-code#7e6e6ae3f692b8c79c1c458803dd56e629217aa5" react-dom@^17.0.2: version "17.0.2"