All files / components/TabsMenu TabsMenu.tsx

100% Statements 190/190
94.44% Branches 34/36
100% Functions 9/9
100% Lines 190/190

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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 1911x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 11x 11x 11x 37x 37x 37x 37x 8x 8x 8x 37x 37x 37x 3x 3x 9x 9x 3x 3x 3x 3x 3x 37x 37x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 3x 37x 37x 2x 2x 2x 2x 2x 2x 6x 6x 2x 2x 2x 2x 2x 2x 2x 1x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 37x 37x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 37x 57x 57x 57x 57x 57x 57x 57x 57x 3x 3x 57x 57x 2x 2x 2x 2x 57x 57x 57x 57x 57x 57x 57x 1x 1x 57x 57x 57x 18x 18x 18x 18x 18x 57x 57x 37x 37x 13x 13x 13x 13x 13x 13x 3x 3x 3x 13x 13x 13x 13x 37x 37x 37x 37x 37x  
import { useEffect, useState } from 'react';
 
import { isEqual } from 'lodash';
 
import { c } from '../../helpers';
import ButtonCloser from '../ButtonCloser/ButtonCloser';
import { IconPlus } from '../Icons';
import TabEditable from './components/TabEditable';
import { type Tab, type TabsMenuProps } from './TabsMenuProps';
 
import styles from './TabsMenu.module.css';
 
export default function TabsMenu({
  addable,
  className,
  editable,
  newTabTemplate,
  onTabAdd,
  onTabClick,
  onTabEdit,
  onTabRemove,
  onTabsChange,
  removable,
  tabs,
  tabsDefault,
}: TabsMenuProps): JSX.Element {
  const [initialTabs, setInitialTabs] = useState<Tab[]>(tabsDefault ?? []);
  const [currentTabs, setCurrentTabs] = useState<Tab[]>(tabsDefault ?? []);
 
  useEffect(() => {
    if (!tabs) return;
    setInitialTabs(tabs ?? []);
    setCurrentTabs(tabs ?? []);
  }, [tabs]);
 
  useEffect(() => {
    if (!isEqual(initialTabs, currentTabs)) {
      onTabsChange?.(currentTabs);
      setInitialTabs(currentTabs);
    }
  }, [onTabsChange, currentTabs, initialTabs]);
 
  const handleTabClick = (currentTabindex: number): void => {
    setCurrentTabs(
      currentTabs.map((tab, index) => ({
        ...tab,
        active: index === currentTabindex,
      }))
    );
    const currentTab = currentTabs?.[currentTabindex];
    onTabClick?.({ label: currentTab?.label, index: currentTabindex });
  };
 
  const handleAddClick = (): void => {
    let newTabs = currentTabs;
 
    newTabs = currentTabs.map(tab => ({
      ...tab,
      active: false,
    }));
 
    newTabs = [...newTabs, { ...(newTabTemplate ?? {}), active: true }];
 
    setCurrentTabs(newTabs);
 
    const newTabIndex = currentTabs.length;
    onTabAdd?.({
      color: newTabTemplate?.color,
      Icon: newTabTemplate?.Icon,
      index: newTabIndex,
      label: newTabTemplate?.label,
    });
  };
 
  const handleRemoveClick = (currentTabindex: number): void => {
    let newTabs = currentTabs;
    const currentTab = currentTabs[currentTabindex];
    const mustActivatePreviousTab = currentTab?.active && currentTabindex > 0;
 
    if (mustActivatePreviousTab) {
      newTabs = newTabs?.map((tab, index) => ({
        ...tab,
        active: index === currentTabindex - 1,
      }));
    }
 
    const mustActivateNextTab =
      currentTab?.active && currentTabindex < newTabs?.length - 1;
 
    if (mustActivateNextTab) {
      newTabs = newTabs?.map((tab, index) => ({
        ...tab,
        active: index === currentTabindex + 1,
      }));
    }
 
    newTabs = newTabs?.filter((_tab, index) => index !== currentTabindex);
 
    setCurrentTabs(newTabs);
    onTabRemove?.({
      color: currentTab?.color,
      Icon: currentTab?.Icon,
      index: currentTabindex,
      label: currentTab?.label,
    });
  };
 
  const handleEditChange = (
    currentTabindex: number,
    newLabel: string
  ): void => {
    const currentTab = currentTabs[currentTabindex];
    const newTabs = currentTabs.map((tab, index) => {
      if (index === currentTabindex) {
        return {
          ...tab,
          label: newLabel,
        };
      }
      return tab;
    });
 
    setCurrentTabs(newTabs);
    onTabEdit?.({
      label: newLabel,
      index: currentTabindex,
      color: currentTab?.color,
      Icon: currentTab?.Icon,
    });
  };
 
  return (
    <nav className={c(styles.tab_menu, className)}>
      <ul>
        {currentTabs.map(({ label, Icon, active, color }, index) => (
          <li
            className={c(styles.tab_item, active ? styles.active : '')}
            key={index}
            title={label}
          >
            <div
              className={c(styles.tab_trigger)}
              onClick={(): void => {
                handleTabClick(index);
              }}
            >
              {color && (
                <span
                  className={c(styles.tab_color)}
                  style={{ background: color }}
                />
              )}
              {Icon && <Icon className={c(styles.tab_icon)} />}
              <TabEditable
                className={c(styles.tab_label)}
                label={label}
                editable={editable}
                onChange={(newLabel: string): void => {
                  handleEditChange(index, newLabel);
                }}
              />
            </div>
            {removable && (
              <ButtonCloser
                title="Remove tab"
                className={c(styles.tab_closer)}
                onClick={(): void => handleRemoveClick(index)}
              />
            )}
          </li>
        ))}
        {addable && (
          <li className={c(styles.add_tab)}>
            <a
              title="Add new tab"
              aria-label="Add new tab"
              href="#"
              onClick={(event): void => {
                event.preventDefault();
                handleAddClick();
              }}
            >
              <IconPlus />
            </a>
          </li>
        )}
      </ul>
    </nav>
  );
}