Add Option element and fix Select's Value

This commit is contained in:
Frank A. Krueger 2017-12-09 13:19:32 -08:00
parent 296a72ce48
commit cfdad03e1c
3 changed files with 68 additions and 0 deletions

View File

@ -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<Node> ();

30
Ooui/Option.cs Normal file
View File

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

View File

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