Skip to content

Commit 669bb7e

Browse files
committed
Table.
1 parent ac3b026 commit 669bb7e

File tree

10 files changed

+160
-19
lines changed

10 files changed

+160
-19
lines changed

src/pg/table/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
The `pg-table` component allows a standard way to create static tables. While it has features like the datatable it is much more lightweight lacking features like column resize and edit.
44

55
- `TableHeaderText`
6-
- `TableHeader`
6+
- `TableCellText`
7+
- `TableCellButton`
8+
- `TableCellButtonIcon`
79

810
```typescript
911
import '@pictogrammers/components/pg/table';
@@ -65,3 +67,24 @@ this.$table.data = [
6567
})
6668
];
6769
```
70+
71+
## Events
72+
73+
All events dispatched will be the same `action` name. This allows the insert of `index` as it bubbles to the parent `this.$table` element.
74+
75+
```typescript
76+
this.$table.addEventListener('action', (e: any) => {
77+
const { getColumn, value } = e.detail;
78+
getColumn('field1').value = value;
79+
});
80+
```
81+
82+
From a custom cell component...
83+
84+
```typescript
85+
this.dispatchEvent(new CustomEvent('action', {
86+
detail {
87+
other: 'new value',
88+
},
89+
}))
90+
```

src/pg/table/__examples__/basic/basic.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
<pg-table part="table"></pg-table>
33
<div>
44
<button part="pushData">Push Data</button>
5+
<button part="deleteLast">Delete Last</button>
6+
</div>
7+
<div>
8+
<code>.getCSV()</code>: <pre part="output"></pre>
59
</div>
610
</div>
Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { Component, Part, Prop } from '@pictogrammers/element';
2+
23
import PgTable, { createTableItem } from '../../table';
4+
import PgTableCellButtonIcon from '../../../tableCellButtonIcon/tableCellButtonIcon';
35

46
import template from './basic.html';
57

