Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event arguments #13671

Closed
Stamo-Gochev opened this issue Sep 4, 2019 · 2 comments
Closed

Event arguments #13671

Stamo-Gochev opened this issue Sep 4, 2019 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Milestone

Comments

@Stamo-Gochev
Copy link

Stamo-Gochev commented Sep 4, 2019

This is about blazor.

Is your feature request related to a problem? Please describe.

Currently, event bubbling works in blazor, but there isn't a way to find out which child element triggered the event.

For example:

<div id="div1" @onclick="@OnClick"> div 1
    <div id="div2"> div 2</div>
     <div id="div3"> div 3</div>
     <div id="div4"> 
           <span class="expand-icon"></span>
           div 4 
     </div>
</div>

@code {
    public void OnClick(UIMouseEventArgs args) 
    {  
           // not currently possible, used here as a suggestion (see below)
           if (args.elementId == 2) // or if (args.elementClass == "expand-icon"
           {
                   Expand();
           }
           else
           {
                  Collapse();
            }
    }

    public void Expand() { ... }
    public void Collapse() { ... }
}

Clicking on div2 will trigger the OnClick handler, but you need to make a JS call in order to find out which element was actually clicked - div2 or div3.

Describe the solution you'd like

A way to find out which element was clicked. Exposing event.target is probably not possible due to wasm and it might not be necessary - what I need is rather an "identifier" for the event.target element rather than the element itself. For example, sending some specific attributes like id or class might be enough - if the <span class="expand-icon"></span> is clicked, getting its class can be used to trigger custom logic (e.g. "expand" or "collapse"). If using specific attributes is not possible due to some synchronizations problems with the DOM, can data- attributes be used instead, e.g.

<div id="div4">
    <span class="expand-icon" data-id="@Id"></span>
    div 4
</div>

@code {
    public Guid Id { get; set; } = GetGuid();

    public void OnClick(UIMouseEventArgs args)
    {
        if (args.target.Attributes["data-id"] == Id)
        {
        ...
        }
    }
}

This should mean that the developer is responsible for providing the event.target "information" (`data-id="Id") explicitly and then handling it.

Additional context

This can be handled from JS and JS is probably better suited for more advanced scenarios, but maybe the UIMouseEventArgs class can expose some additional props for handling simpler scenarios. This might be also possible with using custom blazor components and exposing specific events, but this fails in cases with templates. For example, if the end user of the blazor component can completely override the "default" component that is used in the template. However, by using specific classes/attributes the overriden component might still work as the OnClick event handler checks for them.

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Sep 4, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @Stamo-Gochev.
We've moved this issue to the Backlog milestone. This means that it is not going to happen for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.

@mkArtakMSFT mkArtakMSFT added the enhancement This issue represents an ask for new feature or an enhancement to an existing one label Sep 4, 2019
@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Sep 4, 2019
@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Oct 14, 2020

This is something we'd consider enabling via #17552, so I'll treat this as a duplicate of that and close.

@SteveSandersonMS SteveSandersonMS added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Oct 14, 2020
@ghost ghost added the Status: Resolved label Oct 14, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants