I created a simple element using Litelement that has two properties, use field and value of the string and Array type respectively. When I pass the value as the HTML attribute as seen below, the expected action occurs and the value is displayed(seen in the image below).
<my-el value='[{"use": "official","system": "urn:lumiradx:consult"}]' ></my-el>
import {LitElement, html} from 'lit-element';
import '@material/mwc-select';
import '@material/mwc-list/mwc-list-item';
class MyEl extends LitElement {
static get properties() {
return {
useField: {type: String},
value: {type: Array}
}
}
constructor() {
super();
this.useField = 'true';
this.value = [{}];
}
render() {
if (typeof(this.value) == "string") {
this.value = JSON.parse(this.value);
}
return html`${this.value.map((i, index) => html`
<div id="div">
${this.useField !== 'false' ? html`
<mwc-select class="useField" value="${i.use}" @change="${e => this.value[index].use = e.target.value}">
<mwc-list-item value="usual">Usual</mwc-list-item>
<mwc-list-item value="official">Official</mwc-list-item>
<mwc-list-item value="temp">Temporary</mwc-list-item>
<mwc-list-item value="secondary">Secondary</mwc-list-item>
</mwc-select>` : ''}
</div>
`)}`;
}
}
window.customElements.define('my-el',MyEl);
A problem arises when I use this element as an import in another element. In the new element, the value of the first element is passed as an object in the second as seen below. How can I read an object as an attribute?
<el-two value='{"identifier":[{"use": "official","system": "urn:lumiradx:consult"}]}'></el-two>
import {LitElement, html} from 'lit-element';
import 'my-el/myel.js';
class ElTwo extends LitElement {
static get properties(){
return {
locationId: {type: String},
value: {type: Array}
}
}
/**default value of properties set in constructor*/
constructor() {
super();
this.locationId = 'true';
this.value = [{}];
}
render() {
if (typeof(this.value) == "string") {
this.value = JSON.parse(this.value);
}
return html`
${this.locationId !== 'false' ? html`<my-el value="${this.value.identifier}" id="locationId"></my-el>` : ''}
`;
}
}
window.customElements.define('el-two', ElTwo);
