Skip to content

Commit 197e21b

Browse files
Fix type signature in form example in readme (#60)
fix form example in readme
1 parent 2bf0d5f commit 197e21b

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

readme.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ You can write a basic Formless form in just a few lines of code. You are respons
1919
First, a form type that describes the fields in your form, along with their validation error type, user input type, and validated output type. Note: you can provide whatever custom error types you'd like, use `Void` to represent no possible errors, parse to whatever type you want, and none of your fields need to share any types.
2020

2121
```purescript
22+
import Prelude
23+
import Data.Newtype (class Newtype, unwrap)
24+
2225
type Dog = { name :: String, age :: Age }
2326
2427
newtype Age = Age Int
2528
29+
derive instance newtypeAge :: Newtype Age _
30+
31+
instance showAge :: Show Age where
32+
show = show <<< unwrap
33+
2634
data AgeError = TooLow | TooHigh | InvalidInt
2735
2836
newtype DogForm r f = DogForm (r
@@ -39,14 +47,17 @@ Next, the component input, which is made up of initial values and validation fun
3947
You can generate sensible defaults for all input fields in your form by setting `initialInputs` to `Nothing`, or you can manually provide the starting value for each field in your form.
4048

4149
```purescript
50+
import Data.Either (Either(..))
51+
import Data.Int as Int
52+
import Data.Maybe (Maybe(..))
4253
import Formless as F
4354
4455
input :: forall m. Monad m => F.Input' DogForm m
4556
input =
4657
{ initialInputs: Nothing -- same as: Just (F.wrapInputFields { name: "", age: "" })
4758
, validators: DogForm
4859
{ name: F.noValidation
49-
, age: F.hoistFnE_ \str -> case fromString str of
60+
, age: F.hoistFnE_ \str -> case Int.fromString str of
5061
Nothing -> Left InvalidInt
5162
Just n
5263
| n < 0 -> Left TooLow
@@ -60,15 +71,15 @@ Finally, the component spec, which is made up of a number of optional functions
6071

6172
For our small form, we'll do two things: we'll provide a render function, and when the form is submitted, we'll output a `Dog` to parent components. Along the way we'll wire things up so that input fields display their current value from form state; typing into an input field updates its value in state, also running the correct validation function; we'll display the validation error for `age` if there is one; and we'll wire up a submit button.
6273

63-
Note: If you would like to have your form raise no messages (rare), do not supply a `handleMessage` function. If you would like to raise the usual Formless messages (`Changed`, `Submitted`), then provide `H.raise` as your `handleMessage` function. If you would like to simply raise your form's validated output type (`Dog`, in this example), then provide `F.raiseResult` as your `handleMessage` function. Finally, if you want to do something else, you can write a custom function that does whatever you would like.
74+
Note: If you would like to have your form raise no messages (rare), do not supply a `handleEvent` function. If you would like to raise the usual Formless messages (`Changed`, `Submitted`), then provide `H.raise` as your `handleEvent` function. If you would like to simply raise your form's validated output type (`Dog`, in this example), then provide `F.raiseResult` as your `handleEvent` function. Finally, if you want to do something else, you can write a custom function that does whatever you would like.
6475

6576
```purescript
77+
import Data.Symbol (SProxy(..))
6678
import Halogen.HTML as HH
6779
import Halogen.HTML.Events as HE
6880
import Halogen.HTML.Properties as HP
69-
import Formless as F
7081
71-
spec :: forall m. Monad m => F.Spec' DogForm Dog m
82+
spec :: forall input m. Monad m => F.Spec' DogForm Dog input m
7283
spec = F.defaultSpec { render = render, handleEvent = F.raiseResult }
7384
where
7485
render st@{ form } =
@@ -98,11 +109,13 @@ spec = F.defaultSpec { render = render, handleEvent = F.raiseResult }
98109
Our form is now complete. It's easy to put this form in a parent page component:
99110

100111
```purescript
112+
import Effect.Aff.Class (class MonadAff)
113+
import Effect.Class.Console (logShow)
101114
import Halogen as H
102-
import Formless as F
103115
104116
data Action = HandleDogForm Dog
105117
118+
page :: forall q i o m. MonadAff m => H.Component HH.HTML q i o m
106119
page = H.mkComponent
107120
{ initialState: const unit
108121
, render: const render
@@ -111,7 +124,7 @@ page = H.mkComponent
111124
where
112125
handleAction (HandleDogForm dog) = logShow (dog :: Dog)
113126
114-
render = HH.slot F._formless unit (F.component spec) input handler
127+
render = HH.slot F._formless unit (F.component (const input) spec) unit handler
115128
where
116129
handler = Just <<< HandleDogForm
117130
```

0 commit comments

Comments
 (0)