@scania/tegel
Version:
Tegel Design System
172 lines (168 loc) • 11.8 kB
JavaScript
import { r as registerInstance, c as createEvent, h, H as Host, a as getElement } from './index-9xxNGlso.js';
import { g as generateUniqueId } from './generateUniqueId-Cn4f8w1e.js';
const tableBodyRowExpandableCss = () => `:host{box-sizing:border-box;display:contents}:host *{box-sizing:border-box}:host .tds-table__row,:host .tds-table__row-extend{display:table-row;border-bottom:1px solid var(--tds-table-divider);background-color:var(--tds-table-body-row-background);transition:background-color 200ms ease;color:var(--tds-table-color)}:host .tds-table__row--expanded{border-bottom:none}:host .tds-table__row--selected{background-color:var(--tds-table-body-row-background-selected)}:host .tds-table__row:hover,:host .tds-table__row-extend:hover{background-color:var(--tds-table-body-row-background-hover)}:host .tds-table__row--selected:hover{background-color:var(--tds-table-body-row-background-selected-hover)}:host .tds-table__body-cell--checkbox{min-width:48px;width:48px;padding:0}:host .tds-form-label--table{width:100%;height:48px;display:flex;justify-content:center;align-items:center;cursor:pointer}:host .tds-table__expand-control-container{display:flex;justify-content:center;align-items:center;height:46px;cursor:pointer;padding:0 16px;position:relative}:host .tds-table__expand-control-container .tds-table__expand-input{all:unset;top:0;left:0;width:100%;height:100%;position:absolute;cursor:pointer}:host .tds-table__expand-control-container .tds-table__expand-input:focus{outline:2px solid var(--tds-focus-outline-color);box-shadow:0 0 0 1px var(--tds-white);outline-offset:1px;z-index:1}:host .tds-table__expand-control-container .tds-expendable-row-icon{height:16px;width:16px;transition:transform 200ms ease;transform:rotate(0)}:host .tds-table__row-expand{display:none;transition:background-color 200ms ease}:host .tds-table__row-expand--expanded{border-bottom:1px solid var(--tds-table-divider)}:host .tds-table__row-expand .tds-table__cell-expand{max-width:1px;overflow:auto;padding:16px 16px 16px 66px;color:var(--tds-table-color)}:host .tds-table__row-expand .tds-table__cell-expand--overflow-visible{overflow:visible}:host .tds-table__row-expand .tds-table__cell-expand--overflow-hidden{overflow:hidden}:host(.tds-table__row-expand--active) .tds-table__row{background-color:var(--tds-table-body-row-background-selected)}:host(.tds-table__row-expand--active) .tds-table__expand-control-container .tds-expendable-row-icon{transform:rotate(180deg)}:host(.tds-table__row-expand--active) .tds-table__row-expand{background-color:var(--tds-table-body-row-background-selected);display:table-row}:host(.tds-table__compact) .tds-table__expand-control-container{height:30px}:host(.tds-table__compact) .tds-table__row-expand .tds-table__cell-expand{padding:8px 16px 8px 66px}:host(.tds-table__compact) .tds-form-label--table{height:32px}:host(.tds-table__row--hidden){display:none}:host(.tds-table--divider) .tds-table__cell-expand{border-right:1px solid var(--tds-table-divider)}:host(.tds-table--divider) .tds-table__body-cell--checkbox{border-right:1px solid var(--tds-table-divider)}`;
const relevantTableProps = [
'verticalDividers',
'compactDesign',
'noMinWidth',
'modeVariant',
'multiselect',
];
const TdsTableBodyRowExpandable = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.internalTdsRowExpanded = createEvent(this, "internalTdsRowExpanded", 6);
this.tdsChange = createEvent(this, "tdsChange", 6);
this.tdsSelect = createEvent(this, "tdsSelect", 6);
/** In case that automatic count of columns does not work, user can manually set this one.
* Take in mind that expandable control is column too */
this.colSpan = null;
/** ID for the table row. Randomly generated if not specified. */
this.rowId = generateUniqueId();
/** Controls the overflow behavior of the expandable row content */
this.overflow = 'auto';
/** Sets isExpanded state to true or false internally */
/** Enables auto-collapse of other expandable rows when one row is expanded */
this.autoCollapse = false;
/** Aria label for the expand button, providing an accessible description */
this.tdsAriaLabelExpandButton = '';
/** Marks the row as selected, used for multiselect table. */
this.selected = false;
/** Marks the row as disabled, used for multiselect table. */
this.disabled = false;
/** Sets isExpanded state to true or fals internally */
this.isExpanded = false;
this.tableId = '';
this.columnsNumber = null;
this.verticalDividers = false;
this.compactDesign = false;
this.noMinWidth = false;
this.modeVariant = null;
this.multiselect = 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];
});
}
}
handleRowExpand(event) {
/** Collapse all other rows when autoCollapse is true and a row is expanded */
if (this.autoCollapse &&
event.detail.isExpanded &&
event.detail.rowId !== this.rowId &&
event.detail.tableId === this.tableId) {
this.collapse();
}
}
watchExpanded(newValue) {
if (newValue !== this.isExpanded) {
this.isExpanded = newValue;
this.tdsChange.emit({
rowId: this.rowId,
isExpanded: this.isExpanded,
tableId: this.tableId,
});
}
}
/** Method to expand table row */
async expand() {
this.isExpanded = true;
this.tdsChange.emit({ rowId: this.rowId, isExpanded: this.isExpanded, tableId: this.tableId });
}
/** Method to collapse table row */
async collapse() {
this.isExpanded = false;
this.tdsChange.emit({ rowId: this.rowId, isExpanded: this.isExpanded, tableId: this.tableId });
}
connectedCallback() {
var _a;
/* if user did set a prop we use that as default behaviour */
if (this.expanded !== undefined) {
this.isExpanded = this.expanded;
}
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() {
var _a;
if (this.colSpan !== null) {
this.columnsNumber = this.colSpan;
}
else {
const header = (_a = this.tableEl) === null || _a === void 0 ? void 0 : _a.querySelector('tds-table-header');
if (header) {
// Start with the number of data columns (header cells from slot)
let totalColumns = header.childElementCount;
// Add 1 for the expandable row control column
totalColumns += 1;
// Add 1 if multiselect is enabled (checkbox column)
if (this.multiselect) {
totalColumns += 1;
}
this.columnsNumber = totalColumns;
}
}
}
sendValue() {
this.internalTdsRowExpanded.emit([this.tableId, this.isExpanded]);
this.tdsChange.emit({ rowId: this.rowId, isExpanded: this.isExpanded, tableId: this.tableId });
}
onChangeHandler(event) {
this.isExpanded = event.currentTarget.checked === true;
this.sendValue();
}
async handleCheckboxChange(event) {
var _a;
this.selected = event.detail.checked;
this.tdsSelect.emit({
tableId: this.tableId,
checked: this.selected,
selectedRows: await ((_a = this.tableEl) === null || _a === void 0 ? void 0 : _a.getSelectedRows()),
});
}
render() {
var _a;
return (h(Host, { key: 'a9d04614f8d871750cb2fa00b1a337d6f9e28ace', class: {
'tds-table__row': true,
'tds-table__row-expand--active': this.isExpanded,
'tds-table__compact': this.compactDesign,
'tds-table--divider': this.verticalDividers,
} }, h("tr", { key: '3793225d2c27f912c163412d60a20b02bd2371d8', id: `expandable-content-${this.rowId}`, class: {
'tds-table__row': true,
'tds-table__row--expanded': this.isExpanded,
'tds-table__row--selected': this.selected,
}, part: "row" }, this.multiselect && (h("td", { key: 'e3f677c5a981643bd962322c2af8fa5f211dfedf', class: "tds-table__body-cell tds-table__body-cell--checkbox tds-form-label tds-form-label--table" }, h("tds-checkbox", { key: 'afc00dda710e7e264c37211b4f616899939d1430', onTdsChange: (event) => this.handleCheckboxChange(event), checked: this.selected, disabled: this.disabled }))), h("td", { key: 'b6ec0d7bd81f5006cd3947b2c3381db72b879e37', class: {
'tds-table__cell-expand': true,
} }, h("label", { key: '4f509a0334697c59ee574a9913ced773923c7e8d', class: "tds-table__expand-control-container" }, h("input", { key: 'b9dc3b631f37bf872c4e0072a9bca78980d69252', class: "tds-table__expand-input", type: "checkbox", onChange: (event) => this.onChangeHandler(event), checked: this.isExpanded, "aria-expanded": this.isExpanded ? 'true' : 'false', "aria-controls": `expandable-content-${this.rowId}`, "aria-label": this.tdsAriaLabelExpandButton }), h("span", { key: 'bf6624feb9e7be71c1abf792c8ea9d9009d08ff2', class: "tds-expendable-row-icon" }, h("svg", { key: '6cd57f6ab7df0bed1a5253c4cad14c488ca9b086', fill: "none", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 32 32" }, h("path", { key: '1f06b5699c21f9ba200ceb6fe2103510061c86cb', "fill-rule": "evenodd", "clip-rule": "evenodd", d: "M4.273 9.783a1 1 0 0 1 1.415 0l9.888 9.888a.6.6 0 0 0 .848 0l9.888-9.888a1 1 0 1 1 1.415 1.414l-9.889 9.889a2.6 2.6 0 0 1-3.677 0l-9.888-9.889a1 1 0 0 1 0-1.414Z", fill: "currentColor" }))))), h("slot", { key: 'dff5aa77425f0bae12cf17bbdd2e46c4d7e54e68' })), h("tr", { key: '1e087bbf254e7fb07a7673ebef3008be850c33db', class: {
'tds-table__row-expand': true,
'tds-table__row-expand--expanded': this.isExpanded,
}, part: "expand-row" }, h("td", { key: 'bd1c81dcbadd11aca4483f481e06392d0a508a25', class: {
'tds-table__cell-expand': true,
'tds-table__cell-expand--overflow-hidden': this.overflow === 'hidden',
'tds-table__cell-expand--overflow-visible': this.overflow === 'visible',
}, part: "expand-row-cell", colSpan: (_a = this.columnsNumber) !== null && _a !== void 0 ? _a : undefined }, h("div", { key: 'c99022dc16a229a351bc800989c1b500701b8678', style: {
overflow: this.overflow,
} }, h("slot", { key: '2e8dbbb470f6ac21eee98a712a3542774d5445a5', name: "expand-row" }))))));
}
get host() { return getElement(this); }
static get watchers() { return {
"expanded": [{
"watchExpanded": 0
}]
}; }
};
TdsTableBodyRowExpandable.style = tableBodyRowExpandableCss();
export { TdsTableBodyRowExpandable as tds_table_body_row_expandable };