Skip to content

Commit cc61adb

Browse files
committed
WIP: build.sbt,Form-Scala2.scala,FormConfig.scala
1 parent 9618f63 commit cc61adb

3 files changed

Lines changed: 42 additions & 8 deletions

File tree

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ inThisBuild(
2323

2424
val versions = new {
2525
val outwatch = "1.0.0-RC13"
26-
val colibri = "0.7.8"
26+
val colibri = "0.1.0-SNAPSHOT"
2727
val funPack = "0.2.0"
2828
val scalaTest = "3.2.12"
2929
}

formidable/src/main/scala-2/Form-Scala2.scala

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ trait FormDerivation {
1616
override def default: T = ctx.construct(param => param.default.getOrElse(param.typeclass.default))
1717

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

22-
subStates.sequence.map { subStates =>
23+
combinedFieldState.sequence.map { fieldStates =>
24+
println(
25+
s"product[${ctx.typeName.short}]: state changed: ${ctx.parameters
26+
.map(_.label)
27+
.zip(fieldStates.map(_.now()))
28+
.map { case (label, value) =>
29+
s"$label: $value"
30+
}
31+
.mkString(",")}",
32+
)
2333
config.labeledFormGroup(
2434
ctx.parameters
25-
.zip(subStates)
35+
.zip(fieldStates)
2636
.map { case (param, subState) =>
2737
val subForm = ((s: Var[param.PType], c) => param.typeclass.render(s, c))
2838
.asInstanceOf[(Var[Any], FormConfig) => VModifier]
@@ -39,6 +49,7 @@ trait FormDerivation {
3949
defaultSubtype.typeclass.default
4050
}
4151
override def render(selectedValue: Var[T], config: FormConfig): VModifier = Owned.function { implicit owner =>
52+
println(s"sum[${ctx.typeName.short}]: rendering")
4253

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

62+
val subFormBackup = mutable.HashMap.empty[Subtype[Form, T], (Var[T], VModifier)]
63+
5164
config.unionSubform(
5265
config.selectInput[Subtype[Form, T]](
5366
options = ctx.subtypes,
5467
selectedValue = selectedSubtype,
5568
show = subtype => subtype.typeName.short,
5669
),
57-
subForm = selectedValue.map { value =>
58-
ctx.split(value) { sub =>
59-
VModifier.when(value.isInstanceOf[T])(sub.typeclass.asInstanceOf[Form[T]].render(selectedValue, config))
70+
subForm = selectedValue.map { newValue =>
71+
println(s"sum[${ctx.typeName.short}]: state changed: $newValue")
72+
ctx.split(newValue) { subtype =>
73+
74+
val (formState, form) = subFormBackup.getOrElseUpdate(
75+
key = subtype,
76+
defaultValue = {
77+
val formInstance = subtype.typeclass.asInstanceOf[Form[T]]
78+
val state = Var(formInstance.default)
79+
val form = formInstance.render(state, config) // TODO: this is lazy and always re-rendered....
80+
(state, form)
81+
},
82+
)
83+
84+
println(subFormBackup.size)
85+
86+
formState.set(newValue)
87+
88+
VModifier(
89+
VModifier.managedEval(formState.observable.unsafeForeach(selectedValue.set)),
90+
form,
91+
)
6092
}
6193
},
6294
)

formidable/src/main/scala/FormConfig.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ trait FormConfig {
1515
div(display.flex, VModifier.style("gap") := "0.5rem", subForms)
1616
def labeledFormGroup(subForms: Seq[(String, VModifier)]): VModifier =
1717
table(
18-
subForms.map { case (label, subForm) => tr(td(b(label, ": "), verticalAlign := "top"), td(subForm)) },
18+
subForms.map { case (label, subForm) =>
19+
tr(td(b(label, ": "), verticalAlign := "top"), td(subForm))
20+
},
1921
)
2022

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

0 commit comments

Comments
 (0)