Skip to content

Commit

Permalink
Improve BS5 form errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sake92 committed Nov 22, 2023
1 parent ea1bd63 commit 47f5cae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions DEV.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

$VERSION="v0.15.0"
git tag -a $VERSION -m "Add bootstrap 5"
$VERSION="v0.16.0"
git tag -a $VERSION -m "Release $VERSION"
git push origin $VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ object BootstrapFormComponents {
}

object BootstrapValidationStateClasses extends FormComponents.ValidationStateClasses {
override def success: AttrPair = cls := "has-success"
override def warning: AttrPair = cls := "has-warning"
override def error: AttrPair = cls := "has-error"
override def success: AttrPair = cls := "is-valid"
override def warning: AttrPair = cls := "is-invalid"
override def error: AttrPair = cls := "is-invalid"
override def successFeedback: AttrPair = cls := "valid-feedback"
override def warningFeedback: AttrPair = cls := "invalid-feedback"
override def errorFeedback: AttrPair = cls := "invalid-feedback"
}
}

Expand All @@ -50,10 +53,9 @@ final case class BootstrapFormComponents(
inputTransform: Frag => Frag
) = {
val commonAttrs =
Seq(cls := "form-control", inputType, inputName) ++ inputId.map(id := _) ++ inputAttrs
val inputHelpFrag = inputHelp.map(h => span(cls := "help-block")(h))
val inputMsgsFrag = inputMessages.map(m => span(cls := "help-block")(m))
val inputValidationCls = inputValidationState.map(_.clazz)
Seq(cls := "form-control", inputType, inputName) ++ inputId.map(id := _) ++ inputValidationState.map(_.clazz) ++ inputAttrs
val inputHelpFrag = inputHelp.map(h => span(inputValidationState.map(_.feedbackClazz))(h))
val inputMsgsFrag = inputMessages.map(m => span(inputValidationState.map(_.feedbackClazz))(m))
val inputFrag =
if (inputType.v == "textarea") textarea(commonAttrs)("") // TODO
else input(commonAttrs)
Expand All @@ -62,7 +64,7 @@ final case class BootstrapFormComponents(
formType match {
case ft: Type.Horizontal =>
val (colLabel, colInput) = horizontalRatioClasses(ft, true)
formGroup(inputValidationCls.toSeq: _*)(
formGroup()(
label(cls := s"control-label $colLabel", inputId.map(`for` := _))(
inputLabel
),
Expand All @@ -73,7 +75,7 @@ final case class BootstrapFormComponents(
)
)
case _ =>
formGroup(inputValidationCls.toSeq: _*)(
formGroup()(
inputLabel.map(lbl => label(inputId.map(`for` := _))(lbl)),
inputFragTransformed,
inputMsgsFrag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ object FormComponents {
def success: AttrPair = cls := "success"
def warning: AttrPair = cls := "warning"
def error: AttrPair = cls := "error"
def successFeedback: AttrPair = cls := "success"
def warningFeedback: AttrPair = cls := "warning"
def errorFeedback: AttrPair = cls := "error"
}
}

Expand Down Expand Up @@ -703,20 +706,27 @@ trait FormComponents {
)
}

sealed trait ValidationState { def clazz: AttrPair }
sealed trait ValidationState {
def clazz: AttrPair
def feedbackClazz: AttrPair
}

object ValidationState {

case object Success extends ValidationState {
override def clazz = validationStateClasses.success
override def feedbackClazz = validationStateClasses.successFeedback
}

case object Warning extends ValidationState {
override def clazz = validationStateClasses.warning
override def feedbackClazz = validationStateClasses.warningFeedback
}

case object Error extends ValidationState {
override def clazz = validationStateClasses.error
override def feedbackClazz = validationStateClasses.errorFeedback
}

}
}

0 comments on commit 47f5cae

Please sign in to comment.