import React from 'react'; import { NoData } from 'sync-ql'; import { CellFactoryFunction, DataCellType, DataRow } from '../types/cell.type'; import { DataOption } from '../types/filter.type'; import { DataColumnExtra, TableMessages } from '../types/table.type'; import { SetEditor } from './body.auto'; import { BooleanEditor } from './body.boolean'; import { Value } from './body.content'; import { DateEditor } from './body.date'; import { NumberEditor } from './body.number'; import { TextEditor } from './body.text'; import { History } from './body.history'; import { TableBodyCell, TableBodyCellContent } from './style'; export interface DataTableBodyCellProps { tableRef: any, messages: TableMessages, style: React.CSSProperties, align?: "left" | "center" | "right" | "justify" | "inherit", value: DataCellType | CellFactoryFunction, row: DataRow, column: DataColumnExtra, index: number, editting: boolean, modified: boolean, options: (value: [string, string]) => Promise, onChange: (value: any) => void, onEditting: (editting: boolean) => void, } export const DataTableBodyCell: React.FC = ({ tableRef, messages, style, align, value, row, column, index, editting, modified, options, onChange, onEditting }) => { const cellRef = React.useRef(); const [visible, setVisible] = React.useState(NoData); React.useLayoutEffect(() => { const handleScroll = () => { if (cellRef.current) { const cellRect = cellRef.current.getBoundingClientRect(); const tableRect = tableRef.current.getBoundingClientRect(); const hidden = cellRect.right <= tableRect.left || cellRect.left >= tableRect.right || cellRect.bottom <= tableRect.top || cellRect.top >= tableRect.bottom; if (!hidden !== visible) { setVisible(!hidden) } } } if (visible === NoData) { handleScroll(); } if (tableRef && tableRef.current) { tableRef.current.addEventListener('scroll', handleScroll); } else { window.addEventListener('scroll', handleScroll); } return () => { if (tableRef && tableRef.current) { tableRef.current.removeEventListener('scroll', handleScroll); } else { window.removeEventListener('scroll', handleScroll); } } }, [visible]) const handleChange = (value: any) => { onChange(value); } const handleEditting = (value: boolean) => { onEditting(value); } const outputRow = { ...row }; { delete outputRow['']; } const isCellFactory = typeof value === 'function'; const currentValue = isCellFactory ? value({ value: row[column.name], row: outputRow, column, index, editting, modified, onChange: handleChange, onEditting: handleEditting }) : value const getEditor = () => { switch (column.type) { case 'boolean': return ( ) case 'number': return ( ) case 'date': return ( ) case 'set': if (options) { return ( ) } break; } return ( ) } const handleFocus = () => { if (!column.readonly) { onEditting(true); } } return ( { !!visible && ( !!column.history && (column.readonly || (column.disabled && column.disabled(row)) || !editting) &&
) } { !!visible && ( !column.readonly && (!column.disabled || !column.disabled(row)) && editting && !isCellFactory ? getEditor() : ) }
) }