From a21ad686d65567b57c527cb68a3792993b24396b Mon Sep 17 00:00:00 2001 From: Wassup789 Date: Tue, 28 May 2024 20:48:58 -0700 Subject: [PATCH] refactor: convert ternary / if statements to `when` --- src/components/ExerciseSelectorFilterGroup.ts | 26 +++++++++---------- src/components/GenericSnackbar.ts | 14 +++++----- src/components/RadioGroupElement.ts | 9 ++++--- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/components/ExerciseSelectorFilterGroup.ts b/src/components/ExerciseSelectorFilterGroup.ts index f89ff39..4e6e8c7 100644 --- a/src/components/ExerciseSelectorFilterGroup.ts +++ b/src/components/ExerciseSelectorFilterGroup.ts @@ -2,6 +2,7 @@ import { customElement, property } from "lit/decorators.js"; import { css, LitElement, html, PropertyValues } from "lit"; import StorageService from "../services/StorageService"; import { TypedLitElement } from "../models/TypedEventTarget"; +import { when } from "lit/directives/when.js"; type FilterGroupStorageData = Record; @@ -198,22 +199,19 @@ export default class ExerciseSelectorFilterGroup extends (LitElement as TypedLit } protected render(): unknown { - let headerSuffix; - if (this.togglable) { - headerSuffix = html` - ${this.name} -
this.onHeaderActiveClick(event)}>ACTIVE
-
- `; - } else { - headerSuffix = html`${this.name}`; - } - return html`
-
this.active = !this.active}>${headerSuffix}
+
this.active = !this.active}> + ${when(this.togglable, () => html` + ${this.name} +
this.onHeaderActiveClick(event)}>ACTIVE
+
+ `, () => html` + ${this.name} + `)} +
diff --git a/src/components/GenericSnackbar.ts b/src/components/GenericSnackbar.ts index 4450ebe..66a41cc 100644 --- a/src/components/GenericSnackbar.ts +++ b/src/components/GenericSnackbar.ts @@ -1,6 +1,7 @@ import { LitElement, css, html } from "lit"; import { customElement, state } from "lit/decorators.js"; import { TypedLitElement } from "../models/TypedEventTarget"; +import { when } from "lit/directives/when.js"; @customElement(GenericSnackbar.NAME) export default class GenericSnackbar extends (LitElement as TypedLitElement) { @@ -108,18 +109,15 @@ export default class GenericSnackbar extends (LitElement as TypedLitElement this.onActionClick()}> - ${this.actionLabel} - ` - : ""; - return html`
- ${button} + ${when(this.actionLabel, () => html` + + `, () => "")}
`; diff --git a/src/components/RadioGroupElement.ts b/src/components/RadioGroupElement.ts index 453d687..e713aaf 100644 --- a/src/components/RadioGroupElement.ts +++ b/src/components/RadioGroupElement.ts @@ -3,6 +3,7 @@ import { css, html, LitElement, PropertyValues } from "lit"; import { RadioGroup } from "../models/RadioGroup"; import { RadioGroupValue } from "../models/RadioGroupValue"; import { TypedLitElement } from "../models/TypedEventTarget"; +import { when } from "lit/directives/when.js"; @customElement(RadioGroupElement.NAME) export default class RadioGroupElement extends (LitElement as TypedLitElement, RadioGroupElementEventMap>) { @@ -43,8 +44,9 @@ export default class RadioGroupElement extends (LitElement as TypedLitElement private checkedRadioGroupValue: RadioGroupValue | null = null; protected render(): unknown { - return this.radioGroup ? html` - ${this.radioGroup.values.map((e) => html` + return html` + ${when(this.radioGroup, (radioGroup) => html` + ${radioGroup.values.map((e) => html`
this.setValue(e.value)} @@ -55,7 +57,8 @@ export default class RadioGroupElement extends (LitElement as TypedLitElement class="input-text" @click=${() => this.setValue(e.value)}>${e.label} `)} - ` : ""; + `, () => "")} + `; } protected updated(changedProperties: PropertyValues) {