Nest a list inside an object
Each card holds a few things of its own — a unit's floor plans, each with its floor — and they belong to that card, not to the section.
Goal
A units section where every unit card carries its own floor plans: each plan the unit's own image, with the floor it shows. The plans are one field of the unit's card, edited inside it — and, beside it, plans a user builds from nothing, each with its floor chosen from a short vocabulary.
Prerequisites
- Add a list slot for lists, items, rows and
bindItems. - Add an image slot with a crop for
fromand an image's candidates, and Add a select slot for a select'sfrom.
Steps
1. Know what nests, and what a nested list is
An object's field may be a list of objects, at a section's top level and as a
list's item alike. It nests one level: the inner list's items hold value slots
and rows, never another list of objects. And it is one field of the card that
holds it. A click on any of its items selects that field, and the user edits its
items inside the field's own editor — they are not cards of their own on the
canvas. Each item carries an id of its own that stays the same across re-bakes.
Where the inner list's items come from is narrower than a top-level list's:
- a row's list-valued member —
unit.floor_plans, inside an item that declares the row (steps 2 and 3); - a
compute()returning a list; - authored items — no
source; the user adds them (step 4).
An item's fields read the element, a row holding it, or nothing. A fill on the inner
list or on one of its fields, a compute() on a field, a fact table or another list
slot as its source each throw when the section is declared, naming the field; such a
list belongs at the top level (contract/object).
2. Declare the inner list as a field of the card
import { fact, image, list, object, row, text } from "@homepages/concrete";
const units = list({
label: "Units",
source: fact.property.units,
item: (el) => {
const unit = row({ label: "Unit", from: fact.property.units, source: el });
const name = text.medium({ label: "Unit name", cap: 40, source: unit.label });
return object({
label: name,
fields: {
unit,
name,
floor_plans: list({
label: "Floor plans",
source: unit.floor_plans,
item: (entry) => {
const plan = row({ label: "Plan", from: unit.floor_plans, source: entry, locked: true });
return object({
fields: {
plan,
image: image({ label: "Plan", source: plan.image }),
floor: text.short({ label: "Floor", source: plan.floor }),
},
});
},
}),
},
});
},
});The outer list is the property's units table; each of its items is a card of the
unit's fields. floor_plans is one of those fields: a list over the unit's own
floor plans, unit.floor_plans, whose item is a function of each plan.
Each plan is the unit's own fact data — its image and the floor it shows — with no
table of its own, so the item declares a row from the member it lists,
from: unit.floor_plans, holding its element, plan, and reads its keys:
plan.image and plan.floor. An edit to either goes to that plan of the unit's
(where an edit goes).
image reads the plan, so it holds what the plan holds and
states no from — a from beside a binding does not compile
(contract/sources). The row is locked: a plan is the
unit's, not one the user re-picks. Neither field takes a fill.
The inner object takes no label here — a list's item may omit it, and a label
may only name a text field. Its fields still each state their own.
3. Render the inner items inside the card
import { bindItems, fact, image, list, object, row, Section, section, Slot, text } from "@homepages/concrete";
export const Units = section(
{
key: "units",
label: "Units",
slots: {
units: list({
label: "Units",
source: fact.property.units,
item: (el) => {
const unit = row({ label: "Unit", from: fact.property.units, source: el });
const name = text.medium({ label: "Unit name", cap: 40, source: unit.label });
return object({
label: name,
fields: {
unit,
name,
floor_plans: list({
label: "Floor plans",
source: unit.floor_plans,
item: (entry) => {
const plan = row({ label: "Plan", from: unit.floor_plans, source: entry, locked: true });
return object({
fields: {
plan,
image: image({ label: "Plan", source: plan.image }),
floor: text.short({ label: "Floor", source: plan.floor }),
},
});
},
}),
},
});
},
}),
},
},
({ slot }) => (
<Section>
<Slot.List slot={slot.units} as="ul" className="grid gap-8">
{bindItems(slot.units).map((item) => (
<Slot.Object key={item.index} slot={item} as="li">
<Slot.Text slot={item.name} as="h3" />
<Slot.List slot={item.floor_plans} as="div" className="grid grid-cols-2 gap-4">
{bindItems(item.floor_plans).map((plan) => (
<Slot.Object key={plan.index} slot={plan} as="figure">
<Slot.Image slot={plan.image} className="aspect-square w-full" />
<Slot.Text slot={plan.floor} as="figcaption" />
</Slot.Object>
))}
</Slot.List>
</Slot.Object>
))}
</Slot.List>
</Section>
),
);The inner list is read straight off the unit's binding, item.floor_plans, like any
other field, and bindItems turns it into one binding per plan. Each plan's fields
are read off its binding the same way — plan.image, plan.floor.
Slot.List over the field marks it, so a unit with no plans still renders a
selectable element. Slot.Object over an inner item marks the enclosing field
rather than an item of its own, and so do the Slot.Image and Slot.Text inside
it: whatever the user clicks inside a plan selects the unit's floor-plans field. Render the inner items inside the card that holds them — a
field marked outside its own object's Slot.Object is
render/group-ancestry, and an unlocked field
rendered nowhere is
render/missing-slot-marker.
4. Let the user build the inner list
An inner list with no source is one the user builds, item by item. It sits where
nothing sources the card that holds it — a top-level object, or an item of a list
the user builds. Inside an item of a sourced list, such as the units table above,
every field owes an origin, and a list the user builds has none: concrete check
refuses it as sections/schema-invalid.
import { fact, image, list, object, section, select, text } from "@homepages/concrete";
const FLOORS = ["Main", "Upper", "Lower"] as const;
const name = text.short({ label: "Model name" });
export const Models = section(
{
key: "models",
label: "Models",
slots: {
models: list({
label: "Models",
max: 6,
item: object({
label: name,
fields: {
name,
floor_plans: list({
label: "Floor plans",
max: 4,
item: object({
fields: {
plan: image({ label: "Plan", from: fact.property.floor_plans }),
floor: select({ label: "Floor", from: FLOORS, creatable: true }),
},
}),
}),
},
}),
}),
},
},
() => null,
);Each model is a card the user adds, and each card's plans are a list the user
builds. Here plan is an image bound to no fact, so it states what the user picks
it from: from: fact.property.floor_plans opens the picker on the property's floor plans,
and only there. The render is step 3's, over slot.models.
Verify
Run concrete dev. Each unit is a card in the sidebar, and its floor plans are one
field of it: the user picks each plan's floor, and on a model card adds plans from
the property's floor plans. The editor offers Add at any count: max bounds what a fill
chooses and what concrete check grades, never what the user adds, so render every plan
the list holds. Clicking a plan on the canvas selects the card's floor-plans field, never
the plan alone.
Then run concrete check. Declaring the section refuses a nested list it cannot
hold, the contracts stage grades each field's origin and from, and the render
stage grades the markers:
concrete check passed: 13 stages.