Skip to content

Instantly share code, notes, and snippets.

@chandu0101
Last active April 26, 2016 01:19
Show Gist options
  • Save chandu0101/99db4f1d5cf655971b61adf867c1ff28 to your computer and use it in GitHub Desktop.
Save chandu0101/99db4f1d5cf655971b61adf867c1ff28 to your computer and use it in GitHub Desktop.
def toProps(in : Attr[_,_]*) = {
val p = js.Dictionary.empty[Any]
in.foreach(t => if(t != null) p.update(t.key, t.value))
p
}
def div(attrs: Attr[DivAttr,_]*)(children: ReactNode*) = React.createElement("div", toProps(attrs :_*), children : _*)
def input(attrs: Attr[InputAttr,_]*)(children: ReactNode*) = React.createElement("input", toProps(attrs :_*), children : _*)
sealed trait ElemAttr
final class DivAttr extends ElemAttr
final class InputAttr extends ElemAttr
final class FormAttr extends ElemAttr
class Attr[-AttrType <: ElemAttr,ValueType](val key: String, val value: ValueType)
class Key[ValueType, AttrType <: ElemAttr](name : String) {
def :=(value: ValueType): Attr[AttrType,ValueType] = new Attr[AttrType,ValueType](name,value)
}
val id: Key[String, ElemAttr] = new Key("id")
val className: Key[String, ElemAttr] = new Key("className")
val value: Key[String, InputAttr] = new Key("value")
// this can be used in input/form tag
val accept: Key[String, InputAttr with FormAttr] = new Key("value")
val d = div(id := "hello", className := "hello")()
val i = input(id := "hello", className := "hello", accept := "sdsad")()
/**
[error] found : sri.playground.web.Dom.Attr[sri.playground.web.Dom.InputAttr with sri.playground.web.Dom.FormAttr,String]
[error] required: sri.playground.web.Dom.Attr[sri.playground.web.Dom.InputAttr, _]
[error] val i = input(id := "hello", className := "hello", accept := "sdsad")()
[error] ^
[error] one error found
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment