From cfdad03e1cfee59d7b8bf087e8b6945378d10277 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sat, 9 Dec 2017 13:19:32 -0800 Subject: [PATCH] Add Option element and fix Select's Value --- Ooui/Node.cs | 10 ++++++++++ Ooui/Option.cs | 30 ++++++++++++++++++++++++++++++ Ooui/Select.cs | 28 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 Ooui/Option.cs diff --git a/Ooui/Node.cs b/Ooui/Node.cs index 5a80ca1..2443a2b 100644 --- a/Ooui/Node.cs +++ b/Ooui/Node.cs @@ -74,6 +74,7 @@ namespace Ooui } newChild.MessageSent += HandleChildMessageSent; SendCall ("insertBefore", newChild, referenceChild); + OnChildInsertedBefore (newChild, referenceChild); return newChild; } @@ -88,9 +89,18 @@ namespace Ooui } child.MessageSent -= HandleChildMessageSent; SendCall ("removeChild", child); + OnChildRemoved (child); return child; } + protected virtual void OnChildInsertedBefore (Node newChild, Node referenceChild) + { + } + + protected virtual void OnChildRemoved (Node child) + { + } + protected void ReplaceAll (Node newNode) { var toRemove = new List (); diff --git a/Ooui/Option.cs b/Ooui/Option.cs new file mode 100644 index 0000000..86d918e --- /dev/null +++ b/Ooui/Option.cs @@ -0,0 +1,30 @@ +using System; + +namespace Ooui +{ + public class Option : Element + { + string val = ""; + public string Value { + get => val; + set => SetProperty (ref val, value ?? "", "value"); + } + + string label = ""; + public string Label { + get => label; + set => SetProperty (ref label, value ?? "", "label"); + } + + bool defaultSelected = false; + public bool DefaultSelected { + get => defaultSelected; + set => SetProperty (ref defaultSelected, value, "defaultSelected"); + } + + public Option () + : base ("option") + { + } + } +} diff --git a/Ooui/Select.cs b/Ooui/Select.cs index 7f53175..30a7b64 100644 --- a/Ooui/Select.cs +++ b/Ooui/Select.cs @@ -15,9 +15,37 @@ namespace Ooui remove => RemoveEventListener ("change", value); } + public event TargetEventHandler Inputted { + add => AddEventListener ("input", value); + remove => RemoveEventListener ("input", value); + } + public Select () : base ("select") { + // Subscribe to the change event so we always get up-to-date values + Changed += (s, e) => { }; + } + + public void AddOption (string label, string value) + { + AppendChild (new Option { Label = label, Value = value }); + } + + protected override void OnChildInsertedBefore (Node newChild, Node referenceChild) + { + base.OnChildInsertedBefore (newChild, referenceChild); + if (string.IsNullOrEmpty (val) && newChild is Option o && !string.IsNullOrEmpty (o.Value)) { + val = o.Value; + } + } + + protected override bool TriggerEventFromMessage (Message message) + { + if (message.TargetId == Id && message.MessageType == MessageType.Event && (message.Key == "change" || message.Key == "input")) { + val = message.Value != null ? Convert.ToString (message.Value) : ""; + } + return base.TriggerEventFromMessage (message); } } }