import React, { useEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { columns as mockColumns } from '../../mocks/mockTable'; import { config } from './constants/FORM_2/AHR'; import { useParams } from 'react-router'; function EditCellPortal({ cell, table, EditCell, onSaveStart, onSaveEnd, onError, isEditable }) { const { formId, formType, sheetName, direction } = useParams(); const tableKey = `${formId}_${formType}_${sheetName}_${direction}`; const ref = useRef(null); const [refTbody, setRefTbody] = useState(false); const [isSaving, setIsSaving] = useState(false); const isDisabled = false; useEffect(() => { if (ref.current) { const tbodyRef = ref.current.offsetParent?.offsetParent?.offsetParent; setRefTbody(tbodyRef); } }, []); const handleChange = async (val) => { table.setEditingCell(null); try { if (table.options.meta?.updateCell) { const success = await table.options.meta.updateCell( cell.row, cell.column, val ); if (success) { console.log('Cell updated successfully via WebSocket'); } else { console.error('Failed to update cell via WebSocket'); const errorMsg = 'Не удалось сохранить изменение'; onError?.(errorMsg); } } } catch (error) { console.error('Error updating cell:', error); onError?.(error.message); } }; return ( <>
{refTbody && !isSaving && createPortal( , refTbody, )} {isSaving && ( createPortal(
Saving...
, refTbody ) )} ); } export const getTableColumns = ({ Cell, EditCell, columnsConfig, onCellUpdateError, onCellNumberClick }) => { const columnColors = { ...columnsConfig.colors }; const columns = [...columnsConfig.columns]; const processColumns = (columns, EditCell, Cell) => { columns.forEach((col) => { if (col.header.length > 15) { col.size = 250; } if(col.header.length >= 20){ col.size = 350; } if(col.header.length >= 30){ col.size = 400; } col.Cell = ({ cell, table, column, row }) => ( ); col.Edit = ({ cell, table, row, column }) => ( ); // Рекурсивно обрабатываем вложенные колонки if (col.columns?.length) { processColumns(col.columns, EditCell, Cell); } }); }; processColumns(columns, EditCell, Cell); const rowNumber = { accessorKey: 'sort_order', header: '#', size: 50, enableEditing: false, Cell: ({ cell, row }) => { return }, }; return [rowNumber, ...columns]; };