@@ -7,57 +7,52 @@ package object formidable {
77
88 implicit val stringForm : Form [String ] = new Form [String ] {
99 def default = " "
10- def render (state : Var [String ], config : FormConfig ) = Owned {
10+ def render (state : Var [String ], config : FormConfig ) =
1111 config.textInput(state)
12- }
1312 }
1413
1514 implicit val intForm : Form [Int ] = new Form [Int ] {
1615 def default = 0
17- def render (state : Var [Int ], config : FormConfig ) = Owned {
16+ def render (state : Var [Int ], config : FormConfig ) =
1817 encodedTextInput[Int ](
1918 state,
2019 encode = _.toString,
2120 decode = str => str.toIntOption.toRight(s " ' $str' could not be parsed as Int " ),
2221 config,
2322 )
24- }
2523 }
2624
2725 implicit val longForm : Form [Long ] = new Form [Long ] {
2826 def default = 0
29- def render (state : Var [Long ], config : FormConfig ) = Owned {
27+ def render (state : Var [Long ], config : FormConfig ) =
3028 encodedTextInput[Long ](
3129 state,
3230 encode = _.toString,
3331 decode = str => str.toLongOption.toRight(s " ' $str' could not be parsed as Long " ),
3432 config,
3533 )
36- }
3734 }
3835
3936 implicit val doubleForm : Form [Double ] = new Form [Double ] {
4037 def default = 0.0
41- def render (state : Var [Double ], config : FormConfig ) = Owned {
38+ def render (state : Var [Double ], config : FormConfig ) =
4239 encodedTextInput[Double ](
4340 state,
4441 encode = _.toString,
4542 decode = str => str.toDoubleOption.toRight(s " ' $str' could not be parsed as Double " ),
4643 config,
4744 )
48- }
4945 }
5046
5147 implicit val booleanForm : Form [Boolean ] = new Form [Boolean ] {
5248 def default = false
53- def render (state : Var [Boolean ], config : FormConfig ) = Owned {
49+ def render (state : Var [Boolean ], config : FormConfig ) =
5450 config.checkbox(state)
55- }
5651 }
5752
5853 implicit def optionForm [T : Form ]: Form [Option [T ]] = new Form [Option [T ]] {
5954 def default = None
60- def render (state : Var [Option [T ]], config : FormConfig ) = Owned {
55+ def render (state : Var [Option [T ]], config : FormConfig ) = {
6156 val checkboxState = state.transformVar[Boolean ](_.contramap {
6257 case true => Some (Form [T ].default)
6358 case false => None
@@ -76,7 +71,7 @@ package object formidable {
7671
7772 implicit def seqForm [T : Form ]: Form [Seq [T ]] = new Form [Seq [T ]] {
7873 def default = Seq .empty
79- def render (state : Var [Seq [T ]], config : FormConfig ) = Owned {
74+ def render (state : Var [Seq [T ]], config : FormConfig ) =
8075 state.sequence.map(seq =>
8176 config.formSequence(
8277 seq.zipWithIndex.map { case (innerState, i) =>
@@ -87,8 +82,7 @@ package object formidable {
8782 },
8883 addButton = config.addButton(() => state.update(_ :+ Form [T ].default)),
8984 )
90- ): VMod
91- }
85+ )
9286 }
9387
9488 implicit def vectorForm [T ](implicit seqForm : Form [Seq [T ]]): Form [Vector [T ]] = seqForm.imap(_.toVector)(_.toSeq)
@@ -99,18 +93,14 @@ package object formidable {
9993 encode : T => String ,
10094 decode : String => Either [String , T ],
10195 config : FormConfig ,
102- )(implicit owner : Owner ): VMod = {
103- val fieldState = Var (encode(state.now()))
104- val validationMessage : Var [Option [String ]] = Var (None )
96+ ): VMod = {
97+ val validatedFieldState : Var [(String , Either [String , T ])] = Var .createStateful(
98+ state.contramapIterable { case (_, decoded) => decoded.toOption },
99+ state.map(t => (encode(t), Right (t))),
100+ )
105101
106- state.foreach { value => fieldState.set(encode(value)) }
107- fieldState.map(decode).foreach {
108- case Left (msg) =>
109- validationMessage.set(Some (msg))
110- case Right (value) =>
111- validationMessage.set(None )
112- state.set(value)
113- }
102+ val fieldState = validatedFieldState.imap[String ](str => (str, decode(str)))(_._1)
103+ val validationMessage = validatedFieldState.map(_._2.left.toOption)
114104
115105 config.textInput(fieldState, validationMessage = validationMessage)
116106 }
0 commit comments