All files / blong-browser/src/components/ThumbIndex ThumbIndex.tsx

25% Statements 37/148
100% Branches 1/1
0% Functions 0/3
25% Lines 37/148

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 148 1491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x                                                                                                                                                                                           1x 1x 1x                          
/**
 * ThumbIndex — multi-panel indexer.
 * Supports horizontal tabs, vertical side tabs, and wizard-style steps.
 */
import './ThumbIndex.css';
import {TabPanel, TabView} from '../../primereact/index.js';
 
import {useState, type ReactNode} from 'react';
 
export interface IThumbIndexItem {
    id: string;
    label?: string;
    icon?: string;
    items?: IThumbIndexItem[];
    /** Cards to render when this item is active */
    cards?: (string | string[])[];
}
 
export interface IThumbIndexProps {
    items: IThumbIndexItem[];
    orientation?: 'horizontal' | 'vertical' | 'top' | 'left' | 'bottom' | 'right';
    type?: 'tabs' | 'thumbs' | 'steps';
    /** Children rendering callback — receives the active item */
    renderContent?: (item: IThumbIndexItem) => ReactNode;
    activeId?: string;
    onChange?: (id: string) => void;
    className?: string;
}
 
/** Recursively flatten items to a flat list */
function flattenItems(items: IThumbIndexItem[]): IThumbIndexItem[] {
    return items.flatMap(item => (item.items ? flattenItems(item.items) : [item]));
}

export function ThumbIndex({
    items,
    orientation = 'top',
    type = 'tabs',
    renderContent,
    activeId: controlledId,
    onChange,
    className,
}: IThumbIndexProps) {
    const flatItems = flattenItems(items);
    const [internalActive, setInternalActive] = useState(flatItems[0]?.id ?? '');
    const activeId = controlledId ?? internalActive;

    const handleChange = (id: string) => {
        setInternalActive(id);
        onChange?.(id);
    };

    if (
        type === 'tabs' ||
        orientation === 'top' ||
        orientation === 'bottom' ||
        orientation === 'horizontal'
    ) {
        return (
            <TabView
                activeIndex={flatItems.findIndex(i => i.id === activeId)}
                onTabChange={e => handleChange(flatItems[e.index].id)}
                className={`blong-thumb-index blong-thumb-index--horizontal ${className ?? ''}`}
            >
                {flatItems.map(item => (
                    <TabPanel
                        __TYPE='TabPanel'
                        key={item.id}
                        header={
                            <span className="blong-thumb-index__tab-header">
                                {item.icon && <i className={`pi ${item.icon}`} />}
                                {item.label && <span>{item.label}</span>}
                            </span>
                        }
                    >
                        {renderContent?.(item)}
                    </TabPanel>
                ))}
            </TabView>
        );
    }

    // Vertical (left sidebar) layout
    if (orientation === 'left' || orientation === 'vertical') {
        const activeItem = flatItems.find(i => i.id === activeId);
        return (
            <div className={`blong-thumb-index blong-thumb-index--vertical ${className ?? ''}`}>
                <nav className="blong-thumb-index__nav">
                    {items.map(group => (
                        <div
                            key={group.id}
                            className="blong-thumb-index__nav-group"
                        >
                            {group.icon && (
                                <button
                                    className="blong-thumb-index__nav-icon"
                                    type="button"
                                >
                                    <i className={`pi ${group.icon}`} />
                                </button>
                            )}
                            <ul className="blong-thumb-index__nav-items">
                                {(group.items ?? [group]).map(item =>
                                    renderNavItem(item, activeId, handleChange),
                                )}
                            </ul>
                        </div>
                    ))}
                </nav>
                <div className="blong-thumb-index__content">
                    {activeItem && renderContent?.(activeItem)}
                </div>
            </div>
        );
    }

    return null;
}

function renderNavItem(
    item: IThumbIndexItem,
    activeId: string,
    onSelect: (id: string) => void,
): ReactNode {
    const hasChildren = item.items && item.items.length > 0;
    return (
        <li
            key={item.id}
            className={`blong-thumb-index__nav-item ${activeId === item.id ? 'blong-thumb-index__nav-item--active' : ''}`}
        >
            <button
                type="button"
                className="blong-thumb-index__nav-btn"
                onClick={() => !hasChildren && onSelect(item.id)}
            >
                {hasChildren ? (
                    <i className="pi pi-chevron-down blong-thumb-index__nav-expand" />
                ) : null}
                {item.label}
            </button>
            {hasChildren && (
                <ul className="blong-thumb-index__nav-children">
                    {item.items!.map(child => renderNavItem(child, activeId, onSelect))}
                </ul>
            )}
        </li>
    );
}