import type { TableOptions, SortingState, Table as TableType } from "@tanstack/react-table"; import { useReactTable, getCoreRowModel, getSortedRowModel } from "@tanstack/react-table"; import React, { HTMLProps, useMemo } from "react"; import { Table } from "../table/react-table"; import { createColumnHelper } from "@tanstack/react-table"; import { Icon } from "../icon"; import { Menu, MenuItem } from "../menu"; import { withInjectables } from "@ogre-tools/injectable-react"; import getRandomIdInjectable from "../../../common/utils/get-random-id.injectable"; interface TableProps extends TableOptions { className?: string; selectable?: boolean; configurable?: boolean; } export function TableList({ columns, data, className, selectable = true, configurable = true, }: TableProps) { const [sorting, setSorting] = React.useState([]) const tableColumns = useMemo(() => { const cols = [ ...columns ]; const columnHelper = createColumnHelper() if (selectable) { cols.unshift( columnHelper.display({ id: "select", header: ({ table }) => ( ), cell: ({ row }) => ( ), }) ) } if (configurable) { cols.push( columnHelper.display({ id: "config", header: () => , }) ) } return cols; }, [data]); const table = useReactTable({ data, columns: tableColumns, getCoreRowModel: getCoreRowModel(), state: { sorting, }, onSortingChange: setSorting, getSortedRowModel: getSortedRowModel(), }); return ( ) } function IndeterminateCheckbox({ indeterminate, className = '', ...rest }: { indeterminate?: boolean } & HTMLProps) { const ref = React.useRef(null!) React.useEffect(() => { if (typeof indeterminate === 'boolean') { ref.current.indeterminate = !rest.checked && indeterminate } }, [ref, indeterminate]) return ( ) } interface Dependencies { id: string; } interface Props { table: TableType } function NonInjectableColumnConfigMenu(props: Dependencies & Props) { const id = `${props.id}-column-config`; const [open, setOpen] = React.useState(false); return ( <> setOpen(true)} close={() => setOpen(false)} closeOnScroll closeOnClickItem closeOnClickOutside usePortal > {props.table.getAllLeafColumns().map(column => { return ( ) })} ); } export const ColumnConfigMenu = withInjectables(NonInjectableColumnConfigMenu, { getProps: (di, props) => ({ id: di.inject(getRandomIdInjectable)(), ...props, }), });