From 3c8893e8f980fdb550d5ff6fde411f744259fc0d Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Fri, 23 Mar 2018 13:02:51 -0700 Subject: [PATCH] Fetch value in response to keyup This key function now works like the input and change events. Fixes #111 --- Ooui/Client.js | 8 +++++++- Ooui/Input.cs | 2 +- Samples/TodoSample.cs | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Ooui/Client.js b/Ooui/Client.js index fac6e10..918a60e 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -31,6 +31,12 @@ const mouseEvents = { wheel: true, }; +const inputEvents = { + input: true, + change: true, + keyup: true, +}; + // Try to close the socket gracefully window.onbeforeunload = function() { if (socket != null) { @@ -252,7 +258,7 @@ function msgListen (m) { id: m.id, k: m.k, }; - if (m.k === "change" || m.k === "input") { + if (inputEvents[m.k]) { em.v = (node.tagName === "INPUT" && node.type === "checkbox") ? node.checked : node.value; diff --git a/Ooui/Input.cs b/Ooui/Input.cs index 966bf08..c89a41c 100644 --- a/Ooui/Input.cs +++ b/Ooui/Input.cs @@ -77,7 +77,7 @@ namespace Ooui 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 if (Type == InputType.Checkbox) { UpdateBooleanAttributeProperty ("checked", message.Value != null ? Convert.ToBoolean (message.Value) : false, "IsChecked"); diff --git a/Samples/TodoSample.cs b/Samples/TodoSample.cs index 6cf0e9c..f6a9a0b 100644 --- a/Samples/TodoSample.cs +++ b/Samples/TodoSample.cs @@ -46,6 +46,7 @@ namespace Samples var heading = new Heading ("Todo List"); var subtitle = new Paragraph ("This is the shared todo list of the world."); + var count = new Paragraph ("0 chars"); var inputForm = new Form { ClassName = "form-inline" }; @@ -61,6 +62,10 @@ namespace Samples Type = ButtonType.Submit, ClassName = "btn btn-danger", }; + void UpdateCount () + { + count.Text = $"{input.Value.Length} chars"; + } void AddItem () { if (string.IsNullOrWhiteSpace (input.Value)) @@ -71,6 +76,7 @@ namespace Samples }; items.InsertBefore (item, items.FirstChild); input.Value = ""; + UpdateCount (); } addbtn.Click += (s, e) => { AddItem (); @@ -78,6 +84,9 @@ namespace Samples inputForm.Submit += (s, e) => { AddItem (); }; + input.KeyUp += (s, e) => { + UpdateCount (); + }; clearbtn.Click += (s, e) => { var toremove = new List (); foreach (Item i in items.Children) { @@ -92,6 +101,7 @@ namespace Samples app.AppendChild (subtitle); inputForm.AppendChild (input); inputForm.AppendChild (addbtn); + inputForm.AppendChild (count); app.AppendChild (inputForm); app.AppendChild (items); app.AppendChild (clearbtn);