Skip to content

Commit

Permalink
Enhance Setting
Browse files Browse the repository at this point in the history
 - Seq[Setting] as a Setting
 - TagMod as a Setting
 - FacadeModule#Setting alias
  • Loading branch information
nafg committed Jul 3, 2020
1 parent d557ac3 commit 5d48b51
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ trait FacadeModule extends FacadeModuleBase {
type Props <: PropTypes
def mkProps: Props
def factory: Factory[Props] = facade.factory(mkProps)
type Setting = Factory.Setting[Props]
}

object FacadeModule {
trait Simple extends FacadeModule {
def apply(settings: Factory.Setting[Props]*): Factory[Props] = factory(settings: _*)
def apply(settings: Setting*): Factory[Props] = factory(settings: _*)
}

trait ChildrenOf[C] extends FacadeModule {
type Props <: PropTypes.WithChildren[C]

class ApplyChildren(settings: Factory.Setting[Props]*) {
class ApplyChildren(settings: Setting*) {
def apply(children: C): Factory[Props] = factory(settings: _*)(_.children := children)
}
}
object ChildrenOf {
trait Simple[C] extends ChildrenOf[C] {
def apply(settings: Factory.Setting[Props]*): ApplyChildren = new ApplyChildren(settings: _*)
def apply(settings: Setting*): ApplyChildren = new ApplyChildren(settings: _*)
}
}

Expand All @@ -46,7 +47,7 @@ object FacadeModule {
case many => React.Fragment(many: _*)
}

class ApplyChildren(settings: Factory.Setting[Props]*) {
class ApplyChildren(settings: Setting*) {
def apply(children: VdomNode*): Factory[Props] = factory(settings: _*)(_.children := childrenToNode(children))
}
object ApplyChildren {
Expand All @@ -55,7 +56,7 @@ object FacadeModule {
}
object NodeChildren {
trait Simple extends NodeChildren {
def apply(settings: Factory.Setting[Props]*): ApplyChildren = new ApplyChildren(settings: _*)
def apply(settings: Setting*): ApplyChildren = new ApplyChildren(settings: _*)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Factory {
case class Factory[A](propTypes: A, component: Facade.JsComponentType, values: Seq[PropTypes.Setting] = Vector.empty) {
def apply(pairs: Factory.Setting[A]*): Factory[A] = copy(values = values ++ pairs.map(_.apply(propTypes)))

private def toObject: js.Object = js.Dictionary(values.map(v => (v.key, v.value)): _*).asInstanceOf[js.Object]
private def toObject: js.Object = js.Dictionary(values.flatMap(_.toMap): _*).asInstanceOf[js.Object]

def render: Js.UnmountedWithRawType[js.Object, Null, Js.RawMounted[js.Object, Null]] =
component.apply(toObject)()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
package io.github.nafg.simplefacade

import scala.language.dynamics
import scala.language.{dynamics, implicitConversions}
import scala.scalajs.js

import japgolly.scalajs.react.Key
import japgolly.scalajs.react.vdom.TagMod

import slinky.readwrite.Writer


object PropTypes {
class Setting(val key: String, val value: js.Any)
sealed trait Setting {
def toMap: Map[String, js.Any]
}
object Setting {
implicit class fromBooleanProp(prop: Prop[Boolean]) extends Setting(prop.name, true)
class Single(val key: String, val value: js.Any) extends Setting {
override def toMap = Map(key -> value)
override def toString = s"""$key: $value"""
}
implicit class FromBooleanProp(prop: Prop[Boolean]) extends Single(prop.name, true)
implicit class Multiple(val settings: Seq[Setting]) extends Setting {
override def toMap = settings.flatMap(_.toMap).toMap
}
implicit def fromTagMod[A](tagMod: TagMod): A => Setting = { _ =>
val raw = tagMod.toJs
raw.addKeyToProps()
raw.addStyleToProps()
raw.addClassNameToProps()
new Multiple(
raw.nonEmptyChildren.toList.map(new Single("children", _)) ++
raw.props.asInstanceOf[js.Dictionary[js.Any]].toSeq
.map { case (k, v) => new Single(k, v) }
)
}
}

class Prop[A](val name: String)(implicit writer: Writer[A]) {
def :=(value: A): Setting = new Setting(name, writer.write(value))
def :=?(value: Option[A]): Setting = new Setting(name, value.map(writer.write).getOrElse(js.undefined))
def :=(value: A): Setting = new Setting.Single(name, writer.write(value))
def :=?(value: Option[A]): Setting = new Setting.Single(name, value.map(writer.write).getOrElse(js.undefined))
}

trait WithChildren[C] extends PropTypes {
Expand All @@ -26,7 +46,7 @@ object PropTypes {

trait PropTypes extends Dynamic {
def applyDynamic[A](name: String)(value: A)(implicit writer: Writer[A]): PropTypes.Setting =
new PropTypes.Setting(name, writer.write(value))
new PropTypes.Setting.Single(name, writer.write(value))

def of[A: Writer](implicit name: sourcecode.Name) = new PropTypes.Prop[A](name.value)

Expand Down

0 comments on commit 5d48b51

Please sign in to comment.