/** * @license * Copyright 2021 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import {LitElement, render, TemplateResult} from 'lit'; import {property} from 'lit/decorators.js'; import {ClassInfo, classMap} from 'lit/directives/class-map.js'; import {guard} from 'lit/directives/guard.js'; import {html, literal} from 'lit/static-html.js'; /** Test table interface. */ export interface TestTableTemplate { /** The row display name. May be a Lit static value for rich HTML. */ display: string|ReturnType; /** * A template's render function. It accepts a state string (the column) and * returns a Lit `TemplateResult`. * * @param state The current state to render in. * @return A `TemplateResult` for the given state. */ render(state: S): TemplateResult|null; } /** @soyCompatible */ export class TestTable extends LitElement { static override shadowRootOptions: ShadowRootInit = {mode: 'open'}; @property({type: String}) override title = 'Title'; @property({type: Array}) states: S[] = []; @property({type: Array}) templates: Array> = []; @property({type: Boolean}) dark = false; /** @soyTemplate */ protected override render(): TemplateResult { return html` ${this.states.map(state => html` `)} ${guard([this.templates, this.states], () => this.renderTemplates())}
${state}
${this.title}
`; } /** @soyTemplate */ protected getRenderClasses(): ClassInfo { return { 'md3-test-table--dark': this.dark, }; } /** @soyTemplate */ protected renderTemplates(): TemplateResult { // Render templates in the light DOM for easier styling access render( this.templates.map( (template, rowIndex) => this.states.map((state, colIndex) => { const renderResult = template.render(state); const isEmptyTemplate = renderResult === null; return isEmptyTemplate ? html`` : html`
${renderResult}
`; })), this); return html` ${this.templates.map((template, rowIndex) => html` ${this.getVariantName(template.display)} ${this.states.map((state, colIndex) => html`
N/A
`)} `)} `; } /** Convert the name from camel case to sentence case. */ private getVariantName(display: TestTableTemplate['display']) { if (typeof display !== 'string') { return display; } const withSpaces = display.replace(/([A-Z])/g, ' $1'); return withSpaces[0].toUpperCase() + withSpaces.slice(1); } }