Lists

5 min·Assumes: state, events, computed

@for draws one row per item, and a key tells the page which row is which.

import { state } from '@markless/core';

export default function Shelf() @{
	let books = state([
		{ id: 'dune', title: 'Dune' },
		{ id: 'emma', title: 'Emma' },
	]);

	<ul>
		@for (const book of books; key book.id) {
			<li>{book.title}</li>
		}
	</ul>
}

@for draws one <li> per book. key book.id names each row by its data, so the page can tell the rows apart.

Every row needs a key

A key is a value that names one item for good, like an id. Every @for needs one, even over a list that never changes.

PlaygroundRestored your edit

Definitely normal humans

  • id 1
Try this
  1. To do: Press Clone Mark twice. Three Marks look exactly alike, and each is its own row, because each has its own id.
  2. To do: Try breaking it: delete ; key mark.id. The build stops, because every row needs a key.

Add and remove rows

Change the array the way you change any array: push, splice or filter. The page adds or removes just those rows. @empty shows instead when the list has no rows. Keys must be unique: two patches with the same id would be one row too many.

PlaygroundRestored your edit

Did you even compile this?

  • fix-typo.patch
  • tabs-to-spaces.patch
Try this
  1. To do: Press Reject twice. The queue is empty, so the @empty row takes over.
  2. To do: Make Reject throw out the newest patch instead: change queue.splice(0, 1) to queue.pop().

A row's own state moves with its key

When a row needs state of its own, like whether a guest has sat down, make the row a component in its own file, here place-card.tsrx. Each row gets its own state(), and the key keeps it with its guest when the list moves.

  • Reverse or sort the list, and each row moves with its item.
  • What the row holds rides along.
  • No row is built again.

Press Reverse, or drag a card onto another place.

Three folded name cards stand in three numbered places: Grace, Linus and Ada, with Grace ticked. Press Reverse, or drag a card onto another place, or focus a card and use the arrow keys: the cards walk to their new places and each tick goes with its own card, while the place numbers stay put. Click a card, or press Space on it, to tick it.

PlaygroundRestored your edit

Fellowship dinner

Try this
  1. To do: Press Grandalf to seat him, then press Reverse. Grandalf moves to the end, still in his chair.
  2. To do: Try breaking it: in place-cards.tsrx, write let seated = state(false); straight inside the @for, above <PlaceCard>. The build stops, because a row's state belongs in a row component.

Change one row in place

To change one row, write to its item: seat.status = 'taken'. Only that row updates, and so does anything worked out from the list.

PlaygroundRestored your edit

4 left to rename

Try this
  1. To do: Press Rocket. Melon renames it X: only that sign changes, and the count in his speech bubble drops.
  2. To do: Add a Rename all button under the signs that runs things.forEach((t) => (t.name = 'X')).

A row component can't write to the object it was given. Pass it a function to call instead, as Components shows.

Filter and sort with a computed

The rows you show are worked out from the full list, so put that in a computed. Type on the drawer's label: the cards that don't match sink, and the rest close up in their order.

MiddlemarchBelovedKimUlyssesEmmaDune
An ink drawing of a library card-catalogue drawer with six index cards standing in it, a title on each tab. The brass label on the drawer front is a text box: type in it and the cards whose title doesn't match sink below the drawer edge, while the cards that match close up at the front in the same order. Clear it, or press Escape, and they all rise back.

PlaygroundRestored your edit

Smeagle's stash

  • Ring
  • Fish
Try this
  1. To do: Press Nasty. Smeagle shows his other face, and his nasty pile shows.
  2. To do: Sort them: add .toSorted((a, b) => a.name.localeCompare(b.name)) after the filter.
  3. To do: Press Shared. Nothing in the stash is shared, so the @empty row shows.

Loop over a Set or a Map

@for reads a Set or a Map directly. Pull a Map's key and value apart with [n, item]. For a plain object, loop over computed(() => Object.entries(prices)).

PlaygroundRestored your edit

Ram's roadmap

AGI in 3000 days

Robot butler in 5000 days

Try this
  1. To do: Press Ship it. Every flag runs another thousand days away.
  2. To do: Add a Pivot button that runs road.clear().

Number the rows with index

index i gives each row its place, for display. The key stays on the data:

@for (const book of books; index i; key book.id) {
	<li>{i + 1}. {book.title}</li>
}

Repeat a fixed number of times

Loop over a short array of numbers, keyed by the number:

@for (const n of [1, 2, 3, 4, 5]; key n) {
	<span>{n <= stars ? '★' : '☆'}</span>
}

Async values shows how a page waits for data.