Skip to content

Add support for createRef API #172

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 11 commits into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 2 additions & 7 deletions src/React.purs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module React
, pureComponentWithDerivedState
, statelessComponent
, ReactClass
, ReactRef
, getProps
, getState
, setState
Expand Down Expand Up @@ -68,11 +67,11 @@ module React

import Prelude

import Data.Nullable (Nullable)
import Effect (Effect)
import Effect.Exception (Error)
import Effect.Uncurried (EffectFn1)
import Prim.Row as Row
import React.Ref as Ref
import Type.Row (type (+))
import Unsafe.Coerce (unsafeCoerce)

Expand Down Expand Up @@ -252,10 +251,6 @@ foreign import data ReactClass :: Type -> Type

foreign import fragment :: ReactClass { children :: Children }

-- | Type for React refs. This type is opaque, but you can use `Data.Foreign`
-- | and `DOM` to validate the underlying representation.
foreign import data ReactRef :: Type

-- | Read the component props.
foreign import getProps :: forall props state.
ReactThis props state ->
Expand Down Expand Up @@ -340,7 +335,7 @@ class ReactPropFields (required :: # Type) (given :: # Type)

type ReservedReactPropFields r =
( key :: String
, ref :: SyntheticEventHandler (Nullable ReactRef)
, ref :: Ref.RefHandler Ref.ReactInstance
| r
)

Expand Down
8 changes: 4 additions & 4 deletions src/React/DOM/Props.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ module React.DOM.Props where

import Prelude

import Data.Nullable (Nullable)
import Effect (Effect)
import Effect.Uncurried (mkEffectFn1)
import React (ReactRef)
import React.Ref as Ref
import React.SyntheticEvent
( SyntheticEvent
, SyntheticAnimationEvent
Expand All @@ -20,6 +19,7 @@ import React.SyntheticEvent
, SyntheticUIEvent
, SyntheticWheelEvent
)
import Web.HTML.HTMLElement (HTMLElement)
Copy link
Contributor

@ethul ethul Jul 25, 2019

Choose a reason for hiding this comment

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

Just a quick note. This is coming from purescript-web-html, but I didn't notice the dependency in the bower file. Is it being pulled in from another dependency?

On this note, I am unsure if we want this as a dependency since runtimes like react native, etc., would not necessarily require it. So far we have been attempting to keep dependencies at a minimum. That being said, I am not completely opposed to adding the dependency, but wanted to open it up for discussion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ethul Well spotted; I think I forgot to push the change to the bower file. I had added "purescript-web-html": "^2.2.0" locally.

I suppose that we could have a local HTMLElement type here and not use the library version. It would save on the dependency, but on the other hand anyone using DOM refs would have to write a conversion function (i.e. unsafeCoerce), which might be annoying.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding the dependency.


foreign import data Props :: Type

Expand Down Expand Up @@ -894,8 +894,8 @@ onScrollCapture f = unsafeMkProps "onScrollCapture" (mkEffectFn1 f)
onWheelCapture :: (SyntheticWheelEvent -> Effect Unit) -> Props
onWheelCapture f = unsafeMkProps "onWheelCapture" (mkEffectFn1 f)

ref :: (Nullable ReactRef -> Effect Unit) -> Props
ref f = unsafeMkProps "ref" (mkEffectFn1 f)
ref :: Ref.RefHandler HTMLElement -> Props
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this always HTMLElement here? For example, on React Native, would this be something different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. I haven't used React Native myself, but I looked into this and as far as I can tell Native doesn't have refs - it seems to use something called Direct Manipulation instead. This does raise the question of why the React team put refs in the React library instead of ReactDOM; I'm not sure what the answer is.

ref = unsafeMkProps "ref"

suppressContentEditableWarning :: Boolean -> Props
suppressContentEditableWarning = unsafeMkProps "suppressContentEditableWarning"
Expand Down
10 changes: 10 additions & 0 deletions src/React/Ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

var React = require("react");

exports.createRef = React.createRef;

exports.getCurrentRef_ = function(ref) {
if (ref.current) return ref.current;
else return ref;
}
49 changes: 49 additions & 0 deletions src/React/Ref.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module React.Ref
( Ref
, RefHandler
, ReactInstance
, fromRef
, fromEffect
, getCurrentRef
, createDOMRef
, createInstanceRef
) where

import Prelude
import Effect (Effect)
import Data.Maybe (Maybe)
import Web.HTML.HTMLElement (HTMLElement)
import Data.Nullable (Nullable)
import Data.Nullable as Nullable
import Effect.Uncurried (EffectFn1, runEffectFn1, mkEffectFn1)
import Unsafe.Coerce (unsafeCoerce)


foreign import data ReactInstance :: Type

foreign import data Ref :: Type -> Type

foreign import data RefHandler :: Type -> Type


foreign import createRef :: forall a. Effect (Ref a)

createDOMRef :: Effect (Ref HTMLElement)
createDOMRef = createRef

createInstanceRef :: Effect (Ref ReactInstance)
createInstanceRef = createRef


fromRef :: forall a. Ref a -> RefHandler a
fromRef = unsafeCoerce


fromEffect :: forall a. (Ref a -> Effect Unit) -> RefHandler a
fromEffect = unsafeCoerce <<< mkEffectFn1


foreign import getCurrentRef_ :: forall a. EffectFn1 (Ref a) (Nullable a)

getCurrentRef :: forall a. Ref a -> Effect (Maybe a)
getCurrentRef ref = Nullable.toMaybe <$> runEffectFn1 getCurrentRef_ ref