Skip to content

Move shared Events stuff out of React.Basic.DOM #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/controlled-input/src/ControlledInput.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ControlledInput where

import Prelude

import Data.Maybe (Maybe(..), fromMaybe)
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import React.Basic (ReactComponent, react)
import React.Basic.DOM as R
import React.Basic.DOM.Events (preventDefault, targetValue, timeStamp)
Expand All @@ -24,6 +24,6 @@ component = react
}
]
, R.p_ [ R.text ("Current value = " <> show state.value) ]
, R.p_ [ R.text ("Changed at = " <> fromMaybe "never" (show <$> state.timeStamp)) ]
, R.p_ [ R.text ("Changed at = " <> maybe "never" show state.timeStamp) ]
]
}
12 changes: 9 additions & 3 deletions generated-docs/React/Basic/Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Event data that we receive from React.

``` purescript
newtype EventFn a b
= EventFn (a -> b)
```

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

_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`)_

##### Instances
``` purescript
Semigroupoid EventFn
Category EventFn
(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
```

#### `unsafeEventFn`

``` purescript
unsafeEventFn :: forall a b. (a -> b) -> EventFn a b
```

Unsafely create an `EventFn`. This function should be avoided.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth commenting briefly on why this function is dangerous (it lets SyntheticEvents leave the current scope). Otherwise, this PR looks good to me 👍

Use the helper functions specific to your platform (such as `React.Basic.DOM.Events`).

#### `handler`

``` purescript
Expand Down
32 changes: 16 additions & 16 deletions src/React/Basic/DOM/Events.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module React.Basic.DOM.Events

import Data.Maybe (Maybe)
import Data.Nullable (toMaybe)
import React.Basic.Events (EventFn(..), SyntheticEvent)
import React.Basic.Events (EventFn, SyntheticEvent, unsafeEventFn)
import Unsafe.Coerce (unsafeCoerce)

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

bubbles :: EventFn SyntheticEvent Boolean
bubbles = EventFn \e -> (unsafeCoerce e).bubbles
bubbles = unsafeEventFn \e -> (unsafeCoerce e).bubbles

cancelable :: EventFn SyntheticEvent Boolean
cancelable = EventFn \e -> (unsafeCoerce e).cancelable
cancelable = unsafeEventFn \e -> (unsafeCoerce e).cancelable

currentTarget :: EventFn SyntheticEvent DOMNode
currentTarget = EventFn \e -> (unsafeCoerce e).currentTarget
currentTarget = unsafeEventFn \e -> (unsafeCoerce e).currentTarget

eventPhase :: EventFn SyntheticEvent Int
eventPhase = EventFn \e -> (unsafeCoerce e).eventPhase
eventPhase = unsafeEventFn \e -> (unsafeCoerce e).eventPhase

eventPhaseNone :: Int
eventPhaseNone = 0
Expand All @@ -60,42 +60,42 @@ eventPhaseBubbling :: Int
eventPhaseBubbling = 3

isTrusted :: EventFn SyntheticEvent Boolean
isTrusted = EventFn \e -> (unsafeCoerce e).isTrusted
isTrusted = unsafeEventFn \e -> (unsafeCoerce e).isTrusted

nativeEvent :: EventFn SyntheticEvent DOMEvent
nativeEvent = EventFn \e -> (unsafeCoerce e).nativeEvent
nativeEvent = unsafeEventFn \e -> (unsafeCoerce e).nativeEvent

preventDefault :: EventFn SyntheticEvent SyntheticEvent
preventDefault = EventFn unsafePreventDefault
preventDefault = unsafeEventFn unsafePreventDefault

foreign import unsafePreventDefault :: SyntheticEvent -> SyntheticEvent

isDefaultPrevented :: EventFn SyntheticEvent Boolean
isDefaultPrevented = EventFn unsafeIsDefaultPrevented
isDefaultPrevented = unsafeEventFn unsafeIsDefaultPrevented

foreign import unsafeIsDefaultPrevented :: SyntheticEvent -> Boolean

stopPropagation :: EventFn SyntheticEvent SyntheticEvent
stopPropagation = EventFn unsafeStopPropagation
stopPropagation = unsafeEventFn unsafeStopPropagation

foreign import unsafeStopPropagation :: SyntheticEvent -> SyntheticEvent

isPropagationStopped :: EventFn SyntheticEvent Boolean
isPropagationStopped = EventFn unsafeIsPropagationStopped
isPropagationStopped = unsafeEventFn unsafeIsPropagationStopped

foreign import unsafeIsPropagationStopped :: SyntheticEvent -> Boolean

target :: EventFn SyntheticEvent DOMNode
target = EventFn \e -> (unsafeCoerce e).target
target = unsafeEventFn \e -> (unsafeCoerce e).target

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

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

timeStamp :: EventFn SyntheticEvent Number
timeStamp = EventFn \e -> (unsafeCoerce e).timeStamp
timeStamp = unsafeEventFn \e -> (unsafeCoerce e).timeStamp

type_ :: EventFn SyntheticEvent String
type_ = EventFn \e -> (unsafeCoerce e)."type"
type_ = unsafeEventFn \e -> (unsafeCoerce e)."type"
10 changes: 7 additions & 3 deletions src/React/Basic/Events.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module React.Basic.Events
( EventHandler
, SyntheticEvent
, EventFn(..)
, EventFn
, unsafeEventFn
, handler
, merge
, class Merge
Expand Down Expand Up @@ -34,10 +35,13 @@ foreign import data SyntheticEvent :: Type
-- | \value -> setState \_ -> { value }
-- | }
-- | ```
-- |
-- | _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`)_
newtype EventFn a b = EventFn (a -> b)

-- | Unsafely create an `EventFn`. This function should be avoided.
-- | Use the helper functions specific to your platform (such as `React.Basic.DOM.Events`).
unsafeEventFn :: forall a b. (a -> b) -> EventFn a b
unsafeEventFn = EventFn

derive newtype instance semigroupoidBuilder :: Semigroupoid EventFn
derive newtype instance categoryBuilder :: Category EventFn

Expand Down