/* eslint-disable react/display-name */ import React, { useCallback } from "react"; import { Box, Flex } from "@chakra-ui/layout"; import { Table, Thead, Tr, Th, Tbody, Td } from "@chakra-ui/table"; import { MdArrowDropDown, MdArrowDropUp, MdArrowForward } from "react-icons/md"; import { IoMdArrowDroprightCircle, IoMdArrowDropdownCircle } from "react-icons/io"; import { Column, useExpanded, useGlobalFilter, useGroupBy, usePagination, useRowSelect, useSortBy, useTable } from "react-table"; import { PaginationControl } from "./PaginationControl"; import { HStack, IconButton, Select, Tag, Text } from "@chakra-ui/react"; import { IndeterminateCheckbox } from "./IndeterminateCheckbox"; import { GroupBy } from "./GroupBy"; import { GlobalFilter } from "./GlobalFilter"; import IDataGridProps from "./data-grid.types"; const DataGrid = ({ columns, data, isSortable = false, showPagination = false, multiSelectPosition = "left", isMultiSelect = false, onRowSelection, updateMyData }: IDataGridProps) => { const useMultiSelectHook = (hooks: any) => { hooks.visibleColumns.push((columns: any) => { const selectionColumn = { id: "selection", Header: ({ getToggleAllPageRowsSelectedProps, getToggleAllRowsSelectedProps }: any) => ( {!showPagination ? ( ) : ( )} ), Cell: ({ row }: any) => ( ) }; return multiSelectPosition === "left" ? [...columns, selectionColumn] : [selectionColumn, ...columns]; }); }; function useControlledState(state: any, { instance }: any) { return React.useMemo(() => { if (state.groupBy.length) { return { ...state, hiddenColumns: [...state.hiddenColumns, ...state.groupBy].filter( (d, i, all) => all.indexOf(d) === i ) }; } return state; }, [state]); } const useExpanderHook = (hooks: any) => { hooks.useControlledState.push(useControlledState); hooks.visibleColumns.push((columns: any, { instance }: any) => { if (!instance.state.groupBy.length) { return columns; } return [ { id: "expander", Header: ({ allColumns, state: { groupBy } }: any) => { return groupBy.map((columnId: any) => { const column = allColumns.find((d: any) => d.id === columnId); return ( {" "} {column.canGroupBy ? ( {column.isGrouped ? ( ) : ( )} ) : null} {column.render("Header")} ); }); }, Cell: ({ row }: any) => { if (row.canExpand) { const groupedCell = row.allCells.find((d: any) => d.isGrouped); return ( {row.isExpanded ? ( ) : ( )}{" "} {groupedCell.render("Cell")} {row.subRows.length} ); } return null; } }, ...columns ]; }); }; const plugins: any[] = [ useGroupBy, useGlobalFilter, isSortable ? useSortBy : null, useExpanded, useExpanderHook, showPagination ? usePagination : null, isMultiSelect ? useRowSelect : null, isMultiSelect ? useMultiSelectHook : null ]; const { getTableProps, getTableBodyProps, headerGroups, prepareRow, page, canPreviousPage, canNextPage, pageOptions, pageCount, gotoPage, allColumns, nextPage, toggleGroupBy, previousPage, setPageSize, selectedFlatRows, preGlobalFilteredRows, setGlobalFilter, rows, state: { pageIndex, pageSize, groupBy, expanded, globalFilter } } = useTable( { columns, data, initialState: { pageSize: 20 }, updateMyData }, ...plugins.filter((p) => p !== null) ); useCallback(() => { onRowSelection && onRowSelection(selectedFlatRows); }, [selectedFlatRows && onRowSelection]); return ( {headerGroups.map((headerGroup) => ( {headerGroup.headers.map((column: any) => ( {column.render("Header")} {column.isSorted && ( ) : ( ) } /> )} ))} ))} {(page || rows).map((row, i) => { prepareRow(row); return ( {row.cells.map((cell: any) => ( {cell.isGrouped ? ( // If it's a grouped cell, add an expander and row count <> {row.isExpanded ? ( ) : ( )} {" "} {cell.render("Cell")} ({row.subRows.length}) > ) : cell.isAggregated ? ( cell.render("Aggregated") ) : cell.isPlaceholder ? null : ( // For cells with repeated values, render null // Otherwise, just render the regular cell cell.render("Cell") )} ))} ); })} {showPagination && ( )} ); }; export default DataGrid;