Skip to content

Commit ac3b026

Browse files
committed
Almost works.
1 parent 9a95d78 commit ac3b026

File tree

9 files changed

+126
-47
lines changed

9 files changed

+126
-47
lines changed

src/pg/table/README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The `pg-table` component allows a standard way to create static tables. While it
77

88
```typescript
99
import '@pictogrammers/components/pg/table';
10-
import PgTable from '@pictogrammers/components/pg/table';
10+
import PgTable, { createTableItem } from '@pictogrammers/components/pg/table';
1111
```
1212

1313
```html
@@ -46,19 +46,22 @@ this.$table.columns = [{
4646
```typescript
4747
const IconStar = 'M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z';
4848
const IconStarOutline = 'M12,15.39L8.24,17.66L9.23,13.38L5.91,10.5L10.29,10.13L12,6.09L13.71,10.13L18.09,10.5L14.77,13.38L15.76,17.66M22,9.24L14.81,8.63L12,2L9.19,8.63L2,9.24L7.45,13.97L5.82,21L12,17.27L18.18,21L16.54,13.97L22,9.24Z';
49-
this.$table.data = [{
50-
name: 'Dipper Pines',
51-
age: 12,
52-
favorite: {
53-
type: TableCellButtonIcon,
54-
icon: IconStar,
55-
}
56-
}, {
57-
name: 'Mabel Pines',
58-
age: 12,
59-
favorite: {
60-
type: TableCellButtonIcon,
61-
icon: IconStar,
62-
}
63-
}];
49+
this.$table.data = [
50+
createTableItem({
51+
name: 'Dipper Pines',
52+
age: 12,
53+
favorite: {
54+
type: TableCellButtonIcon,
55+
icon: IconStar,
56+
}
57+
}),
58+
createTableItem({
59+
name: 'Mabel Pines',
60+
age: 12,
61+
favorite: {
62+
type: TableCellButtonIcon,
63+
icon: IconStar,
64+
}
65+
})
66+
];
6467
```
Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, Part, Prop } from '@pictogrammers/element';
2-
import PgTable from '../../table';
2+
import PgTable, { createTableItem } from '../../table';
33

44
import template from './basic.html';
55

@@ -10,6 +10,7 @@ import template from './basic.html';
1010
export default class XPgTableBasic extends HTMLElement {
1111

1212
@Part() $table: PgTable;
13+
@Part() $pushData: HTMLButtonElement;
1314

1415
connectedCallback() {
1516
// this.$table.addEventListener('click', this.handleClick.bind(this));
@@ -26,25 +27,33 @@ export default class XPgTableBasic extends HTMLElement {
2627
}];
2728
const IconStar = 'M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z';
2829
const IconStarOutline = 'M12,15.39L8.24,17.66L9.23,13.38L5.91,10.5L10.29,10.13L12,6.09L13.71,10.13L18.09,10.5L14.77,13.38L15.76,17.66M22,9.24L14.81,8.63L12,2L9.19,8.63L2,9.24L7.45,13.97L5.82,21L12,17.27L18.18,21L16.54,13.97L22,9.24Z';
29-
this.$table.data = [{
30-
name: 'Dipper Pines',
31-
age: 12,
32-
// favorite: {
33-
// type: TableCellButtonIcon,
34-
// icon: IconStar,
35-
// }
36-
}, {
37-
name: 'Mabel Pines',
38-
age: 12,
39-
// favorite: {
40-
// type: TableCellButtonIcon,
41-
// icon: IconStar,
42-
// }
43-
}];
30+
this.$table.data = [
31+
createTableItem({
32+
name: 'Dipper Pines',
33+
age: 12,
34+
// favorite: {
35+
// type: TableCellButtonIcon,
36+
// icon: IconStar,
37+
// }
38+
}),
39+
createTableItem({
40+
name: 'Mabel Pines',
41+
age: 12,
42+
// favorite: {
43+
// type: TableCellButtonIcon,
44+
// icon: IconStar,
45+
// }
46+
})
47+
];
48+
this.$pushData.addEventListener('click', this.handlePushData.bind(this));
4449
}
4550

46-
handleClick() {
47-
51+
newCount = 0;
52+
handlePushData() {
53+
this.$table.data.push({
54+
name: `new ${this.newCount++}`,
55+
age: this.newCount
56+
})
4857
}
4958

5059
}

src/pg/table/table.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,41 @@ import { Component, Prop, Part, forEach } from '@pictogrammers/element';
22

33
import PgTableColumn from '../tableColumn/tableColumn';
44
import PgTableRow from '../tableRow/tableRow';
5+
import PgTableCellText from '../tableCellText/tableCellText';
56

67
import template from './table.html';
78
import style from './table.css';
89

10+
/**
11+
* Create a table data item that can react to data changes. While cumbersome
12+
* this greatly increases performance and uses less memory.
13+
*
14+
* @param obj ex: { field1: 'value', field2: 42 }
15+
* @returns formatted object
16+
*/
17+
export function createTableItem(obj: object) {
18+
const keys = Object.keys(obj);
19+
const items: any[] = [];
20+
keys.forEach((key) => {
21+
if (typeof obj[key] === 'object' && obj[key] !== null) {
22+
if (obj[key].hasOwnProperty('type')) {
23+
obj[key].type = PgTableCellText;
24+
}
25+
items.push({
26+
key,
27+
...obj[key]
28+
});
29+
} else {
30+
items.push({
31+
key,
32+
value: `${obj[key] || ''}`,
33+
type: PgTableCellText,
34+
});
35+
}
36+
});
37+
return { items };
38+
}
39+
940
@Component({
1041
selector: 'pg-table',
1142
style,
@@ -18,6 +49,8 @@ export default class PgTable extends HTMLElement {
1849
@Part() $columns: HTMLDivElement;
1950
@Part() $rows: HTMLDivElement;
2051

52+
trackedData = [];
53+
2154
connectedCallback() {
2255
forEach({
2356
container: this.$columns,
@@ -27,13 +60,7 @@ export default class PgTable extends HTMLElement {
2760
forEach({
2861
container: this.$rows,
2962
items: this.data,
30-
type: () => PgTableRow,
63+
type: () => PgTableRow
3164
});
3265
}
33-
34-
render(changes) {
35-
if (changes.data) {
36-
console.log(this.data);
37-
}
38-
}
39-
}
66+
}

src/pg/tableCellText/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `PgTableCellText`
2+
3+
See `PgTable`.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:host {
2+
display: contents;
3+
}
4+
5+
[part=cells] {
6+
display: table-row;
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div part="label"></div>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Component, Prop, Part } from '@pictogrammers/element';
2+
3+
import template from './tableCellText.html';
4+
import style from './tableCellText.css';
5+
6+
@Component({
7+
selector: 'pg-table-cell-text',
8+
style,
9+
template
10+
})
11+
export default class PgTableCellText extends HTMLElement {
12+
@Prop() value: string = '';
13+
@Prop() key: string = '';
14+
15+
@Part() $label: HTMLDivElement;
16+
17+
render(changes) {
18+
if (changes.value) {
19+
this.$label.textContent = this.value;
20+
}
21+
}
22+
}

src/pg/tableRow/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# `PgTableColumn`
1+
# `PgTableRow`
22

3-
See `PgTableRow`.
3+
See `PgTable`.

src/pg/tableRow/tableRow.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Component, Prop, Part } from '@pictogrammers/element';
1+
import { Component, Prop, Part, forEach } from '@pictogrammers/element';
2+
3+
import PgTableCellText from '../tableCellText/tableCellText';
24

35
import template from './tableRow.html';
46
import style from './tableRow.css';
@@ -9,12 +11,17 @@ import style from './tableRow.css';
911
template
1012
})
1113
export default class PgTableRow extends HTMLElement {
12-
@Prop() label: string = '';
14+
@Prop() items: any = [];
1315
@Prop() key: string = '';
1416

1517
@Part() $cells: HTMLDivElement;
1618

1719
connectedCallback() {
18-
20+
forEach({
21+
container: this.$cells,
22+
items: this.items,
23+
type: (item) => item.type ? item.type : PgTableCellText,
24+
});
25+
console.log(this.items);
1926
}
2027
}

0 commit comments

Comments
 (0)