Add basic support for onchange
This commit is contained in:
parent
88cc2caf45
commit
ea3a18af25
|
|
@ -178,6 +178,13 @@ export class BrowserRenderer {
|
||||||
toDomElement.addEventListener('click', listener);
|
toDomElement.addEventListener('click', listener);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'onchange': {
|
||||||
|
toDomElement.removeEventListener('change', toDomElement['_blazorChangeListener']);
|
||||||
|
const listener = evt => raiseEvent(browserRendererId, componentId, eventHandlerId, 'change', { Type: 'change', Value: evt.target.value });
|
||||||
|
toDomElement['_blazorChangeListener'] = listener;
|
||||||
|
toDomElement.addEventListener('change', listener);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'onkeypress': {
|
case 'onkeypress': {
|
||||||
toDomElement.removeEventListener('keypress', toDomElement['_blazorKeypressListener']);
|
toDomElement.removeEventListener('keypress', toDomElement['_blazorKeypressListener']);
|
||||||
const listener = evt => {
|
const listener = evt => {
|
||||||
|
|
@ -256,4 +263,4 @@ function raiseEvent(browserRendererId: number, componentId: number, eventHandler
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventInfoType = 'mouse' | 'keyboard';
|
type EventInfoType = 'mouse' | 'keyboard' | 'change';
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Interop
|
||||||
|
|
||||||
private static object CoerceShallow(object deserializedValue, Type typeOfT)
|
private static object CoerceShallow(object deserializedValue, Type typeOfT)
|
||||||
{
|
{
|
||||||
|
if (typeOfT == typeof(object)) { return deserializedValue; }
|
||||||
|
|
||||||
if (deserializedValue == null)
|
if (deserializedValue == null)
|
||||||
{
|
{
|
||||||
// Return default value for type
|
// Return default value for type
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,8 @@ namespace Microsoft.AspNetCore.Blazor.Browser.Rendering
|
||||||
return Json.Deserialize<UIMouseEventArgs>(eventArgsJson);
|
return Json.Deserialize<UIMouseEventArgs>(eventArgsJson);
|
||||||
case "keyboard":
|
case "keyboard":
|
||||||
return Json.Deserialize<UIKeyboardEventArgs>(eventArgsJson);
|
return Json.Deserialize<UIKeyboardEventArgs>(eventArgsJson);
|
||||||
|
case "change":
|
||||||
|
return Json.Deserialize<UIChangeEventArgs>(eventArgsJson);
|
||||||
default:
|
default:
|
||||||
throw new ArgumentException($"Unsupported value '{eventArgsType}'.", nameof(eventArgsType));
|
throw new ArgumentException($"Unsupported value '{eventArgsType}'.", nameof(eventArgsType));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -186,5 +186,14 @@ namespace Microsoft.AspNetCore.Blazor.Components
|
||||||
protected RenderTreeFrame onclick(Action handler)
|
protected RenderTreeFrame onclick(Action handler)
|
||||||
// Note that the 'sequence' value is updated later when inserted into the tree
|
// Note that the 'sequence' value is updated later when inserted into the tree
|
||||||
=> RenderTreeFrame.Attribute(0, "onclick", _ => handler());
|
=> RenderTreeFrame.Attribute(0, "onclick", _ => handler());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles change events by invoking <paramref name="handler"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="handler">The handler to be invoked when the event occurs. The handler will receive the new value as a parameter.</param>
|
||||||
|
/// <returns>A <see cref="RenderTreeFrame"/> that represents the event handler.</returns>
|
||||||
|
protected RenderTreeFrame onchange(Action<object> handler)
|
||||||
|
// Note that the 'sequence' value is updated later when inserted into the tree
|
||||||
|
=> RenderTreeFrame.Attribute(0, "onchange", args => handler(((UIChangeEventArgs)args).Value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,4 +31,16 @@ namespace Microsoft.AspNetCore.Blazor.RenderTree
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Key { get; set; }
|
public string Key { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Supplies information about an input change event that is being raised.
|
||||||
|
/// </summary>
|
||||||
|
public class UIChangeEventArgs : UIEventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the new value of the input. This may be a <see cref="string"/>
|
||||||
|
/// or a <see cref="bool"/>.
|
||||||
|
/// </summary>
|
||||||
|
public object Value { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue