import "./ItemList.css"; import { type Accessor, type Component, createEffect, createSignal, For, Match, Show, type Signal, Switch, } from "solid-js"; import { ArrowNarrowDownIcon, ArrowNarrowUpIcon, ArrowsSortIcon, ChevronDownIcon, } from "#/dev/icons/index.ts"; export type ListItem = { label: string; isActive: boolean }; export type ItemHandler = (list: Array, index: number) => void; export type Direction = "default" | "asc" | "desc"; function useDirection( signal: Signal, ): [Accessor, (forcedDirection?: Direction) => Direction] { const [direction, setDirection] = signal; return [ direction, (forcedDirection) => { if (forcedDirection) { setDirection(forcedDirection); return direction(); } switch (direction()) { case "asc": { setDirection("desc"); break; } case "desc": { setDirection("default"); break; } default: { setDirection("asc"); } } return direction(); }, ]; } export const ItemList: Component<{ name: string; items: Accessor>; handler: ItemHandler; }> = (props) => { const [showList, setShowList] = createSignal(true); const [items, setItems] = createSignal([...props.items()]); const [direction, toggleDirection] = useDirection( createSignal("default"), ); createEffect(() => { sortItems(props.items(), direction()); }); const sortItems = (items: Array, direction: Direction) => { if (direction === "default") { setItems([...props.items()]); return; } const newItems = [...items]; newItems.sort((a, b) => { const labelA = a.label.toLocaleLowerCase(); const labelB = b.label.toLocaleLowerCase(); if (labelA < labelB) { return direction === "asc" ? -1 : 1; } if (labelA > labelB) { return direction === "asc" ? 1 : -1; } return 0; }); setItems(newItems); }; return (
setShowList(!showList())} > {props.name}
    {(item, index) => { return (
  • props.handler(items(), index())} > {item.label}
  • ); }}
); };