|
|
|
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 |
|
*/ |
|
|
|
|
|
|