Skip to content

Commit 0135377

Browse files
committed
Liberate Props from React
1 parent a943401 commit 0135377

File tree

5 files changed

+374
-370
lines changed

5 files changed

+374
-370
lines changed

src/Concur/Core/Props.purs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Concur.Core.Props where
2+
3+
import Control.Applicative (pure)
4+
import Data.Function ((<<<))
5+
import Data.Functor (class Functor)
6+
import Data.Unit (Unit, unit)
7+
import Effect (Effect)
8+
9+
data Props p a
10+
= PrimProp p
11+
| Handler ((a -> Effect Unit) -> p)
12+
13+
instance functorProps :: Functor (Props p) where
14+
map _ (PrimProp p) = PrimProp p
15+
map f (Handler h) = Handler \k -> h (k <<< f)
16+
17+
-- | Internal. Do not use. Use unsafeMkProp, or unsafeMkPropHandler instead.
18+
mkProp
19+
:: forall p a
20+
. (a -> Effect Unit)
21+
-> Props p a
22+
-> p
23+
mkProp _ (PrimProp a) = a
24+
mkProp h (Handler f) = f h
25+
26+
-- | Use `handleProp` to handle an event manually
27+
handleProp
28+
:: forall p a b
29+
. (a -> Effect Unit)
30+
-> Props p a
31+
-> Props p b
32+
handleProp _ (PrimProp p) = PrimProp p
33+
handleProp f (Handler g) = PrimProp (g f)
34+
35+
-- | Use this to filter the output of an event handler prop.
36+
-- | For example, to only handle the enter key - `filterProp isEnterEvent onKeyDown`
37+
filterProp ::
38+
forall p a.
39+
(a -> Boolean) ->
40+
Props p a ->
41+
Props p a
42+
filterProp _ p@(PrimProp _) = p
43+
filterProp ok (Handler g) = Handler \h ->
44+
(g \a -> if ok a
45+
then h a
46+
else pure unit)

src/Concur/React.purs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import Prelude
55
import Concur.Core (mkLeafWidget, wrapViewEvent)
66
import Concur.Core.Discharge (discharge, dischargePartialEffect)
77
import Concur.Core.LiftWidget (class LiftWidget, liftWidget)
8+
import Concur.Core.Props (mkProp)
89
import Concur.Core.Types (Widget)
9-
import Concur.React.Props (Props, mkProp)
10+
import Concur.React.Props (ReactProps)
1011
import Control.MultiAlternative (class MultiAlternative, orr)
1112
import Control.ShiftMap (class ShiftMap, shiftMap)
1213
import Data.Either (Either(..))
@@ -32,7 +33,7 @@ el ::
3233
forall m a.
3334
ShiftMap (Widget HTML) m =>
3435
NodeTag ->
35-
Array (Props a) ->
36+
Array (ReactProps a) ->
3637
m a ->
3738
m a
3839
el e props = shiftMap (\f w -> wrapViewEvent (\h v ->
@@ -43,7 +44,7 @@ elLeaf ::
4344
forall m a.
4445
LiftWidget HTML m =>
4546
LeafTag ->
46-
Array (Props a) ->
47+
Array (ReactProps a) ->
4748
m a
4849
elLeaf e props = liftWidget $ mkLeafWidget \h ->
4950
[e (map (mkProp h) props)]
@@ -54,7 +55,7 @@ el' ::
5455
ShiftMap (Widget HTML) m =>
5556
MultiAlternative m =>
5657
NodeTag ->
57-
Array (Props a) ->
58+
Array (ReactProps a) ->
5859
Array (m a) ->
5960
m a
6061
el' e props = el e props <<< orr

src/Concur/React/DOM.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import Prelude hiding (div,map,sub)
55
import Concur.Core.LiftWidget (class LiftWidget, liftWidget)
66
import Concur.Core.Types (Widget, display)
77
import Concur.React (HTML, el, el', elLeaf)
8-
import Concur.React.Props (Props)
8+
import Concur.React.Props (ReactProps)
99
import Control.MultiAlternative (class MultiAlternative)
1010
import Control.ShiftMap (class ShiftMap)
1111
import React.DOM as D
1212

1313
-- Wrappers for all DOM elements from purescript-react
1414
-- TODO: Generate these mechanically somehow
1515
type El1
16-
= forall m a. ShiftMap (Widget HTML) m => Array (Props a) -> m a -> m a
16+
= forall m a. ShiftMap (Widget HTML) m => Array (ReactProps a) -> m a -> m a
1717

1818
type El
19-
= forall m a. MultiAlternative m => ShiftMap (Widget HTML) m => Array (Props a) -> Array (m a) -> m a
19+
= forall m a. MultiAlternative m => ShiftMap (Widget HTML) m => Array (ReactProps a) -> Array (m a) -> m a
2020

2121
type El'
2222
= forall m a. MultiAlternative m => ShiftMap (Widget HTML) m => Array (m a) -> m a
2323

2424
type ElLeaf
25-
= forall m a. LiftWidget HTML m => Array (Props a) -> m a
25+
= forall m a. LiftWidget HTML m => Array (ReactProps a) -> m a
2626

2727
type ElLeaf'
2828
= forall m a. LiftWidget HTML m => m a

0 commit comments

Comments
 (0)