| 1 |
/** |
| 2 |
* resolve-row-key.ts |
| 3 |
* |
| 4 |
* Forked from reactabular-table version 8.14.0 |
| 5 |
* https://github.com/reactabular/reactabular/tree/v8.14.0/packages/reactabular-table/src |
| 6 |
*/ |
| 7 |
|
| 8 |
import { RowType, RowKeyType } from '../../../../components/Table/base/types'; |
| 9 |
|
| 10 |
/** |
| 11 |
* @param {{rowData: RowType, rowIndex: number, rowKey: RowKeyType}} rowData - row data |
| 12 |
*/ |
| 13 |
export function resolveRowKey({ |
| 14 |
rowData, |
| 15 |
rowIndex, |
| 16 |
rowKey |
| 17 |
}: { |
| 18 |
rowData: RowType; |
| 19 |
rowIndex: number; |
| 20 |
rowKey: RowKeyType; |
| 21 |
}) { |
| 22 |
if (typeof rowKey === 'function') { |
| 23 |
return `${rowKey({ rowData, rowIndex })}-row`; |
| 24 |
} else if (process.env.NODE_ENV !== 'production') { |
| 25 |
// Arrays cannot have rowKeys by definition so we have to go by index there. |
| 26 |
if (!Array.isArray(rowData) && (rowData as any)[rowKey] === undefined) { |
| 27 |
// eslint-disable-next-line no-console |
| 28 |
console.warn('Table.Body - Missing valid rowKey!', rowData, rowKey); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
if ((rowData as any)[rowKey] === 0) { |
| 33 |
return `${(rowData as any)[rowKey] as string}-row`; |
| 34 |
} |
| 35 |
|
| 36 |
return `${((rowData as any)[rowKey] as string) || rowIndex}-row`; |
| 37 |
} |