UNPKG

@scania/tegel

Version:
144 lines (140 loc) 10.3 kB
import { r as registerInstance, c as createEvent, h, H as Host, a as getElement } from './index-9xxNGlso.js'; const tableHeaderCellCss = () => `:host{display:contents}:host(.tds-table__header-cell) th{box-sizing:border-box;font:var(--tds-headline-07);letter-spacing:var(--tds-headline-07-ls);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) th *{box-sizing:border-box}:host(.tds-table__header-cell) th .tds-table__header-text{padding:8px 16px;margin:0}:host(.tds-table__header-cell) th .tds-table__header-text-no-padding{padding:0}:host(.tds-table__header-cell) th:hover{background-color:var(--tds-table-header-background-hover)}:host(.tds-table__header-cell--sortable) th .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) th .tds-table__header-button:focus{outline:2px solid var(--tds-table-header-btn-focus);outline-offset:-2px}:host(.tds-table__header-cell--sortable) th .tds-table__header-button:focus .tds-table__header-button-icon{opacity:1}:host(.tds-table__header-cell--sortable) th .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) th .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) th .tds-table__header-button:hover .tds-table__header-button-icon{opacity:1}:host(.tds-table__header-cell--is-sorted) th .tds-table__header-button{background-color:var(--tds-table-header-btn-background)}:host(.tds-table__header-cell--is-sorted) th .tds-table__header-button:hover{background-color:var(--tds-table-header-btn-background-hover)}:host(.tds-table__header-cell--is-sorted) th .tds-table__header-button .tds-table__header-button-icon{opacity:1}:host(.tds-table__header-cell--is-sorted) th .tds-table__header-button .tds-table__header-button-icon--rotate{transform:rotate(180deg)}:host(.tds-table--compact) th{height:33px}:host(.tds-table--divider) th{border-right:1px solid var(--tds-table-divider)}:host(.tds-table--divider:last-of-type) th{border-right:none}:host(.tds-table--no-min-width) th{min-width:unset}:host(.tds-table--no-padding) th{height:unset}:host(.tds-table__header-cell:first-of-type) th{border-top-left-radius:4px}:host(.tds-table__header-cell:last-of-type) th{border-top-right-radius:4px}:host(.tds-table--extra-column:first-of-type) th{border-top-left-radius:0}:host(.tds-table--toolbar-available) th{border-radius:0}:host(.tds-table--extra-column.tds-table--toolbar-available) th{border-radius:0}:host(.tds-table__header-cell.tds-table--toolbar-available:first-of-type) th{border-top-left-radius:0}:host(.tds-table__header-cell.tds-table--toolbar-available:last-of-type) th{border-top-right-radius:0}`; const relevantTableProps = [ 'multiselect', 'expandableRows', 'compactDesign', 'noMinWidth', 'verticalDividers', ]; const TdsTableHeaderCell = class { constructor(hostRef) { registerInstance(this, hostRef); this.tdsSort = createEvent(this, "tdsSort", 7); this.internalSortButtonClicked = createEvent(this, "internalSortButtonClicked", 6); this.internalTdsTextAlign = createEvent(this, "internalTdsTextAlign", 6); this.internalTdsHover = createEvent(this, "internalTdsHover", 6); /** Enables sorting on that column */ this.sortable = false; /** Setting for text align, default is "left". Other accepted values are "left", "start", "right" or "end". */ this.textAlign = 'left'; /** Disables internal padding. Useful when passing other components to cell. */ this.disablePadding = false; /** Aria label for the sort button, providing an accessible description */ this.tdsAriaLabelSortButton = ''; this.textAlignState = 'left'; this.sortedByMyKey = false; this.verticalDividers = false; this.compactDesign = false; this.noMinWidth = false; this.multiselect = false; this.enableToolbarDesign = false; this.tableId = ''; this.expandableRows = false; 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: key !== null && key !== void 0 ? key : '', }); }; } 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() { var _a; this.tableEl = this.host.closest('tds-table'); this.tableId = (_a = this.tableEl) === null || _a === void 0 ? void 0 : _a.tableId; } componentWillLoad() { relevantTableProps.forEach((tablePropName) => { var _a; this[tablePropName] = (_a = this.tableEl) === null || _a === void 0 ? void 0 : _a[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]); const closesetTable = this.host.closest('tds-table'); if (closesetTable) { this.enableToolbarDesign = closesetTable.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: 'be64d88104f52df5d328b61a052eb489bcc01b72', 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, } }, h("th", { key: '5ce91b5905d96bf9be4061a2b7a53c1af18fc390', style: { minWidth: this.customWidth }, onMouseOver: () => this.onHeadCellHover(this.cellKey), onFocus: () => this.onHeadCellHover(this.cellKey), onMouseLeave: () => this.onHeadCellHover(''), onBlur: () => this.onHeadCellHover(''), "aria-sort": this.getAriaSort(), colSpan: this.colSpan, rowSpan: this.rowSpan }, this.headerCellContent()))); } get host() { return getElement(this); } }; TdsTableHeaderCell.style = tableHeaderCellCss(); export { TdsTableHeaderCell as tds_header_cell };