@shakibdshy/react-tablegrid
Version:
A modern, flexible, headless and advanced table component for React with grid layout
134 lines (129 loc) • 4.92 kB
text/typescript
import { Column, SortDirection, TableState } from './types.mjs';
import { ClassValue } from 'clsx';
import 'react';
/**
* Helper functions for creating and managing table columns
* Provides type-safe column definitions with various configuration options
*/
/**
* Column definition with display options
* @template TData - Type of data being displayed
* @template TValue - Type of value in the column
*/
type DisplayColumnDef<TData, TValue = unknown> = Omit<Column<TData>, "id" | "accessorKey"> & {
/** Optional column identifier */
id?: keyof TData;
/** Custom cell renderer function */
cell?: (props: {
value: TValue;
row: TData;
}) => React.ReactNode;
};
/**
* Column definition with grouping support
* @template TData - Type of data being displayed
* @template TValue - Type of value in the column
*/
type GroupColumnDef<TData, TValue = unknown> = DisplayColumnDef<TData, TValue> & {
/** Nested columns within the group */
columns?: Column<TData>[];
};
interface ColumnHelper<TData> {
accessor: <TKey extends keyof TData & string>(accessorKey: TKey, columnDef?: Omit<DisplayColumnDef<TData, TData[TKey]>, "id" | "accessorKey">) => Column<TData>;
display: (columnDef: DisplayColumnDef<TData>) => Column<TData>;
group: (columnDef: GroupColumnDef<TData>) => Column<TData>;
}
/**
* Creates a column helper with type-safe methods for defining columns
* @template TData - Type of data being displayed in the table
*/
declare function createColumnHelper<TData>(): ColumnHelper<TData>;
declare function cn(...inputs: ClassValue[]): string;
/**
* Helper functions for table operations and data manipulation
*/
/**
* Sorts table data based on column and direction
* @template T - Type of data being sorted
* @param data - Array of data to sort
* @param column - Column to sort by
* @param direction - Sort direction
* @returns Sorted array
*/
declare function sortTableData<T>(data: T[], column: Column<T>, direction: SortDirection): T[];
/**
* Filters table data based on filter value and columns
* @template T - Type of data being filtered
* @param data - Array of data to filter
* @param columns - Array of columns to search in
* @param filterValue - Value to filter by
* @returns Filtered array
*/
declare function filterTableData<T>(data: T[], columns: Column<T>[], filterValue: string): T[];
/**
* Gets the grid template columns CSS value based on column widths
* @template T - Type of data in the table
* @param columns - Array of columns
* @param columnSizing - Column sizing configuration
* @returns CSS grid-template-columns value
*/
declare function getGridTemplateColumns<T>(columns: Column<T>[], columnSizing?: {
columnSizes: {
[key: string]: number;
};
}): string;
/**
* Reorders columns based on pinned status
* @template T - Type of data in the table
* @param columns - Array of columns to reorder
* @param pinnedColumns - Configuration of pinned columns
* @returns Reordered array of columns
*/
declare function reorderColumns<T>(columns: Column<T>[], pinnedColumns: {
left: Array<keyof T>;
right: Array<keyof T>;
}): Column<T>[];
/**
* Groups columns by their group property
* @template T - Type of data in the table
* @param columns - Array of columns to group
* @returns Object with grouped columns
*/
declare function groupColumns<T>(columns: Column<T>[]): {
[key: string]: Column<T>[];
};
/**
* Calculates column width based on content
* @template T - Type of data in the table
* @param column - Column to calculate width for
* @param data - Table data
* @param minWidth - Minimum width in pixels
* @returns Calculated width in pixels
*/
declare function calculateColumnWidth<T>(column: Column<T>, data: T[], minWidth?: number): number;
/**
* Creates initial table state
* @template T - Type of data in the table
* @param data - Initial data
* @param columns - Table columns
* @returns Initial table state
*/
declare function createInitialTableState<T>(data: T[], columns: Column<T>[]): TableState<T>;
/**
* Gets visible columns based on state
* @template T - Type of data in the table
* @param columns - All columns
* @param visibleColumns - Array of visible column IDs
* @returns Array of visible columns
*/
declare function getVisibleColumns<T>(columns: Column<T>[], visibleColumns: Array<keyof T>): Column<T>[];
/**
* Processes table data through sorting and filtering
* @template T - Type of data in the table
* @param data - Raw table data
* @param state - Current table state
* @param columns - Table columns
* @returns Processed data
*/
declare function processTableData<T>(data: T[], state: TableState<T>, columns: Column<T>[]): T[];
export { calculateColumnWidth, cn, createColumnHelper, createInitialTableState, filterTableData, getGridTemplateColumns, getVisibleColumns, groupColumns, processTableData, reorderColumns, sortTableData };