-
Notifications
You must be signed in to change notification settings - Fork 2
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
Make ReactElement a proper Monoid and other ergonomic tweaks #84
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
module Elmish.React | ||
( ReactElement | ||
, ReactComponent | ||
, ReactComponentInstance | ||
, class ValidReactProps | ||
, class ReactChildren, asReactChildren | ||
, assignState | ||
, createElement | ||
, createElement' | ||
, getField | ||
, getState | ||
, hydrate | ||
, setField | ||
, setState | ||
, render | ||
, renderToString | ||
, unmount | ||
, module Ref | ||
) where | ||
( ReactElement | ||
, ReactComponent | ||
, ReactComponentInstance | ||
, class ValidReactProps | ||
, class ReactChildren, asReactChildren | ||
, assignState | ||
, createElement | ||
, createElement' | ||
, empty | ||
, fragment | ||
, getState | ||
, hydrate | ||
, setState | ||
, render | ||
, renderToString | ||
, text | ||
, unmount | ||
, module Ref | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Function.Uncurried (Fn3, runFn3) | ||
import Data.Maybe (Maybe) | ||
import Data.Nullable (Nullable) | ||
import Effect (Effect) | ||
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, runEffectFn1, runEffectFn2, runEffectFn3) | ||
import Elmish.Foreign (class CanPassToJavaScript, class CanReceiveFromJavaScript, Foreign, readForeign) | ||
import Elmish.Foreign (class CanPassToJavaScript) | ||
import Elmish.React.Ref (Ref, callbackRef) as Ref | ||
import Prim.TypeError (Text, class Fail) | ||
import Unsafe.Coerce (unsafeCoerce) | ||
|
@@ -34,6 +34,9 @@ import Web.DOM as HTML | |
-- | Instantiated subtree of React DOM. JSX syntax produces values of this type. | ||
foreign import data ReactElement :: Type | ||
|
||
instance Semigroup ReactElement where append = appendElement_ | ||
instance Monoid ReactElement where mempty = empty | ||
|
||
-- | This type represents constructor of a React component with a particular | ||
-- | behavior. The type prameter is the record of props (in React lingo) that | ||
-- | this component expects. Such constructors can be "rendered" into | ||
|
@@ -82,6 +85,20 @@ createElement' :: ∀ props | |
-> ReactElement | ||
createElement' component props = createElement component props ([] :: Array ReactElement) | ||
|
||
-- | Empty React element. | ||
empty :: ReactElement | ||
empty = unsafeCoerce false | ||
|
||
-- | Render a plain string as a React element. | ||
text :: String -> ReactElement | ||
text = unsafeCoerce | ||
|
||
-- | Wraps multiple React elements as a single one (import of React.Fragment) | ||
fragment :: Array ReactElement -> ReactElement | ||
fragment = createElement fragment_ {} | ||
|
||
foreign import fragment_ :: ReactComponent {} | ||
foreign import appendElement_ :: ReactElement -> ReactElement -> ReactElement | ||
|
||
-- | Asserts that the given type is a valid React props structure. Currently | ||
-- | there are three rules for what is considered "valid": | ||
|
@@ -112,14 +129,6 @@ setState :: ∀ state. ReactComponentInstance -> state -> (Effect Unit) -> Effec | |
setState = runEffectFn3 setState_ | ||
foreign import setState_ :: ∀ state. EffectFn3 ReactComponentInstance state (Effect Unit) Unit | ||
|
||
getField :: ∀ @a. CanReceiveFromJavaScript a => String -> ReactComponentInstance -> Effect (Maybe a) | ||
getField field object = runEffectFn2 getField_ field object <#> readForeign @a | ||
foreign import getField_ :: EffectFn2 String ReactComponentInstance Foreign | ||
|
||
setField :: ∀ @a. CanPassToJavaScript a => String -> a -> ReactComponentInstance -> Effect Unit | ||
setField = runEffectFn3 setField_ | ||
foreign import setField_ :: ∀ a. EffectFn3 String a ReactComponentInstance Unit | ||
Comment on lines
-115
to
-121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This moved to a separate module |
||
|
||
-- | The equivalent of `this.state = x`, as opposed to `setState`, which is the | ||
-- | equivalent of `this.setState(x)`. This function is used in a component's | ||
-- | constructor to set the initial state. | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const getField_ = (field, obj) => obj[field] | ||
export const setField_ = (field, value, obj) => obj[field] = value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Elmish.React.Internal | ||
( Field(..) | ||
, getField | ||
, setField | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Maybe (Maybe) | ||
import Data.Symbol (class IsSymbol, reflectSymbol) | ||
import Effect (Effect) | ||
import Effect.Uncurried (EffectFn2, EffectFn3, runEffectFn2, runEffectFn3) | ||
import Elmish.Foreign (class CanPassToJavaScript, class CanReceiveFromJavaScript, Foreign, readForeign) | ||
import Elmish.React (ReactComponentInstance) | ||
import Type.Proxy (Proxy(..)) | ||
|
||
data Field (f :: Symbol) (a :: Type) = Field | ||
|
||
getField :: ∀ f a. CanReceiveFromJavaScript a => IsSymbol f => Field f a -> ReactComponentInstance -> Effect (Maybe a) | ||
getField _ object = runEffectFn2 getField_ (reflectSymbol $ Proxy @f) object <#> readForeign @a | ||
foreign import getField_ :: EffectFn2 String ReactComponentInstance Foreign | ||
|
||
setField :: ∀ f a. CanPassToJavaScript a => IsSymbol f => Field f a -> a -> ReactComponentInstance -> Effect Unit | ||
setField _ = runEffectFn3 setField_ $ reflectSymbol $ Proxy @f | ||
foreign import setField_ :: ∀ a. EffectFn3 String a ReactComponentInstance Unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Field
value binds together field name and type, making mistakes less likely.