mirror of https://github.com/grafana/grafana
Chore: Add react-table typings to Table (#21418)
* add typings * introduce tyings and refactor accordingly * extract setting celltype * update tests to reflect changes * removing unused things * renaming getCellType -> getCellDisplayType * fix width type error * remove caret * move cell back to utils, fix story * remove unused import * rename typepull/21462/head
parent
b6c75b10d1
commit
30eef76162
@ -0,0 +1,30 @@ |
||||
import React, { CSSProperties, FC } from 'react'; |
||||
import { TableCellProps } from './types'; |
||||
import tinycolor from 'tinycolor2'; |
||||
import { formattedValueToString } from '@grafana/data'; |
||||
|
||||
export const BackgroundColoredCell: FC<TableCellProps> = props => { |
||||
const { cell, tableStyles, field } = props; |
||||
|
||||
if (!field.display) { |
||||
return null; |
||||
} |
||||
|
||||
const themeFactor = tableStyles.theme.isDark ? 1 : -0.7; |
||||
const displayValue = field.display(cell.value); |
||||
|
||||
const bgColor2 = tinycolor(displayValue.color) |
||||
.darken(10 * themeFactor) |
||||
.spin(5) |
||||
.toRgbString(); |
||||
|
||||
const styles: CSSProperties = { |
||||
background: `linear-gradient(120deg, ${bgColor2}, ${displayValue.color})`, |
||||
borderRadius: '0px', |
||||
color: 'white', |
||||
height: tableStyles.cellHeight, |
||||
padding: tableStyles.cellPadding, |
||||
}; |
||||
|
||||
return <div style={styles}>{formattedValueToString(displayValue)}</div>; |
||||
}; |
@ -0,0 +1,37 @@ |
||||
import React, { FC } from 'react'; |
||||
import { Cell } from 'react-table'; |
||||
import { Field } from '@grafana/data'; |
||||
import { getTextAlign } from './utils'; |
||||
import { TableFilterActionCallback } from './types'; |
||||
import { TableStyles } from './styles'; |
||||
|
||||
interface Props { |
||||
cell: Cell; |
||||
field: Field; |
||||
tableStyles: TableStyles; |
||||
onCellClick?: TableFilterActionCallback; |
||||
} |
||||
|
||||
export const TableCell: FC<Props> = ({ cell, field, tableStyles, onCellClick }) => { |
||||
const filterable = field.config.filterable; |
||||
const cellProps = cell.getCellProps(); |
||||
|
||||
let onClick: ((event: React.SyntheticEvent) => void) | undefined = undefined; |
||||
if (filterable && onCellClick) { |
||||
if (cellProps.style) { |
||||
cellProps.style.cursor = 'pointer'; |
||||
} |
||||
|
||||
onClick = () => onCellClick(cell.column.Header as string, cell.value); |
||||
} |
||||
const fieldTextAlign = getTextAlign(field); |
||||
if (fieldTextAlign && cellProps.style) { |
||||
cellProps.style.textAlign = fieldTextAlign; |
||||
} |
||||
|
||||
return ( |
||||
<div {...cellProps} onClick={onClick}> |
||||
{cell.render('Cell', { field, tableStyles })} |
||||
</div> |
||||
); |
||||
}; |
Loading…
Reference in new issue