Skip to content

Commit 505ea7a

Browse files
committed
* added Form.subscribeToUpdates(f: => Callback) to provide a hook to react on formUpdates
1 parent e41c47a commit 505ea7a

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ flexibility of the form design.
1616
Currently it is available as a sonatype snapshot, add this to your build.sbt:
1717

1818
```
19-
libraryDependencies += "com.github.torstenrudolf.scalajs-react-form-binder" %%% "core" % "0.0.15-SNAPSHOT"
19+
libraryDependencies += "com.github.torstenrudolf.scalajs-react-form-binder" %%% "core" % "0.0.16-SNAPSHOT"
2020
```
2121

2222
If you want to use the materialui form-field descriptors, you'll need this as well:
2323
```
24-
libraryDependencies += "com.github.torstenrudolf.scalajs-react-form-binder" %%% "extras" % "0.0.15-SNAPSHOT"
24+
libraryDependencies += "com.github.torstenrudolf.scalajs-react-form-binder" %%% "extras" % "0.0.16-SNAPSHOT"
2525
```
2626

2727

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def commonSettings: Project => Project =
33
_.enablePlugins(ScalaJSPlugin)
44
.settings(
55
organization := "com.github.torstenrudolf.scalajs-react-form-binder",
6-
version := "0.0.15-SNAPSHOT",
6+
version := "0.0.16-SNAPSHOT",
77
homepage := Some(url("https://github.com/torstenrudolf/scalajs-react-form-binder")),
88
licenses += ("MIT", url("https://opensource.org/licenses/MIT")),
99
scalaVersion := "2.11.8",

core/src/main/scala/torstenrudolf/scalajs/react/formbinder/Form.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ trait Form[DataModel] {
2929

3030
def resetAllFields: Callback
3131

32+
def subscribeToUpdates(cb: Callback): Unit
33+
3234
}
3335

3436

core/src/main/scala/torstenrudolf/scalajs/react/formbinder/Macros.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ trait FormAPI[T] extends Form[T] {
443443

444444
private var _validatedFormData: Option[T] = None
445445
private var _formGlobalValidationResult: Option[ValidationResult] = None
446+
private val _formOnChangeSubscribers: scala.collection.mutable.ListBuffer[Callback] =
447+
scala.collection.mutable.ListBuffer.empty[Callback]
446448

447449
private def formGlobalValidationResult: ValidationResult = _formGlobalValidationResult match {
448450
case Some(vr) => vr
@@ -491,7 +493,9 @@ trait FormAPI[T] extends Form[T] {
491493

492494
def onChangeCB: Callback = {
493495
validate(showUninitializedError = false)
496+
494497
formLayout.onChange(validatedData = _validatedFormData) >>
498+
_formOnChangeSubscribers.foldLeft(Callback.empty)(_ >> _) >>
495499
forceGlobalValidationMessageUpdate.getOrElse(Callback.empty)
496500
}
497501

@@ -551,6 +555,8 @@ trait FormAPI[T] extends Form[T] {
551555
case Failure(e) => throw FormUninitialized
552556
}
553557

558+
override def subscribeToUpdates(cb: Callback): Unit = _formOnChangeSubscribers.append(cb)
559+
554560
private var forceGlobalValidationMessageUpdate: Option[Callback] = None
555561

556562
override def globalValidationMessage: ReactNode = {

0 commit comments

Comments
 (0)