All files / src/components/ui/lists SortableList.tsx

0% Statements 0/45
0% Branches 0/32
0% Functions 0/11
0% Lines 0/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                                                                                                                                                                                                                                                                                     
import React, { useCallback, useEffect, useRef, useState } from "react";
import { SortableItem } from "ui/lists/SortableItem";
import { StyledSortableList } from "./style";
import { throttle } from "lodash";
 
export type SortableListOrientation = "horizontal" | "vertical";
 
interface SortableListProps<T> {
  orientation?: SortableListOrientation;
  gap?: number;
  padding?: number;
  itemType: string;
  items: T[];
  selectedIndex: number;
  renderItem: (
    item: T,
    {
      isSelected,
      isOver,
    }: {
      isSelected: boolean;
      isDragging: boolean;
      isDraggingAny: boolean;
      isOver: boolean;
    }
  ) => JSX.Element;
  extractKey: (item: T) => string;
  onSelect: (item: T, e?: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
  moveItems: (dragIndex: number, hoverIndex: number) => void;
  onKeyDown?: (e: KeyboardEvent) => boolean | void;
  appendComponent?: JSX.Element;
}
 
export const SortableList = <T,>({
  itemType,
  items,
  orientation = "horizontal",
  gap = 10,
  padding = 10,
  selectedIndex,
  renderItem,
  extractKey,
  onSelect,
  moveItems,
  onKeyDown,
  appendComponent,
}: SortableListProps<T>) => {
  const [hasFocus, setHasFocus] = useState(false);
  const [dragging, setDragging] = useState(false);
 
  const handleKeys = useCallback(
    (e: KeyboardEvent) => {
      Iif (!hasFocus) {
        return;
      }
      Iif (onKeyDown?.(e)) {
        // If onkeydown returns true don't handle input internally
        return;
      }
      if (
        (orientation === "horizontal" && e.key === "ArrowRight") ||
        (orientation === "vertical" && e.key === "ArrowDown")
      ) {
        e.preventDefault();
        throttledNext.current(items, selectedIndex || -1);
      } else if (
        (orientation === "horizontal" && e.key === "ArrowLeft") ||
        (orientation === "vertical" && e.key === "ArrowUp")
      ) {
        e.preventDefault();
        throttledPrev.current(items, selectedIndex || -1);
      } else if (e.key === "Home") {
        Iif (items[0]) {
          onSelect(items[0]);
        }
      } else Iif (e.key === "End") {
        Iif (items[items.length - 1]) {
          onSelect(items[items.length - 1]);
        }
      }
    },
    [hasFocus, onKeyDown, orientation, items, selectedIndex, onSelect]
  );
 
  const throttledNext = useRef(
    throttle((frames: T[], selectedIndex: number) => {
      const nextIndex = (selectedIndex + 1) % frames.length;
      const nextItem = frames[nextIndex];
      Iif (nextItem) {
        onSelect(nextItem);
      }
    }, 150)
  );
 
  const throttledPrev = useRef(
    throttle((frames: T[], selectedIndex: number) => {
      const prevIndex = (frames.length + selectedIndex - 1) % frames.length;
      const prevItem = frames[prevIndex];
      Iif (prevItem) {
        onSelect(prevItem);
      }
    }, 150)
  );
 
  useEffect(() => {
    window.addEventListener("keydown", handleKeys);
    return () => {
      window.removeEventListener("keydown", handleKeys);
    };
  });
 
  return (
    <StyledSortableList
      tabIndex={0}
      onFocus={() => setHasFocus(true)}
      onBlur={() => setHasFocus(false)}
      $orientation={orientation}
      $gap={gap}
      $padding={padding}
    >
      {items.map((item, index) => (
        <SortableItem
          key={extractKey(item)}
          itemType={itemType}
          item={item}
          index={index}
          renderItem={(item, { isOver, isDragging }) => {
            return renderItem(item, {
              isSelected: selectedIndex === index,
              isOver,
              isDragging,
              isDraggingAny: dragging,
            });
          }}
          onSelect={(e) => {
            onSelect(item, e);
          }}
          orientation={orientation}
          moveItems={moveItems}
          setDragging={setDragging}
        />
      ))}
      {appendComponent}
    </StyledSortableList>
  );
};