Fix clearing the value property on inputs

This commit is contained in:
Frank A. Krueger 2018-04-18 17:14:05 -07:00
parent 50a4e7f221
commit 62533cd175
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
3 changed files with 51 additions and 7 deletions

View File

@ -12,9 +12,10 @@ namespace Ooui
set => SetAttributeProperty ("type", value); set => SetAttributeProperty ("type", value);
} }
string val = "";
public string Value { public string Value {
get => GetStringAttribute ("value", ""); get => val;
set => SetAttributeProperty ("value", value ?? ""); set => SetProperty (ref val, value ?? "", "value");
} }
public double NumberValue { public double NumberValue {
@ -78,12 +79,15 @@ namespace Ooui
protected override bool TriggerEventFromMessage (Message message) protected override bool TriggerEventFromMessage (Message message)
{ {
if (message.TargetId == Id && message.MessageType == MessageType.Event && (message.Key == "change" || message.Key == "input" || message.Key == "keyup")) { 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) { if (Type == InputType.Checkbox) {
UpdateBooleanAttributeProperty ("checked", message.Value != null ? Convert.ToBoolean (message.Value) : false, "IsChecked"); UpdateBooleanAttributeProperty ("checked", message.Value != null ? Convert.ToBoolean (message.Value) : false, "IsChecked");
} }
else { 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); return base.TriggerEventFromMessage (message);

View File

@ -47,9 +47,12 @@ namespace Ooui
protected override bool TriggerEventFromMessage (Message message) protected override bool TriggerEventFromMessage (Message message)
{ {
if (message.TargetId == Id && message.MessageType == MessageType.Event && (message.Key == "change" || message.Key == "input")) { 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 var v = message.Value != null ? Convert.ToString (message.Value) : "";
val = message.Value != null ? Convert.ToString (message.Value) : ""; if (val != v) {
val = v;
OnPropertyChanged ("Value");
}
} }
return base.TriggerEventFromMessage (message); return base.TriggerEventFromMessage (message);
} }

37
Tests/InputTests.cs Normal file
View File

@ -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");
}
}
}