Description
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