( table: Table<TData>, columnDef: ColumnDef<TData, TValue>, depth: number, parent?: Column<TData, TValue> )
| 63 | } |
| 64 | |
| 65 | export function createColumn<TData extends RowData, TValue>( |
| 66 | table: Table<TData>, |
| 67 | columnDef: ColumnDef<TData, TValue>, |
| 68 | depth: number, |
| 69 | parent?: Column<TData, TValue> |
| 70 | ): Column<TData, TValue> { |
| 71 | const defaultColumn = table._getDefaultColumnDef() |
| 72 | |
| 73 | const resolvedColumnDef = { |
| 74 | ...defaultColumn, |
| 75 | ...columnDef, |
| 76 | } as ColumnDefResolved<TData> |
| 77 | |
| 78 | const accessorKey = resolvedColumnDef.accessorKey |
| 79 | |
| 80 | let id = |
| 81 | resolvedColumnDef.id ?? |
| 82 | (accessorKey |
| 83 | ? typeof String.prototype.replaceAll === 'function' |
| 84 | ? accessorKey.replaceAll('.', '_') |
| 85 | : accessorKey.replace(/\./g, '_') |
| 86 | : undefined) ?? |
| 87 | (typeof resolvedColumnDef.header === 'string' |
| 88 | ? resolvedColumnDef.header |
| 89 | : undefined) |
| 90 | |
| 91 | let accessorFn: AccessorFn<TData> | undefined |
| 92 | |
| 93 | if (resolvedColumnDef.accessorFn) { |
| 94 | accessorFn = resolvedColumnDef.accessorFn |
| 95 | } else if (accessorKey) { |
| 96 | // Support deep accessor keys |
| 97 | if (accessorKey.includes('.')) { |
| 98 | accessorFn = (originalRow: TData) => { |
| 99 | let result = originalRow as Record<string, any> |
| 100 | |
| 101 | for (const key of accessorKey.split('.')) { |
| 102 | result = result?.[key] |
| 103 | if (process.env.NODE_ENV !== 'production' && result === undefined) { |
| 104 | console.warn( |
| 105 | `"${key}" in deeply nested key "${accessorKey}" returned undefined.` |
| 106 | ) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return result |
| 111 | } |
| 112 | } else { |
| 113 | accessorFn = (originalRow: TData) => |
| 114 | (originalRow as any)[resolvedColumnDef.accessorKey] |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (!id) { |
| 119 | if (process.env.NODE_ENV !== 'production') { |
| 120 | throw new Error( |
| 121 | resolvedColumnDef.accessorFn |
| 122 | ? `Columns require an id when using an accessorFn` |
no test coverage detected
searching dependent graphs…