All files / src/components/data-grid/header/cell DataHeaderCell.ts

44.44% Statements 12/27
8.33% Branches 1/12
60% Functions 3/5
44.44% Lines 12/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1013x 3x 3x   3x     3x   3x       9x         9x                               8x   8x   8x                                                                                                                           3x
import CustomElement from "../../../../custom-element/CustomElement";
import getStyle from "../../../../custom-element/helpers/css/style/getStyle";
import defineCustomElement from "../../../../custom-element/helpers/defineCustomElement";
import { CustomElementPropertyMetadata } from "../../../../custom-element/interfaces";
import html from "../../../../renderer/html";
import { NodePatchingData } from "../../../../renderer/NodePatcher";
import DataGridFieldDescriptor from "../../DataGridFieldDescriptor";
import styles from "./DataHeaderCell.css";
 
export default class DataHeaderCell extends CustomElement {
 
    static get styles(): string {
 
        return styles as any;
    }
 
    static get properties(): Record<string, CustomElementPropertyMetadata> {
 
        return {
 
            /**
             * The descriptor of the field to render the header cell
             */
            field: {
                type: [Object, Function, String],
                required: true
            }
        };
    }
 
    render(): NodePatchingData {
 
        const {
            field,
        } = this;
 
        Eif (typeof field === 'string') {
 
            return html`${field}`;
        }
        else {
 
            const {
                name,
                display
            } = field as DataGridFieldDescriptor;
 
            if (display !== undefined) {
 
                if (typeof display === 'function') {
 
                    return this.renderCellContainer(field, display());
                }
                else {
 
                    return this.renderCellContainer(field, html`<span>${display}</span>`);
                }
            }
            else {
 
                return this.renderCellContainer(field, html`<span>${name}</span>`);
            }
        }
    }
 
    renderCellContainer(field: DataGridFieldDescriptor, display: NodePatchingData): NodePatchingData {
 
        const {
            headerStyle
        } = field as DataGridFieldDescriptor;
 
        if (headerStyle !== undefined) {
 
            const style = typeof headerStyle === 'string'?
                headerStyle :
                getStyle(headerStyle);
 
            return html`<gcl-row style=${style}>${display}${this.renderSorter()}</gcl-row>`;
        }
        else {
 
            return html`<gcl-row>${display}${this.renderSorter()}</gcl-row>`;
        }
    }
 
    renderSorter(): NodePatchingData {
 
        const {
            field
        } = this;
 
        if (field.sortable !== true) {
 
            return null;
        }
 
        return html`<gcl-sorter-tool field=${field.name}></gcl-sorter-tool>`;
    }
}
 
defineCustomElement('gcl-data-header-cell', DataHeaderCell);