Created
June 6, 2018 09:45
-
-
Save jvliwanag/5992cd40320c1099a7023f82d84a6844 to your computer and use it in GitHub Desktop.
React createElement with optional fields
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
module Main where | |
import Prim.Row (class Union, class Nub) | |
-- Dummy | |
data ReactElement = ReactElement | |
-- Declaring a ReactClass now requires two rows - required and optional fields | |
data ReactClass (required :: # Type) (optional :: # Type) = ReactClass | |
class ReactPropFields (required :: # Type) (optional :: # Type) (given :: # Type) | |
instance reactPropFields :: | |
( Union required optional all | |
, Union given leftover all -- all given in either required or optional | |
, Union given required given' -- all required in given | |
, Nub given' given'' | |
, Union given'' () given | |
) => | |
ReactPropFields required optional given | |
createElement :: forall required optional given | |
. ReactPropFields required optional given | |
=> ReactClass required optional | |
-> {|given} -> ReactElement | |
createElement c p = ReactElement | |
-- sample | |
button :: ReactClass (label :: String, onPress :: Int) (border :: Int) | |
button = ReactClass | |
mkButtonElement1 :: ReactElement | |
mkButtonElement1 = | |
createElement button { label: "Hello" | |
, onPress: 5 | |
, border: 5 | |
} | |
mkButtonElement2 :: ReactElement | |
mkButtonElement2 = | |
createElement button { label: "Hello" | |
, onPress: 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment