Skip to content

Blazor Html WebComponents make binding/setting Array/Object Properties easier  #27654

Closed
@MichaelPeter

Description

@MichaelPeter

Hello AspNetCore Team,

it is possible to define for Html Web Components complex objects or arrays as parameters.

Like if I want to pass a list of objects as table content or a list of links in a web component.

Here my sample component:

customElements.define('webcomp-property', class extends HTMLElement {
    constructor() {
        super();

        this._linkList = [{ href: "http://dummy.com", text: "dummy link" }];

        this._shadowRoot = this.attachShadow({ mode: 'open' });

        // https://developer.mozilla.org/de/docs/Web/Web_Components/Using_custom_elements
        this.render();
    }

    get linklist() {
        return this._linkList;
    }

    set linklist(value) {
        this._linkList = value;
        this.render();
    }

    //attributeChangedCallback(attrName, oldVal, newVal) {
    //    if (attrName === LinkListAttributeName)
    //        this.render();
    //}

    render() {
        var lnkListVal = this._linkList;

        this._shadowRoot.innerHTML = `
         <div>
          ${lnkListVal.map(lnk => `<a href="${lnk.href}">${lnk.text}</a>`)}
          <br />
         </div>
    `;
    }
});

Now currently if I want to set this property I need the following code:

Index.razor:

<webcomp-property @ref="_webcompSetProperty"></webcomp-property>

@code {
    private ElementReference _webcompSetProperty;

    List<Link> _links = new List<Link>()
        {
            new Link(href: "Counter", text: "Counter" ),
            new Link(href: "http://www.google.com", text: "Google" ),
            new Link(href: "http://www.bing.com", text: "Bing" ),
        };

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        // Links
        await JSRuntime.InvokeAsync<string>("setWebComponentProperty", _webcompSetProperty, "linklist", _links);
    }

Javascript:

function setWebComponentProperty(webComp, propertyName, value) {
    webComp[propertyName] = value;
}

How it could be done differently:

<webcomp-property @custom:linklist="_links"></webcomp-property>

@code {
List<Link> _links = new List<Link>()
        {
            new Link(href: "Counter", text: "Counter" ),
            new Link(href: "http://www.google.com", text: "Google" ),
            new Link(href: "http://www.bing.com", text: "Bing" ),
        };
}

Could this be improved?

I build a repository with differnet html web component issues, there are also other issues in there :
https://github.com/MichaelPeter/BlazorWebComponentTestApp

importaint are
https://github.com/MichaelPeter/BlazorWebComponentTestApp/blob/master/Pages/Index.razor
and
https://github.com/MichaelPeter/BlazorWebComponentTestApp/blob/master/wwwroot/scripts/TestWebComponents.js

Related:
#27070
#27651

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.Needs: DesignThis issue requires design work before implementating.Needs: SpecIndicates that a spec defining user experience is requiredStatus: Resolvedaffected-very-fewThis issue impacts very few customersarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing oneseverity-minorThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions