DEV Community

Cover image for Key repeated window rows by identity, rather than list order
The Doctor
The Doctor

Posted on

Key repeated window rows by identity, rather than list order

Today the roller shutter planner asks for a band such as 3–5 openings and a location category. It does not store one row per window. A follow-on plan editor could collect such a schedule. It would need a stable key so deleting the first window cannot move an unsaved control preference onto the next window.

Generate identity once on insert

The array index tells you where an opening appears. It changes when rows move. A persistent ID survives sorting and is a suitable render key when the editor uses keyed components. Use the ID for updates too, since an event may arrive after the display order has changed.

type Opening = { id: string; room: string; control: 'manual' | 'powered' | null };
const makeOpening = (): Opening =>
  ({ id: crypto.randomUUID(), room: '', control: null });
const selectManual = (rows: Opening[], id: string) =>
  rows.map(row => row.id === id ? { ...row, control: 'manual' as const } : row);
Enter fullscreen mode Exit fullscreen mode

Exercise the sequence that exposes the bug

In a browser test for the proposed editor, add three openings and give them different room names. Change the middle opening’s control. Delete the first opening, reorder the survivors and check the value remains with the same room. If drafts can be saved later, reload and confirm the same ID is restored. Also check that adding a fourth row creates a new ID.

The current site retains a compact count for its planning summary. The row-level design is for a later feature, with a separate data-model and accessibility job. It would need clear per-row labels and sensible keyboard focus after removal as well as stable identities.

If the interface later persists rows, validate ID uniqueness when loading a draft. Duplicate keys can corrupt both rendering and updates. A duplicate-ID fixture belongs in the loader tests alongside the interactive reorder sequence.

Perth Quotes roller shutter planner is the live preview. Online enquiries are closed.

Top comments (0)