@scania/tegel
Version:
Tegel Design System
212 lines (211 loc) • 7.58 kB
JavaScript
import { h, Host } from "@stencil/core";
const relevantTableProps = [
'verticalDividers',
'compactDesign',
'noMinWidth',
];
/**
* @slot <default> - <b>Unnamed slot.</b> For the cell contents.
*/
export class TdsTableBodyCell {
constructor() {
this.cellValue = undefined;
this.cellKey = undefined;
this.disablePadding = false;
this.textAlign = undefined;
this.textAlignState = undefined;
this.activeSorting = false;
this.verticalDividers = false;
this.compactDesign = false;
this.noMinWidth = false;
this.tableId = '';
}
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];
});
}
}
// Listen to headKey from table-header-element
internalTdsHoverListener(event) {
const { tableId, key } = event.detail;
if (tableId === this.tableId) {
this.activeSorting = this.cellKey === key;
}
}
// Listen to internalTdsTextAlign from table-header-element
internalTdsTextAlignListener(event) {
const [receivedID, receivedKey, receivedTextAlign] = event.detail;
if (this.tableId === receivedID) {
if (this.cellKey === receivedKey) {
if (this.textAlign) {
this.textAlignState = this.textAlign;
}
else {
this.textAlignState = ['left', 'start', 'center', 'right', 'end'].includes(receivedTextAlign)
? receivedTextAlign
: 'left';
}
}
}
}
connectedCallback() {
var _a;
this.tableEl = this.host.closest('tds-table');
this.tableId = (_a = this.tableEl) === null || _a === void 0 ? void 0 : _a.tableId;
}
componentWillLoad() {
if (this.tableEl) {
relevantTableProps.forEach((tablePropName) => {
this[tablePropName] = this.tableEl[tablePropName];
});
}
if (this.textAlign) {
this.textAlignState = this.textAlign;
}
}
render() {
let paddingStyle = 'var(--tds-spacing-element-16)'; // Default padding
if (this.disablePadding) {
paddingStyle = '0';
}
else if (this.compactDesign) {
paddingStyle = 'var(--tds-spacing-element-8) var(--tds-spacing-element-16)';
}
const dynamicStyles = {
textAlign: this.textAlignState,
// Conditionally set padding style
padding: paddingStyle,
};
return (h(Host, { key: '726912df89435650c3ad1f39729fdff7d2a497e3', class: {
'tds-table__body-cell': true,
'tds-table__body-cell--hover': this.activeSorting,
'tds-table__compact': this.compactDesign,
'tds-table--divider': this.verticalDividers,
'tds-table--no-min-width': this.noMinWidth,
}, style: dynamicStyles, role: "cell" }, this.cellValue, h("slot", { key: 'ff6fb6613f29e94552916cfa75932b670e22ea80' })));
}
static get is() { return "tds-body-cell"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["table-body-cell.scss"]
};
}
static get styleUrls() {
return {
"$": ["table-body-cell.css"]
};
}
static get properties() {
return {
"cellValue": {
"type": "any",
"mutable": false,
"complexType": {
"original": "string | number",
"resolved": "number | string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Value that will be presented as text inside a cell"
},
"attribute": "cell-value",
"reflect": true
},
"cellKey": {
"type": "any",
"mutable": false,
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Passing the same cell key for all body cells which is used in head cell enables features of text align and hovering"
},
"attribute": "cell-key",
"reflect": true
},
"disablePadding": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Disables internal padding. Useful when passing other components to cell."
},
"attribute": "disable-padding",
"reflect": true,
"defaultValue": "false"
},
"textAlign": {
"type": "string",
"mutable": false,
"complexType": {
"original": "'left' | 'start' | 'right' | 'end' | 'center'",
"resolved": "\"center\" | \"end\" | \"left\" | \"right\" | \"start\"",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Setting for text align, default value \"left\". Other accepted values are \"left\", \"start\", \"right\", \"end\" or \"center\"."
},
"attribute": "text-align",
"reflect": true
}
};
}
static get states() {
return {
"textAlignState": {},
"activeSorting": {},
"verticalDividers": {},
"compactDesign": {},
"noMinWidth": {},
"tableId": {}
};
}
static get elementRef() { return "host"; }
static get listeners() {
return [{
"name": "internalTdsPropChange",
"method": "internalTdsPropChangeListener",
"target": "body",
"capture": false,
"passive": false
}, {
"name": "internalTdsHover",
"method": "internalTdsHoverListener",
"target": "body",
"capture": false,
"passive": false
}, {
"name": "internalTdsTextAlign",
"method": "internalTdsTextAlignListener",
"target": "body",
"capture": false,
"passive": false
}];
}
}