1- import type { KeyboardEvent , MouseEvent , ReactNode } from 'react'
1+ import type { KeyboardEvent , MouseEvent } from 'react'
22import { useCallback , useContext , useEffect , useMemo , useRef } from 'react'
33
4+ import { CellCallbacksContext , RenderCellContentContext , StringifyContext } from '../contexts/CellConfigurationContext.js'
45import { ColumnWidthsContext } from '../contexts/ColumnWidthsContext.js'
5- import type { ResolvedValue } from '../helpers/dataframe/index.js'
66import { useCellFocus } from '../hooks/useCellFocus.js'
77import { useOnCopy } from '../hooks/useOnCopyToClipboard.js'
88
9- export interface CellContentProps {
10- stringify : ( value : unknown ) => string | undefined
11- cell ?: ResolvedValue
12- col : number
13- row ?: number // the row index in the original data, undefined if the value has not been fetched yet
14- }
15-
169interface Props {
1710 /** aria column index */
1811 ariaColIndex : number
@@ -22,30 +15,23 @@ interface Props {
2215 columnIndex : number
2316 /** index in the visible columns array (used for styling/widths) */
2417 visibleColumnIndex : number
25- /** function to stringify the cell value, used for default rendering and for copy to clipboard */
26- stringify : ( value : unknown ) => string | undefined
2718 /** cell value, undefined if the value has not been fetched yet, or if the value is actually undefined. Use hasResolved to distinguish these cases. */
2819 cellValue ?: unknown
2920 /** whether the cell value has been resolved */
3021 hasResolved ?: boolean
3122 /** class name */
3223 className ?: string
33- /** double click callback */
34- onDoubleClickCell ?: ( event : MouseEvent , col : number , row : number ) => void
35- /** mouse down callback */
36- onMouseDownCell ?: ( event : MouseEvent , col : number , row : number ) => void
37- /** key down callback, for accessibility, it should be passed if onDoubleClickCell is passed. It can handle more than that action though. */
38- onKeyDownCell ?: ( event : KeyboardEvent , col : number , row : number ) => void
3924 /** the row index in the original data, undefined if the value has not been fetched yet */
4025 rowNumber ?: number
41- /** custom cell content component, if not provided, the default stringified value will be used */
42- renderCellContent ?: ( props : CellContentProps ) => ReactNode
4326}
4427
4528/**
4629 * Render a table cell <td> with title and optional custom rendering
4730 */
48- export default function Cell ( { cellValue, hasResolved, onDoubleClickCell, onMouseDownCell, onKeyDownCell, stringify, columnIndex, visibleColumnIndex, className, ariaColIndex, ariaRowIndex, rowNumber, renderCellContent } : Props ) {
31+ export default function Cell ( { cellValue, hasResolved, columnIndex, visibleColumnIndex, className, ariaColIndex, ariaRowIndex, rowNumber } : Props ) {
32+ const { onDoubleClickCell, onMouseDownCell, onKeyDownCell } = useContext ( CellCallbacksContext )
33+ const stringify = useContext ( StringifyContext )
34+ const renderCellContent = useContext ( RenderCellContentContext )
4935 const { tabIndex, navigateToCell, focusIfNeeded } = useCellFocus ( { ariaColIndex, ariaRowIndex } )
5036
5137 const cell = useMemo ( ( ) => {
@@ -79,7 +65,9 @@ export default function Cell({ cellValue, hasResolved, onDoubleClickCell, onMous
7965 }
8066 } , [ str ] )
8167 const content = useMemo ( ( ) => {
82- return renderCellContent ?.( { cell, stringify, col : columnIndex , row : rowNumber } ) ?? str
68+ return renderCellContent === undefined
69+ ? str
70+ : renderCellContent ( { cell, stringify, col : columnIndex , row : rowNumber } )
8371 } , [ cell , stringify , columnIndex , rowNumber , renderCellContent , str ] )
8472
8573 const handleMouseDown = useCallback ( ( event : MouseEvent ) => {
0 commit comments