Skip to content

Commit 088bbf7

Browse files
author
Marcin Szamotulski
committed
refs
* React.DOM.Props.ref - set ref property (string) * React.readRef - read ref through Foreign
1 parent f753b9f commit 088bbf7

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/React.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module React
4343

4444
, getProps
4545
, getRefs
46+
, readRef
4647
, getChildren
4748

4849
, readState
@@ -65,7 +66,11 @@ module React
6566
) where
6667

6768
import Prelude
69+
6870
import Control.Monad.Eff (kind Effect, Eff)
71+
import DOM.Node.Types (Node, readNode)
72+
import Data.Foreign (F, Foreign, toForeign)
73+
import Data.Foreign.Index (readProp)
6974
import Unsafe.Coerce (unsafeCoerce)
7075

7176
-- | Name of a tag.
@@ -294,6 +299,16 @@ foreign import getRefs :: forall props state access eff.
294299
ReactThis props state ->
295300
Eff (refs :: ReactRefs (read :: Read | access) | eff) Refs
296301

302+
-- | Read named ref from Refs
303+
readRef :: forall access eff.
304+
String ->
305+
Refs ->
306+
Eff (refs :: ReactRefs (read :: Read | access) | eff) (F Node)
307+
readRef name refs = pure $ join (readNode <$> prop)
308+
where
309+
prop :: F Foreign
310+
prop = readProp name (toForeign refs)
311+
297312
-- | Read the component children property.
298313
foreign import getChildren :: forall props state eff.
299314
ReactThis props state ->

src/React/DOM/Props.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ radioGroup = unsafeMkProps "radioGroup"
297297
readOnly :: Boolean -> Props
298298
readOnly = unsafeMkProps "readOnly"
299299

300+
ref :: String -> Props
301+
ref = unsafeMkProps "ref"
302+
300303
rel :: String -> Props
301304
rel = unsafeMkProps "rel"
302305

0 commit comments

Comments
 (0)