Skip to content

Commit

Permalink
chore(re-code): Update dev dependency ReCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin COMBRIAT committed May 25, 2021
1 parent 7f1b75c commit 66cff90
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
44 changes: 14 additions & 30 deletions example/Example.res
Original file line number Diff line number Diff line change
Expand Up @@ -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": ""}],
}),
(),
),
(),
Expand Down Expand Up @@ -93,7 +81,7 @@ module Form = {
<Controller
name={Values.hobby(index)}
control
defaultValue={Js.Json.string(field["name"])}
defaultValue={ReCode.Encode.string(field["name"])}
rules={Rules.make(~required=true, ())}
render={({field: {name, onBlur, onChange, ref, value}}) =>
<div>
Expand All @@ -119,7 +107,8 @@ module Form = {
<button type_="button" onClick={_event => append(. {"id": Uuid.v4(), "name": ""})}>
{"Add hobby"->React.string}
</button>
<button type_="button" onClick={_event => setValue(. "firstName", Js.Json.string("foo"))}>
<button
type_="button" onClick={_event => setValue(. "firstName", ReCode.Encode.string("foo"))}>
{"Set value"->React.string}
</button>
<button type_="button" onClick={_event => reset(. None)}> {"Reset"->React.string} </button>
Expand All @@ -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()
}
}

Expand Down
35 changes: 30 additions & 5 deletions example/Values.res
Original file line number Diff line number Diff line change
@@ -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<Hobby.t>}
Expand All @@ -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"])),
])
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 66cff90

Please sign in to comment.