@@ -193,15 +193,18 @@ function EditableTable<T extends object = Record<string, unknown>>(
193193 const updatedRow = { ...currentRow , [ dataIndex ] : value } ;
194194 const linkedResult = col . onFieldChange ( value , updatedRow ) ;
195195 if ( linkedResult instanceof Promise ) {
196+ // 捕获当前行的 rowKey,避免异步期间行增删导致 rowIndex 错位
197+ const rowId = String ( currentRow [ rowKey ] ) ;
196198 linkedResult . then ( ( updates ) => {
197- if ( updates ) {
198- setData ( ( prev ) => {
199- const next = [ ...prev ] ;
200- next [ rowIndex ] = { ...next [ rowIndex ] , ...updates } ;
201- onChange ?.( next ) ;
202- return next ;
203- } ) ;
204- }
199+ if ( ! updates ) return ;
200+ setData ( ( prev ) => {
201+ const idx = prev . findIndex ( ( r ) => String ( r [ rowKey ] ) === rowId ) ;
202+ if ( idx === - 1 ) return prev ;
203+ const next = [ ...prev ] ;
204+ next [ idx ] = { ...next [ idx ] , ...updates } ;
205+ onChange ?.( next ) ;
206+ return next ;
207+ } ) ;
205208 } ) ;
206209 } else if ( linkedResult ) {
207210 setData ( ( prev ) => {
@@ -228,7 +231,7 @@ function EditableTable<T extends object = Record<string, unknown>>(
228231 }
229232 }
230233 } ,
231- [ columns , onChange , validateTrigger ] ,
234+ [ columns , onChange , validateTrigger , rowKey ] ,
232235 ) ;
233236
234237 const handleSubmit = useCallback ( ( ) => {
@@ -277,19 +280,16 @@ function EditableTable<T extends object = Record<string, unknown>>(
277280
278281 const handleCancelEdit = useCallback (
279282 ( id : string ) => {
283+ const rowIndex = data . findIndex ( ( r ) => String ( r [ rowKey ] ) === id ) ;
280284 const original = originalDataRef . current . get ( id ) ;
281- if ( original ) {
282- const rowIndex = data . findIndex ( ( r ) => String ( r [ rowKey ] ) === id ) ;
283- if ( rowIndex !== - 1 ) {
284- setData ( ( prev ) => {
285- const n = [ ...prev ] ;
286- n [ rowIndex ] = original ;
287- return n ;
288- } ) ;
289- }
290- originalDataRef . current . delete ( id ) ;
285+ if ( original && rowIndex !== - 1 ) {
286+ setData ( ( prev ) => {
287+ const n = [ ...prev ] ;
288+ n [ rowIndex ] = original ;
289+ return n ;
290+ } ) ;
291291 }
292- const rowIndex = data . findIndex ( ( r ) => String ( r [ rowKey ] ) === id ) ;
292+ originalDataRef . current . delete ( id ) ;
293293 if ( rowIndex !== - 1 ) {
294294 setErrors ( ( prev ) => {
295295 const n : Record < string , string > = { } ;
@@ -326,7 +326,9 @@ function EditableTable<T extends object = Record<string, unknown>>(
326326 ? opsWidthProp != null
327327 ? typeof opsWidthProp === 'number'
328328 ? `${ opsWidthProp } px`
329- : opsWidthProp
329+ : / ^ \d + $ / . test ( opsWidthProp )
330+ ? `${ opsWidthProp } px`
331+ : opsWidthProp
330332 : '120px'
331333 : undefined ;
332334
@@ -343,7 +345,7 @@ function EditableTable<T extends object = Record<string, unknown>>(
343345
344346 // ===== 列固定偏移量 =====
345347 const fixedOffsets = useMemo ( ( ) => {
346- const offsets : { left ?: number ; right ?: number } [ ] = new Array ( columns . length ) . fill ( { } ) ;
348+ const offsets : { left ?: number ; right ?: number } [ ] = Array . from ( { length : columns . length } , ( ) => ( { } ) ) ;
347349 const widths = columns . map ( ( col ) => ( typeof col . width === 'number' ? col . width : 150 ) ) ;
348350
349351 // 从左往右累计 left fixed
0 commit comments