From 0a9868eec1de01d8c74df79495b38b21698d1b69 Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Thu, 12 Jan 2023 12:44:36 +1000 Subject: [PATCH] show/hide checkbox/icon in table rows based on config --- .../shared/components/table/table.element.ts | 74 ++++++++++++------- .../shared/components/table/table.stories.ts | 1 + 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/table/table.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/table/table.element.ts index 2f3c4c15f1..722377a020 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/table/table.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/table/table.element.ts @@ -1,5 +1,7 @@ import { UUITextStyles } from '@umbraco-ui/uui-css'; -import { css, html, LitElement, nothing } from 'lit'; +import { css, html, LitElement } from 'lit'; +import { ifDefined } from 'lit-html/directives/if-defined.js'; +import { when } from 'lit-html/directives/when.js'; import { customElement, property, state } from 'lit/decorators.js'; import { repeat } from 'lit/directives/repeat.js'; @@ -22,6 +24,7 @@ export interface UmbTableColumn { export interface UmbTableConfig { allowSelection: boolean; + hideIcon?: boolean; } export class UmbTableSelectedEvent extends Event { @@ -77,16 +80,16 @@ export class UmbTableElement extends LitElement { display: none; } - uui-table-row:focus uui-icon, - uui-table-row:focus-within uui-icon, - uui-table-row:hover uui-icon, + uui-table-row[selectable]:focus uui-icon, + uui-table-row[selectable]:focus-within uui-icon, + uui-table-row[selectable]:hover uui-icon, uui-table-row[select-only] uui-icon { display: none; } - uui-table-row:focus uui-checkbox, - uui-table-row:focus-within uui-checkbox, - uui-table-row:hover uui-checkbox, + uui-table-row[selectable]:focus uui-checkbox, + uui-table-row[selectable]:focus-within uui-checkbox, + uui-table-row[selectable]:hover uui-checkbox, uui-table-row[select-only] uui-checkbox { display: inline-block; } @@ -137,6 +140,7 @@ export class UmbTableElement extends LitElement { @property({ type: Object, attribute: false }) public config: UmbTableConfig = { allowSelection: false, + hideIcon: false, }; /** @@ -201,18 +205,10 @@ export class UmbTableElement extends LitElement { } render() { - return html` + return html` - - - - - ${this.columns.map((column) => this._renderHeaderCell(column))} + ${this._renderHeaderCheckboxCell()} ${this.columns.map((column) => this._renderHeaderCell(column))} ${repeat(this.items, (item) => item.key, this._renderRow)} `; @@ -231,28 +227,52 @@ export class UmbTableElement extends LitElement { `; } + private _renderHeaderCheckboxCell() { + if (this.config.hideIcon && !this.config.allowSelection) return; + + return html` + ${when( + this.config.allowSelection, + () => html` + ` + )} + `; + } + private _renderRow = (item: UmbTableItem) => { return html` this._selectRow(item.key)} @unselected=${() => this._deselectRow(item.key)}> - - ${item.icon ? html`` : nothing} - this._renderRowCell(column, item))} + `; + }; + + private _renderRowCheckboxCell(item: UmbTableItem) { + if (this.config.hideIcon && !this.config.allowSelection) return; + + return html` + ${when(!this.config.hideIcon, () => html``)} + ${when( + this.config.allowSelection, + () => html` e.stopPropagation()} @change=${(event: Event) => this._handleRowCheckboxChange(event, item)} ?checked="${this._isSelected(item.key)}"> - - - ${this.columns.map((column) => this._renderRowCell(column, item))} - `; - }; + ` + )} + `; + } private _renderRowCell(column: UmbTableColumn, item: UmbTableItem) { - return html`${this._renderCellContent(column, item)} `; diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/table/table.stories.ts b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/table/table.stories.ts index 329ae94aea..b26be13e10 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/table/table.stories.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/shared/components/table/table.stories.ts @@ -72,6 +72,7 @@ const items: Array = [ const config: UmbTableConfig = { allowSelection: true, + hideIcon: false, }; export const AAAOverview: Story = () =>