Skip to content

Use Maybe in targetValue and targetChecked #25

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 1 commit into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions examples/controlled-input/src/ControlledInput.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ module ControlledInput where

import Prelude

import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.Nullable (toMaybe)
import Data.Maybe (Maybe(..), fromMaybe)
import React.Basic (ReactComponent, react)
import React.Basic.DOM as R
import React.Basic.DOM.Events (targetValue, timeStamp)
Expand All @@ -16,14 +15,15 @@ component = react
, receiveProps: \_ _ _ -> pure unit
, render: \_ state setState ->
R.div_
[ R.p_ [ R.input { onChange: Events.handler (Events.preventDefault >>> Events.merge { targetValue, timeStamp }) \{ timeStamp, targetValue } ->
setState \_ -> { value: fromMaybe "" (toMaybe targetValue)
, timeStamp: Just timeStamp
}
[ R.p_ [ R.input { onChange: Events.handler (Events.preventDefault >>> Events.merge { targetValue, timeStamp })
\{ timeStamp, targetValue } -> setState \_ ->
{ value: fromMaybe "" targetValue
, timeStamp: Just timeStamp
}
, value: state.value
}
]
, R.p_ [ R.text ("Current value = " <> show state.value) ]
, R.p_ (maybe [] (\ts -> [R.text ("Changed at: " <> show ts)]) state.timeStamp)
, R.p_ [ R.text ("Changed at = " <> fromMaybe "never" (show <$> state.timeStamp)) ]
]
}
11 changes: 6 additions & 5 deletions src/React/Basic/DOM/Events.purs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Uncurried (EffFn1, mkEffFn1)
import Data.Nullable (Nullable)
import Data.Maybe (Maybe)
import Data.Nullable (toMaybe)
import Data.Record (delete, get, insert)
import Data.Symbol (class IsSymbol, SProxy(SProxy))
import React.Basic (ReactFX)
Expand Down Expand Up @@ -175,11 +176,11 @@ foreign import unsafeIsPropagationStopped :: SyntheticEvent -> Boolean
target :: EventFn SyntheticEvent DOMNode
target = EventFn \e -> (unsafeCoerce e).target

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

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

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