@scania/tegel
Version:
Tegel Design System
183 lines (178 loc) • 11.2 kB
JavaScript
import { p as proxyCustomElement, H, d as createEvent, h, c as Host } from './p-28ef5186.js';
import { d as defineCustomElement$2 } from './p-b390ece5.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 = /*@__PURE__*/ proxyCustomElement(class TdsTableHeaderCell extends H {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.tdsSort = createEvent(this, "tdsSort", 7);
this.internalSortButtonClicked = createEvent(this, "internalSortButtonClicked", 6);
this.internalTdsTextAlign = createEvent(this, "internalTdsTextAlign", 6);
this.internalTdsHover = 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 (h("button", { class: "tds-table__header-button", onClick: () => this.sortButtonClick(), style: { justifyContent: this.textAlignState }, "aria-label": this.tdsAriaLabelSortButton }, h("span", { class: "tds-table__header-button-text" }, this.cellValue, h("slot", null)), this.sortingDirection === undefined && (h("tds-icon", { svgTitle: "sorting", class: "tds-table__header-button-icon", name: "sorting", size: "16px" })), this.sortingDirection && ['asc', 'desc'].includes(this.sortingDirection) && (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 (h("p", { class: {
'tds-table__header-text': true,
'tds-table__header-text-no-padding': this.disablePadding,
}, style: { textAlign: this.textAlignState } }, this.cellValue, 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 (h(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 this; }
static get style() { return TdsHeaderCellStyle0; }
}, [1, "tds-header-cell", {
"cellKey": [513, "cell-key"],
"cellValue": [513, "cell-value"],
"customWidth": [513, "custom-width"],
"sortable": [4],
"textAlign": [513, "text-align"],
"disablePadding": [516, "disable-padding"],
"tdsAriaLabelSortButton": [513, "tds-aria-label-sort-button"],
"textAlignState": [32],
"sortingDirection": [32],
"sortedByMyKey": [32],
"verticalDividers": [32],
"compactDesign": [32],
"noMinWidth": [32],
"multiselect": [32],
"enableToolbarDesign": [32],
"tableId": [32],
"expandableRows": [32]
}, [[16, "internalTdsPropChange", "internalTdsPropChangeListener"], [16, "internalSortButtonClicked", "updateOptionsContent"]]]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["tds-header-cell", "tds-icon"];
components.forEach(tagName => { switch (tagName) {
case "tds-header-cell":
if (!customElements.get(tagName)) {
customElements.define(tagName, TdsTableHeaderCell);
}
break;
case "tds-icon":
if (!customElements.get(tagName)) {
defineCustomElement$2();
}
break;
} });
}
defineCustomElement$1();
const TdsHeaderCell = TdsTableHeaderCell;
const defineCustomElement = defineCustomElement$1;
export { TdsHeaderCell, defineCustomElement };