Skip to content

Commit 523f5a0

Browse files
natefaubionethul
authored andcommitted
Replace ReactRender with IsReactElement (#137)
1 parent 095f934 commit 523f5a0

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

src/React.js

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ exports.pureComponentImpl = createClass(React.PureComponent);
5555

5656
exports.statelessComponent = function(x) { return x; };
5757

58+
exports.fragment = React.Fragment;
59+
5860
function getProps(this_) {
5961
return function(){
6062
return this_.props;

src/React.purs

+48-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
-- | This module defines foreign types and functions which wrap React's functionality.
22

33
module React
4-
( class ReactRender
5-
, ReactElement
4+
( ReactElement
65
, ReactComponent
76
, ReactThis
87
, TagName
@@ -66,6 +65,9 @@ module React
6665
, childrenCount
6766

6867
, class ReactPropFields
68+
, class IsReactElement
69+
, toElement
70+
, fragmentWithKey
6971
) where
7072

7173
import Prelude
@@ -74,6 +76,7 @@ import Control.Monad.Eff (kind Effect, Eff)
7476
import Control.Monad.Eff.Exception (Error)
7577
import Control.Monad.Eff.Uncurried (EffFn2, runEffFn2)
7678
import Data.Nullable (Nullable)
79+
import Unsafe.Coerce (unsafeCoerce)
7780

7881
-- | Name of a tag.
7982
type TagName = String
@@ -145,25 +148,13 @@ type EventHandlerContext eff props state result =
145148
| eff
146149
) result
147150

148-
class ReactRender a
149-
150-
instance arrayReactRender :: ReactRender (Array ReactElement)
151-
152-
instance reactElementReactRender :: ReactRender ReactElement
153-
154-
instance stringReactRender :: ReactRender String
155-
156-
instance intReactRender :: ReactRender Int
157-
158-
instance numberReactRender :: ReactRender Number
159-
160151
-- | A render function.
161-
type Render render eff =
152+
type Render eff =
162153
Eff
163154
( props :: ReactProps
164155
, state :: ReactState ReadOnly
165156
| eff
166-
) render
157+
) ReactElement
167158

168159
-- | A component will mount function.
169160
type ComponentWillMount eff =
@@ -238,9 +229,9 @@ type ComponentWillUnmount eff =
238229
) Unit
239230

240231
-- | Required fields for constructing a ReactClass.
241-
type ReactSpecRequired state render eff r =
232+
type ReactSpecRequired state eff r =
242233
( state :: state
243-
, render :: Render render eff
234+
, render :: Render eff
244235
| r
245236
)
246237

@@ -260,40 +251,38 @@ type ReactSpecShouldComponentUpdate props state eff =
260251
( shouldComponentUpdate :: ShouldComponentUpdate props state eff
261252
)
262253

263-
type ReactSpecAll props state render eff =
264-
ReactSpecRequired state render eff (ReactSpecOptional props state eff (ReactSpecShouldComponentUpdate props state eff))
254+
type ReactSpecAll props state eff =
255+
ReactSpecRequired state eff (ReactSpecOptional props state eff (ReactSpecShouldComponentUpdate props state eff))
265256

266-
type ReactSpecPure props state render eff =
267-
ReactSpecRequired state render eff (ReactSpecOptional props state eff ())
257+
type ReactSpecPure props state eff =
258+
ReactSpecRequired state eff (ReactSpecOptional props state eff ())
268259

269260
-- | The signature for a ReactClass constructor. A constructor takes the
270261
-- | `ReactThis` context and returns a record with appropriate lifecycle
271262
-- | methods.
272-
type ReactClassConstructor props state render eff r =
263+
type ReactClassConstructor props state eff r =
273264
ReactThis props state ->
274265
Eff
275266
( props :: ReactProps
276267
, state :: ReactState Disallowed
277268
| eff
278-
) (Record (ReactSpecRequired state render eff r))
269+
) (Record (ReactSpecRequired state eff r))
279270

280271
-- | Creates a `ReactClass`` inherited from `React.Component`.
281272
component
282-
:: forall props state render eff r x
283-
. Union (ReactSpecRequired (Record state) render eff r) x (ReactSpecAll (Record props) (Record state) render eff)
284-
=> ReactRender render
273+
:: forall props state eff r x
274+
. Union (ReactSpecRequired (Record state) eff r) x (ReactSpecAll (Record props) (Record state) eff)
285275
=> String
286-
-> ReactClassConstructor (Record props) (Record state) render eff r
276+
-> ReactClassConstructor (Record props) (Record state) eff r
287277
-> ReactClass (Record props)
288278
component = componentImpl
289279

290280
-- | Creates a `ReactClass`` inherited from `React.PureComponent`.
291281
pureComponent
292-
:: forall props state render eff r x
293-
. Union (ReactSpecRequired (Record state) render eff r) x (ReactSpecPure (Record props) (Record state) render eff)
294-
=> ReactRender render
282+
:: forall props state eff r x
283+
. Union (ReactSpecRequired (Record state) eff r) x (ReactSpecPure (Record props) (Record state) eff)
295284
=> String
296-
-> ReactClassConstructor (Record props) (Record state) render eff r
285+
-> ReactClassConstructor (Record props) (Record state) eff r
297286
-> ReactClass (Record props)
298287
pureComponent = pureComponentImpl
299288

@@ -314,6 +303,8 @@ foreign import statelessComponent :: forall props.
314303
-- | React class for components.
315304
foreign import data ReactClass :: Type -> Type
316305

306+
foreign import fragment :: ReactClass { children :: Children }
307+
317308
-- | Type for React refs. This type is opaque, but you can use `Data.Foreign`
318309
-- | and `DOM` to validate the underlying representation.
319310
foreign import data Ref :: Type
@@ -435,3 +426,28 @@ foreign import childrenCount :: Children -> Int
435426
foreign import preventDefault :: forall eff. Event -> Eff eff Unit
436427

437428
foreign import stopPropagation :: forall eff. Event -> Eff eff Unit
429+
430+
class IsReactElement a where
431+
toElement :: a -> ReactElement
432+
433+
instance isReactElementString :: IsReactElement String where
434+
toElement = unsafeCoerce
435+
436+
instance isReactElementNumber :: IsReactElement Number where
437+
toElement = unsafeCoerce
438+
439+
instance isReactElementInt :: IsReactElement Int where
440+
toElement = unsafeCoerce
441+
442+
instance isReactElementChildren :: IsReactElement Children where
443+
toElement = unsafeCoerce
444+
445+
instance isReactElementReactElement :: IsReactElement ReactElement where
446+
toElement = id
447+
448+
instance isReactElementArray :: IsReactElement (Array ReactElement) where
449+
toElement = createElement fragment {}
450+
451+
-- | Creates a keyed fragment.
452+
fragmentWithKey :: String -> Array ReactElement -> ReactElement
453+
fragmentWithKey = createElement fragment <<< { key: _ }

0 commit comments

Comments
 (0)