es-grid-template
Version:
es-grid-template
814 lines (775 loc) • 28.5 kB
JavaScript
import { flexRender } from "@tanstack/react-table";
import Space from "rc-master-ui/es/space";
import Command from "../components/command/Command";
import { addRowsDown, addRowsDownWithCtrl, addRowsUp, addRowsUpWithCtrl, flattenArray, flattenData,
// getCellsByPosition,
getColIdsBetween, getCommonPinningStyles, getEditType, getRowIdsBetween, getSelectedCellMatrix, isEditable, isObjEmpty, isObjEqual, newGuid, unFlattenData, updateOrInsert } from "../hook/utils";
// import { nonActionColumn } from "../hook/constant";
import Checkbox from "rc-master-ui/es/checkbox/Checkbox";
import classNames from "classnames";
// import type { Dispatch, SetStateAction } from "react";
import React from "react";
import { TableContext } from "../useContext";
import EditableCell from "./EditableCell";
import { nonActionColumn } from "../hook/constant";
// import { nonActionColumn } from "../hook/constant";
const renderCellIndex = props => {
const {
parrents,
cell
} = props;
return /*#__PURE__*/React.createElement("span", null, parrents.map(pr => {
return `${pr.index + 1}.`;
}), cell.row.index + 1);
};
const renderCommand = args => {
const {
cell,
commandClick
} = args;
const col = cell.column.columnDef.meta ?? {};
const record = cell.row.original;
// const commandItems = args.cell.column.columnDef?.meta?.commandItems ?? []
const commands = col.commandItems ? col.commandItems.map(it => {
return {
...it,
visible: typeof it.visible === 'function' ? it.visible?.(record) : it.visible
};
}) : [];
return /*#__PURE__*/React.createElement(Space, null, commands.filter(it => it.visible !== false).map(item => {
return /*#__PURE__*/React.createElement(Command, {
key: item.id,
item: item,
record: record,
onClick: () => {
commandClick?.({
id: item.id,
// rowId: getRowKey(record, index) as any,
rowId: record.rowId,
rowData: record,
index: cell.row.index
// rows: [...resource]
});
}
});
}));
};
const renderSelection = args => {
const {
row
} = args.cell;
return /*#__PURE__*/React.createElement("div", {
style: {
// paddingLeft: `${row.depth * 2}rem`,
}
}, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Checkbox, {
checked: row.getIsSelected(),
indeterminate: row.getIsSomeSelected(),
onChange: row.getToggleSelectedHandler()
})));
};
const TableBodyCellEdit = props => {
const {
cell,
commandClick,
// tableId,
table,
isEditing,
columnVirtualizer,
rowVirtualizer
} = props;
const {
rowKey,
editingKey,
prefix,
focusedCell,
rangeState,
setFocusedCell,
setEditingKey,
setRangeState,
setRangePasteState,
endCell,
setEndCell,
isSelecting,
isPasting,
setEndPasteCell,
endPasteCell,
setIsPasting,
setIsSelecting,
setStartCell,
setStartPasteCell,
startCell,
startPasteCell,
originData,
dataSource,
triggerPaste,
handleDeleteContent,
reset,
setValue,
handleAddMulti
// expandable
} = React.useContext(TableContext);
// const expandIconColumnIndex = expandable?.expandIconColumnIndex ?? 0
const record = cell.row.original;
const canEdit = isEditable(cell.column.columnDef.meta, record);
// const allRows = table.getRowModel().rows;
const allRows = table.getRowModel().flatRows;
// const rowNumber = cell.row.index
const rowNumber = allRows.findIndex(it => it.id === cell.row.id);
const colIndex = cell.column.getIndex();
const isPinned = cell.column.getIsPinned();
const isLastLeftPinnedColumn = isPinned === "left" && cell.column.getIsLastColumn("left");
const isFirstRightPinnedColumn = isPinned === "right" && cell.column.getIsFirstColumn("right");
const isCellSelected = React.useMemo(() => {
if (!startCell || !endCell) return false;
const rowIds = getRowIdsBetween(table, startCell.rowId, endCell.rowId);
const colIds = getColIdsBetween(table, startCell.colId, endCell.colId);
return rowIds.includes(cell.row.id) && colIds.includes(cell.column.id);
}, [cell.column.id, cell.row.id, endCell, startCell, table]);
const columnMeta = cell.column.columnDef.meta ?? {};
const editType = typeof columnMeta.editType === 'function' ? columnMeta.editType(record) : columnMeta.editType;
const {
rowRange,
colRange,
rowIds,
colIds,
startColIndex,
startRowIndex,
endColIndex,
endRowIndex
} = rangeState ?? {};
const pasteRangeState = getSelectedCellMatrix(table, startPasteCell, endPasteCell);
const isInRange = colRange && rowRange && rowRange.includes(cell.row.id) && colRange.includes(cell.column.id);
const isInRangePaste = pasteRangeState.rowRange && pasteRangeState.rowRange.includes(cell.row.id) && pasteRangeState.colRange.includes(cell.column.id);
// const isInRangePaste = rowRange && rowRange.includes(cell.row.id) && colRange.includes(cell.column.id);
const rowIdx = rowIds ? rowIds.indexOf(cell.row.id) : -1;
const colIdx = colIds ? colIds.indexOf(cell.column.id) : -1;
const rowIdxPaste = pasteRangeState.rowIds.indexOf(cell.row.id);
const colIdxPaste = pasteRangeState.colIds.indexOf(cell.column.id);
const isTop = rowIdx === startRowIndex;
const isBottom = rowIdx === endRowIndex;
const isLeft = colIdx === startColIndex;
const isRight = colIdx === endColIndex;
const isTopPaste = rowIdxPaste === pasteRangeState.startRowIndex;
const isBottomPaste = rowIdxPaste === pasteRangeState.endRowIndex;
const isLeftPaste = colIdxPaste === pasteRangeState.startColIndex;
const isRightPaste = colIdxPaste === pasteRangeState.endColIndex;
const parrents = cell.row.getParentRows();
if (cell.column.id === "#") {
return /*#__PURE__*/React.createElement("td", {
key: cell.id,
className: classNames(`${prefix}-grid-cell`, {
[`${prefix}-grid-cell-ellipsis`]: true,
[`${prefix}-grid-cell-fix-left-last`]: isLastLeftPinnedColumn,
[`${prefix}-grid-cell-fix-right-first`]: isFirstRightPinnedColumn
}),
style: {
display: 'flex',
height: '36px',
width: cell.column.getSize(),
...getCommonPinningStyles(cell.column)
}
}, renderCellIndex({
parrents,
cell
}));
}
if (cell.column.id === "command") {
return /*#__PURE__*/React.createElement("td", {
key: cell.id,
className: classNames(`${prefix}-grid-cell`, {
[`${prefix}-grid-cell-ellipsis`]: true,
[`${prefix}-grid-cell-fix-left-last`]: isLastLeftPinnedColumn,
[`${prefix}-grid-cell-fix-right-first`]: isFirstRightPinnedColumn
}),
style: {
display: 'flex',
height: '36px',
width: cell.column.getSize(),
...getCommonPinningStyles(cell.column)
}
}, renderCommand({
cell,
commandClick
}));
}
if (cell.column.id === "selection_column") {
return /*#__PURE__*/React.createElement("td", {
key: cell.id,
className: classNames(`${prefix}-grid-cell cell-editable111 `, {
[`${prefix}-grid-cell-ellipsis`]: true,
[`${prefix}-grid-cell-fix-left-last`]: isLastLeftPinnedColumn,
[`${prefix}-grid-cell-fix-right-first`]: isFirstRightPinnedColumn
}),
style: {
display: 'flex',
height: '36px',
width: cell.column.getSize(),
...getCommonPinningStyles(cell.column)
}
}, renderSelection({
cell
}));
}
const triggerDragPaste = (pasteState, ctrlKey) => {
const tmpCols = table.getVisibleLeafColumns();
const rowPasteIds = getRowIdsBetween(table, startPasteCell?.rowId ?? '', endPasteCell?.rowId ?? '');
const colPasteds = getColIdsBetween(table, startPasteCell?.colId ?? '', endPasteCell?.colId ?? '');
const rowSelectIds = getRowIdsBetween(table, startCell?.rowId ?? '', endCell?.rowId ?? '');
const colSelectIds = getColIdsBetween(table, startCell?.colId ?? '', endCell?.colId ?? '');
const dataSelected = [];
rowSelectIds.forEach(rowId => {
const row = allRows.find(r => r.id === rowId);
if (!row) return;
const rowData = [];
colSelectIds.forEach(colId => {
const cellll = row.getVisibleCells().find(c => c.column.id === colId);
const value = cellll?.getValue();
rowData.push(value !== undefined ? String(value) : '');
});
dataSelected.push(rowData);
});
const copyRows = flattenData('children', dataSource).filter(it => rowSelectIds.includes(it[rowKey]));
let newRange;
if ((startRowIndex ?? 0) < (pasteRangeState.startRowIndex ?? 0)) {
// kéo xuóng
setRangeState?.(getSelectedCellMatrix(table, startCell, endPasteCell));
newRange = ctrlKey ? addRowsDownWithCtrl(dataSelected, rowPasteIds.length) : addRowsDown(dataSelected, rowPasteIds.length);
} else {
// kéo lên
setStartCell?.(endCell);
setRangeState?.(getSelectedCellMatrix(table, endCell, endPasteCell));
newRange = ctrlKey ? addRowsUpWithCtrl(dataSelected, rowPasteIds.length) : addRowsUp(dataSelected, rowPasteIds.length);
}
// Cập nhật data mới
const newData = flattenArray([...dataSource]);
const pastedRows = [];
newRange.addedRows.forEach((rowValues, rowIndex1) => {
const targetRow = pasteState.startRowIndex + rowIndex1;
// Nếu vượt quá số dòng hiện có, thêm dòng mới
if (targetRow >= newData.length) {
newData.push({
id: undefined,
rowId: newGuid()
});
}
rowValues.forEach((cellValue, colIndex1) => {
const targetCol = startColIndex + colIndex1;
if (targetCol >= tmpCols.length) {
// Không vượt quá số cột
return;
}
const columnKey = tmpCols[targetCol].id;
newData[targetRow] = {
...newData[targetRow],
[columnKey]: typeof cellValue === 'string' ? cellValue.trim() : cellValue
};
});
// Lưu dòng được paste
pastedRows.push(newData[targetRow]);
});
const rsFilterData = updateOrInsert(flattenArray([...originData]), newData);
const rs = unFlattenData(rsFilterData);
triggerPaste?.(pastedRows, colPasteds, rs, copyRows);
// // setRangeState?.(getSelectedCellMatrix(table, startCell, endPasteCell))
setEndCell?.(endPasteCell);
setEndPasteCell?.(undefined);
setStartPasteCell?.(undefined);
setRangePasteState?.(undefined);
};
const triggerPointPaste = (pasteState, cellStart, cellEnd, ctrlKey) => {
const tmpCols = table.getVisibleLeafColumns();
const rowPasteIds = getRowIdsBetween(table, cellStart?.rowId ?? '', cellEnd?.rowId ?? '');
const colPasteds = getColIdsBetween(table, cellStart?.colId ?? '', cellEnd?.colId ?? '');
const rowSelectIds = getRowIdsBetween(table, startCell?.rowId ?? '', endCell?.rowId ?? '');
const colSelectIds = getColIdsBetween(table, startCell?.colId ?? '', endCell?.colId ?? '');
const dataSelected = [];
rowSelectIds.forEach(rowId => {
const row = allRows.find(r => r.id === rowId);
if (!row) return;
const rowData = [];
colSelectIds.forEach(colId => {
const cellll = row.getVisibleCells().find(c => c.column.id === colId);
const value = cellll?.getValue();
rowData.push(value !== undefined ? String(value) : '');
});
dataSelected.push(rowData);
});
const copyRows = flattenData('children', dataSource).filter(it => rowSelectIds.includes(it[rowKey]));
// kéo xuóng
// setRangeState?.(getSelectedCellMatrix(table, startCell, endPasteCell))
const newRange = ctrlKey ? addRowsDownWithCtrl(dataSelected, rowPasteIds.length) : addRowsDown(dataSelected, rowPasteIds.length);
// Cập nhật data mới
const newData = flattenArray([...dataSource]);
const pastedRows = [];
newRange.addedRows.forEach((rowValues, rowIndex1) => {
const targetRow = pasteState.startRowIndex + rowIndex1;
// Nếu vượt quá số dòng hiện có, thêm dòng mới
if (targetRow >= newData.length) {
newData.push({
id: undefined,
rowId: newGuid()
});
}
rowValues.forEach((cellValue, colIndex1) => {
const targetCol = startColIndex + colIndex1;
if (targetCol >= tmpCols.length) {
// Không vượt quá số cột
return;
}
const columnKey = tmpCols[targetCol].id;
newData[targetRow] = {
...newData[targetRow],
[columnKey]: typeof cellValue === 'string' ? cellValue.trim() : cellValue
};
});
// Lưu dòng được paste
pastedRows.push(newData[targetRow]);
});
const rsFilterData = updateOrInsert(flattenArray([...originData]), newData);
const rs = unFlattenData(rsFilterData);
triggerPaste?.(pastedRows, colPasteds, rs, copyRows);
};
function handleMouseDown(rowId, colId) {
if ((startCell?.rowId !== rowId || startCell?.colId !== colId) && (endCell?.rowId !== rowId || endCell?.colId !== colId)) {
setStartCell?.({
rowId,
colId
});
setEndCell?.({
rowId,
colId
});
setIsSelecting?.(true);
setRangeState?.(getSelectedCellMatrix(table, {
rowId,
colId
}, {
rowId,
colId
}));
}
if (focusedCell?.rowId !== cell.row.id || focusedCell?.colId !== cell.column.id) {
setFocusedCell?.({
rowId: cell.row.id,
colId: cell.column.id
});
}
}
function handleMouseEnter(rowId, colId) {
if (isSelecting) {
setEndCell?.({
rowId,
colId
});
}
if (isPasting) {
const absRowIndex = table.getRowModel().flatRows.findIndex(it => it.id === cell.row.id);
if ((startRowIndex ?? 0) > absRowIndex) {
// kéo lên
const startRowPaste = allRows.find(it => it.index === (startRowIndex ?? 0) - 1);
setStartPasteCell?.({
colId: startCell?.colId ?? '',
rowId: startRowPaste?.id ?? ''
});
setEndPasteCell?.({
colId: endCell?.colId ?? '',
rowId
});
setRangePasteState?.(getSelectedCellMatrix(table, {
colId: startCell?.colId ?? '',
rowId: startRowPaste?.id ?? ''
}, {
colId: endCell?.colId ?? '',
rowId
}));
}
if ((startRowIndex ?? 0) < absRowIndex) {
// kéo xuống
const findIndex = allRows.findIndex(it => it.id === endCell?.rowId);
const startRowPaste = allRows[findIndex + 1];
setStartPasteCell?.({
colId: startCell?.colId ?? '',
rowId: startRowPaste?.id ?? ''
});
setEndPasteCell?.({
colId: endCell?.colId ?? '',
rowId
});
setRangePasteState?.(getSelectedCellMatrix(table, {
colId: startCell?.colId ?? '',
rowId: startRowPaste?.id ?? ''
}, {
colId: endCell?.colId ?? '',
rowId
}));
}
}
}
const handlePointDoubleClick = e => {
e.preventDefault();
e.stopPropagation();
const rowEnd = table.getRowModel().flatRows[table.getRowModel().flatRows.length - 1];
const cellEnd = {
rowId: rowEnd.id,
colId: cell.column.id
};
const currentRowId = allRows.findIndex(it => it.id === cell.row.id);
const pasteStartRow = allRows[currentRowId + 1];
const cellStart = {
rowId: pasteStartRow.id,
colId: endCell?.colId ?? ''
};
const selectState = getSelectedCellMatrix(table, startCell, cellEnd);
const selectPasteState = getSelectedCellMatrix(table, cellStart, cellEnd);
setRangeState?.(selectState);
setEndCell?.(cellEnd);
triggerPointPaste(selectPasteState, cellStart, cellEnd, e.ctrlKey);
};
function handleMouseUp(e) {
setIsSelecting?.(false);
setIsPasting?.(false);
const selectState = getSelectedCellMatrix(table, startCell, endCell);
const selectPasteState = getSelectedCellMatrix(table, startPasteCell, endPasteCell);
if (endPasteCell && endCell?.rowId !== endPasteCell?.rowId && isPasting) {
triggerDragPaste(selectPasteState, e.ctrlKey);
} else {
// if ( endCell?.rowId !== rowId || endCell?.colId !== colId) {
// console.log('fffffffffffff')
// setRangeState?.(selectState)
// }
if (!isObjEqual(rangeState, selectState)) {
console.log('dfdfghh');
setRangeState?.(selectState);
}
}
// setRangePasteState(selectPasteState)
}
const handleEdit = e => {
setEditingKey?.(record[rowKey]);
// setTooltipContent('')
reset?.();
// const formattedRecord = { ...record };
if (!isObjEmpty(record)) {
Object.entries(record).forEach(([name, value]) => {
setValue?.(name, value);
});
}
if (e.key === 'Enter' || editType === 'date' || e.type === 'dblclick') {} else {
setValue(columnMeta.field, editType === 'numeric' ? !isNaN(Number(e.key)) ? Number(e.key) : '' : e.key);
}
if (editType === 'select') {
// setSearchValue(e.key)
// setOpen(true)
}
if (focusedCell?.rowId) {
setTimeout(() => {
// const input = document.querySelector(`.ui-rc-table-row .cell-editing[data-row-index="${startSelectedCells.current.row}"].cell-editing[data-col-index="${startSelectedCells.current.col}"] input`) as HTMLInputElement
// const textarea = document.querySelector(`.ui-rc-table-row .cell-editing[data-row-index="${rowNumber}"].cell-editing[data-col-index="${startSelectedCells.current.col}"] textarea`) as any
const textarea = document.querySelector(`.ui-rc-grid-row .cell-editing[data-row-key="${focusedCell.rowId}"].cell-editing[data-col-key="${focusedCell.colId}"] textarea`);
// const select = document.querySelector(`div.cell-editing[tabindex="${startSelectedCells.current.col}"] .ant-select-selection-search input`)
if (textarea) {
textarea.focus();
textarea.setSelectionRange(textarea.value.length, textarea.value.length);
}
// if (input) {
// input.focus()
// }
// if (select) {
// // @ts-ignore
// select.searchValue = e.key
// // @ts-ignore
// select.focus()
// }
}, 10);
}
};
function handleKeyDown(e, rowId, colId) {
if (e.key === 'Tab') {
e.preventDefault();
const allCols = table.getVisibleLeafColumns();
const currentColIndex = table.getColumn(colId)?.getIndex() ?? 0;
const nextCol = allCols[currentColIndex + 1];
if (nextCol) {
setFocusedCell?.({
rowId,
colId: nextCol.id
});
setStartCell?.({
rowId,
colId: nextCol.id
});
setEndCell?.({
rowId,
colId: nextCol.id
});
setRangeState?.(getSelectedCellMatrix(table, {
rowId,
colId: nextCol.id
}, {
rowId,
colId: nextCol.id
}));
columnVirtualizer.scrollToIndex(nextCol.getIndex(), {
align: 'center'
});
} else {
// Nếu là cột cuối cùng, có thể nhảy xuống dòng tiếp theo
const currentRowIndex = allRows.findIndex(r => r.id === rowId);
const nextRow = allRows[currentRowIndex + 1];
const nextRowCol = allCols[2];
if (nextRow) {
setFocusedCell?.({
rowId: nextRow.id,
colId: nextRowCol.id
}); // sang dòng, cột đầu
setStartCell?.({
rowId: nextRow.id,
colId: nextRowCol.id
});
setEndCell?.({
rowId: nextRow.id,
colId: nextRowCol.id
});
setRangeState?.(getSelectedCellMatrix(table, {
rowId: nextRow.id,
colId: nextRowCol.id
}, {
rowId: nextRow.id,
colId: nextRowCol.id
}));
columnVirtualizer.scrollToIndex(nextRowCol.getIndex());
rowVirtualizer.scrollToIndex(nextRow.index, {
align: 'auto'
});
}
}
}
if (e.key === 'ArrowRight') {
e.preventDefault();
e.stopPropagation();
const allCols = table.getVisibleLeafColumns().filter(it => !nonActionColumn.includes(it.id));
const currentColIndex = allCols.findIndex(it => it.id === cell.column.id);
const nextCol = allCols[currentColIndex + 1];
if (nextCol) {
setFocusedCell?.({
rowId,
colId: nextCol.id
});
setStartCell?.({
rowId,
colId: nextCol.id
});
setEndCell?.({
rowId,
colId: nextCol.id
});
setRangeState?.(getSelectedCellMatrix(table, {
rowId,
colId: nextCol.id
}, {
rowId,
colId: nextCol.id
}));
columnVirtualizer.scrollToIndex(nextCol.getIndex(), {
align: 'center'
});
}
}
if (e.key === 'ArrowLeft') {
e.preventDefault();
const allCols = table.getVisibleLeafColumns().filter(it => !nonActionColumn.includes(it.id));
// const currentColIndex = table.getColumn(colId)?.getIndex() ?? 0;
const currentColIndex = allCols.findIndex(it => it.id === cell.column.id);
const nextCol = allCols[currentColIndex - 1];
if (nextCol) {
setFocusedCell?.({
rowId,
colId: nextCol.id
});
setStartCell?.({
rowId,
colId: nextCol.id
});
setEndCell?.({
rowId,
colId: nextCol.id
});
setRangeState?.(getSelectedCellMatrix(table, {
rowId,
colId: nextCol.id
}, {
rowId,
colId: nextCol.id
}));
columnVirtualizer.scrollToIndex(nextCol.getIndex(), {
align: 'center'
});
}
}
if (e.key === 'Delete') {
handleDeleteContent?.();
}
if (e.key === 'Enter') {
handleEdit(e);
}
}
return /*#__PURE__*/React.createElement("td", {
key: cell.id,
ref: el => {
if (focusedCell?.rowId === cell.row.id && focusedCell?.colId === cell.column.id && !isEditing) {
el?.focus();
}
},
tabIndex: focusedCell?.rowId === cell.row.id && focusedCell?.colId === cell.column.id ? 0 : -1,
"data-col-index": colIndex,
"data-row-index": rowNumber,
"data-col-key": cell.column.id,
"data-row-key": cell.row.id,
className: classNames(`${prefix}-grid-cell cell-editable `, {
[`${prefix}-grid-cell-ellipsis`]: true,
'cell-editing': isEditing,
[`${prefix}-grid-cell-selected`]: isCellSelected,
'cell-border-bottom': !isEditing && isInRange && isBottom,
'cell-border-right': !isEditing && isInRange && isRight,
'cell-border-top': !isEditing && isInRange && isTop,
'cell-border-left': !isEditing && isInRange && isLeft,
'cell-paste-border-bottom': isInRangePaste && isBottomPaste && (pasteRangeState.endRowIndex ?? 0) > (endRowIndex ?? 0),
'cell-paste-border-right': isInRangePaste && isRightPaste,
'cell-paste-border-top': isInRangePaste && isTopPaste && (pasteRangeState.endRowIndex ?? 0) < (endRowIndex ?? 0),
'cell-paste-border-left': isInRangePaste && isLeftPaste,
[`${prefix}-grid-cell-fix-left-last`]: isLastLeftPinnedColumn,
[`${prefix}-grid-cell-fix-right-first`]: isFirstRightPinnedColumn
}),
style: {
display: 'flex',
width: cell.column.getSize(),
height: '36px',
...getCommonPinningStyles(cell.column),
cursor: isPasting ? 'crosshair' : undefined
// background: isCellSelected ? '#d0e6ff' : undefined,
// outline: focusedCell?.rowId === cell.row.id && focusedCell?.colId === cell.column.id ? '1px solid blue' : undefined,
},
onMouseDown: () => {
if (record[rowKey] === editingKey) {} else {
handleMouseDown(cell.row.id, cell.column.id);
}
if (record[rowKey] !== editingKey) {
setTimeout(() => {
setEditingKey?.('');
});
}
},
onMouseEnter: () => handleMouseEnter(cell.row.id, cell.column.id),
onMouseUp: e => handleMouseUp(e),
onMouseMove: () => {},
onKeyDown: e => {
if (!isEditing) {
handleKeyDown(e, cell.row.id, cell.column.id);
} else {
// setStartCell?.(undefined)
if (e.shiftKey && e.key === 'Enter') {} else {
if (e.key === 'Enter') {
e.preventDefault();
e.stopPropagation();
setEditingKey?.('');
const currentRowIndex = allRows.findIndex(r => r.id === cell.row.id);
const nextRow = allRows[currentRowIndex + 1];
if (nextRow) {
setFocusedCell?.({
rowId: nextRow.id,
colId: cell.column.id
});
setStartCell?.({
rowId: nextRow.id,
colId: cell.column.id
});
setEndCell?.({
rowId: nextRow.id,
colId: cell.column.id
});
setRangeState?.(getSelectedCellMatrix(table, {
rowId: nextRow.id,
colId: cell.column.id
}, {
rowId: nextRow.id,
colId: cell.column.id
}));
// columnVirtualizer.scrollToIndex(nextCol.getIndex(), { align: 'center' })
} else {
const newRowId = newGuid();
handleAddMulti?.({}, 1, newRowId);
setFocusedCell?.({
rowId: newRowId,
colId: cell.column.id
});
setStartCell?.({
rowId: newRowId,
colId: cell.column.id
});
setEndCell?.({
rowId: newRowId,
colId: cell.column.id
});
setTimeout(() => {
setRangeState?.(getSelectedCellMatrix(table, {
rowId: newRowId,
colId: cell.column.id
}, {
rowId: newRowId,
colId: cell.column.id
}));
// rowVirtualizer.scrollToIndex(101)
}, 0);
}
if (editingKey && editingKey !== '' && (rowNumber ?? 0) + 1 < flattenArray(dataSource).length) {
// handleFocusCell((rowNumber ?? 0) + 1, colIndex, column, 'vertical', event)
} else {
// // focus cell hiện tại và tắt edit
// handleFocusCell((rowNumber ?? 0), colIndex, column, 'vertical', event)
// setEditingKey('')
// thêm dòng mới
// handleAddSingle()
// handleFocusCell((rowNumber ?? 0) + 1, colIndex, column, 'vertical', event, true)
}
}
}
}
},
onDoubleClick: e => {
console.log('e', e);
if (!(record[rowKey] === editingKey && canEdit)) {
handleEdit(e);
}
}
// onClick={() => {
// // if (focusedCell?.rowId !== cell.row.id && focusedCell?.colId !== cell.column.id) {
// // setFocusedCell({ rowId: cell.row.id, colId: cell.column.id })
// // }
// }}
}, isEditing && canEdit ? /*#__PURE__*/React.createElement(EditableCell, {
cellEditing: {},
column: cell.column.columnDef.meta,
dataIndex: cell.column.id,
editType: getEditType(cell.column.columnDef.meta, record),
indexCol: cell.column.getIndex(),
indexRow: cell.row.index,
record: record
// rowKey={rowKey}
}) : /*#__PURE__*/React.createElement("div", {
className: "ui-rc_cell-content"
}, flexRender(cell.column.columnDef.cell, cell.getContext())), !isSelecting && !isEditing && cell.row.id === endCell?.rowId && cell.column.getIndex() === rangeState?.endColIndex && /*#__PURE__*/React.createElement("div", {
className: 'dragging-point',
onMouseDown: e => {
e.preventDefault();
e.stopPropagation();
if (e.button === 0) {
setIsPasting?.(true);
}
},
onDoubleClick: handlePointDoubleClick
}, /*#__PURE__*/React.createElement("span", {
className: 'dot-point'
})));
};
export default TableBodyCellEdit;