Skip to content

Commit c6ae2e2

Browse files
committed
Hide EventFn constructor and expose unsafeEventFn
1 parent a148686 commit c6ae2e2

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

examples/controlled-input/src/ControlledInput.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module ControlledInput where
22

33
import Prelude
44

5-
import Data.Maybe (Maybe(..), fromMaybe)
5+
import Data.Maybe (Maybe(..), fromMaybe, maybe)
66
import React.Basic (ReactComponent, react)
77
import React.Basic.DOM as R
88
import React.Basic.DOM.Events (preventDefault, targetValue, timeStamp)
@@ -24,6 +24,6 @@ component = react
2424
}
2525
]
2626
, R.p_ [ R.text ("Current value = " <> show state.value) ]
27-
, R.p_ [ R.text ("Changed at = " <> fromMaybe "never" (show <$> state.timeStamp)) ]
27+
, R.p_ [ R.text ("Changed at = " <> maybe "never" show state.timeStamp) ]
2828
]
2929
}

generated-docs/React/Basic/Events.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Event data that we receive from React.
2121

2222
``` purescript
2323
newtype EventFn a b
24-
= EventFn (a -> b)
2524
```
2625

2726
Encapsulates a safe event operation. `EventFn`s can be composed
@@ -35,15 +34,22 @@ input { onChange: handler (preventDefault >>> targetValue)
3534
}
3635
```
3736

38-
_Note: Misusing the `EventFn` *constructor* is UNSAFE and should be avoided -- use the helper functions specific to your platform (such as `React.Basic.DOM.Events`)_
39-
4037
##### Instances
4138
``` purescript
4239
Semigroupoid EventFn
4340
Category EventFn
4441
(IsSymbol l, RowCons l (EventFn a b) fns_rest fns, RowCons l b r_rest r, RowLacks l fns_rest, RowLacks l r_rest, Merge rest fns_rest a r_rest) => Merge (Cons l (EventFn a b) rest) fns a r
4542
```
4643

44+
#### `unsafeEventFn`
45+
46+
``` purescript
47+
unsafeEventFn :: forall a b. (a -> b) -> EventFn a b
48+
```
49+
50+
Unsafely create an `EventFn`. This function should be avoided.
51+
Use the helper functions specific to your platform (such as `React.Basic.DOM.Events`).
52+
4753
#### `handler`
4854

4955
``` purescript

src/React/Basic/DOM/Events.purs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module React.Basic.DOM.Events
2626

2727
import Data.Maybe (Maybe)
2828
import Data.Nullable (toMaybe)
29-
import React.Basic.Events (EventFn(..), SyntheticEvent)
29+
import React.Basic.Events (EventFn, SyntheticEvent, unsafeEventFn)
3030
import Unsafe.Coerce (unsafeCoerce)
3131

3232
-- | An _actual_ DOM node (not a virtual DOM element!)
@@ -36,16 +36,16 @@ foreign import data DOMNode :: Type
3636
foreign import data DOMEvent :: Type
3737

3838
bubbles :: EventFn SyntheticEvent Boolean
39-
bubbles = EventFn \e -> (unsafeCoerce e).bubbles
39+
bubbles = unsafeEventFn \e -> (unsafeCoerce e).bubbles
4040

4141
cancelable :: EventFn SyntheticEvent Boolean
42-
cancelable = EventFn \e -> (unsafeCoerce e).cancelable
42+
cancelable = unsafeEventFn \e -> (unsafeCoerce e).cancelable
4343

4444
currentTarget :: EventFn SyntheticEvent DOMNode
45-
currentTarget = EventFn \e -> (unsafeCoerce e).currentTarget
45+
currentTarget = unsafeEventFn \e -> (unsafeCoerce e).currentTarget
4646

4747
eventPhase :: EventFn SyntheticEvent Int
48-
eventPhase = EventFn \e -> (unsafeCoerce e).eventPhase
48+
eventPhase = unsafeEventFn \e -> (unsafeCoerce e).eventPhase
4949

5050
eventPhaseNone :: Int
5151
eventPhaseNone = 0
@@ -60,42 +60,42 @@ eventPhaseBubbling :: Int
6060
eventPhaseBubbling = 3
6161

