@@ -15,6 +15,14 @@ export class TableElement extends LitElement {
15
15
return 'wc-table' ;
16
16
}
17
17
18
+ constructor ( ) {
19
+ super ( ) ;
20
+
21
+ // Set default values for properties
22
+ this . data = [ ] ;
23
+ this . backgroundColor = '' ;
24
+ }
25
+
18
26
static get properties ( ) {
19
27
return {
20
28
/**
@@ -24,6 +32,23 @@ export class TableElement extends LitElement {
24
32
* The array of data loaded into the table. Each element may be an array or object.
25
33
*/
26
34
data : { type : Array } ,
35
+
36
+ /**
37
+ * @property {Boolean } stripedRows
38
+ * @memberof TableElement
39
+ *
40
+ * Whether to offset the colour of the rows in the table
41
+ */
42
+ stripedRows : { type : Boolean } ,
43
+
44
+ /**
45
+ * @property {String } backgroundColor
46
+ * @memberof TableElement
47
+ * @default ''
48
+ *
49
+ * The colour of the background of the table. Either primary, secondary, light, dark, black or white.
50
+ */
51
+ backgroundColor : { type : String } ,
27
52
} ;
28
53
}
29
54
@@ -34,13 +59,39 @@ export class TableElement extends LitElement {
34
59
border-collapse: collapse;
35
60
border: 1px solid gray;
36
61
}
62
+ table.bg-color-primary {
63
+ background-color: var(--primary-color);
64
+ color: var(--light-color);
65
+ }
66
+ table.bg-color-secondary {
67
+ background-color: var(--secondary-color);
68
+ color: var(--light-color);
69
+ }
70
+ table.bg-color-light {
71
+ background-color: var(--light-color);
72
+ color: var(--dark-color);
73
+ }
74
+ table.bg-color-dark {
75
+ background-color: var(--dark-color);
76
+ color: var(--light-color);
77
+ }
78
+ table.bg-color-white {
79
+ background-color: var(--white-color);
80
+ color: var(--black-color);
81
+ }
82
+ table.bg-color-black {
83
+ background-color: var(--black-color);
84
+ color: var(--white-color);
85
+ }
37
86
table th {
38
- background-color: lightgray;
39
- border: 1px solid gray;
40
87
text-align: left;
88
+ backdrop-filter: var(--table-row-filter-header)
41
89
}
42
90
table td,th {
43
- padding: 2px;
91
+ padding: var(--table-cell-padding-y) var(--table-cell-padding-x);
92
+ }
93
+ table.striped-rows tbody tr:nth-child(odd) {
94
+ backdrop-filter: var(--table-row-filter-odd);
44
95
}
45
96
` ;
46
97
}
@@ -54,15 +105,15 @@ export class TableElement extends LitElement {
54
105
] ;
55
106
}
56
107
57
- renderTableHeader ( ) {
108
+ # renderTableHeader( ) {
58
109
return html `
59
110
< tr >
60
111
${ this . tableColumns . map ( ( column ) => html `< th > ${ column . name } </ th > ` ) }
61
112
</ tr >
62
113
` ;
63
114
}
64
115
65
- renderTableCell ( column , i , j ) {
116
+ # renderTableCell( column , i , j ) {
66
117
const row = this . data [ i ] ;
67
118
if ( Array . isArray ( row ) && row . length <= j ) {
68
119
return row [ j ] ;
@@ -73,21 +124,32 @@ export class TableElement extends LitElement {
73
124
return nothing ;
74
125
}
75
126
76
- renderTableRow ( i ) {
77
- return this . tableColumns . map ( ( column , j ) => html `< td > ${ this . renderTableCell ( column , i , j ) } </ td > ` ) ;
127
+ # renderTableRow( i ) {
128
+ return this . tableColumns . map ( ( column , j ) => html `< td > ${ this . # renderTableCell( column , i , j ) } </ td > ` ) ;
78
129
}
79
130
80
- renderTableRows ( ) {
131
+ # renderTableRows( ) {
81
132
return html `
82
- ${ this . data . map ( ( _ , i ) => html `< tr > ${ this . renderTableRow ( i ) } </ tr > ` ) }
133
+ ${ this . data . map ( ( _ , i ) => html `< tr > ${ this . # renderTableRow( i ) } </ tr > ` ) }
83
134
` ;
84
135
}
85
136
137
+ get className ( ) {
138
+ const classes = [ 'table' ] ;
139
+ if ( this . backgroundColor ) {
140
+ classes . push ( `bg-color-${ this . backgroundColor } ` ) ;
141
+ }
142
+ if ( this . stripedRows ) {
143
+ classes . push ( 'striped-rows' ) ;
144
+ }
145
+ return classes . join ( ' ' ) ;
146
+ }
147
+
86
148
render ( ) {
87
- const tableHeader = this . renderTableHeader ( ) ;
88
- const tableRows = this . renderTableRows ( ) ;
149
+ const tableHeader = this . # renderTableHeader( ) ;
150
+ const tableRows = this . # renderTableRows( ) ;
89
151
return html `
90
- < table >
152
+ < table class = ${ this . className || nothing } >
91
153
< thead >
92
154
${ tableHeader }
93
155
</ thead >
0 commit comments