|
1 | 1 | module ControlledInput where
|
2 | 2 |
|
3 | 3 | import Prelude
|
4 |
| - |
5 | 4 | import Data.Maybe (Maybe(..), fromMaybe, maybe)
|
| 5 | +import Effect (Effect) |
6 | 6 | import React.Basic.DOM as R
|
7 | 7 | import React.Basic.DOM.Events (preventDefault, stopPropagation, targetValue, timeStamp)
|
8 | 8 | import React.Basic.Events (EventHandler, handler, merge)
|
9 |
| -import React.Basic.Hooks (CreateComponent, UseState, Hook, component, fragment, useState, (/\)) |
| 9 | +import React.Basic.Hooks (ReactComponent, UseState, Hook, component, fragment, useState, (/\)) |
10 | 10 | import React.Basic.Hooks as React
|
11 | 11 |
|
12 |
| -mkControlledInput :: CreateComponent {} |
| 12 | +mkControlledInput :: Effect (ReactComponent {}) |
13 | 13 | mkControlledInput = do
|
14 | 14 | component "ControlledInput" \props -> React.do
|
15 | 15 | firstName <- useInput "hello"
|
16 | 16 | lastName <- useInput "world"
|
17 |
| - |
18 |
| - pure $ R.form_ |
19 |
| - [ renderInput firstName |
20 |
| - , renderInput lastName |
21 |
| - ] |
| 17 | + pure |
| 18 | + $ R.form_ |
| 19 | + [ renderInput firstName |
| 20 | + , renderInput lastName |
| 21 | + ] |
22 | 22 | where
|
23 |
| - renderInput input = |
24 |
| - fragment |
25 |
| - [ R.input { onChange: input.onChange, value: input.value } |
26 |
| - , R.p_ [ R.text ("Current value = " <> show input.value) ] |
27 |
| - , R.p_ [ R.text ("Changed at = " <> maybe "never" show input.lastChanged) ] |
28 |
| - ] |
| 23 | + renderInput input = |
| 24 | + fragment |
| 25 | + [ R.input { onChange: input.onChange, value: input.value } |
| 26 | + , R.p_ [ R.text ("Current value = " <> show input.value) ] |
| 27 | + , R.p_ [ R.text ("Changed at = " <> maybe "never" show input.lastChanged) ] |
| 28 | + ] |
29 | 29 |
|
30 |
| -useInput |
31 |
| - :: String |
32 |
| - -> Hook |
33 |
| - (UseState { value :: String, lastChanged :: Maybe Number }) |
34 |
| - { onChange :: EventHandler |
35 |
| - , value :: String |
36 |
| - , lastChanged :: Maybe Number |
37 |
| - } |
| 30 | +useInput :: |
| 31 | + String -> |
| 32 | + Hook |
| 33 | + (UseState { value :: String, lastChanged :: Maybe Number }) |
| 34 | + { onChange :: EventHandler |
| 35 | + , value :: String |
| 36 | + , lastChanged :: Maybe Number |
| 37 | + } |
38 | 38 | useInput initialValue = React.do
|
39 | 39 | { value, lastChanged } /\ replaceState <- useState { value: initialValue, lastChanged: Nothing }
|
40 | 40 | pure
|
41 |
| - { onChange: handler |
42 |
| - (preventDefault >>> stopPropagation >>> merge { targetValue, timeStamp }) |
43 |
| - \{ timeStamp, targetValue } -> do |
44 |
| - replaceState \_ -> |
45 |
| - { value: fromMaybe "" targetValue |
46 |
| - , lastChanged: Just timeStamp |
47 |
| - } |
| 41 | + { onChange: |
| 42 | + handler |
| 43 | + (preventDefault >>> stopPropagation >>> merge { targetValue, timeStamp }) \{ timeStamp, targetValue } -> do |
| 44 | + replaceState \_ -> |
| 45 | + { value: fromMaybe "" targetValue |
| 46 | + , lastChanged: Just timeStamp |
| 47 | + } |
48 | 48 | , value
|
49 | 49 | , lastChanged
|
50 | 50 | }
|
0 commit comments