-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathtable-cell.tsx
More file actions
53 lines (47 loc) · 1.5 KB
/
table-cell.tsx
File metadata and controls
53 lines (47 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React from 'react'
import { TableDataItemBase, TableAbstractColumn, TableOnCellClick } from './table-types'
interface Props<TableDataItem extends TableDataItemBase> {
columns: Array<TableAbstractColumn<TableDataItem>>
row: TableDataItem
rowIndex: number
emptyText: string
onCellClick?: TableOnCellClick<TableDataItem>
}
export type TableCellData<TableDataItem> = {
row: number
column: number
rowValue: TableDataItem
}
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props<any>>
export type TableCellProps<TableDataItem extends TableDataItemBase> =
Props<TableDataItem> & NativeAttrs
const TableCell = <TableDataItem extends TableDataItemBase>({
columns,
row,
rowIndex,
emptyText,
onCellClick,
}: TableCellProps<TableDataItem>) => {
/* eslint-disable react/jsx-no-useless-fragment */
return (
<>
{columns.map((column, index) => {
const currentRowValue = row[column.prop]
const cellValue = currentRowValue || emptyText
const shouldBeRenderElement = column.renderHandler(currentRowValue, row, rowIndex)
return (
<td
key={`row-td-${index}-${String(column.prop)}`}
onClick={() => onCellClick && onCellClick(currentRowValue, rowIndex, index)}
className={column.className}>
<div className="cell">
{shouldBeRenderElement ? shouldBeRenderElement : cellValue}
</div>
</td>
)
})}
</>
)
/* eslint-enable */
}
export default TableCell