6262
isTrusted :: EventFn SyntheticEvent Boolean
63-
isTrusted = EventFn \e -> (unsafeCoerce e).isTrusted
63+
isTrusted = unsafeEventFn \e -> (unsafeCoerce e).isTrusted
6464

6565
nativeEvent :: EventFn SyntheticEvent DOMEvent
66-
nativeEvent = EventFn \e -> (unsafeCoerce e).nativeEvent
66+
nativeEvent = unsafeEventFn \e -> (unsafeCoerce e).nativeEvent
6767

6868
preventDefault :: EventFn SyntheticEvent SyntheticEvent
69-
preventDefault = EventFn unsafePreventDefault
69+
preventDefault = unsafeEventFn unsafePreventDefault
7070

7171
foreign import unsafePreventDefault :: SyntheticEvent -> SyntheticEvent
7272

7373
isDefaultPrevented :: EventFn SyntheticEvent Boolean
74-
isDefaultPrevented = EventFn unsafeIsDefaultPrevented
74+
isDefaultPrevented = unsafeEventFn unsafeIsDefaultPrevented
7575

7676
foreign import unsafeIsDefaultPrevented :: SyntheticEvent -> Boolean
7777

7878
stopPropagation :: EventFn SyntheticEvent SyntheticEvent
79-
stopPropagation = EventFn unsafeStopPropagation
79+
stopPropagation = unsafeEventFn unsafeStopPropagation
8080

8181
foreign import unsafeStopPropagation :: SyntheticEvent -> SyntheticEvent
8282

8383
isPropagationStopped :: EventFn SyntheticEvent Boolean
84-
isPropagationStopped = EventFn unsafeIsPropagationStopped
84+
isPropagationStopped = unsafeEventFn unsafeIsPropagationStopped
8585

8686
foreign import unsafeIsPropagationStopped :: SyntheticEvent -> Boolean
8787

8888
target :: EventFn SyntheticEvent DOMNode
89-
target = EventFn \e -> (unsafeCoerce e).target
89+
target = unsafeEventFn \e -> (unsafeCoerce e).target
9090

9191
targetChecked :: EventFn SyntheticEvent (Maybe Boolean)
92-
targetChecked = EventFn \e -> toMaybe (unsafeCoerce e).target.checked
92+
targetChecked = unsafeEventFn \e -> toMaybe (unsafeCoerce e).target.checked
9393

9494
targetValue :: EventFn SyntheticEvent (Maybe String)
95-
targetValue = EventFn \e -> toMaybe (unsafeCoerce e).target.value
95+
targetValue = unsafeEventFn \e -> toMaybe (unsafeCoerce e).target.value
9696

9797
timeStamp :: EventFn SyntheticEvent Number
98-
timeStamp = EventFn \e -> (unsafeCoerce e).timeStamp
98+
timeStamp = unsafeEventFn \e -> (unsafeCoerce e).timeStamp
9999

100100
type_ :: EventFn SyntheticEvent String
101-
type_ = EventFn \e -> (unsafeCoerce e)."type"
101+
type_ = unsafeEventFn \e -> (unsafeCoerce e)."type"

src/React/Basic/Events.purs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module React.Basic.Events
22
( EventHandler
33
, SyntheticEvent
4-
, EventFn(..)
4+
, EventFn
5+
, unsafeEventFn
56
, handler
67
, merge
78
, class Merge
@@ -34,10 +35,13 @@ foreign import data SyntheticEvent :: Type
3435
-- | \value -> setState \_ -> { value }
3536
-- | }
3637
-- | ```
37-
-- |
38-
-- | _Note: Misusing the `EventFn` *constructor* is UNSAFE and should be avoided -- use the helper functions specific to your platform (such as `React.Basic.DOM.Events`)_
3938
newtype EventFn a b = EventFn (a -> b)
4039

40+
-- | Unsafely create an `EventFn`. This function should be avoided.
41+
-- | Use the helper functions specific to your platform (such as `React.Basic.DOM.Events`).
42+
unsafeEventFn :: forall a b. (a -> b) -> EventFn a b
43+
unsafeEventFn = EventFn
44+
4145
derive newtype instance semigroupoidBuilder :: Semigroupoid EventFn
4246
derive newtype instance categoryBuilder :: Category EventFn
4347

0 commit comments

Comments
 (0)