metis-desk/packages/rdx-ui/src/lib/reorder-items.ts
David ccb52fb534 feat: add catalog field picker dialog and list components
- Introduced `CatalogFieldPickerDialog` and `CatalogFieldPickerList` for selecting catalog fields.
- Implemented `appendTemplateFields` helper function for managing template fields.
- Added tests for catalog field selection and template field management.
- Created reusable components for field picking and reordering in the UI library.
- Enhanced drag-and-drop functionality for reordering items in lists.
2026-10-15 15:58:01 +02:00

34 lines
869 B
TypeScript

export type DropPlacement = "before" | "after";
/** Devuelve un orden nuevo o ausencia de cambio; nunca modifica la colección recibida. */
export function reorderItems<T>(
items: readonly T[],
sourceIndex: number,
targetIndex: number,
placement: DropPlacement
): T[] | undefined {
if (
!(Number.isInteger(sourceIndex) && Number.isInteger(targetIndex)) ||
sourceIndex < 0 ||
targetIndex < 0 ||
sourceIndex >= items.length ||
targetIndex >= items.length
) {
return undefined;
}
const insertionIndex = targetIndex + (placement === "after" ? 1 : 0);
const destination = insertionIndex - (sourceIndex < insertionIndex ? 1 : 0);
if (sourceIndex === destination) {
return undefined;
}
const next = [...items];
const removed = next.splice(sourceIndex, 1);
next.splice(destination, 0, ...removed);
return next;
}