Ooui-tws-port/Ooui/Select.cs

53 lines
1.7 KiB
C#
Raw Normal View History

2017-06-16 06:41:38 +00:00
using System;
namespace Ooui
{
public class Select : FormControl
{
public string Value {
2018-02-02 02:43:23 +00:00
get => GetStringAttribute ("value", "");
set => SetAttributeProperty ("value", value ?? "");
2017-06-16 06:41:38 +00:00
}
public event TargetEventHandler Change {
2017-06-16 06:41:38 +00:00
add => AddEventListener ("change", value);
remove => RemoveEventListener ("change", value);
}
public event TargetEventHandler Input {
add => AddEventListener ("input", value);
remove => RemoveEventListener ("input", value);
}
2017-06-16 06:41:38 +00:00
public Select ()
: base ("select")
{
// Subscribe to the change event so we always get up-to-date values
Change += (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);
2018-02-02 02:43:23 +00:00
var val = Value;
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")) {
2018-02-02 02:43:23 +00:00
SetAttribute ("value", message.Value != null ? Convert.ToString (message.Value) : "");
OnPropertyChanged ("Value");
}
return base.TriggerEventFromMessage (message);
2017-06-16 06:41:38 +00:00
}
}
}