8+
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';
9+
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';
10+
611
@Component({
712
selector: 'x-pg-table-basic',
813
template
@@ -11,6 +16,8 @@ export default class XPgTableBasic extends HTMLElement {
1116

1217
@Part() $table: PgTable;
1318
@Part() $pushData: HTMLButtonElement;
19+
@Part() $deleteLast: HTMLButtonElement;
20+
@Part() $output: HTMLPreElement;
1421

1522
connectedCallback() {
1623
// this.$table.addEventListener('click', this.handleClick.bind(this));
@@ -25,35 +32,57 @@ export default class XPgTableBasic extends HTMLElement {
2532
key: 'favorite',
2633
hideLabel: true,
2734
}];
28-
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';
29-
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';
3035
this.$table.data = [
3136
createTableItem({
3237
name: 'Dipper Pines',
3338
age: 12,
34-
// favorite: {
35-
// type: TableCellButtonIcon,
36-
// icon: IconStar,
37-
// }
39+
favorite: {
40+
type: PgTableCellButtonIcon,
41+
icon: IconStarOutline,
42+
}
3843
}),
3944
createTableItem({
4045
name: 'Mabel Pines',
4146
age: 12,
42-
// favorite: {
43-
// type: TableCellButtonIcon,
44-
// icon: IconStar,
45-
// }
47+
favorite: {
48+
type: PgTableCellButtonIcon,
49+
icon: IconStarOutline,
50+
}
4651
})
4752
];
4853
this.$pushData.addEventListener('click', this.handlePushData.bind(this));
54+
this.$deleteLast.addEventListener('click', this.handleDeleteLast.bind(this));
55+
this.$table.addEventListener('action', (e: any) => {
56+
const { getColumn } = e.detail;
57+
getColumn('favorite').value = !getColumn('favorite').value;
58+
if (getColumn('favorite').value) {
59+
getColumn('favorite').icon = IconStar;
60+
} else {
61+
getColumn('favorite').icon = IconStarOutline;
62+
}
63+
// CSV
64+
this.$output.textContent = this.$table.getCSV();
65+
});
4966
}
5067

5168
newCount = 0;
5269
handlePushData() {
53-
this.$table.data.push({
70+
this.$table.data.push(createTableItem({
5471
name: `new ${this.newCount++}`,
55-
age: this.newCount
56-
})
72+
age: this.newCount,
73+
favorite: {
74+
type: PgTableCellButtonIcon,
75+
icon: IconStarOutline,
76+
},
77+
}));
78+
// CSV
79+
this.$output.textContent = this.$table.getCSV();
80+
}
81+
82+
handleDeleteLast() {
83+
this.$table.data.pop();
84+
// CSV
85+
this.$output.textContent = this.$table.getCSV();
5786
}
5887

5988
}

src/pg/table/table.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function createTableItem(obj: object) {
1919
const items: any[] = [];
2020
keys.forEach((key) => {
2121
if (typeof obj[key] === 'object' && obj[key] !== null) {
22-
if (obj[key].hasOwnProperty('type')) {
22+
if (!obj[key].hasOwnProperty('type')) {
2323
obj[key].type = PgTableCellText;
2424
}
2525
items.push({
@@ -60,7 +60,28 @@ export default class PgTable extends HTMLElement {
6060
forEach({
6161
container: this.$rows,
6262
items: this.data,
63-
type: () => PgTableRow
63+
type: () => PgTableRow,
64+
create: ($item, item) => {
65+
$item.addEventListener('action', (e: any) => {
66+
e.stopPropagation();
67+
this.dispatchEvent(new CustomEvent('action', {
68+
detail: e.detail,
69+
}));
70+
})
71+
}
6472
});
6573
}
74+
75+
getCSV() {
76+
let text = this.columns.map((column) => {
77+
return column.label;
78+
}).join(',');
79+
text += '\n';
80+
text += this.data.map(({ items }) => {
81+
return items.map((item) => {
82+
return item.value;
83+
}).join(',')
84+
}).join('\n');
85+
return text;
86+
}
6687
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `PgTableCellButtonIcon`
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=label] {
6+
display: table-cell;
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<pg-button part="button">
2+
<pg-icon part="icon"></pg-icon>
3+
</pg-button>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Component, Prop, Part } from '@pictogrammers/element';
2+
3+
import PgButton from '../button/button';
4+
import PgIcon from '../icon/icon';
5+
6+
import template from './tableCellButtonIcon.html';
7+
import style from './tableCellButtonIcon.css';
8+
9+
@Component({
10+
selector: 'pg-table-cell-button-icon',
11+
style,
12+
template
13+
})
14+
export default class PgTableCellButtonIcon extends HTMLElement {
15+
@Prop() value: boolean = false;
16+
@Prop() icon: string = '';
17+
18+
@Part() $button: PgButton;
19+
@Part() $icon: PgIcon;
20+
21+
connectedCallback() {
22+
this.$button.addEventListener('click', () => {
23+
this.dispatchEvent(new CustomEvent('action', {
24+
detail: {},
25+
}));
26+
});
27+
}
28+
29+
render(changes) {
30+
if (changes.icon) {
31+
this.$icon.path = this.icon;
32+
}
33+
}
34+
}

src/pg/tableCellText/tableCellText.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
display: contents;
33
}
44

5-
[part=cells] {
6-
display: table-row;
5+
[part=label] {
6+
display: table-cell;
77
}

src/pg/tableRow/tableRow.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import style from './tableRow.css';
1111
template
1212
})
1313
export default class PgTableRow extends HTMLElement {
14+
@Prop() index: number;
1415
@Prop() items: any = [];
1516
@Prop() key: string = '';
1617

@@ -21,7 +22,23 @@ export default class PgTableRow extends HTMLElement {
2122
container: this.$cells,
2223
items: this.items,
2324
type: (item) => item.type ? item.type : PgTableCellText,
25+
create: ($item) => {
26+
$item.addEventListener('action', (e: any) => {
27+
e.stopPropagation();
28+
this.dispatchEvent(new CustomEvent('action', {
29+
detail: {
30+
...e.detail,
31+
index: this.index,
32+
getRow() {
33+
return this.items;
34+
},
35+
getColumn: (key: string) => {
36+
return this.items.find(x => x.key === key);
37+
}
38+
},
39+
}));
40+
});
41+
},
2442
});
25-
console.log(this.items);
2643
}
2744
}

0 commit comments

Comments
 (0)