Skip to content

Commit 03d52a5

Browse files
Marcin Szamotulskiethul
Marcin Szamotulski
authored andcommitted
refs (#100)
* refs * React.DOM.Props.ref - set ref property (string) * React.readRef - read ref through Foreign * readRef from Foreign rather than Refs * React.DOM.Props.refCb * readRef and writeRef through ffi * Remove DOM dependency, use foreign data type for Ref Still adds two small dependencies * purescript-maybe * purescript-nullable * Rename: withRef && added comments. * Nullable Ref in withRefs
1 parent 76b400a commit 03d52a5

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

bower.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
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-maybe": "^3.0.0",
24+
"purescript-nullable": "^3.0.0"
2325
},
2426
"devDependencies": {
2527
"purescript-console": "^3.0.0",

src/React.js

+24
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

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module React
1919
, ReactRefs
2020

2121
, Refs
22+
, Ref
2223

2324
, Render
2425
, GetInitialState
@@ -43,6 +44,8 @@ module React
4344

4445
, getProps
4546
, getRefs
47+
, readRef
48+
, writeRef
4649
, getChildren
4750

4851
, readState
@@ -73,6 +76,8 @@ module React
7376
import Prelude
7477

7578
import Control.Monad.Eff (kind Effect, Eff)
79+
import Data.Maybe (Maybe)
80+
import Data.Nullable (Nullable, toMaybe)
7681
import Control.Monad.Eff.Uncurried (EffFn2, runEffFn2)
7782
import Unsafe.Coerce (unsafeCoerce)
7883

@@ -302,6 +307,30 @@ foreign import getRefs :: forall props state access eff.
302307
ReactThis props state ->
303308
Eff (refs :: ReactRefs (read :: Read | access) | eff) Refs
304309

310+
-- | Ref type. You can store `Ref` types on `Refs` object (which in
311+
-- | corresponds to `this.refs`). Use `ReactDOM.refToNode` if you want to
312+
-- store a `DOM.Node.Types.Node`
313+
foreign import data Ref :: Type
314+
315+
foreign import readRefImpl :: forall props state access eff.
316+
ReactThis props state ->
317+
String ->
318+
Eff (refs :: ReactRefs (read :: Read | access) | eff) (Nullable Ref)
319+
320+
-- | Read named ref from `Refs`.
321+
readRef :: forall props state access eff.
322+
ReactThis props state ->
323+
String ->
324+
Eff (refs :: ReactRefs (read :: Read | access) | eff) (Maybe Ref)
325+
readRef this name = toMaybe <$> readRefImpl this name
326+
327+
-- | Write a `Ref` to `Refs`
328+
foreign import writeRef :: forall props state access eff.
329+
ReactThis props state ->
330+
String ->
331+
Nullable Ref ->
332+
Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit
333+
305334
-- | Read the component children property.
306335
foreign import getChildren :: forall props state eff.
307336
ReactThis props state ->

src/React/DOM/Props.purs

+18-1
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 Data.Nullable (Nullable)
6+
import Prelude (Unit, (<<<))
7+
import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, ReactRefs, Ref, Write, handle)
48

59
foreign import data Props :: Type
610

@@ -297,6 +301,19 @@ radioGroup = unsafeMkProps "radioGroup"
297301
readOnly :: Boolean -> Props
298302
readOnly = unsafeMkProps "readOnly"
299303

304+
ref :: String -> Props
305+
ref = unsafeMkProps "ref"
306+
307+
-- | You can use `writeRef` to store a reference on `Refs`.
308+
-- | ``` purescrript
309+
-- | div [ withRef (writeRef this "inputElement") ] [...]
310+
-- | ```
311+
withRef
312+
:: forall access eff
313+
. (Nullable Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit)
314+
-> Props
315+
withRef cb = unsafeMkProps "ref" (unsafePerformEff <<< cb)
316+
300317
rel :: String -> Props
301318
rel = unsafeMkProps "rel"
302319

0 commit comments

Comments
 (0)