mirror of https://github.com/grafana/grafana
TableNG: Use same hover behavior for one and multiple AutoCell links (#108194)
parent
08afd73c0c
commit
4b1558b3ea
@ -1,13 +1,14 @@ |
||||
import { formattedValueToString } from '@grafana/data'; |
||||
|
||||
import { renderSingleLink } from '../../DataLinksActionsTooltip'; |
||||
import { useSingleLink } from '../hooks'; |
||||
import { MaybeWrapWithLink } from '../MaybeWrapWithLink'; |
||||
import { AutoCellProps } from '../types'; |
||||
|
||||
export default function AutoCell({ value, field, rowIdx }: AutoCellProps) { |
||||
const displayValue = field.display!(value); |
||||
const formattedValue = formattedValueToString(displayValue); |
||||
const link = useSingleLink(field, rowIdx); |
||||
|
||||
return link != null ? renderSingleLink(link, formattedValue) : formattedValue; |
||||
return ( |
||||
<MaybeWrapWithLink field={field} rowIdx={rowIdx}> |
||||
{formattedValue} |
||||
</MaybeWrapWithLink> |
||||
); |
||||
} |
||||
|
@ -0,0 +1,32 @@ |
||||
import { memo, ReactNode } from 'react'; |
||||
|
||||
import { Field } from '@grafana/data'; |
||||
|
||||
import { renderSingleLink } from '../DataLinksActionsTooltip'; |
||||
|
||||
import { getCellLinks } from './utils'; |
||||
|
||||
interface MaybeWrapWithLinkProps { |
||||
field: Field; |
||||
rowIdx: number; |
||||
children: ReactNode; |
||||
} |
||||
|
||||
export const MaybeWrapWithLink = memo(({ field, rowIdx, children }: MaybeWrapWithLinkProps): ReactNode => { |
||||
const linksCount = field.config.links?.length ?? 0; |
||||
const actionsCount = field.config.actions?.length ?? 0; |
||||
|
||||
// as real, single link
|
||||
if (linksCount === 1 && actionsCount === 0) { |
||||
let link = (getCellLinks(field, rowIdx) ?? [])[0]; |
||||
return renderSingleLink(link, children); |
||||
} |
||||
// as faux link that acts as hit-area for tooltip activation
|
||||
else if (linksCount + actionsCount > 0) { |
||||
// eslint-disable-next-line jsx-a11y/anchor-is-valid
|
||||
return <a aria-haspopup="menu">{children}</a>; |
||||
} |
||||
|
||||
// raw value
|
||||
return children; |
||||
}); |
Loading…
Reference in new issue