@scania/tegel
Version:
Tegel Design System
356 lines (354 loc) • 14.2 kB
JavaScript
import { h, Host, } from "@stencil/core";
import generateUniqueId from "../../../utils/generateUniqueId";
const relevantTableProps = [
'verticalDividers',
'compactDesign',
'noMinWidth',
'modeVariant',
];
/**
* @slot <default> - <b>Unnamed slot.</b> For the cells.
* @slot expand-row - Slot for the expanded row.
* @part row - Selector for the main row of the table.
* @part expand-row - Selector for the expanded row of the table.
* @part expand-row-cell - Selector for the cell in the expanded row of the table.
*/
export class TdsTableBodyRowExpandable {
constructor() {
this.colSpan = null;
this.rowId = generateUniqueId();
this.expanded = undefined;
this.overflow = 'auto';
this.autoCollapse = false;
this.tdsAriaLabelExpandButton = '';
this.isExpanded = false;
this.tableId = '';
this.columnsNumber = null;
this.verticalDividers = false;
this.compactDesign = false;
this.noMinWidth = false;
this.modeVariant = null;
}
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() {
/* 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 = this.tableEl.tableId;
}
componentWillLoad() {
relevantTableProps.forEach((tablePropName) => {
this[tablePropName] = this.tableEl[tablePropName];
});
}
componentWillRender() {
if (this.colSpan !== null) {
this.columnsNumber = this.colSpan;
}
else {
this.columnsNumber = this.tableEl.querySelector('tds-table-header').childElementCount + 1;
}
}
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();
}
render() {
return (h(Host, { key: '36be80e7cd0916cb8e196f52676281fcabc1996e', 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: '868377d055d9d66fcf1824ed811e4a56f67b79e0', id: `expandable-content-${this.rowId}`, class: {
'tds-table__row': true,
'tds-table__row--expanded': this.isExpanded,
}, part: "row" }, h("td", { key: '059858ff5293deda48d039fdc81dd4a5d8ae6fce', class: {
'tds-table__cell-expand': true,
} }, h("label", { key: '6e05d93a509cb77cb3fd76f3d869b820b5d83163', class: "tds-table__expand-control-container" }, h("input", { key: 'ed7091d4dfdac43c082ee63548c7bff2e87379dd', 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: '84622275a00b28685834e0de8552f5c7a36bc983', class: "tds-expendable-row-icon" }, h("svg", { key: '633d1030453895343295b30142410ece7f15dec3', fill: "none", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 32 32" }, h("path", { key: 'e4d21588ca0bb6fbcd68af3a7afb48bafe180bc1', "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: '5c66154a24fe8d21bb6b9d91930ad7efd4cb1ec5' })), h("tr", { key: '1417c7143b8aaafbb2143557e53443cde9afcbde', class: {
'tds-table__row-expand': true,
'tds-table__row-expand--expanded': this.isExpanded,
}, part: "expand-row" }, h("td", { key: 'b72d532f22b5c7c0bc7c7b23caf5887d4228b9d0', 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: this.columnsNumber }, h("div", { key: '256a827b43a8099b9ea4e5a5a43023ff9bf0b828', style: {
overflow: this.overflow,
} }, h("slot", { key: '6bbc0eeca7b7a654da9c1b7da6992aa8928e1974', name: "expand-row" }))))));
}
static get is() { return "tds-table-body-row-expandable"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["table-body-row-expandable.scss"]
};
}
static get styleUrls() {
return {
"$": ["table-body-row-expandable.css"]
};
}
static get properties() {
return {
"colSpan": {
"type": "number",
"mutable": false,
"complexType": {
"original": "number",
"resolved": "number",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "In case that automatic count of columns does not work, user can manually set this one.\nTake in mind that expandable control is column too"
},
"attribute": "col-span",
"reflect": false,
"defaultValue": "null"
},
"rowId": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "ID for the table row. Randomly generated if not specified."
},
"attribute": "row-id",
"reflect": true,
"defaultValue": "generateUniqueId()"
},
"expanded": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Sets isExpanded state to true or false externally"
},
"attribute": "expanded",
"reflect": true
},
"overflow": {
"type": "string",
"mutable": false,
"complexType": {
"original": "'auto' | 'hidden' | 'visible'",
"resolved": "\"auto\" | \"hidden\" | \"visible\"",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Controls the overflow behavior of the expandable row content"
},
"attribute": "overflow",
"reflect": true,
"defaultValue": "'auto'"
},
"autoCollapse": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Enables auto-collapse of other expandable rows when one row is expanded"
},
"attribute": "auto-collapse",
"reflect": false,
"defaultValue": "false"
},
"tdsAriaLabelExpandButton": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Aria label for the expand button, providing an accessible description"
},
"attribute": "tds-aria-label-expand-button",
"reflect": true,
"defaultValue": "''"
}
};
}
static get states() {
return {
"isExpanded": {},
"tableId": {},
"columnsNumber": {},
"verticalDividers": {},
"compactDesign": {},
"noMinWidth": {},
"modeVariant": {}
};
}
static get events() {
return [{
"method": "internalTdsRowExpanded",
"name": "internalTdsRowExpanded",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": "Sends out expanded status which is used by the Table header component"
}],
"text": ""
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}, {
"method": "tdsChange",
"name": "tdsChange",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Sends unique table row identifier and isExpanded status when it is expanded/collapsed."
},
"complexType": {
"original": "{\n rowId: string;\n isExpanded: boolean;\n tableId: string;\n }",
"resolved": "{ rowId: string; isExpanded: boolean; tableId: string; }",
"references": {}
}
}];
}
static get methods() {
return {
"expand": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global",
"id": "global::Promise"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Method to expand table row",
"tags": []
}
},
"collapse": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global",
"id": "global::Promise"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "Method to collapse table row",
"tags": []
}
}
};
}
static get elementRef() { return "host"; }
static get watchers() {
return [{
"propName": "expanded",
"methodName": "watchExpanded"
}];
}
static get listeners() {
return [{
"name": "internalTdsTablePropChange",
"method": "internalTdsPropChangeListener",
"target": "body",
"capture": false,
"passive": false
}, {
"name": "tdsChange",
"method": "handleRowExpand",
"target": "body",
"capture": false,
"passive": false
}];
}
}