From 088bbf71faf014e36840ac171e689f99169b2f06 Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Wed, 28 Jun 2017 00:10:51 +0200 Subject: [PATCH 1/7] refs * React.DOM.Props.ref - set ref property (string) * React.readRef - read ref through Foreign --- src/React.purs | 15 +++++++++++++++ src/React/DOM/Props.purs | 3 +++ 2 files changed, 18 insertions(+) diff --git a/src/React.purs b/src/React.purs index d3c9d63..e2312eb 100644 --- a/src/React.purs +++ b/src/React.purs @@ -43,6 +43,7 @@ module React , getProps , getRefs + , readRef , getChildren , readState @@ -65,7 +66,11 @@ module React ) where import Prelude + import Control.Monad.Eff (kind Effect, Eff) +import DOM.Node.Types (Node, readNode) +import Data.Foreign (F, Foreign, toForeign) +import Data.Foreign.Index (readProp) import Unsafe.Coerce (unsafeCoerce) -- | Name of a tag. @@ -294,6 +299,16 @@ foreign import getRefs :: forall props state access eff. ReactThis props state -> Eff (refs :: ReactRefs (read :: Read | access) | eff) Refs +-- | Read named ref from Refs +readRef :: forall access eff. + String -> + Refs -> + Eff (refs :: ReactRefs (read :: Read | access) | eff) (F Node) +readRef name refs = pure $ join (readNode <$> prop) + where + prop :: F Foreign + prop = readProp name (toForeign refs) + -- | Read the component children property. foreign import getChildren :: forall props state eff. ReactThis props state -> diff --git a/src/React/DOM/Props.purs b/src/React/DOM/Props.purs index 8bac25c..702857f 100644 --- a/src/React/DOM/Props.purs +++ b/src/React/DOM/Props.purs @@ -297,6 +297,9 @@ radioGroup = unsafeMkProps "radioGroup" readOnly :: Boolean -> Props readOnly = unsafeMkProps "readOnly" +ref :: String -> Props +ref = unsafeMkProps "ref" + rel :: String -> Props rel = unsafeMkProps "rel" From 90ea09b89de6df8896d19d3566840338e489a578 Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Wed, 28 Jun 2017 09:52:00 +0200 Subject: [PATCH 2/7] readRef from Foreign rather than Refs --- src/React.purs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/React.purs b/src/React.purs index e2312eb..7cdef24 100644 --- a/src/React.purs +++ b/src/React.purs @@ -69,7 +69,7 @@ import Prelude import Control.Monad.Eff (kind Effect, Eff) import DOM.Node.Types (Node, readNode) -import Data.Foreign (F, Foreign, toForeign) +import Data.Foreign (F, Foreign) import Data.Foreign.Index (readProp) import Unsafe.Coerce (unsafeCoerce) @@ -302,12 +302,9 @@ foreign import getRefs :: forall props state access eff. -- | Read named ref from Refs readRef :: forall access eff. String -> - Refs -> + Foreign -> Eff (refs :: ReactRefs (read :: Read | access) | eff) (F Node) -readRef name refs = pure $ join (readNode <$> prop) - where - prop :: F Foreign - prop = readProp name (toForeign refs) +readRef name refs = pure $ join (readNode <$> readProp name refs) -- | Read the component children property. foreign import getChildren :: forall props state eff. From 796debd99d119b15818dbffc51794dfd2e042a8f Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Wed, 28 Jun 2017 09:52:23 +0200 Subject: [PATCH 3/7] React.DOM.Props.refCb --- src/React/DOM/Props.purs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/React/DOM/Props.purs b/src/React/DOM/Props.purs index 702857f..fe6f8c4 100644 --- a/src/React/DOM/Props.purs +++ b/src/React/DOM/Props.purs @@ -300,6 +300,12 @@ readOnly = unsafeMkProps "readOnly" ref :: String -> Props ref = unsafeMkProps "ref" +refCb + :: forall props state + . (ReactThis props state -> Eff (refs :: ReactRefs (write :: Write | access) | eff)) + -> Props +refCb cb = unsafeMkProps "ref" (unsafePerformEff <<< cb) + rel :: String -> Props rel = unsafeMkProps "rel" From aaa1ba8e51bbcdd802ef8e980b7d28fc999c55ba Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Sat, 8 Jul 2017 13:41:36 +0200 Subject: [PATCH 4/7] readRef and writeRef through ffi --- bower.json | 3 ++- src/React.js | 24 ++++++++++++++++++++++++ src/React.purs | 26 +++++++++++++++++++------- src/React/DOM/Props.purs | 14 +++++++++++--- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/bower.json b/bower.json index b1677d4..4af39cd 100644 --- a/bower.json +++ b/bower.json @@ -19,7 +19,8 @@ "dependencies": { "purescript-eff": "^3.0.0", "purescript-prelude": "^3.0.0", - "purescript-unsafe-coerce": "^3.0.0" + "purescript-unsafe-coerce": "^3.0.0", + "purescript-dom": "^4.5.0" }, "devDependencies": { "purescript-console": "^3.0.0", diff --git a/src/React.js b/src/React.js index b4fee1e..f4e2efb 100644 --- a/src/React.js +++ b/src/React.js @@ -40,6 +40,30 @@ function getChildren(this_) { } exports.getChildren = getChildren; +function readRefImpl (this_) { + return function(name) { + return function() { + var refs = this_.refs || {}; + return refs[name]; + } + } +} +exports.readRefImpl = readRefImpl; + +function writeRef(this_) { + return function(name) { + return function(node) { + return function() { + var refs = this_.refs || {}; + refs[name] = node; + this_.refs = refs; + return {}; + } + } + } +} +exports.writeRef = writeRef; + function writeState(this_) { return function(state){ return function(){ diff --git a/src/React.purs b/src/React.purs index 7cdef24..c0ab56e 100644 --- a/src/React.purs +++ b/src/React.purs @@ -44,6 +44,7 @@ module React , getProps , getRefs , readRef + , writeRef , getChildren , readState @@ -68,9 +69,9 @@ module React import Prelude import Control.Monad.Eff (kind Effect, Eff) -import DOM.Node.Types (Node, readNode) -import Data.Foreign (F, Foreign) -import Data.Foreign.Index (readProp) +import DOM.Node.Types (Node) +import Data.Maybe (Maybe) +import Data.Nullable (Nullable, toMaybe) import Unsafe.Coerce (unsafeCoerce) -- | Name of a tag. @@ -300,11 +301,22 @@ foreign import getRefs :: forall props state access eff. Eff (refs :: ReactRefs (read :: Read | access) | eff) Refs -- | Read named ref from Refs -readRef :: forall access eff. +foreign import readRefImpl :: forall props state access eff. + ReactThis props state -> + String -> + Eff (refs :: ReactRefs (read :: Read | access) | eff) (Nullable Node) + +readRef :: forall props state access eff. + ReactThis props state -> + String -> + Eff (refs :: ReactRefs (read :: Read | access) | eff) (Maybe Node) +readRef this name = toMaybe <$> readRefImpl this name + +foreign import writeRef :: forall props state access eff. + ReactThis props state -> String -> - Foreign -> - Eff (refs :: ReactRefs (read :: Read | access) | eff) (F Node) -readRef name refs = pure $ join (readNode <$> readProp name refs) + Node -> + Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit -- | Read the component children property. foreign import getChildren :: forall props state eff. diff --git a/src/React/DOM/Props.purs b/src/React/DOM/Props.purs index fe6f8c4..75c4d92 100644 --- a/src/React/DOM/Props.purs +++ b/src/React/DOM/Props.purs @@ -1,6 +1,10 @@ module React.DOM.Props where -import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, handle) +import Control.Monad.Eff (Eff) +import Control.Monad.Eff.Unsafe (unsafePerformEff) +import DOM.Node.Types (Node) +import Prelude (Unit, (<<<)) +import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, ReactRefs, Write, handle) foreign import data Props :: Type @@ -300,9 +304,13 @@ readOnly = unsafeMkProps "readOnly" ref :: String -> Props ref = unsafeMkProps "ref" +-- | You can use `writeRef` to store a reference on `Refs`. +-- | ``` purescrript +-- | div [ refCb (writeRef this "inputEl") ] [...] +-- | ``` refCb - :: forall props state - . (ReactThis props state -> Eff (refs :: ReactRefs (write :: Write | access) | eff)) + :: forall access eff + . (Node -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) -> Props refCb cb = unsafeMkProps "ref" (unsafePerformEff <<< cb) From 00d51f6e5c7fc9edc736aa32eab971cf70f77910 Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Tue, 5 Sep 2017 20:02:22 +0200 Subject: [PATCH 5/7] Remove DOM dependency, use foreign data type for Ref Still adds two small dependencies * purescript-maybe * purescript-nullable --- bower.json | 3 ++- src/React.purs | 10 ++++++---- src/React/DOM/Props.purs | 5 ++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/bower.json b/bower.json index 4af39cd..73374eb 100644 --- a/bower.json +++ b/bower.json @@ -20,7 +20,8 @@ "purescript-eff": "^3.0.0", "purescript-prelude": "^3.0.0", "purescript-unsafe-coerce": "^3.0.0", - "purescript-dom": "^4.5.0" + "purescript-maybe": "^3.0.0", + "purescript-nullable": "^3.0.0" }, "devDependencies": { "purescript-console": "^3.0.0", diff --git a/src/React.purs b/src/React.purs index c0ab56e..fd97b6c 100644 --- a/src/React.purs +++ b/src/React.purs @@ -19,6 +19,7 @@ module React , ReactRefs , Refs + , Ref , Render , GetInitialState @@ -69,7 +70,6 @@ module React import Prelude import Control.Monad.Eff (kind Effect, Eff) -import DOM.Node.Types (Node) import Data.Maybe (Maybe) import Data.Nullable (Nullable, toMaybe) import Unsafe.Coerce (unsafeCoerce) @@ -300,22 +300,24 @@ foreign import getRefs :: forall props state access eff. ReactThis props state -> Eff (refs :: ReactRefs (read :: Read | access) | eff) Refs +foreign import data Ref :: Type + -- | Read named ref from Refs foreign import readRefImpl :: forall props state access eff. ReactThis props state -> String -> - Eff (refs :: ReactRefs (read :: Read | access) | eff) (Nullable Node) + Eff (refs :: ReactRefs (read :: Read | access) | eff) (Nullable Ref) readRef :: forall props state access eff. ReactThis props state -> String -> - Eff (refs :: ReactRefs (read :: Read | access) | eff) (Maybe Node) + Eff (refs :: ReactRefs (read :: Read | access) | eff) (Maybe Ref) readRef this name = toMaybe <$> readRefImpl this name foreign import writeRef :: forall props state access eff. ReactThis props state -> String -> - Node -> + Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit -- | Read the component children property. diff --git a/src/React/DOM/Props.purs b/src/React/DOM/Props.purs index 75c4d92..a2f09f3 100644 --- a/src/React/DOM/Props.purs +++ b/src/React/DOM/Props.purs @@ -2,9 +2,8 @@ module React.DOM.Props where import Control.Monad.Eff (Eff) import Control.Monad.Eff.Unsafe (unsafePerformEff) -import DOM.Node.Types (Node) import Prelude (Unit, (<<<)) -import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, ReactRefs, Write, handle) +import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, ReactRefs, Ref, Write, handle) foreign import data Props :: Type @@ -310,7 +309,7 @@ ref = unsafeMkProps "ref" -- | ``` refCb :: forall access eff - . (Node -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) + . (Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) -> Props refCb cb = unsafeMkProps "ref" (unsafePerformEff <<< cb) From dc2b66a6cb85ac9cb754dbe2fb674587a93cb2d4 Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Sat, 30 Sep 2017 12:35:28 +0200 Subject: [PATCH 6/7] Rename: withRef && added comments. --- src/React.purs | 6 +++++- src/React/DOM/Props.purs | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/React.purs b/src/React.purs index fd97b6c..b6e066c 100644 --- a/src/React.purs +++ b/src/React.purs @@ -300,20 +300,24 @@ foreign import getRefs :: forall props state access eff. ReactThis props state -> Eff (refs :: ReactRefs (read :: Read | access) | eff) Refs +-- | Ref type. You can store `Ref` types on `Refs` object (which in +-- | corresponds to `this.refs`). Use `ReactDOM.refToNode` if you want to +-- store a `DOM.Node.Types.Node` foreign import data Ref :: Type --- | Read named ref from Refs foreign import readRefImpl :: forall props state access eff. ReactThis props state -> String -> Eff (refs :: ReactRefs (read :: Read | access) | eff) (Nullable Ref) +-- | Read named ref from `Refs`. readRef :: forall props state access eff. ReactThis props state -> String -> Eff (refs :: ReactRefs (read :: Read | access) | eff) (Maybe Ref) readRef this name = toMaybe <$> readRefImpl this name +-- | Write a `Ref` to `Refs` foreign import writeRef :: forall props state access eff. ReactThis props state -> String -> diff --git a/src/React/DOM/Props.purs b/src/React/DOM/Props.purs index a2f09f3..f3078fe 100644 --- a/src/React/DOM/Props.purs +++ b/src/React/DOM/Props.purs @@ -305,13 +305,13 @@ ref = unsafeMkProps "ref" -- | You can use `writeRef` to store a reference on `Refs`. -- | ``` purescrript --- | div [ refCb (writeRef this "inputEl") ] [...] +-- | div [ withRef (writeRef this "inputElement") ] [...] -- | ``` -refCb +withRef :: forall access eff . (Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) -> Props -refCb cb = unsafeMkProps "ref" (unsafePerformEff <<< cb) +withRef cb = unsafeMkProps "ref" (unsafePerformEff <<< cb) rel :: String -> Props rel = unsafeMkProps "rel" From 1b0d54244fc5e9f48ad2a7b075cf2654e0c0251c Mon Sep 17 00:00:00 2001 From: Marcin Szamotulski Date: Sat, 30 Sep 2017 18:55:52 +0200 Subject: [PATCH 7/7] Nullable Ref in withRefs --- src/React.purs | 2 +- src/React/DOM/Props.purs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/React.purs b/src/React.purs index b6e066c..8d5a0a3 100644 --- a/src/React.purs +++ b/src/React.purs @@ -321,7 +321,7 @@ readRef this name = toMaybe <$> readRefImpl this name foreign import writeRef :: forall props state access eff. ReactThis props state -> String -> - Ref -> + Nullable Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit -- | Read the component children property. diff --git a/src/React/DOM/Props.purs b/src/React/DOM/Props.purs index f3078fe..fb4d1f1 100644 --- a/src/React/DOM/Props.purs +++ b/src/React/DOM/Props.purs @@ -2,6 +2,7 @@ module React.DOM.Props where import Control.Monad.Eff (Eff) import Control.Monad.Eff.Unsafe (unsafePerformEff) +import Data.Nullable (Nullable) import Prelude (Unit, (<<<)) import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, ReactRefs, Ref, Write, handle) @@ -309,7 +310,7 @@ ref = unsafeMkProps "ref" -- | ``` withRef :: forall access eff - . (Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) + . (Nullable Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit) -> Props withRef cb = unsafeMkProps "ref" (unsafePerformEff <<< cb)