Skip to content

Commit

Permalink
feat: Update rendering of form fields in UI5WidgetFactory
Browse files Browse the repository at this point in the history
This commit updates the `UI5WidgetFactory` in the `modules/ui5` directory to improve the rendering of form fields. The `renderVar` method now uses the `asForm` extension method from the `Form` object to render the form fields. This change ensures consistency in rendering across different types of form fields in UI5 widgets.

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
cheleb and renovate[bot] committed Aug 23, 2024
1 parent e45c4ec commit 2737f8a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/EitherSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ val either = Sample(
s"$item"
)
},
Form.renderVar(eitherVar)
eitherVar.asForm
)
}
)
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/EnumSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ val enums = Sample(
s"$item"
)
},
Form.renderVar(eitherVar)
eitherVar.asForm
)
}
)
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/ListElement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ val list = Sample(
s"$item"
)
},
Form.renderVar(listPersonVar),
listPersonVar.asForm,
child <-- listIntVar.signal.map { item =>
div(
s"$item"
Expand Down
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/Persons.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ val person = Sample(
s"$item"
)
},
Form.renderVar(personVar)
personVar.asForm
)
)
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/SimpleSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ val simple = Sample(
s"$item"
)
},
Form.renderVar(eitherVar)
eitherVar.asForm
)
}
)
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ val tree = Sample(
child <-- treeVar2.signal
.distinctByFn(Tree.isSameStructure)
.map { item =>
Form.renderVar(treeVar2)
treeVar2.asForm
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/client/src/main/scala/samples/Validation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ val validation = Sample(
s"$item"
)
},
Form.renderVar(ironSampleVar)
ironSampleVar.asForm
)
)
49 changes: 47 additions & 2 deletions modules/core/src/main/scala/dev/cheleb/scalamigen/Form.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import magnolia1.*
import scala.util.Try
import com.raquo.airstream.state.Var
import org.scalajs.dom.HTMLDivElement
import org.scalajs.dom.HTMLElement
import com.raquo.laminar.nodes.ReactiveHtmlElement

extension [A](v: Var[A])
def asForm(using WidgetFactory, Form[A]) = Form.renderVar(v)

trait Form[A] { self =>

Expand Down Expand Up @@ -62,9 +67,9 @@ object Form extends AutoDerivation[Form] {
WidgetFactory
)(using
fa: Form[A]
) = {
): ReactiveHtmlElement[HTMLElement] =
fa.render(v, syncParent)
}

def join[A](
caseClass: CaseClass[Typeclass, A]
): Form[A] = new Form[A] {
Expand Down Expand Up @@ -150,3 +155,43 @@ object Form extends AutoDerivation[Form] {
}

}

/** Use this form to render a string that can be converted to A, can be used for
* Opaque types.
*/
def stringForm[A](to: String => A) = new Form[A]:
override def render(
variable: Var[A],
syncParent: () => Unit,
values: List[A] = List.empty
)(using factory: WidgetFactory): HtmlElement =
factory.renderText.amend(
value <-- variable.signal.map(_.toString),
onInput.mapToValue.map(to) --> { v =>
variable.set(v)
syncParent()
}
)

/** Form for a numeric type.
*/
def numericForm[A](f: String => Option[A], zero: A): Form[A] = new Form[A] {
self =>
override def fromString(s: String): Option[A] =
f(s).orElse(Some(zero))
override def render(
variable: Var[A],
syncParent: () => Unit,
values: List[A] = List.empty
)(using factory: WidgetFactory): HtmlElement =
factory.renderNumeric
.amend(
value <-- variable.signal.map { str =>
str.toString()
},
onInput.mapToValue --> { v =>
fromString(v).foreach(variable.set)
syncParent()
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,46 +98,6 @@ given Form[String] with
}
)

/** Use this form to render a string that can be converted to A, can be used for
* Opaque types.
*/
def stringForm[A](to: String => A) = new Form[A]:
override def render(
variable: Var[A],
syncParent: () => Unit,
values: List[A] = List.empty
)(using factory: WidgetFactory): HtmlElement =
factory.renderText.amend(
value <-- variable.signal.map(_.toString),
onInput.mapToValue.map(to) --> { v =>
variable.set(v)
syncParent()
}
)

/** Form for a numeric type.
*/
def numericForm[A](f: String => Option[A], zero: A): Form[A] = new Form[A] {
self =>
override def fromString(s: String): Option[A] =
f(s).orElse(Some(zero))
override def render(
variable: Var[A],
syncParent: () => Unit,
values: List[A] = List.empty
)(using factory: WidgetFactory): HtmlElement =
factory.renderNumeric
.amend(
value <-- variable.signal.map { str =>
str.toString()
},
onInput.mapToValue --> { v =>
fromString(v).foreach(variable.set)
syncParent()
}
)
}

given Form[Nothing] = new Form[Nothing] {
override def render(
variable: Var[Nothing],
Expand Down

0 comments on commit 2737f8a

Please sign in to comment.