@scania/tegel
Version:
Tegel Design System
147 lines (146 loc) • 5.09 kB
JavaScript
import { h, Host } from "@stencil/core";
const relevantTableProps = ['compactDesign'];
export class TdsTableHeaderInputWrapper {
constructor() {
/** Controls if the search icon is shown */
this.showIcon = true;
this.renderSlot = true;
this.inputFocused = false;
this.compactDesign = 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') {
console.error(`Table prop is not supported: ${changedProp}`); // More informative error
throw new Error(`Table prop is not supported: ${changedProp}`);
}
this[changedProp] = event.detail[changedProp];
});
}
}
connectedCallback() {
const tableEl = this.host.closest('tds-table');
if (tableEl) {
this.tableId = tableEl.getAttribute('table-id');
}
else {
console.error('Failed to find parent tds-table element.');
}
}
componentWillLoad() {
const tableEl = this.host.closest('tds-table');
if (tableEl) {
relevantTableProps.forEach((tablePropName) => {
this[tablePropName] = tableEl[tablePropName];
});
}
else {
console.error('Failed to find parent tds-table element.');
}
}
handleSlotChange() {
this.validateSlot();
}
validateSlot() {
const children = Array.from(this.host.children).filter((element) => element.tagName === 'INPUT');
if (children.length !== 1) {
console.warn('TABLE-HEADER-INPUT-WRAPPER: Wrapper only accepts input as children.');
this.renderSlot = false;
}
else {
if (!this.renderSlot)
this.renderSlot = true;
const input = children[0];
input.addEventListener('focus', () => {
this.inputFocused = true;
});
input.addEventListener('blur', () => {
this.inputFocused = false;
});
}
}
render() {
return (h(Host, { key: '84cc720addbfd6252297b0fd67114f7dfa920cc9', class: {
'focused-input-wrapper': this.inputFocused,
'show-icon': this.showIcon,
'tds-table__compact': this.compactDesign,
} }, this.renderSlot ? h("slot", { onSlotchange: () => this.handleSlotChange() }) : null, this.showIcon ? (h("tds-icon", { svgTitle: "search", class: "search-icon", slot: "icon", size: "16px", name: "search" })) : null));
}
static get is() { return "tds-table-header-input-wrapper"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["table-header-input-wrapper.scss"]
};
}
static get styleUrls() {
return {
"$": ["table-header-input-wrapper.css"]
};
}
static get properties() {
return {
"showIcon": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Controls if the search icon is shown"
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "show-icon",
"defaultValue": "true"
},
"compactDesign": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": ""
},
"getter": false,
"setter": false,
"reflect": false,
"attribute": "compact-design",
"defaultValue": "false"
}
};
}
static get states() {
return {
"renderSlot": {},
"inputFocused": {},
"tableId": {}
};
}
static get elementRef() { return "host"; }
static get listeners() {
return [{
"name": "internalTdsTablePropChange",
"method": "internalTdsPropChangeListener",
"target": "body",
"capture": false,
"passive": false
}];
}
}