All files / src/components/EditableTableV2 ToolbarButtons.tsx

96.66% Statements 29/30
95% Branches 19/20
100% Functions 9/9
95.83% Lines 23/24

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                                                                  19x   420x                 19x 283x 283x 283x 283x 47x 17x     283x     283x             283x             283x             283x         48x 17x   31x                       283x     33x     66x     98x     35x     51x      
import React, { createContext, useContext, useEffect } from 'react';
 
import {
  DensityLarge,
  DensityMedium,
  DensitySmall,
  FilterList,
  FilterListOff,
  Fullscreen,
  FullscreenExit,
  Search,
  SearchOff,
  ViewColumn,
} from '@mui/icons-material';
import { Box, BoxProps, IconButton, Tooltip } from '@mui/material';
 
import { useStrings } from '../../strings';
import { EditableTableInstanceV2 } from './types';
 
type ToolbarState = {
  table: object;
  globalFilterVisible: boolean;
  columnFiltersVisible: boolean;
  density: number;
  fullScreen: boolean;
  toggleGlobalFilter: () => void;
  toggleColumnFilters: () => void;
  toggleDensity: () => void;
  toggleFullScreen: () => void;
  openColumns: (anchor: HTMLElement) => void;
  registerColumnsButton: () => () => void;
};
 
const ToolbarContext = createContext<ToolbarState | null>(null);
 
export const EditableTableToolbarRoot = ({ value, ...props }: BoxProps & { value: ToolbarState }) => (
  <ToolbarContext.Provider value={value}>
    <Box {...props} />
  </ToolbarContext.Provider>
);
 
type ButtonProps<TData extends object> = { table: EditableTableInstanceV2<TData> };
type Action = 'globalFilter' | 'filters' | 'columns' | 'density' | 'fullScreen';
 
const ToolbarButton = <TData extends object>({ table, action }: ButtonProps<TData> & { action: Action }) => {
  const state = useContext(ToolbarContext);
  const strings = useStrings();
  const registerColumnsButton = state?.registerColumnsButton;
  useEffect(() => {
    if (action === 'columns') {
      return registerColumnsButton?.();
    }
  }, [action, registerColumnsButton]);
  Iif (!state || state.table !== table) {
    throw new Error('EditableTableV2 toolbar buttons must be rendered inside their table.');
  }
  const label = {
    globalFilter: strings.TABLE_TOGGLE_SEARCH,
    filters: strings.TABLE_TOGGLE_FILTERS,
    columns: strings.TABLE_COLUMNS,
    density: strings.TABLE_TOGGLE_DENSITY,
    fullScreen: strings.TABLE_TOGGLE_FULLSCREEN,
  }[action];
  const pressed = {
    globalFilter: state.globalFilterVisible,
    filters: state.columnFiltersVisible,
    fullScreen: state.fullScreen,
    columns: undefined,
    density: undefined,
  }[action];
  const icon = {
    globalFilter: state.globalFilterVisible ? <SearchOff /> : <Search />,
    filters: state.columnFiltersVisible ? <FilterListOff /> : <FilterList />,
    columns: <ViewColumn />,
    density: state.density === 0 ? <DensitySmall /> : state.density === 1 ? <DensityMedium /> : <DensityLarge />,
    fullScreen: state.fullScreen ? <FullscreenExit /> : <Fullscreen />,
  }[action];
  const button = (
    <IconButton
      aria-label={label}
      aria-pressed={pressed}
      onClick={(event) => {
        if (action === 'columns') {
          state.openColumns(event.currentTarget);
        } else {
          ({
            globalFilter: state.toggleGlobalFilter,
            filters: state.toggleColumnFilters,
            density: state.toggleDensity,
            fullScreen: state.toggleFullScreen,
          })[action]();
        }
      }}
    >
      {icon}
    </IconButton>
  );
  return action === 'columns' ? button : <Tooltip title={label}>{button}</Tooltip>;
};
 
export const EditableTableToggleGlobalFilterButtonV2 = <TData extends object>(props: ButtonProps<TData>) => (
  <ToolbarButton {...props} action='globalFilter' />
);
export const EditableTableToggleFiltersButtonV2 = <TData extends object>(props: ButtonProps<TData>) => (
  <ToolbarButton {...props} action='filters' />
);
export const EditableTableShowHideColumnsButtonV2 = <TData extends object>(props: ButtonProps<TData>) => (
  <ToolbarButton {...props} action='columns' />
);
export const EditableTableToggleDensePaddingButtonV2 = <TData extends object>(props: ButtonProps<TData>) => (
  <ToolbarButton {...props} action='density' />
);
export const EditableTableToggleFullScreenButtonV2 = <TData extends object>(props: ButtonProps<TData>) => (
  <ToolbarButton {...props} action='fullScreen' />
);