11package formidable
22
33import outwatch ._
4- import outwatch .dsl ._
54import colibri .reactive ._
65
76import magnolia1 ._
@@ -13,7 +12,7 @@ trait FormDerivation extends AutoDerivation[Form] {
1312 override def join [T ](ctx : CaseClass [Typeclass , T ]): Form [T ] = new Form [T ] {
1413 override def default : T = ctx.construct(param => param.default.getOrElse(param.typeclass.default))
1514
16- override def render (state : Var [T ], config : FormConfig ): VModifier = Owned {
15+ override def render (state : Var [T ], config : FormConfig ): VModifier = Owned .function { implicit owner =>
1716 val subStates : Var [Seq [Any ]] =
1817 state.imap[Seq [Any ]](seq => ctx.rawConstruct(seq))(_.asInstanceOf [Product ].productIterator.toList)
1918
@@ -35,26 +34,25 @@ trait FormDerivation extends AutoDerivation[Form] {
3534 val defaultSubtype = ctx.subtypes.find(_.annotations.exists(_.isInstanceOf [Default ])).getOrElse(ctx.subtypes.head)
3635 defaultSubtype.typeclass.default
3736 }
38- override def render (state : Var [T ], config : FormConfig ): VModifier = Owned {
39- val labelToSubtype =
40- ctx.subtypes.view.map { sub => sub.typeInfo.short -> sub }.toMap
41-
42- div(
43- select(
44- ctx.subtypes.map { subtype =>
45- option(
46- subtype.typeInfo.short,
47- selected <-- state.map(value => ctx.choose(value)(_.subtype == subtype)),
48- )
49- }.toSeq,
50- onChange.value.map(label => labelToSubtype(label).typeclass.default) --> state,
37+ override def render (selectedValue : Var [T ], config : FormConfig ): VModifier = Owned .function { implicit owner =>
38+
39+ val selectedSubtype : Var [SealedTrait .Subtype [Form , T , _]] =
40+ selectedValue.imap[SealedTrait .Subtype [Form , T , _]](subType => subType.typeclass.default)(value =>
41+ ctx.choose(value)(_.subtype),
42+ )
43+
44+ config.unionSubform(
45+ config.selectInput[SealedTrait .Subtype [Form , T , _]](
46+ options = ctx.subtypes,
47+ selectedValue = selectedSubtype,
48+ show = subtype => subtype.typeInfo.short,
5149 ),
52- state .map { value =>
50+ selectedValue .map { value =>
5351 ctx.choose(value) { sub =>
54- VModifier .when(value.isInstanceOf [T ])(sub.typeclass.asInstanceOf [Form [T ]].render(state , config))
52+ VModifier .when(value.isInstanceOf [T ])(sub.typeclass.asInstanceOf [Form [T ]].render(selectedValue , config))
5553 }
5654 },
57- ): VModifier
55+ )
5856 }
5957
6058 }
0 commit comments