tp-table/tp-table-item.js

143 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2022-06-17 16:13:55 +02:00
/**
@license
Copyright (c) 2021 EDV Wasmeier
*/
import '@tp/tp-checkbox/tp-checkbox.js';
import { LitElement, html, css } from 'lit';
import { DomQuery } from '@tp/helpers/dom-query.js';
/**
# ef-base-table-item
## Example
```html
<ef-base-table-item></ef-base-table-item>
```
*/
const mixins = [
DomQuery
];
/* @litElement */
const BaseElement = mixins.reduce((baseClass, mixin) => {
return mixin(baseClass);
}, LitElement);
class TpTableItem extends BaseElement {
static get styles() {
return [
css`
:host {
display: block;
padding: 0;
}
.wrap {
display: grid;
grid-template-columns: 0.5fr 3fr 0.5fr 0.5fr 0.5fr; /* Overridden by javascript */
align-items: center;
}
2022-11-07 10:01:24 +01:00
[part="chk"] {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
tp-checkbox::part(label) {
display: none;
}
2022-06-17 16:13:55 +02:00
[part="cell"] {
align-self: stretch;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 10px 20px;
border-right: solid 1px #c1c1c1;
}
.wrap > div.chk {
border-right: none;
}
`
];
}
render() {
2023-01-13 11:52:10 +01:00
const { columns, item, selected } = this;
2022-06-17 16:13:55 +02:00
return html`
<div id="grid" class="wrap" part="row">
${this.selectable ? html`
2022-11-07 10:01:24 +01:00
<div class="chk" part="chk">
2023-01-13 11:52:10 +01:00
<tp-checkbox id="selectionChk" .checked=${selected} @click=${this._updateSelectionState}></tp-checkbox>
2022-06-17 16:13:55 +02:00
</div>
` : null}
2022-11-07 10:01:24 +01:00
${Array.isArray(columns) ? columns.map((column, idx) => {
if (column.visible !== true && column.required !== true) return;
2023-09-19 15:40:12 +02:00
return this.renderColumn(column, item, idx) || null;
}) : null
2022-11-07 10:01:24 +01:00
}
2022-06-17 16:13:55 +02:00
</div>
`;
}
static get properties() {
return {
index: { type: Array },
2022-11-07 10:01:24 +01:00
item: { type: Object, hasChanged: () => true },
2022-06-17 16:13:55 +02:00
columns: { type: Array },
selectable: { type: Boolean },
2023-01-13 11:52:10 +01:00
selected: { type: Boolean, reflect: true },
2022-06-17 16:13:55 +02:00
};
}
renderColumn(column, item) {
return html`<div part="cell">${item[column.name]}</div>`;
}
async updated(changes) {
if (changes.has('columns') && Array.isArray(this.columns)) {
let colWidths = this.columns.filter(col => col.required || col.visible).map(col => col.width).join(' ');
if (this.selectable) {
colWidths = '40px ' + colWidths;
}
this.$grid.style.gridTemplateColumns = colWidths;
}
if (changes.has('index')) {
if (this.index % 2 === 0) {
this.part.add('odd');
} else {
this.part.remove('odd');
}
}
}
get $grid() {
if (this.__$grid) {
return this.__$grid;
}
this.__$grid = this.shadowRoot.getElementById('grid');
return this.__$grid;
}
_updateSelectionState(e) {
const target = e.target;
2023-01-13 11:52:10 +01:00
this.selected = e.target.checked;
this.dispatchEvent(new CustomEvent('row-selection-changed', { detail: { item: this.item, selected: target.checked }, bubbles: true, composed: true }));
2022-06-17 16:13:55 +02:00
}
}
window.customElements.define('tp-table-item', TpTableItem);
export default TpTableItem