@@ -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 )
0 commit comments