From 565b47bfdbd716fc7eff518265a6f621e0d834fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E8=8F=9C=20Cai?= Date: Mon, 3 Nov 2025 03:29:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(Table):=20=20=E4=BF=AE=E5=A4=8D=E4=BD=BF?= =?UTF-8?q?=E7=94=A8`updateEditedCellValue`=20=20=E5=AF=B9=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=20cell=20=E7=9A=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/components/table/EditableCell.tsx | 37 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/components/table/EditableCell.tsx b/packages/components/table/EditableCell.tsx index 0fa23c02ea..e6fe831559 100644 --- a/packages/components/table/EditableCell.tsx +++ b/packages/components/table/EditableCell.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useMemo, useRef, useState, MouseEvent } from 'react'; +import React, { useEffect, useMemo, useRef, useState, MouseEvent, useCallback } from 'react'; import { get, set, isFunction, cloneDeep } from 'lodash-es'; import { Edit1Icon as TdEdit1Icon } from 'tdesign-icons-react'; import classNames from 'classnames'; @@ -78,14 +78,39 @@ const EditableCell = (props: EditableCellProps) => { const cellValue = get(row, col.colKey); const currentRow = getCurrentRow(row, col.colKey, editValue); - const updateEditedCellValue = (val: any) => { - setEditValue(val); - }; + const updateEditedCellValue = useCallback( + (val: any) => { + if (typeof val === 'object' && val !== null && !Array.isArray(val)) { + const { rowValue, ...otherFields } = val; + const newRow = { ...currentRow }; + // 更新当前单元格的值 + if (rowValue !== undefined) { + set(newRow, col.colKey, rowValue); + setEditValue(rowValue); + } + // 更新其他字段 + Object.keys(otherFields).forEach((key) => { + set(newRow, key, otherFields[key]); + }); + + const params = { + ...cellParams, + value: rowValue !== undefined ? rowValue : get(newRow, col.colKey), + editedRow: newRow, + }; + props.onChange?.(params); + props.onRuleChange?.(params); + } else { + setEditValue(val); + } + }, + [currentRow, cellParams, col, props], + ); const editOnListeners = useMemo( () => col.edit?.on?.({ ...cellParams, editedRow: currentRow, updateEditedCellValue }) || {}, // eslint-disable-next-line react-hooks/exhaustive-deps - [cellParams, currentRow], + [cellParams, currentRow, updateEditedCellValue], ); const cellNode = useMemo(() => { @@ -114,7 +139,7 @@ const EditableCell = (props: EditableCellProps) => { delete editProps[item]; }); return editProps; - }, [currentRow, cellParams, col]); + }, [currentRow, cellParams, col, updateEditedCellValue]); const isAbortEditOnChange = useMemo(() => { const { edit } = col;