Skip to content

Commit e5997bf

Browse files
committed
Make all the benchmarks work/pass for Fast
1 parent 924bfe7 commit e5997bf

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

frameworks/keyed/fast/src/App.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const template = html<BenchmarkApp>`
2424
html`
2525
<data-table
2626
:rows=${x => x.rows}
27+
:selectedRowId=${x => x.selectedRowId}
2728
@action=${(x, c) => {
2829
x.onAction(c.event);
2930
}}
@@ -46,6 +47,7 @@ const template = html<BenchmarkApp>`
4647
})
4748
export class BenchmarkApp extends FASTElement {
4849
@observable rows?: RowItem[];
50+
backupData?: RowItem[];
4951

5052
createOneThousandRows() {
5153
this.clear();
@@ -57,8 +59,7 @@ export class BenchmarkApp extends FASTElement {
5759
}
5860

5961
appendOneThousandRows() {
60-
const lastRowId = this.rows ? this.rows[this.rows.length - 1].id : 0;
61-
this.rows = this.rows ? this.rows.concat(buildData(1000, lastRowId)) : buildData();
62+
this.rows = this.rows ? this.rows.concat(buildData(1000)) : buildData();
6263
}
6364

6465
updateEveryTenthRowLabel() {
@@ -101,13 +102,13 @@ export class BenchmarkApp extends FASTElement {
101102
const eventDetails = (event as CustomEvent).detail;
102103
const { name, data } = eventDetails;
103104

104-
if (name === 'run') return this.createOneThousandRows();
105-
if (name === 'runlots') return this.createTenThousandRows();
106105
if (name === 'add') return this.appendOneThousandRows();
106+
if (name === 'run') return this.createOneThousandRows();
107107
if (name === 'update') return this.updateEveryTenthRowLabel();
108+
if (name === 'runlots') return this.createTenThousandRows();
108109
if (name === 'clear') return this.clear();
109110
if (name === 'swaprows') return this.swapTwoRows();
110-
if (name === 'deleteRow') return this.deleteSingleRow(data);
111+
if (name === 'deleteSingleRow') return this.deleteSingleRow(data);
111112

112113
throw new Error('unknown event name!');
113114
}

frameworks/keyed/fast/src/components/Table.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { customElement, FASTElement, html, repeat, observable } from '@microsoft
22
import { RowItem } from 'src/utils/build-dummy-data';
33

44
const template = html<Table>`
5-
<table class="table table-hover table-striped test-data">
5+
<table class="table table-hover table-striped test-data" @click=${(x, c) => x.handleClick(c.event)}>
66
<tbody id="tbody">
77
${repeat(
88
x => x.rows,
99
html`
10-
<tr data-id="${row => row.id}">
10+
<tr data-id="${row => row.id}" class="${(row, c) => (c.parent.selectedRowId === row.id ? 'danger' : '')}">
1111
<td class="col-md-1">${row => row.id}</td>
1212
<td class="col-md-4">
13-
<a class="lbl">${row => row.label}</a>
13+
<a class="lbl" data-row-id="${row => row.id}" }>${row => row.label}</a>
1414
</td>
1515
<td class="col-md-1">
16-
<a class="remove" data-row-id="${row => row.id}" @click=${(x, c) => c.parent.handleClick(c.event)}>
16+
<a class="remove" data-row-id="${row => row.id}">
1717
<span class="remove glyphicon glyphicon-remove" aria-hidden="true"></span
1818
></a>
1919
</td>
@@ -23,13 +23,13 @@ const template = html<Table>`
2323
/**
2424
* List Rendering without view recycling
2525
*
26-
* With positioning set to true, and resycle set to false,
27-
* Fast wil re-render when internal properties of
26+
* With recycle set to false,
27+
* Fast will re-render when internal properties of
2828
* RowItem[] change (e.g. id or label)
2929
*
3030
* https://www.fast.design/docs/fast-element/using-directives
3131
*/
32-
{ positioning: true, recycle: false }
32+
{ recycle: false }
3333
)}
3434
</tbody>
3535
</table>
@@ -49,13 +49,28 @@ const template = html<Table>`
4949
})
5050
export class Table extends FASTElement {
5151
@observable rows!: RowItem[];
52+
@observable selectedRowId = -1;
5253

5354
handleClick(event: Event) {
54-
const currentTarget = event.currentTarget as HTMLElement;
55+
const target = event.target as HTMLElement;
5556

56-
if (currentTarget.classList.contains('remove')) {
57-
const rowId = currentTarget.dataset['rowId'];
58-
this.$emit('action', { name: 'deleteRow', data: Number(rowId) });
57+
if (target.classList.contains('remove')) {
58+
const rowId = target.parentElement?.dataset['rowId'];
59+
this.$emit('action', { name: 'deleteSingleRow', data: Number(rowId) });
60+
return;
61+
}
62+
63+
if (target.classList.contains('lbl')) {
64+
const rowId = target.dataset['rowId'];
65+
this.toggleSelectRow(Number(rowId));
66+
return;
67+
}
68+
}
69+
70+
toggleSelectRow(rowId: number) {
71+
const rowIndex = this.rows.findIndex(row => row.id === rowId);
72+
if (rowIndex > -1) {
73+
this.selectedRowId = rowIndex + 1;
5974
}
6075
}
6176
}

frameworks/keyed/fast/src/utils/build-dummy-data.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export function _random(max: number) {
77
return Math.round(Math.random() * 1000) % max;
88
}
99

10-
export function buildData(count = 1000, lastRowId = 0): RowItem[] {
10+
let id = 1;
11+
12+
export function buildData(count = 1000): RowItem[] {
1113
const adjectives = [
1214
'pretty',
1315
'large',
@@ -53,9 +55,8 @@ export function buildData(count = 1000, lastRowId = 0): RowItem[] {
5355
'keyboard'
5456
];
5557
const data = [];
56-
let id = lastRowId + 1;
5758

58-
for (let i = lastRowId; i < lastRowId + count; i++) {
59+
for (let i = 0; i < count; i++) {
5960
const adjective = adjectives[_random(adjectives.length)];
6061
const color = colors[_random(colors.length)];
6162
const noun = nouns[_random(nouns.length)];

0 commit comments

Comments
 (0)