[英]How can I conditionally render LitElement with TypeScript?
So I am mapping through an array of objects and looking to display on the page when a radio button is selected.因此,我正在映射一组对象,并希望在选择单选按钮时显示在页面上。 For example, if there are two objects in the array, there will be two radio buttons.例如,如果数组中有两个对象,就会有两个单选按钮。 If you press radio1, it should render form1.如果您按 radio1,它应该呈现 form1。 If you press radio2, it should hide form1 and show form2.如果您按 radio2,它应该隐藏 form1 并显示 form2。
I created a property called formIndex to keep track of which button is being pressed so that I know which form to call but I'm having trouble implementing.我创建了一个名为 formIndex 的属性来跟踪按下了哪个按钮,以便我知道要调用哪个表单,但我在实现时遇到了麻烦。
Current behavior: on page load, both radios appear with no data rendered yet.当前行为:在页面加载时,两个收音机都出现,但尚未呈现任何数据。 When I press radio1, it displays both form1 and form2.当我按下 radio1 时,它同时显示 form1 和 form2。 When I press radio2, it also displays both form1 and form2.当我按下 radio2 时,它也同时显示 form1 和 form2。
I'm using LitElement in a TypeScript file.我在 TypeScript 文件中使用 LitElement。
Here is the property I created.这是我创建的属性。 Since I'm mapping through an array starting at 0, I initialized this property to -1:由于我要映射从 0 开始的数组,因此我将此属性初始化为 -1:
@state()
formIndex: number = -1;
Here is where I am rendering the forms:这是我渲染表单的地方:
protected renderMultipleForms(formConfig: any): TemplateResult {
return html`
${formConfig?.formHeading ? html`<h3>${formConfig.formHeading}</h3>` : ''}
${formConfig.forms?.map((data: any) => html`
<!-- <adc-radio-group>
<adc-radio-button id="radioBtn" label=${data.label} @click="${this.handleClick}"></adc-radio-button>
</adc-radio-group> -->
<!-- RADIOS -->
<input type="radio" id=${data.label} name="paymentRadios" @click="${this.handleClick}">${data.label} <br />
<!-- RENDERING FORMS -->
<p id=${this.formIndex}>${this.formIndex > -1 ? this.renderForm(data.form, data.paymentIcons) : ''}</p>
`)}
`;
}
Finally, here is the method to handle the clicking of the radios:最后,这里是处理无线电点击的方法:
protected handleClick(e: any){
if(e.target.id == this._data.forms[0].label){
this.formIndex = 0
} else if(e.target.id == this._data.forms[1].label) {
this.formIndex = 1
}
console.log(this.formIndex);
}
How can I make it to where only the first form is displayed when the first radio is clicked and only the second form is displayed when the second radio is clicked?如何在单击第一个单选时只显示第一个表单,而在单击第二个单选时只显示第二个表单? Thanks!谢谢!
I would just use conditional rendering as described on the lit.dev docs我只会使用lit.dev 文档中描述的条件渲染
A minimal example looks like this:一个最小的例子看起来像这样:
@customElement('test-cond-render')
export class CondRender extends LitElement {
@state()
selectedForm = -1;
formData = [
{id: 0, title: 'form one'},
{id: 1, title: 'form two'}
];
renderForm() {
if(this.selectedForm === -1) {
return html`
<span>Please select a form</span>
`;
} else if (this.selectedForm === 0) {
return html`
<form action="#" target="_blank">
<p>
Select your interests:<br>
<input type="checkbox" name="movies"> Movies<br>
<input type="checkbox" name="sports"> Sports<br>
<input type="checkbox" name="videogames"> Videogames
</p>
<input type="submit" value="Send data">
</form>
`;
} else if (this.selectedForm === 1) {
return html`
<form action="#" target="_blank">
<p>
Enter your full name: <input type="text" name="fullname">
<input type="submit" value="Send data">
</p>
</form>
`;
} else {
return html`
<span>Something went wrong..</span>
`;
}
}
handleClick(form_id:number) {
console.log('handle click:', form_id);
this.selectedForm = form_id;
}
render() {
return html `
<h2>Cond render</h2>
${this.formData.map((data) =>
html`
<input type="radio" name="form-group" value="${data.id}" id="${data.id}" @click="${this.handleClick.bind(this, data.id)}">
<label for="${data.id}">${data.title}</label>
`
)}
<div>${this.renderForm()}</div>
`;
}
}
Your forms might be more complicated but the principle should be the same.你的表格可能更复杂,但原则应该是一样的。
You can also use the cache directive for the render so it won't re-create the DOM for the selected form every time it is switched.您还可以为渲染使用缓存指令,这样它就不会在每次切换时为所选表单重新创建 DOM。
<div>${cache(this.renderForm())}</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.