Fix Select and Option to use textContent instead of label
This also fixes getting the initial value of the select. Fixes #103
This commit is contained in:
parent
d7d7290c87
commit
d08dd3a2bf
|
@ -1,12 +1,21 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Ooui
|
namespace Ooui
|
||||||
{
|
{
|
||||||
public class Select : FormControl
|
public class Select : FormControl
|
||||||
{
|
{
|
||||||
|
bool gotValue = false;
|
||||||
|
|
||||||
public string Value {
|
public string Value {
|
||||||
get => GetStringAttribute ("value", "");
|
get {
|
||||||
set => SetAttributeProperty ("value", value ?? "");
|
if (gotValue) return GetStringAttribute ("value", "");
|
||||||
|
return GetDefaultValue ();
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
gotValue = true;
|
||||||
|
SetAttributeProperty ("value", value ?? "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public event TargetEventHandler Change {
|
public event TargetEventHandler Change {
|
||||||
|
@ -23,28 +32,29 @@ namespace Ooui
|
||||||
: base ("select")
|
: base ("select")
|
||||||
{
|
{
|
||||||
// Subscribe to the change event so we always get up-to-date values
|
// Subscribe to the change event so we always get up-to-date values
|
||||||
Change += (s, e) => { };
|
Change += (s, e) => { gotValue = true; };
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddOption (string label, string value)
|
public void AddOption (string label, string value)
|
||||||
{
|
{
|
||||||
AppendChild (new Option { Label = label, Value = value });
|
AppendChild (new Option { Text = label, Value = value });
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnChildInsertedBefore (Node newChild, Node referenceChild)
|
string GetDefaultValue ()
|
||||||
{
|
{
|
||||||
base.OnChildInsertedBefore (newChild, referenceChild);
|
var options = Children.OfType<Option> ();
|
||||||
var val = Value;
|
var r = options.FirstOrDefault (x => x.DefaultSelected);
|
||||||
if (string.IsNullOrEmpty (val) && newChild is Option o && !string.IsNullOrEmpty (o.Value)) {
|
if (r != null)
|
||||||
val = o.Value;
|
return r.Value;
|
||||||
}
|
r = options.FirstOrDefault ();
|
||||||
|
return r?.Value ?? "";
|
||||||
}
|
}
|
||||||
|
|
||||||
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")) {
|
||||||
SetAttribute ("value", message.Value != null ? Convert.ToString (message.Value) : "");
|
gotValue = true;
|
||||||
OnPropertyChanged ("Value");
|
UpdateAttributeProperty ("value", message.Value != null ? Convert.ToString (message.Value) : "", "Value");
|
||||||
}
|
}
|
||||||
return base.TriggerEventFromMessage (message);
|
return base.TriggerEventFromMessage (message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace Samples
|
||||||
var heading = new Heading ("Draw");
|
var heading = new Heading ("Draw");
|
||||||
var subtitle = new Paragraph ("Click to draw a masterpiece");
|
var subtitle = new Paragraph ("Click to draw a masterpiece");
|
||||||
var toolSel = new Select ();
|
var toolSel = new Select ();
|
||||||
toolSel.AppendChild (new Option { Label = "Boxes", Value = "box" });
|
toolSel.AppendChild (new Option { Text = "Boxes", Value = "box" });
|
||||||
toolSel.AddOption ("Circles", "circle");
|
toolSel.AddOption ("Circles", "circle");
|
||||||
var canvas = new Canvas {
|
var canvas = new Canvas {
|
||||||
Width = 320,
|
Width = 320,
|
||||||
|
|
Loading…
Reference in New Issue