|
| 1 | +import { For } from 'solid-js' |
| 2 | +import { useTableDevtoolsContext } from '../TableContextProvider' |
| 3 | +import { useTableStore } from '../useTableStore' |
| 4 | +import { useStyles } from '../styles/use-styles' |
| 5 | + |
| 6 | +import type { Column, RowData, TableFeatures } from '@tanstack/table-core' |
| 7 | + |
| 8 | +type AnyColumn = Column<TableFeatures, RowData, unknown> |
| 9 | + |
| 10 | +function getColumnDefSummary(column: AnyColumn): string { |
| 11 | + const def = column.columnDef as Record<string, unknown> |
| 12 | + const header = def.header |
| 13 | + const accessorKey = def.accessorKey |
| 14 | + const accessorFn = def.accessorFn |
| 15 | + const parts: Array<string> = [] |
| 16 | + if (typeof accessorKey === 'string') parts.push(`key: ${accessorKey}`) |
| 17 | + if (typeof accessorFn === 'function') parts.push('accessorFn') |
| 18 | + if (header !== undefined) { |
| 19 | + const headerStr = |
| 20 | + typeof header === 'string' |
| 21 | + ? header |
| 22 | + : typeof header === 'function' |
| 23 | + ? '[fn]' |
| 24 | + : String(header) |
| 25 | + parts.push(`header: ${headerStr}`) |
| 26 | + } |
| 27 | + return parts.length > 0 ? parts.join(', ') : '-' |
| 28 | +} |
| 29 | + |
| 30 | +export function ColumnsPanel() { |
| 31 | + const styles = useStyles() |
| 32 | + const { table } = useTableDevtoolsContext() |
| 33 | + |
| 34 | + const tableInstance = table() |
| 35 | + const tableState = useTableStore( |
| 36 | + tableInstance ? tableInstance.store : undefined, |
| 37 | + (state) => state, |
| 38 | + ) |
| 39 | + |
| 40 | + const getColumns = (): Array<AnyColumn> => { |
| 41 | + tableState?.() |
| 42 | + if (!tableInstance) return [] |
| 43 | + |
| 44 | + const tableWithColumnFns = tableInstance as unknown as { |
| 45 | + getAllFlatColumns?: () => Array<AnyColumn> |
| 46 | + getAllLeafColumns?: () => Array<AnyColumn> |
| 47 | + } |
| 48 | + |
| 49 | + return ( |
| 50 | + tableWithColumnFns.getAllFlatColumns?.() ?? |
| 51 | + tableWithColumnFns.getAllLeafColumns?.() ?? |
| 52 | + [] |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + const columns = getColumns() |
| 57 | + |
| 58 | + if (!tableInstance) { |
| 59 | + return ( |
| 60 | + <div class={styles().panelScroll}> |
| 61 | + <div class={styles().sectionTitle}>Columns</div> |
| 62 | + <div class={styles().rowModelItem}> |
| 63 | + No table instance is connected. Pass a table instance to |
| 64 | + TableDevtoolsPanel. |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <div class={styles().panelScroll}> |
| 72 | + <div class={styles().sectionTitle}>Columns ({columns.length})</div> |
| 73 | + <div class={styles().tableWrapper}> |
| 74 | + <table class={styles().rowsTable}> |
| 75 | + <thead> |
| 76 | + <tr> |
| 77 | + <th class={styles().headerCell}>#</th> |
| 78 | + <th class={styles().headerCell}>id</th> |
| 79 | + <th class={styles().headerCell}>depth</th> |
| 80 | + <th class={styles().headerCell}>accessor</th> |
| 81 | + <th class={styles().headerCell}>columnDef</th> |
| 82 | + </tr> |
| 83 | + </thead> |
| 84 | + <tbody> |
| 85 | + <For each={columns}> |
| 86 | + {(column, index) => ( |
| 87 | + <tr> |
| 88 | + <td class={styles().bodyCellMono}>{index() + 1}</td> |
| 89 | + <td class={styles().bodyCellMono}>{column.id}</td> |
| 90 | + <td class={styles().bodyCellMono}>{column.depth}</td> |
| 91 | + <td class={styles().bodyCellMono}> |
| 92 | + {column.accessorFn ? '✓' : '○'} |
| 93 | + </td> |
| 94 | + <td class={styles().bodyCell}> |
| 95 | + {getColumnDefSummary(column)} |
| 96 | + </td> |
| 97 | + </tr> |
| 98 | + )} |
| 99 | + </For> |
| 100 | + </tbody> |
| 101 | + </table> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + ) |
| 105 | +} |
0 commit comments