From 62533cd175d9c8573ba2b6099153a08e3fcfa9ed Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Wed, 18 Apr 2018 17:14:05 -0700 Subject: [PATCH] Fix clearing the value property on inputs --- Ooui/Input.cs | 12 ++++++++---- Ooui/TextArea.cs | 9 ++++++--- Tests/InputTests.cs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 Tests/InputTests.cs diff --git a/Ooui/Input.cs b/Ooui/Input.cs index c89a41c..590b610 100644 --- a/Ooui/Input.cs +++ b/Ooui/Input.cs @@ -12,9 +12,10 @@ namespace Ooui set => SetAttributeProperty ("type", value); } + string val = ""; public string Value { - get => GetStringAttribute ("value", ""); - set => SetAttributeProperty ("value", value ?? ""); + get => val; + set => SetProperty (ref val, value ?? "", "value"); } public double NumberValue { @@ -78,12 +79,15 @@ namespace Ooui protected override bool TriggerEventFromMessage (Message message) { if (message.TargetId == Id && message.MessageType == MessageType.Event && (message.Key == "change" || message.Key == "input" || message.Key == "keyup")) { - // Don't need to notify here because the base implementation will fire the event if (Type == InputType.Checkbox) { UpdateBooleanAttributeProperty ("checked", message.Value != null ? Convert.ToBoolean (message.Value) : false, "IsChecked"); } else { - UpdateAttributeProperty ("value", message.Value != null ? Convert.ToString (message.Value) : "", "Value"); + var v = message.Value != null ? Convert.ToString (message.Value) : ""; + if (val != v) { + val = v; + OnPropertyChanged ("Value"); + } } } return base.TriggerEventFromMessage (message); diff --git a/Ooui/TextArea.cs b/Ooui/TextArea.cs index 8f0a075..0ff17ca 100644 --- a/Ooui/TextArea.cs +++ b/Ooui/TextArea.cs @@ -47,9 +47,12 @@ namespace Ooui protected override bool TriggerEventFromMessage (Message message) { - if (message.TargetId == Id && message.MessageType == MessageType.Event && (message.Key == "change" || message.Key == "input")) { - // Don't need to notify here because the base implementation will fire the event - val = message.Value != null ? Convert.ToString (message.Value) : ""; + if (message.TargetId == Id && message.MessageType == MessageType.Event && (message.Key == "change" || message.Key == "input" || message.Key == "keyup")) { + var v = message.Value != null ? Convert.ToString (message.Value) : ""; + if (val != v) { + val = v; + OnPropertyChanged ("Value"); + } } return base.TriggerEventFromMessage (message); } diff --git a/Tests/InputTests.cs b/Tests/InputTests.cs new file mode 100644 index 0000000..6788a5e --- /dev/null +++ b/Tests/InputTests.cs @@ -0,0 +1,37 @@ +using System; + +#if NUNIT +using NUnit.Framework; +using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; +using TestMethodAttribute = NUnit.Framework.TestCaseAttribute; +#else +using Microsoft.VisualStudio.TestTools.UnitTesting; +#endif + +using Ooui; + +namespace Tests +{ + [TestClass] + public class InputTests + { + [TestMethod] + public void ValuePropertyChangedOnReceiver () + { + var e = new Ooui.Input (); + var count = 0; + e.PropertyChanged += (s, ev) => { + if (ev.PropertyName == "Value") + count++; + }; + e.Receive (new Message { + MessageType = MessageType.Event, + TargetId = e.Id, + Key = "change", + Value = "woo", + }); + Assert.AreEqual (1, count); + Assert.AreEqual (e.Value, "woo"); + } + } +}