Skip to content

Commit bc9ae25

Browse files
committed
Upated tables
1 parent 6af8866 commit bc9ae25

File tree

3 files changed

+80
-17
lines changed

3 files changed

+80
-17
lines changed

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<h1>Tables</h1>
5252

5353
<!-- Table of data -->
54-
<wc-table id="table">Table of Fruit</wc-table>
54+
<wc-table id="table" backgroundColor="black" stripedRows>Table of Fruit</wc-table>
5555
<script>
5656
document.getElementById('table').data = [
5757
{ fruit: 'apple', color: 'green', weight: '100gr' },

src/table/TableElement.js

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export class TableElement extends LitElement {
1515
return 'wc-table';
1616
}
1717

18+
constructor() {
19+
super();
20+
21+
// Set default values for properties
22+
this.data = [];
23+
this.backgroundColor = '';
24+
}
25+
1826
static get properties() {
1927
return {
2028
/**
@@ -24,6 +32,23 @@ export class TableElement extends LitElement {
2432
* The array of data loaded into the table. Each element may be an array or object.
2533
*/
2634
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 },
2752
};
2853
}
2954

@@ -34,13 +59,39 @@ export class TableElement extends LitElement {
3459
border-collapse: collapse;
3560
border: 1px solid gray;
3661
}
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+
}
3786
table th {
38-
background-color: lightgray;
39-
border: 1px solid gray;
4087
text-align: left;
88+
backdrop-filter: var(--table-row-filter-header)
4189
}
4290
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);
4495
}
4596
`;
4697
}
@@ -54,15 +105,15 @@ export class TableElement extends LitElement {
54105
];
55106
}
56107

57-
renderTableHeader() {
108+
#renderTableHeader() {
58109
return html`
59110
<tr>
60111
${this.tableColumns.map((column) => html`<th>${column.name}</th>`)}
61112
</tr>
62113
`;
63114
}
64115

65-
renderTableCell(column, i, j) {
116+
#renderTableCell(column, i, j) {
66117
const row = this.data[i];
67118
if (Array.isArray(row) && row.length <= j) {
68119
return row[j];
@@ -73,21 +124,32 @@ export class TableElement extends LitElement {
73124
return nothing;
74125
}
75126

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>`);
78129
}
79130

80-
renderTableRows() {
131+
#renderTableRows() {
81132
return html`
82-
${this.data.map((_, i) => html`<tr>${this.renderTableRow(i)}</tr>`)}
133+
${this.data.map((_, i) => html`<tr>${this.#renderTableRow(i)}</tr>`)}
83134
`;
84135
}
85136

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+
86148
render() {
87-
const tableHeader = this.renderTableHeader();
88-
const tableRows = this.renderTableRows();
149+
const tableHeader = this.#renderTableHeader();
150+
const tableRows = this.#renderTableRows();
89151
return html`
90-
<table>
152+
<table class=${this.className || nothing}>
91153
<thead>
92154
${tableHeader}
93155
</thead>

src/table/table.css

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
:root {
2+
--table-cell-padding-x: calc(var(--spacer) * 0.2);
3+
--table-cell-padding-y: calc(var(--spacer) * 0.2);
14

2-
table {
3-
width: 100%;
4-
border-collapse: collapse;
5-
border: 1px solid var(--grey-10-color);
5+
--table-row-filter-header: brightness(50%);
6+
--table-row-filter-odd: brightness(90%);
67
}

0 commit comments

Comments
 (0)