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

Razor Components - Parameter Overloading #6956

Closed
vertonghenb opened this issue Jan 23, 2019 · 2 comments
Closed

Razor Components - Parameter Overloading #6956

vertonghenb opened this issue Jan 23, 2019 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@vertonghenb
Copy link
Contributor

vertonghenb commented Jan 23, 2019

Currently there is no way to overload [Parameter]'s in Razor Components. For example if you have an OnClick parameter, it has to be an Action<UIMouseEvent> or Func<Task>. This creates 2 parameters on a <MyButton/> component, if you want a async delegate and a sync delegate. Else you'll have to write stuff like

OnClick=@(async (e) => await DoSomething())

The issue

If you look at the Child Component MyButton we have 2 parameters to handle an action OnClick or OnClickAsync. Isn't it possible to combine these 2 parameters into 1?

Parent Component

<h1>Parent Component</h1>
<MyButton OnClickAsync="@DoSomething">My Button</MyButton>
@functions {
    async Task DoSomething()
    {
        await Task.Delay(250);
        StateHasChanged();
    }
}

Child Component (The button itself)

<button class="btn btn-success" onclick="@HandleOnClick">
    @ChildContent
</button>

@functions{
    [Parameter] protected RenderFragment ChildContent { get; set; }
    [Parameter] protected Action<UIMouseEventArgs> OnClick { get; set; }
    [Parameter] protected Func<Task> OnClickAsync { get; set; }

    private async Task HandleOnClick(UIMouseEventArgs args)
    {
        if (OnClick != null && OnClickAsync != null)
            throw new Exception("Only 1 handler can be set.");

        if(OnClick != null)
            OnClick.Invoke(args);

        if (OnClickAsync != null)
            await OnClickAsync.Invoke();
    }
}

Basically it would clean up the calling code to

<MyButton OnClick="@DoSomething">My Button</MyButton>

coming from:

<MyButton OnClickAsync=@(async (e) => await DoSomething())>My Button</MyButton>
  • DoSomething() being a Action<UIMouseEvent> or Func<Task> .
@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 25, 2019
@danroth27 danroth27 added the area-blazor Includes: Blazor, Razor Components label Feb 6, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @vertonghenb.
What you're trying to do can be achieved using Event Callbacks, which you can read more about at #6351

@vertonghenb
Copy link
Contributor Author

vertonghenb commented Mar 13, 2019

@mkArtakMSFT Correct, Thanks for adding these!

@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

4 participants