Skip to content

Commit aaa1ba8

Browse files
author
Marcin Szamotulski
committed
readRef and writeRef through ffi
1 parent 796debd commit aaa1ba8

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"dependencies": {
2020
"purescript-eff": "^3.0.0",
2121
"purescript-prelude": "^3.0.0",
22-
"purescript-unsafe-coerce": "^3.0.0"
22+
"purescript-unsafe-coerce": "^3.0.0",
23+
"purescript-dom": "^4.5.0"
2324
},
2425
"devDependencies": {
2526
"purescript-console": "^3.0.0",

src/React.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ function getChildren(this_) {
4040
}
4141
exports.getChildren = getChildren;
4242

43+
function readRefImpl (this_) {
44+
return function(name) {
45+
return function() {
46+
var refs = this_.refs || {};
47+
return refs[name];
48+
}
49+
}
50+
}
51+
exports.readRefImpl = readRefImpl;
52+
53+
function writeRef(this_) {
54+
return function(name) {
55+
return function(node) {
56+
return function() {
57+
var refs = this_.refs || {};
58+
refs[name] = node;
59+
this_.refs = refs;
60+
return {};
61+
}
62+
}
63+
}
64+
}
65+
exports.writeRef = writeRef;
66+
4367
function writeState(this_) {
4468
return function(state){
4569
return function(){

src/React.purs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module React
4444
, getProps
4545
, getRefs
4646
, readRef
47+
, writeRef
4748
, getChildren
4849

4950
, readState
@@ -68,9 +69,9 @@ module React
6869
import Prelude
6970

7071
import Control.Monad.Eff (kind Effect, Eff)
71-
import DOM.Node.Types (Node, readNode)
72-
import Data.Foreign (F, Foreign)
73-
import Data.Foreign.Index (readProp)
72+
import DOM.Node.Types (Node)
73+
import Data.Maybe (Maybe)
74+
import Data.Nullable (Nullable, toMaybe)
7475
import Unsafe.Coerce (unsafeCoerce)
7576

7677
-- | Name of a tag.
@@ -300,11 +301,22 @@ foreign import getRefs :: forall props state access eff.
300301
Eff (refs :: ReactRefs (read :: Read | access) | eff) Refs
301302

302303
-- | Read named ref from Refs
303-
readRef :: forall access eff.
304+
foreign import readRefImpl :: forall props state access eff.
305+
ReactThis props state ->
306+
String ->
307+
Eff (refs :: ReactRefs (read :: Read | access) | eff) (Nullable Node)
308+
309+
readRef :: forall props state access eff.
310+
ReactThis props state ->
311+
String ->
312+
Eff (refs :: ReactRefs (read :: Read | access) | eff) (Maybe Node)
313+
readRef this name = toMaybe <$> readRefImpl this name
314+
315+
foreign import writeRef :: forall props state access eff.
316+
ReactThis props state ->
304317
String ->
305-
Foreign ->
306-
Eff (refs :: ReactRefs (read :: Read | access) | eff) (F Node)
307-
readRef name refs = pure $ join (readNode <$> readProp name refs)
318+
Node ->
319+
Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit
308320

309321
-- | Read the component children property.
310322
foreign import getChildren :: forall props state eff.

src/React/DOM/Props.purs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module React.DOM.Props where
22

3-
import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, handle)
3+
import Control.Monad.Eff (Eff)
4+
import Control.Monad.Eff.Unsafe (unsafePerformEff)
5+
import DOM.Node.Types (Node)
6+
import Prelude (Unit, (<<<))
7+
import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, ReactRefs, Write, handle)
48

59
foreign import data Props :: Type
610

@@ -300,9 +304,13 @@ readOnly = unsafeMkProps "readOnly"
300304
ref :: String -> Props
301305
ref = unsafeMkProps "ref"
302306

307+
-- | You can use `writeRef` to store a reference on `Refs`.
308+
-- | ``` purescrript
309+
-- | div [ refCb (writeRef this "inputEl") ] [...]
310+
-- | ```
303311
refCb
304-
:: forall props state
305-
. (ReactThis props state -> Eff (refs :: ReactRefs (write :: Write | access) | eff))
312+
:: forall access eff
313+
. (Node -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit)
306314
-> Props
307315
refCb cb = unsafeMkProps "ref" (unsafePerformEff <<< cb)
308316

0 commit comments

Comments
 (0)