Repository navigation
fix(admin): preserve RepeaterField item keys across value round-trip - #678
Conversation
The sync useEffect was running ensureKeys on every parent-prop update. Since emitChange strips _key before onChange, the round-tripped value always came back keyless, causing ensureKeys to mint fresh Date.now() keys. React then unmounted and remounted every SortableRepeaterItem on every keystroke — inputs lost focus and the item card appeared to collapse. Preserve each item's _key by position: when the incoming value lacks _key, reuse the key at the same index from previous state. Stable React keys mean inputs keep focus and expand/collapse state survives edits.
|
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/blocks
@emdash-cms/cloudflare
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
Pull request overview
Fixes unstable RepeaterField item identity in the admin content editor by preserving each item’s internal _key across value round-trips (where _key is stripped before emitting to the parent). This prevents SortableRepeaterItem unmount/remount churn that was causing focus loss and collapse-state resets while typing.
Changes:
- Updates the
useEffect([value])sync to reuse prior item_keys by index when the incoming value lacks keys. - Keeps
_keystripping behavior intact for persisted values while stabilizing React/DnD keys during editing.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Sync from external value changes. | ||
| // Preserve each item's _key by position so round-trips through onChange | ||
| // (which strips _key) don't remount children on every keystroke. | ||
| React.useEffect(() => { | ||
| const incoming = Array.isArray(value) ? value : []; | ||
| setItems(ensureKeys(incoming)); | ||
| setItems((prev) => | ||
| incoming.map((item, i) => { | ||
| const obj = (typeof item === "object" && item !== null ? item : {}) as Record< | ||
| string, | ||
| unknown | ||
| >; | ||
| const existingKey = (obj._key as string) || prev[i]?._key; | ||
| return { | ||
| ...obj, | ||
| _key: existingKey || `item-${i}-${Date.now()}`, | ||
| }; |
There was a problem hiding this comment.
The useEffect sync re-implements most of ensureKeys() inline (object coercion + key minting). This duplicates key-generation logic (now in ensureKeys, the effect, and handleAdd) and risks the behaviors drifting over time. Consider extracting a single helper (e.g. reconcileKeys(incoming, prev) or a shared createRepeaterKey(...)) and reuse it for both initial state and the sync effect.
Adds a `repeater` Block Kit element for plugin block forms: an array-of-objects input with scalar sub-fields, drag-to-reorder (dnd-kit), and collapsible item cards. Plugin blocks can now capture repeating data (FAQ rows, carousel slides, card grids) inline in the portable-text editor. `@emdash-cms/blocks` - `RepeaterElement` + `RepeaterSubField` types (text/number/select/ toggle/checkbox/combobox/date/radio as sub-fields — no nested repeaters, no buttons) - `elements.repeater(...)` builder - Schema validation for the repeater: sub-field type whitelist, non-negative integer `min_items` / `max_items` with `min_items <= max_items` - Exhaustive runtime `renderElement` arm (returns null — repeater is admin-authoring only) `@emdash-cms/admin` - `BlockKitRepeater` + `BlockKitRepeaterItem` rendered inside `PortableTextEditor` - Drag-to-reorder handle, collapsible item cards, Add / Remove controls - Keyboard-accessible item header (real `<button>` with `aria-expanded`, labeled drag handle) - Stable `_key` preservation per item (same pattern as the merged `RepeaterField` fix in emdash-cms#678) - `PluginBlockModal` body grows `max-h-[70vh] overflow-y-auto` so the modal scrolls when a repeater has many rows Split from the original bundled emdash-cms#679 per maintainer review. Image-link piece shipped in emdash-cms#704; `media_picker` follows in emdash-cms#731.
Adds a `repeater` Block Kit element for plugin block forms: an array-of-objects input with scalar sub-fields, drag-to-reorder (dnd-kit), and collapsible item cards. Plugin blocks can now capture repeating data (FAQ rows, carousel slides, card grids) inline in the portable-text editor. `@emdash-cms/blocks` - `RepeaterElement` + `RepeaterSubField` types. Sub-fields are restricted to the four scalar inputs the admin widget renders inline: `text_input`, `number_input`, `select`, `toggle`. No nested repeaters, no buttons. - `elements.repeater(...)` builder - Schema validation for the repeater: non-empty `fields` array, sub-field type whitelist, non-negative integer `min_items` / `max_items` with `min_items <= max_items`, and `initial_value` entries must be objects - Exhaustive runtime `renderElement` arm (returns null — repeater is admin-authoring only) `@emdash-cms/admin` - `BlockKitRepeater` + `BlockKitRepeaterItem` rendered inside `PortableTextEditor` - Drag-to-reorder handle, collapsible item cards, Add / Remove controls - Keyboard-accessible item header (real `<button>` with `aria-expanded`, labeled drag handle) - `handleRemove` drops the removed item's key from the `expanded` Set so it doesn't retain stale entries - Stable `_key` preservation per item (same pattern as the merged `RepeaterField` fix in emdash-cms#678) - `PluginBlockModal` body grows `max-h-[70vh] overflow-y-auto` so the modal scrolls when a repeater has many rows Split from the original bundled emdash-cms#679 per maintainer review. Image-link piece shipped in emdash-cms#704; `media_picker` follows in emdash-cms#731.
Adds a `repeater` Block Kit element for plugin block forms: an array-of-objects input with scalar sub-fields, drag-to-reorder (dnd-kit), and collapsible item cards. Plugin blocks can now capture repeating data (FAQ rows, carousel slides, card grids) inline in the portable-text editor. `@emdash-cms/blocks` - `RepeaterElement` + `RepeaterSubField` types. Sub-fields are restricted to the four scalar inputs the admin widget renders inline: `text_input`, `number_input`, `select`, `toggle`. No nested repeaters, no buttons. - `elements.repeater(...)` builder - Schema validation for the repeater: non-empty `fields` array, sub-field type whitelist, non-negative integer `min_items` / `max_items` with `min_items <= max_items`, and `initial_value` entries must be objects - Unit tests in `tests/validation.test.ts` covering the new rules (valid repeater, empty fields, disallowed sub-field type, non-integer / inverted min/max, malformed `initial_value`) - Exhaustive runtime `renderElement` arm (returns null — repeater is admin-authoring only) `@emdash-cms/admin` - `BlockKitRepeater` + `BlockKitRepeaterItem` rendered inside `PortableTextEditor` - Drag-to-reorder handle, collapsible item cards, Add / Remove controls - Keyboard-accessible item header (real `<button>` with `aria-expanded`, labeled drag handle) - `handleRemove` drops the removed item's key from the `expanded` Set so it doesn't retain stale entries - Item-card summary trims the first text_input before falling back to "Item N" so whitespace-only values still show the index - Stable `_key` preservation per item (same pattern as the merged `RepeaterField` fix in emdash-cms#678) - `PluginBlockModal` body grows `max-h-[70vh] overflow-y-auto` so the modal scrolls when a repeater has many rows Split from the original bundled emdash-cms#679 per maintainer review. Image-link piece shipped in emdash-cms#704; `media_picker` follows in emdash-cms#731.
Adds a `repeater` Block Kit element for plugin block forms: an array-of-objects input with scalar sub-fields, drag-to-reorder (dnd-kit), and collapsible item cards. Plugin blocks can now capture repeating data (FAQ rows, carousel slides, card grids) inline in the portable-text editor. `@emdash-cms/blocks` - `RepeaterElement` + `RepeaterSubField` types. Sub-fields are restricted to the four scalar inputs the admin widget renders inline: `text_input`, `number_input`, `select`, `toggle`. No nested repeaters, no buttons. - `elements.repeater(...)` builder - Schema validation for the repeater: non-empty `fields` array, sub-field type whitelist, non-negative integer `min_items` / `max_items` with `min_items <= max_items`, and `initial_value` entries must be objects - Unit tests in `tests/validation.test.ts` covering the new rules (valid repeater, empty fields, disallowed sub-field type, non-integer / inverted min/max, malformed `initial_value`) - Exhaustive runtime `renderElement` arm (returns null — repeater is admin-authoring only) `@emdash-cms/admin` - `BlockKitRepeater` + `BlockKitRepeaterItem` rendered inside `PortableTextEditor` - Drag-to-reorder handle, collapsible item cards, Add / Remove controls - Keyboard-accessible item header (real `<button>` with `aria-expanded`, labeled drag handle) - `handleRemove` drops the removed item's key from the `expanded` Set so it doesn't retain stale entries - Item-card summary trims the first text_input before falling back to "Item N" so whitespace-only values still show the index - Stable `_key` preservation per item (same pattern as the merged `RepeaterField` fix in emdash-cms#678) - `PluginBlockModal` body grows `max-h-[70vh] overflow-y-auto` so the modal scrolls when a repeater has many rows Split from the original bundled emdash-cms#679 per maintainer review. Image-link piece shipped in emdash-cms#704; `media_picker` follows in emdash-cms#731.
…mdash-cms#678) * fix(admin): preserve RepeaterField item keys across value round-trip The sync useEffect was running ensureKeys on every parent-prop update. Since emitChange strips _key before onChange, the round-tripped value always came back keyless, causing ensureKeys to mint fresh Date.now() keys. React then unmounted and remounted every SortableRepeaterItem on every keystroke — inputs lost focus and the item card appeared to collapse. Preserve each item's _key by position: when the incoming value lacks _key, reuse the key at the same index from previous state. Stable React keys mean inputs keep focus and expand/collapse state survives edits. * style: format --------- Co-authored-by: emdashbot[bot] <emdashbot[bot]@users.noreply.github.com>
Summary
The
RepeaterFieldsync effect was minting fresh item keys on every parent update, causingSortableRepeaterItemto unmount/remount on every keystroke. Inputs lost focus and the item card appeared to collapse back.Reproducer
Root cause
packages/admin/src/components/RepeaterField.tsx:ensureKeys()assigns_key: \item-${i}-${Date.now()}`when an item has no_key`.stripKeys()removes_keybeforeonChange, so the parent's value never carries keys.useEffect([value])sync rerunsensureKeys()against the round-tripped (keyless) value → freshDate.now()keys on every emit.SortableRepeaterItemis keyed byitem._key, so changed keys = full unmount/remount of every item.collapsedItems: Set<string>is keyed by_keyand becomes orphaned on every keystroke.Fix
Preserve each item's
_keyby position: when the incoming value lacks_key, reuse the key from the previous state at the same index. Round-trips viastripKeysstop churning keys; new external values (add/remove/reorder) still get stable keys either from the incoming object or the previous slot. Initial loads fall back to the existingitem-\${i}-\${Date.now()}generator.No changes to
emitChange,handleAdd,handleRemove,handleDragEnd, ortoggleCollapse— they already operated correctly on_key-bearing items.Test plan
_keyfields (still stripped inemitChange).