Files
resolutionflow/frontend/src/components/tree-editor/DynamicArrayField.tsx
Michael Chihlas 4cee013733 Implement Tree Editor with visual preview and documentation updates
Tree Editor Features:
- Zustand store with immer middleware and zundo for undo/redo
- Form-based node editing (Decision, Action, Solution types)
- Visual tree preview with solution connection indicators
- NodePicker with type-grouped dropdown (Decisions/Actions/Solutions)
- SharedLinksMap for detecting nodes with multiple sources
- Modal component with scrollable body, fixed header/footer

New Components:
- TreeEditorLayout, TreeMetadataForm, NodeList, NodeEditorModal
- NodeFormDecision, NodeFormAction, NodeFormResolution
- DynamicArrayField, NodePicker
- TreePreviewPanel, TreePreviewNode

Documentation:
- Updated README.md status to Phase 2
- Added Tree Editor details to CURRENT-STATE.md
- Added modal/Zustand lessons to LESSONS-LEARNED.md
- Updated file structure in CLAUDE-SETUP.md
- Added Tree Editor progress to PROGRESS.md

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 03:00:00 -05:00

113 lines
3.3 KiB
TypeScript

import type { ReactNode } from 'react'
import { Plus, Trash2, ChevronUp, ChevronDown } from 'lucide-react'
import { cn } from '@/lib/utils'
interface DynamicArrayFieldProps<T> {
items: T[]
onAdd: () => void
onRemove: (index: number) => void
onReorder?: (fromIndex: number, toIndex: number) => void
renderItem: (item: T, index: number) => ReactNode
addLabel?: string
maxItems?: number
minItems?: number
className?: string
}
export function DynamicArrayField<T>({
items,
onAdd,
onRemove,
onReorder,
renderItem,
addLabel = 'Add Item',
maxItems,
minItems = 0,
className
}: DynamicArrayFieldProps<T>) {
const canAdd = maxItems === undefined || items.length < maxItems
const canRemove = items.length > minItems
const handleMoveUp = (index: number) => {
if (onReorder && index > 0) {
onReorder(index, index - 1)
}
}
const handleMoveDown = (index: number) => {
if (onReorder && index < items.length - 1) {
onReorder(index, index + 1)
}
}
return (
<div className={cn('space-y-2', className)}>
{items.map((item, index) => (
<div key={index} className="group flex items-start gap-2">
{/* Reorder buttons */}
{onReorder && items.length > 1 && (
<div className="flex flex-col gap-0.5 opacity-0 group-hover:opacity-100">
<button
type="button"
onClick={() => handleMoveUp(index)}
disabled={index === 0}
className="rounded p-0.5 text-muted-foreground hover:bg-accent hover:text-accent-foreground disabled:opacity-30"
title="Move up"
>
<ChevronUp className="h-3 w-3" />
</button>
<button
type="button"
onClick={() => handleMoveDown(index)}
disabled={index === items.length - 1}
className="rounded p-0.5 text-muted-foreground hover:bg-accent hover:text-accent-foreground disabled:opacity-30"
title="Move down"
>
<ChevronDown className="h-3 w-3" />
</button>
</div>
)}
{/* Item content */}
<div className="flex-1">{renderItem(item, index)}</div>
{/* Remove button */}
{canRemove && (
<button
type="button"
onClick={() => onRemove(index)}
className="mt-1 rounded p-1 text-muted-foreground hover:bg-destructive/20 hover:text-destructive"
title="Remove"
>
<Trash2 className="h-4 w-4" />
</button>
)}
</div>
))}
{/* Add button */}
{canAdd && (
<button
type="button"
onClick={onAdd}
className={cn(
'flex w-full items-center justify-center gap-1 rounded-md border border-dashed border-input',
'px-3 py-2 text-sm text-muted-foreground',
'hover:border-primary hover:text-primary'
)}
>
<Plus className="h-4 w-4" />
{addLabel}
</button>
)}
{/* Empty state */}
{items.length === 0 && !canAdd && (
<p className="text-center text-sm text-muted-foreground">No items</p>
)}
</div>
)
}
export default DynamicArrayField