Skip to content

Commit

Permalink
Merge pull request #616 from marner2/feature/notify_when_HasErrors_ch…
Browse files Browse the repository at this point in the history
…anges

Notify OnPropertyChanged when HasErrors changes
  • Loading branch information
marner2 authored Oct 1, 2024
2 parents f9f2879 + bf0c2ba commit 68b2129
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### 4.0.0-beta-56
* Add NotifyPropertyChanged for the HasErrors property.

#### 4.0.0-beta-55
* Add TwoWayT bindings for workflows that reuse bindings.
* Add OneWayToSeqT bindings.
Expand Down
20 changes: 20 additions & 0 deletions src/Elmish.WPF.Tests/DynamicViewModelTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,26 @@ module TwoWayValidate =
}


[<Fact>]
let ``when model is updated, should trigger NotifyPropertyChanged on HasErrors iff the value returned by validate changes`` () =
Property.check <| property {
let! name = GenX.auto<string>
let! m1 = GenX.auto<int>
let! m2 = GenX.auto<int>

let get _ = ()
let set _ _ = ()
let validate m = if m < 0 then ValueSome (string m) else ValueNone

let binding = twoWayValidate name get set validate
let vm = TestVm(m1, binding)

vm.UpdateModel m2

test <@ vm.NumPcTriggersFor "HasErrors" = if (validate m1 |> ValueOption.isNone) = (validate m2 |> ValueOption.isNone) then 0 else 1 @>
}


[<Fact>]
let ``when validate returns ValueNone, HasErrors should return false and GetErrors should return an empty collection`` () =
Property.check <| property {
Expand Down
2 changes: 1 addition & 1 deletion src/Elmish.WPF/Elmish.WPF.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageProjectUrl>https://github.com/elmish/Elmish.WPF</PackageProjectUrl>
<PackageTags>WPF F# fsharp Elmish Elm</PackageTags>
<PackageIcon>elmish-wpf-logo-128x128.png</PackageIcon>
<Version>4.0.0-beta-55</Version>
<Version>4.0.0-beta-56</Version>
<PackageReleaseNotes>https://github.com/elmish/Elmish.WPF/blob/master/RELEASE_NOTES.md</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<!--Turn on warnings for unused values (arguments and let bindings) -->
Expand Down
2 changes: 2 additions & 0 deletions src/Elmish.WPF/ViewModels.fs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ type [<AllowNullLiteral>] internal DynamicViewModel<'model, 'msg>
member _.CurrentModel : 'model = helper.Model

member _.UpdateModel (newModel: 'model) : unit =
let prevHasErrors = (helper :> INotifyDataErrorInfo).HasErrors
let eventsToRaise = ViewModelHelper.getEventsToRaise newModel helper
helper <- { helper with Model = newModel }
let eventsToRaise = if prevHasErrors = (helper :> INotifyDataErrorInfo).HasErrors then eventsToRaise else (PropertyChanged "HasErrors") :: eventsToRaise
ViewModelHelper.raiseEvents eventsToRaise helper

override _.TryGetMember (binder, result) =
Expand Down

0 comments on commit 68b2129

Please sign in to comment.