From 1e38bae6dbf47dc4b4ac5352391aba15343f893d Mon Sep 17 00:00:00 2001 From: Joshua Marner Date: Mon, 22 Apr 2024 19:39:11 -0500 Subject: [PATCH] Add opt and vopt helper functions for OneWayT, TwoWayT and OneWayToSourceT --- src/Elmish.WPF/Binding.fs | 98 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/src/Elmish.WPF/Binding.fs b/src/Elmish.WPF/Binding.fs index d3c07694..e2a7bf54 100644 --- a/src/Elmish.WPF/Binding.fs +++ b/src/Elmish.WPF/Binding.fs @@ -106,6 +106,36 @@ module Binding = OneWay.id |> createBindingT + /// Creates a one-way binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let opt<'a, 'msg when 'a : null> : string -> Binding<'a option, 'msg, 'a> = + id<'a, 'msg> + >> mapModel Option.toObj + + /// Creates a one-way binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let vopt<'a, 'msg when 'a : null> : string -> Binding<'a voption, 'msg, 'a> = + id<'a, 'msg> + >> mapModel ValueOption.toObj + + /// Creates a one-way binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let opt2 x : Binding<'a option, 'msg, System.Nullable<'a>> = + x + |> id + |> mapModel Option.toNullable + + /// Creates a one-way binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let vopt2 x : Binding<'a voption, 'msg, System.Nullable<'a>> = + x + |> id + |> mapModel ValueOption.toNullable + /// /// Strongly-typed bindings that update the model from the view. /// @@ -116,6 +146,36 @@ module Binding = OneWayToSource.id |> createBindingT + /// Creates a one-way-to-source binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let opt<'a, 'model when 'a : null> : string -> Binding<'model, 'a option, 'a> = + id<'model, 'a> + >> mapMsg Option.ofObj + + /// Creates a one-way-to-source binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let vopt<'a, 'model when 'a : null> : string -> Binding<'model, 'a voption, 'a> = + id<'model, 'a> + >> mapMsg ValueOption.ofObj + + /// Creates a one-way-to-source binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let opt2 x : Binding<'model, 'a option, System.Nullable<'a>> = + x + |> id + |> mapMsg Option.ofNullable + + /// Creates a one-way-to-source binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let vopt2 x : Binding<'model, 'a voption, System.Nullable<'a>> = + x + |> id + |> mapMsg ValueOption.ofNullable + /// /// Strongly-typed bindings that update both ways /// @@ -126,6 +186,40 @@ module Binding = TwoWay.id |> createBindingT + /// Creates a two-way binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let opt<'a when 'a : null> : string -> Binding<'a option, 'a option, 'a> = + id<'a> + >> mapModel Option.toObj + >> mapMsg Option.ofObj + + /// Creates a two-way binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let vopt<'a when 'a : null> : string -> Binding<'a voption, 'a voption, 'a> = + id<'a> + >> mapMsg ValueOption.ofObj + >> mapModel ValueOption.toObj + + /// Creates a two-way binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let opt2 x : Binding<'a option, 'a option, System.Nullable<'a>> = + x + |> id + |> mapMsg Option.ofNullable + |> mapModel Option.toNullable + + /// Creates a two-way binding to an optional value. The binding + /// automatically converts between a missing value in the model and + /// a null value in the view. + let vopt2 x : Binding<'a voption, 'a voption, System.Nullable<'a>> = + x + |> id + |> mapMsg ValueOption.ofNullable + |> mapModel ValueOption.toNullable + /// /// Strongly-typed bindings that dispatch messages from the view. /// @@ -249,7 +343,7 @@ module Binding = TwoWay.id |> createBinding - /// Creates a one-way-to-source binding to an optional value. The binding + /// Creates a two-way binding to an optional value. The binding /// automatically converts between a missing value in the model and /// a null value in the view. let vopt<'a> : string -> Binding<'a voption, 'a voption> = @@ -257,7 +351,7 @@ module Binding = >> mapModel ValueOption.box >> mapMsg ValueOption.unbox - /// Creates a one-way-to-source binding to an optional value. The binding + /// Creates a two-way binding to an optional value. The binding /// automatically converts between a missing value in the model and /// a null value in the view. let opt<'a> : string -> Binding<'a option, 'a option> =