A list of counters
We have written two Counter elements by hand. Now a board will own a collection of counters and add them with a button.
children.many(CounterModel) declares the child container. For displays its contents, passing each ready instance through the row's model prop.
import { children, staticModel } from "@virentia/core/models";
import { action, component, For } from "@virentia/ui";
import { Counter, CounterModel } from "./Counter";
export const BoardModel = staticModel({
data: { counters: children.many(CounterModel) },
});
export const Board = component({
model: BoardModel,
view: (_props, self) =>
<For each={self.model.counters} view={Counter} />,
});
The board owns the collection. For passes each ready instance to Counter.
BoardModelparent modelcounterschild containerCounter × 2two displaysCreate Board.tsx, importing Counter and CounterModel from ./Counter. Replace the import and render root in main:
import { Board } from "./Board";
render(() => <Board counters={[
{ label: "Coffee", count: 2 },
{ label: "Tea", count: 5 },
]} />, container, { scope: appScope });The board model creates child instances from the counters input.
Adding creates one row. Removing disposes its model and display; other rows stay in place. This list needs no separate keys: its source supplies the exact operation.
Complete example
Run the finished example. Add counters, change their counts and remove the last one. Values in the remaining counters should not reset.
Sources
/** @jsxImportSource @virentia/ui */
import { f, staticModel } from "@virentia/core/models";
import { action, component, computed } from "@virentia/ui";
export const CounterModel = staticModel({
name: "LearningCounter",
data: {
count: f.number(0),
label: f.string("Counter"),
expanded: f.boolean(true),
},
setup(self) {
const increment = action(() => { self.count.value++; });
const doubled = computed(() => self.count.value * 2);
const toggle = action(() => { self.expanded.value = !self.expanded.value; });
return { increment, doubled, toggle };
},
});
export const Counter = component({
model: CounterModel,
view(_props, self) {
const counter = self.model;
function details() {
if (!counter.expanded.value) return null;
return <output>Doubled: {counter.doubled}</output>;
}
return <section class="counter">
<h2>{counter.label}</h2>
<button onClick={counter.increment}>Count: {counter.count}</button>
<button onClick={counter.toggle}>Details</button>
{details}
</section>;
},
});/** @jsxImportSource @virentia/ui */
import { children, staticModel } from "@virentia/core/models";
import { action, component, For } from "@virentia/ui";
import { Counter, CounterModel } from "./Counter";
export const BoardModel = staticModel({
name: "LearningBoard",
data: { counters: children.many(CounterModel) },
setup(self) {
const add = action(() => { self.counters.add({ label: "New counter" }); });
const removeLast = action(() => {
const items = self.counters.items;
const last = items[items.length - 1];
if (last) return self.counters.remove(last);
});
return { add, removeLast };
},
});
export const Board = component({
model: BoardModel,
view: (_props, self) => <main>
<h1>Counters</h1>
<nav aria-label="List actions">
<button onClick={self.model.add}>Add counter</button>
<button onClick={self.model.removeLast}>Remove last</button>
</nav>
<div class="counters">
<For each={self.model.counters} view={Counter} fallback={<p>No counters yet</p>} />
</div>
</main>,
});/** @jsxImportSource @virentia/ui */
import { render, scope } from "@virentia/ui";
import { Board } from "./Board";
const appScope = scope();
const container = document.getElementById("app")!;
render(() => <Board counters={[
{ label: "Coffee", count: 2 },
{ label: "Tea", count: 5 },
]} />, container, { scope: appScope });Continue with model ownership, or choose a guide.