Skip to content

Commit 1428430

Browse files
committed
refactor: enhance virtual list and form components with improved styling and data binding
1 parent cf3682a commit 1428430

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

app/form/form.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template id="list-item">
22
<li class="list-item">
3-
<span>__name__</span>
4-
<span>__age__</span>
5-
<span>__email__</span>
3+
<span class="name">__name__</span>
4+
<span class="age">__age__</span>
5+
<span class="email">__email__</span>
66
</li>
77
</template>
88

app/form/form.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export default class FormView extends HTMLElement {
2929
ComponentModule.on_ready({
3030
element: virtualList,
3131
callback: () => {
32-
virtualList.load(data, template, 32, this.#inflate.bind(this));
32+
const listItemStyle = import.meta.url.replace("form.js", "list-items.css");
33+
virtualList.load(data, template, 32, this.#inflate.bind(this), [listItemStyle]);
3334
}
3435
})
3536

@@ -41,7 +42,9 @@ export default class FormView extends HTMLElement {
4142
}
4243

4344
#inflate(element, data) {
44-
45+
element.querySelector(".name").textContent = data.name;
46+
element.querySelector(".age").textContent = data.age;
47+
element.querySelector(".email").textContent = data.email;
4548
}
4649

4750
async #click(event) {

app/form/list-items.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.list-item {
2+
display: flex;
3+
flex-direction: row;
4+
height: 2rem;
5+
align-items: center;
6+
gap: var(--padding);
7+
box-sizing: border-box;
8+
padding: 0 var(--padding);
9+
}
10+
11+
.list-item .name {
12+
font-weight: bold;
13+
width: 5rem;
14+
}
15+
16+
.list-item .age {
17+
flex: 1;
18+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const HTML = `
22
<link rel="stylesheet" href="__css__" />
33
4-
<ul role="list"></ul>
4+
<ul id="container" role="list"></ul>
55
`.replace("__css__", import.meta.url.replace(".html.js", ".css"));

components/virtual-list/virtual-list.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,42 @@ export class VirtualList extends HTMLElement {
4545

4646
#loadElements() {
4747
const height = this.offsetHeight;
48+
const visibleRange = this.#sizeManager.getVisibleRange(0, height);
49+
const totalHeight = this.#sizeManager.totalSize;
50+
51+
if (height <= totalHeight) {
52+
this.#createElements(visibleRange);
53+
}
54+
}
55+
56+
#createElements(visibleRange) {
57+
const fragment = document.createDocumentFragment();
58+
const { start, end } = visibleRange;
59+
60+
for (let i = start; i <= end; i++) {
61+
const item = this.#data[i];
62+
const element = this.#template.content.cloneNode(true);
63+
this.#inflateFn(element, item);
64+
fragment.appendChild(element);
65+
}
66+
67+
const container = this.shadowRoot.querySelector("#container");
68+
container.innerHTML = "";
69+
container.appendChild(fragment);
4870
}
4971

5072
/**
5173
* Method to update the list items.
5274
* @param {Array} items - The list items to display.
5375
*/
54-
async load(items, template, height, inflateFn) {
76+
async load(items, template, height, inflateFn, styles = []) {
77+
for (const style of styles) {
78+
const link = document.createElement("link");
79+
link.rel = "stylesheet";
80+
link.href= style;
81+
this.shadowRoot.appendChild(link);
82+
}
83+
5584
this.#template = template;
5685
this.#inflateFn = inflateFn;
5786

0 commit comments

Comments
 (0)