|
| 1 | +/* eslint-disable */ |
| 2 | +// @ts-nocheck |
| 3 | +// todo: dev |
| 4 | + |
| 5 | +import type { OnChangeFn, RowData, Table, TableFeature, Updater } from '@tanstack/react-table'; |
| 6 | +import { makeStateUpdater } from '@tanstack/react-table'; |
| 7 | +import { functionalUpdate } from '@tanstack/react-table'; |
| 8 | +import { useEffect } from 'react'; |
| 9 | + |
| 10 | +// Use declaration merging to add our new feature APIs and state types to TanStack Table's existing types. |
| 11 | +declare module '@tanstack/react-table' { |
| 12 | + //merge our new feature's state with the existing table state |
| 13 | + interface TableState extends DensityTableState {} |
| 14 | + //merge our new feature's options with the existing table options |
| 15 | + interface TableOptionsResolved<TData extends RowData> extends DensityOptions {} |
| 16 | + //merge our new feature's instance APIs with the existing table instance APIs |
| 17 | + interface Table<TData extends RowData> extends DensityInstance {} |
| 18 | + // if you need to add cell instance APIs... |
| 19 | + // interface Cell<TData extends RowData, TValue> extends DensityCell |
| 20 | + // if you need to add row instance APIs... |
| 21 | + // interface Row<TData extends RowData> extends DensityRow |
| 22 | + // if you need to add column instance APIs... |
| 23 | + // interface Column<TData extends RowData, TValue> extends DensityColumn |
| 24 | + // if you need to add header instance APIs... |
| 25 | + // interface Header<TData extends RowData, TValue> extends DensityHeader |
| 26 | + |
| 27 | + // Note: declaration merging on `ColumnDef` is not possible because it is a type, not an interface. |
| 28 | + // But you can still use declaration merging on `ColumnDef.meta` |
| 29 | +} |
| 30 | + |
| 31 | +export type DensityState = 'sm' | 'md' | 'lg'; |
| 32 | +export interface DensityTableState { |
| 33 | + density: DensityState; |
| 34 | +} |
| 35 | +// define types for our new feature's table options |
| 36 | +export interface DensityOptions { |
| 37 | + enableDensity?: boolean; |
| 38 | + onDensityChange?: OnChangeFn<DensityState>; |
| 39 | +} |
| 40 | + |
| 41 | +// Define types for our new feature's table APIs |
| 42 | +export interface DensityInstance { |
| 43 | + setDensity: (updater: Updater<DensityState>) => void; |
| 44 | + toggleDensity: (value?: DensityState) => void; |
| 45 | +} |
| 46 | + |
| 47 | +export const DensityFeature: TableFeature<any> = { |
| 48 | + // define the new feature's initial state |
| 49 | + getInitialState: (state): DensityTableState => { |
| 50 | + return { |
| 51 | + density: 'md', |
| 52 | + ...state |
| 53 | + }; |
| 54 | + }, |
| 55 | + |
| 56 | + // define the new feature's default options |
| 57 | + getDefaultOptions: <TData extends RowData>(table: Table<TData>): DensityOptions => { |
| 58 | + return { |
| 59 | + enableDensity: true, |
| 60 | + onDensityChange: makeStateUpdater('density', table) |
| 61 | + } as DensityOptions; |
| 62 | + }, |
| 63 | + // if you need to add a default column definition... |
| 64 | + // getDefaultColumnDef: <TData extends RowData>(): Partial<ColumnDef<TData>> => { |
| 65 | + // return { meta: {} } //use meta instead of directly adding to the columnDef to avoid typescript stuff that's hard to workaround |
| 66 | + // }, |
| 67 | + |
| 68 | + // define the new feature's table instance methods |
| 69 | + createTable: <TData extends RowData>(table: Table<TData>): void => { |
| 70 | + table.setDensity = (updater) => { |
| 71 | + const safeUpdater: Updater<DensityState> = (old) => { |
| 72 | + const newState = functionalUpdate(updater, old); |
| 73 | + return newState; |
| 74 | + }; |
| 75 | + return table.options.onDensityChange?.(safeUpdater); |
| 76 | + }; |
| 77 | + table.toggleDensity = (value) => { |
| 78 | + table.setDensity((old) => { |
| 79 | + if (value) return value; |
| 80 | + return old === 'lg' ? 'md' : old === 'md' ? 'sm' : 'lg'; //cycle through the 3 options |
| 81 | + }); |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + // if you need to add row instance APIs... |
| 86 | + // createRow: <TData extends RowData>(row, table): void => {}, |
| 87 | + // if you need to add cell instance APIs... |
| 88 | + // createCell: <TData extends RowData>(cell, column, row, table): void => {}, |
| 89 | + // if you need to add column instance APIs... |
| 90 | + // createColumn: <TData extends RowData>(column, table): void => {}, |
| 91 | + // if you need to add header instance APIs... |
| 92 | + // createHeader: <TData extends RowData>(header, table): void => {}, |
| 93 | +}; |
| 94 | + |
| 95 | +export const ColumnModesFeature: TableFeature<any> = { |
| 96 | + // define the new feature's initial state |
| 97 | + getInitialState: (state): any => { |
| 98 | + return { ...state, tableWidth: 0 }; |
| 99 | + }, |
| 100 | + |
| 101 | + // define the new feature's default options |
| 102 | + getDefaultOptions: <TData extends RowData>(table: Table<TData>): any => { |
| 103 | + return { |
| 104 | + columnMode: 'Default' |
| 105 | + }; |
| 106 | + }, |
| 107 | + // if you need to add a default column definition... |
| 108 | + // getDefaultColumnDef: <TData extends RowData>(): Partial<ColumnDef<TData>> => { |
| 109 | + // return { meta: {} } //use meta instead of directly adding to the columnDef to avoid typescript stuff that's hard to workaround |
| 110 | + // }, |
| 111 | + |
| 112 | + // define the new feature's table instance methods |
| 113 | + createTable: <TData extends RowData>(table: Table<TData>): void => { |
| 114 | + const state = table.getState(); |
| 115 | + // console.log(table); |
| 116 | + }, |
| 117 | + |
| 118 | + // if you need to add row instance APIs... |
| 119 | + // createRow: <TData extends RowData>(row, table): void => {}, |
| 120 | + // if you need to add cell instance APIs... |
| 121 | + // createCell: <TData extends RowData>(cell, column, row, table): void => {}, |
| 122 | + // if you need to add column instance APIs... |
| 123 | + createColumn: <TData extends RowData>(column, table): void => { |
| 124 | + console.log(column); |
| 125 | + // console.log(column); |
| 126 | + // column.columnDef.size = 2000; |
| 127 | + // console.log(column, table.getState()); |
| 128 | + } |
| 129 | + // if you need to add header instance APIs... |
| 130 | + // createHeader: <TData extends RowData>(header, table): void => {}, |
| 131 | +}; |
0 commit comments