Wrapper Components in .NET 8 Blazor Web App

Hey,

Is it possible to create "wrapper components" from Radzen components? I have now sat for hours trying. The reason for this is that if we, as a company, were to change the component library to something else in the future, it is a little easier to do it in one place instead of in several places in the application.

This is what I have tried:

  1. I started a new (solution) where I set up two new projects, one (Razor Class Library) and one (Blazor Web App) project. I referenced the class library in the Blazor project.

  2. I installed the Radzen.Blazor nuget package in the class library.

  3. I added "@using Radzen" and "@using Radzen.Blazor" to "_Imports.razor" for both projects.

  4. I added both: <link rel="stylesheet" href="_content/Radzen.Blazor/css/material-base.css"> and <script src="_content/Radzen.Blazor/Radzen.Blazor.js"></script> in the "App.razor" in the Blazor Web App project.

  5. I added the services to the "Program.cs" file and the necessary components for these services at the top of "MainLayout.razor" in the Blazor Web App project.

  6. I created a component in my Razor Class Library for testing. I named it "LM_Button.Razor". It is the one that I will use as, for example, a (Wrapper component) for RadzenButton.

  7. I added this code to the LM_Button.Razor component:

<RadzenButton @attributes="AdditionalAttributes" 
              BusyText="@BusyText" 
              ButtonStyle="@ButtonStyle" 
              ButtonType="@ButtonType" 
              Disabled="@Disabled" 
              Icon="@Icon" 
              IconColor="@IconColor" 
              Image="@Image" 
              ImageAlternateText="@ImageAlternateText" 
              IsBusy="@IsBusy" 
              Shade="@Shade" 
              Size="@Size" 
              TabIndex="@TabIndex" 
              Text="@Text" 
              Variant="@Variant" 
              Click="@Click">
    @ChildContent
</RadzenButton>

@code {
    [Parameter] public string BusyText { get; set; }
    [Parameter] public ButtonStyle ButtonStyle { get; set; }
    [Parameter] public ButtonType ButtonType { get; set; }
    [Parameter] public bool Disabled { get; set; }
    [Parameter] public string Icon { get; set; }
    [Parameter] public string IconColor { get; set; }
    [Parameter] public string Image { get; set; }
    [Parameter] public string ImageAlternateText { get; set; }
    [Parameter] public bool IsBusy { get; set; }
    [Parameter] public Shade Shade { get; set; }
    [Parameter] public ButtonSize Size { get; set; }
    [Parameter] public int TabIndex { get; set; }
    [Parameter] public string Text { get; set; }
    [Parameter] public Variant Variant { get; set; }
    [Parameter] public EventCallback<MouseEventArgs> Click { get; set; }
    [Parameter] public RenderFragment ChildContent { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public IDictionary<string, object> AdditionalAttributes { get; set; }
}
  1. When I then use my new component that is a copy of RadzenButton like this:
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Gap="1rem" Wrap="FlexWrap.Wrap">
    <RadzenButton Text="Primary" ButtonStyle="ButtonStyle.Primary" />
    <RadzenButton Text="Secondary" ButtonStyle="ButtonStyle.Secondary" />
    <RadzenButton Text="Info" ButtonStyle="ButtonStyle.Info" />
    <RadzenButton Text="Success" ButtonStyle="ButtonStyle.Success" />
    <RadzenButton Text="Warning" ButtonStyle="ButtonStyle.Warning" />
    <RadzenButton Text="Danger" ButtonStyle="ButtonStyle.Danger" />
</RadzenStack>

<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Gap="1rem" Wrap="FlexWrap.Wrap">
    <LM_Button Text="Primary" ButtonStyle="ButtonStyle.Primary" />
    <LM_Button Text="Secondary" ButtonStyle="ButtonStyle.Secondary" />
    <LM_Button Text="Info" ButtonStyle="ButtonStyle.Info" />
    <LM_Button Text="Success" ButtonStyle="ButtonStyle.Success" />
    <LM_Button Text="Warning" ButtonStyle="ButtonStyle.Warning" />
    <LM_Button Text="Danger" ButtonStyle="ButtonStyle.Danger" />
</RadzenStack>
  1. I get two completely different results:

Skärmbild 2024-02-22 160730

Did I miss something important?

Thanks in advance.

@kalsson I think "ChildContent" is the problem

Indeed RadzenButton prefers ChildContent to Text (if both are set). Try changing your declaration to

<RadzenButton @attributes="AdditionalAttributes" 
              BusyText="@BusyText" 
              ButtonStyle="@ButtonStyle" 
              ButtonType="@ButtonType" 
              Disabled="@Disabled" 
              Icon="@Icon" 
              IconColor="@IconColor" 
              Image="@Image" 
              ImageAlternateText="@ImageAlternateText" 
              IsBusy="@IsBusy" 
              Shade="@Shade" 
              Size="@Size" 
              TabIndex="@TabIndex" 
              Text="@Text" 
              Variant="@Variant"
              ChildContent="@ChildContent"
              Click="@Click" />

Also some of your properties do not have the same default values as in RadzenButton. This causes the different (lighter) appearance.

Correct @GodzSky , thanks!

Thanks @korchev , that declaration fixed the ChildContent. :+1: :grin:
Regarding the default value for the properties, where do I find them. I'm just looking at the API but if you know a better place to look, maybe you can pinpoint me to the right place? :laughing:

Thanks in advanced!

EDIT: @korchev , I have found it on the Radzen Github page. Thanks again!

1 Like