Skip to content

refs #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 11, 2017
Merged

refs #100

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"dependencies": {
"purescript-eff": "^3.0.0",
"purescript-prelude": "^3.0.0",
"purescript-unsafe-coerce": "^3.0.0"
"purescript-unsafe-coerce": "^3.0.0",
"purescript-maybe": "^3.0.0",
"purescript-nullable": "^3.0.0"
},
"devDependencies": {
"purescript-console": "^3.0.0",
Expand Down
24 changes: 24 additions & 0 deletions src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
29 changes: 29 additions & 0 deletions src/React.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module React
, ReactRefs

, Refs
, Ref

, Render
, GetInitialState
Expand All @@ -43,6 +44,8 @@ module React

, getProps
, getRefs
, readRef
, writeRef
, getChildren

, readState
Expand Down Expand Up @@ -73,6 +76,8 @@ module React
import Prelude

import Control.Monad.Eff (kind Effect, Eff)
import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toMaybe)
import Control.Monad.Eff.Uncurried (EffFn2, runEffFn2)
import Unsafe.Coerce (unsafeCoerce)

Expand Down Expand Up @@ -302,6 +307,30 @@ 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

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 ->
Nullable Ref ->
Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit

-- | Read the component children property.
foreign import getChildren :: forall props state eff.
ReactThis props state ->
Expand Down
19 changes: 18 additions & 1 deletion src/React/DOM/Props.purs
Original file line number Diff line number Diff line change
@@ -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 Data.Nullable (Nullable)
import Prelude (Unit, (<<<))
import React (Event, EventHandlerContext, KeyboardEvent, MouseEvent, ReactRefs, Ref, Write, handle)

foreign import data Props :: Type

Expand Down Expand Up @@ -297,6 +301,19 @@ radioGroup = unsafeMkProps "radioGroup"
readOnly :: Boolean -> Props
readOnly = unsafeMkProps "readOnly"

ref :: String -> Props
ref = unsafeMkProps "ref"

-- | You can use `writeRef` to store a reference on `Refs`.
-- | ``` purescrript
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick note: should be purescript

-- | div [ withRef (writeRef this "inputElement") ] [...]
-- | ```
withRef
:: forall access eff
. (Nullable Ref -> Eff (refs :: ReactRefs (write :: Write | access) | eff) Unit)
-> Props
withRef cb = unsafeMkProps "ref" (unsafePerformEff <<< cb)

rel :: String -> Props
rel = unsafeMkProps "rel"

Expand Down