Skip to content

Commit

Permalink
WIP: build.sbt,Form-Scala2.scala,FormConfig.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
fdietze committed Dec 15, 2022
1 parent 9618f63 commit cc61adb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ inThisBuild(

val versions = new {
val outwatch = "1.0.0-RC13"
val colibri = "0.7.8"
val colibri = "0.1.0-SNAPSHOT"
val funPack = "0.2.0"
val scalaTest = "3.2.12"
}
Expand Down
44 changes: 38 additions & 6 deletions formidable/src/main/scala-2/Form-Scala2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ trait FormDerivation {
override def default: T = ctx.construct(param => param.default.getOrElse(param.typeclass.default))

override def render(state: Var[T], config: FormConfig): VModifier = Owned.function { implicit owner =>
val subStates: Var[Seq[Any]] =
println(s"product[${ctx.typeName.short}]: rendering")
val combinedFieldState: Var[Seq[Any]] =
state.imap[Seq[Any]](seq => ctx.rawConstruct(seq))(_.asInstanceOf[Product].productIterator.toList)

subStates.sequence.map { subStates =>
combinedFieldState.sequence.map { fieldStates =>
println(
s"product[${ctx.typeName.short}]: state changed: ${ctx.parameters
.map(_.label)
.zip(fieldStates.map(_.now()))
.map { case (label, value) =>
s"$label: $value"
}
.mkString(",")}",
)
config.labeledFormGroup(
ctx.parameters
.zip(subStates)
.zip(fieldStates)
.map { case (param, subState) =>
val subForm = ((s: Var[param.PType], c) => param.typeclass.render(s, c))
.asInstanceOf[(Var[Any], FormConfig) => VModifier]
Expand All @@ -39,6 +49,7 @@ trait FormDerivation {
defaultSubtype.typeclass.default
}
override def render(selectedValue: Var[T], config: FormConfig): VModifier = Owned.function { implicit owner =>
println(s"sum[${ctx.typeName.short}]: rendering")

val valueBackup = mutable.HashMap.empty[Subtype[Form, T], T].withDefault(_.typeclass.default)
val selectedSubtype: Var[Subtype[Form, T]] =
Expand All @@ -48,15 +59,36 @@ trait FormDerivation {
subtype
}

val subFormBackup = mutable.HashMap.empty[Subtype[Form, T], (Var[T], VModifier)]

config.unionSubform(
config.selectInput[Subtype[Form, T]](
options = ctx.subtypes,
selectedValue = selectedSubtype,
show = subtype => subtype.typeName.short,
),
subForm = selectedValue.map { value =>
ctx.split(value) { sub =>
VModifier.when(value.isInstanceOf[T])(sub.typeclass.asInstanceOf[Form[T]].render(selectedValue, config))
subForm = selectedValue.map { newValue =>
println(s"sum[${ctx.typeName.short}]: state changed: $newValue")
ctx.split(newValue) { subtype =>

val (formState, form) = subFormBackup.getOrElseUpdate(
key = subtype,
defaultValue = {
val formInstance = subtype.typeclass.asInstanceOf[Form[T]]
val state = Var(formInstance.default)
val form = formInstance.render(state, config) // TODO: this is lazy and always re-rendered....
(state, form)
},
)

println(subFormBackup.size)

formState.set(newValue)

VModifier(
VModifier.managedEval(formState.observable.unsafeForeach(selectedValue.set)),
form,
)
}
},
)
Expand Down
4 changes: 3 additions & 1 deletion formidable/src/main/scala/FormConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ trait FormConfig {
div(display.flex, VModifier.style("gap") := "0.5rem", subForms)
def labeledFormGroup(subForms: Seq[(String, VModifier)]): VModifier =
table(
subForms.map { case (label, subForm) => tr(td(b(label, ": "), verticalAlign := "top"), td(subForm)) },
subForms.map { case (label, subForm) =>
tr(td(b(label, ": "), verticalAlign := "top"), td(subForm))
},
)

def formSequence(subForms: Seq[VModifier], addButton: VModifier): VModifier =
Expand Down

0 comments on commit cc61adb

Please sign in to comment.