@scania/tegel
Version:
Tegel Design System
144 lines (138 loc) • 9.66 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-ca8040ad.js');
const tableHeaderCellCss = ":host(.tds-table__header-cell){box-sizing:border-box;font:var(--tds-headline-07);letter-spacing:var(--tds-headline-07-ls);display:table-cell;text-align:left;color:var(--tds-table-color);background-color:var(--tds-table-header-background);border-bottom:1px solid var(--tds-table-header-background-hover);padding:0;height:48px;min-width:192px;box-sizing:border-box;vertical-align:middle;overflow:hidden;transition:background-color 200ms ease}:host(.tds-table__header-cell) *{box-sizing:border-box}:host(.tds-table__header-cell) .tds-table__header-text{padding:8px 16px;margin:0}:host(.tds-table__header-cell) .tds-table__header-text-no-padding{padding:0}:host(.tds-table__header-cell--sortable) .tds-table__header-button{display:flex;align-items:center;gap:8px;justify-content:start;flex-direction:row;width:100%;height:100%;padding:0 16px;background-color:transparent;border:none;cursor:pointer;transition:background-color 200ms ease}:host(.tds-table__header-cell--sortable) .tds-table__header-button:focus{outline:2px solid var(--tds-table-header-btn-focus);outline-offset:-2px}:host(.tds-table__header-cell--sortable) .tds-table__header-button:focus .tds-table__header-button-icon{opacity:1}:host(.tds-table__header-cell--sortable) .tds-table__header-button .tds-table__header-button-text{font:var(--tds-headline-07);letter-spacing:var(--tds-headline-07-ls);text-align:left;color:var(--tds-table-header-btn-color)}:host(.tds-table__header-cell--sortable) .tds-table__header-button .tds-table__header-button-icon{flex:0 0 16px;height:16px;width:16px;opacity:0;transform-origin:center;transition:opacity 200ms ease-in, transform 200ms ease;color:var(--tds-table-color)}:host(.tds-table__header-cell--sortable) .tds-table__header-button:hover .tds-table__header-button-icon{opacity:1}:host(.tds-table__header-cell--is-sorted) .tds-table__header-button{background-color:var(--tds-table-header-btn-background)}:host(.tds-table__header-cell--is-sorted) .tds-table__header-button:hover{background-color:var(--tds-table-header-btn-background-hover)}:host(.tds-table__header-cell--is-sorted) .tds-table__header-button .tds-table__header-button-icon{opacity:1}:host(.tds-table__header-cell--is-sorted) .tds-table__header-button .tds-table__header-button-icon--rotate{transform:rotate(180deg)}:host(.tds-table--compact){height:33px}:host(.tds-table--divider){border-right:1px solid var(--tds-table-divider)}:host(.tds-table--divider:last-of-type){border-right:none}:host(.tds-table--no-min-width){min-width:unset}:host(.tds-table--no-padding){height:unset}:host(.tds-table__header-cell:first-of-type){border-top-left-radius:4px}:host(.tds-table__header-cell:last-of-type){border-top-right-radius:4px}:host(.tds-table--extra-column:first-of-type){border-top-left-radius:0}:host(.tds-table--toolbar-available){border-radius:0}:host(.tds-table--extra-column.tds-table--toolbar-available){border-radius:0}:host(.tds-table__header-cell.tds-table--toolbar-available:first-of-type){border-top-left-radius:0}:host(.tds-table__header-cell.tds-table--toolbar-available:last-of-type){border-top-right-radius:0}";
const TdsHeaderCellStyle0 = tableHeaderCellCss;
const relevantTableProps = [
'multiselect',
'expandableRows',
'compactDesign',
'noMinWidth',
'verticalDividers',
];
const TdsTableHeaderCell = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.tdsSort = index.createEvent(this, "tdsSort", 7);
this.internalSortButtonClicked = index.createEvent(this, "internalSortButtonClicked", 6);
this.internalTdsTextAlign = index.createEvent(this, "internalTdsTextAlign", 6);
this.internalTdsHover = index.createEvent(this, "internalTdsHover", 6);
this.sortButtonClick = () => {
if (this.sortingDirection !== 'asc' && this.sortingDirection !== 'desc') {
this.sortingDirection = 'asc';
}
else {
this.sortingDirection = this.sortingDirection === 'asc' ? 'desc' : 'asc';
}
this.sortedByMyKey = true;
this.tdsSort.emit({
tableId: this.tableId,
columnKey: this.cellKey,
sortingDirection: this.sortingDirection,
});
this.internalSortButtonClicked.emit({
tableId: this.tableId,
key: this.cellKey,
});
};
this.headerCellContent = () => {
if (this.sortable) {
return (index.h("button", { class: "tds-table__header-button", onClick: () => this.sortButtonClick(), style: { justifyContent: this.textAlignState }, "aria-label": this.tdsAriaLabelSortButton }, index.h("span", { class: "tds-table__header-button-text" }, this.cellValue, index.h("slot", null)), this.sortingDirection === undefined && (index.h("tds-icon", { svgTitle: "sorting", class: "tds-table__header-button-icon", name: "sorting", size: "16px" })), this.sortingDirection && ['asc', 'desc'].includes(this.sortingDirection) && (index.h("tds-icon", { svgTitle: "arrow down", class: `tds-table__header-button-icon ${this.sortingDirection === 'asc' ? 'tds-table__header-button-icon--rotate' : ''}`, name: "arrow_down", size: "16px" }))));
}
return (index.h("p", { class: {
'tds-table__header-text': true,
'tds-table__header-text-no-padding': this.disablePadding,
}, style: { textAlign: this.textAlignState } }, this.cellValue, index.h("slot", null)));
};
this.onHeadCellHover = (key) => {
this.internalTdsHover.emit({
tableId: this.tableId,
key,
});
};
this.cellKey = undefined;
this.cellValue = undefined;
this.customWidth = undefined;
this.sortable = false;
this.textAlign = 'left';
this.disablePadding = false;
this.tdsAriaLabelSortButton = '';
this.textAlignState = undefined;
this.sortingDirection = undefined;
this.sortedByMyKey = false;
this.verticalDividers = false;
this.compactDesign = false;
this.noMinWidth = false;
this.multiselect = false;
this.enableToolbarDesign = false;
this.tableId = '';
this.expandableRows = false;
}
internalTdsPropChangeListener(event) {
if (this.tableId === event.detail.tableId) {
event.detail.changed
.filter((changedProp) => relevantTableProps.includes(changedProp))
.forEach((changedProp) => {
if (typeof this[changedProp] === 'undefined') {
throw new Error(`Table prop is not supported: ${changedProp}`);
}
this[changedProp] = event.detail[changedProp];
});
}
}
updateOptionsContent(event) {
const { tableId, key } = event.detail;
if (this.tableId === tableId) {
if (this.cellKey !== key) {
this.sortedByMyKey = false;
// To sync with CSS transition timing
setTimeout(() => {
this.sortingDirection = undefined;
}, 200);
}
}
}
connectedCallback() {
this.tableEl = this.host.closest('tds-table');
this.tableId = this.tableEl.tableId;
}
componentWillLoad() {
relevantTableProps.forEach((tablePropName) => {
this[tablePropName] = this.tableEl[tablePropName];
});
}
componentWillRender() {
// if text alignment matches any of the acceptable values, use it. Otherwise, set "left" as default
this.textAlignState = ['left', 'start', 'right', 'end', 'center'].includes(this.textAlign)
? this.textAlign
: 'left';
// To enable body cells text align per rules set in head cell
this.internalTdsTextAlign.emit([this.tableId, this.cellKey, this.textAlignState]);
this.enableToolbarDesign =
this.host.closest('tds-table').getElementsByTagName('tds-table-toolbar').length >= 1;
}
getAriaSort() {
if (this.sortingDirection === 'asc')
return 'ascending';
if (this.sortingDirection === 'desc')
return 'descending';
return 'none';
}
render() {
return (index.h(index.Host, { key: 'c832982801653ad4ed4be858fa88f6d3d3de02ef', class: {
'tds-table__header-cell': true,
'tds-table__header-cell--sortable': this.sortable,
'tds-table__header-cell--is-sorted': this.sortedByMyKey,
'tds-table__header-cell--custom-width': this.customWidth !== '',
'tds-table--compact': this.compactDesign,
'tds-table--divider': this.verticalDividers,
'tds-table--no-min-width': this.noMinWidth,
'tds-table--extra-column': this.multiselect || this.expandableRows,
'tds-table--toolbar-available': this.enableToolbarDesign,
'tds-table--no-padding': this.disablePadding,
}, style: { minWidth: this.customWidth }, onMouseOver: () => this.onHeadCellHover(this.cellKey), onMouseLeave: () => this.onHeadCellHover(''), role: "columnheader", "aria-sort": this.getAriaSort() }, this.headerCellContent()));
}
get host() { return index.getElement(this); }
};
TdsTableHeaderCell.style = TdsHeaderCellStyle0;
exports.tds_header_cell = TdsTableHeaderCell;