From e6f183e39edcfe699de25370a91ca2c42bfbe508 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Thu, 26 Sep 2019 11:01:25 +0100 Subject: [PATCH] Add RenderTreeBuilder extension methods for "prevent default" and "stop bubbling" --- .../src/Web/RenderTreeBuilderExtensions.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/Components/Web/src/Web/RenderTreeBuilderExtensions.cs diff --git a/src/Components/Web/src/Web/RenderTreeBuilderExtensions.cs b/src/Components/Web/src/Web/RenderTreeBuilderExtensions.cs new file mode 100644 index 0000000000..89dbc5684b --- /dev/null +++ b/src/Components/Web/src/Web/RenderTreeBuilderExtensions.cs @@ -0,0 +1,53 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.Components.Rendering; +using Microsoft.AspNetCore.Components.RenderTree; + +namespace Microsoft.AspNetCore.Components.Web +{ + /// + /// Provides methods for building a collection of entries. + /// + public static class RenderTreeBuilderExtensions + { + // The "prevent default" and "stop bubbling" flags behave like attributes, in that: + // - you can have multiple of them on a given element (for separate events) + // - you can add and remove them dynamically + // - they are independent of other attributes (e.g., you can "stop bubbling" of a given + // event type on an element that doesn't itself have a handler for that event) + // As such, they are represented as attributes to give the right diffing behavior. + // + // As a private implementation detail, their internal representation is magic-named + // attributes. This may change in the future. If we add support for multiple-same + // -named-attributes-per-element (#14365), then we will probably also declare a new + // AttributeType concept, and have specific attribute types for these flags, and + // the "name" can simply be the name of the event being modified. + + /// + /// Appends a frame representing an instruction to prevent the default action + /// for a specified event. + /// + /// The . + /// An integer that represents the position of the instruction in the source code. + /// The name of the event to be affected. + /// True if the default action is to be prevented, otherwise false. + public static void AddEventPreventDefaultAttribute(this RenderTreeBuilder builder, int sequence, string eventName, bool value) + { + builder.AddAttribute(sequence, $"__internal_preventDefault_{eventName}", value); + } + + /// + /// Appends a frame representing an instruction to stop the specified event from + /// bubbling up beyond the current element. + /// + /// The . + /// An integer that represents the position of the instruction in the source code. + /// The name of the event to be affected. + /// True if bubbling should stop bubbling here, otherwise false. + public static void AddEventStopBubblingAttribute(this RenderTreeBuilder builder, int sequence, string eventName, bool value) + { + builder.AddAttribute(sequence, $"__internal_stopBubbling_{eventName}", value); + } + } +}