@@ -3,6 +3,7 @@ import { cloneDeep } from 'lodash-es';
33import { message } from 'ant-design-vue' ;
44import type { DynamicTableProps } from '../dynamic-table' ;
55import type { TableState } from './useTableState' ;
6+ import type { TableColumn } from '@/components/core/dynamic-table/src/types/column' ;
67
78type UseTableMethodsContext = {
89 state : TableState ;
@@ -40,6 +41,34 @@ export const useEditable = ({ state, props }: UseTableMethodsContext) => {
4041 } ) ;
4142 } ;
4243
44+ /** 获取要编辑的值 */
45+ const getEditValue = (
46+ recordKey : Key ,
47+ currentRow ?: Recordable ,
48+ columns ?: TableColumn < Recordable < any > > [ ] ,
49+ ) => {
50+ // 克隆当前行数据作为临时编辑的表单数据,避免直接修改原数据
51+ const editValue = cloneDeep (
52+ currentRow ?? tableData . value . find ( ( n ) => n [ String ( props . rowKey ) ] === recordKey ) ,
53+ ) ;
54+ // 用户设置的默认值优先
55+ columns ?. forEach ( ( item ) => {
56+ const { formItemProps, editFormItemProps } = item ;
57+ const field = ( item . dataIndex || item . key ) as string ;
58+ if (
59+ ! Object . is ( editFormItemProps ?. extendSearchFormProps , false ) &&
60+ formItemProps &&
61+ Reflect . has ( formItemProps , 'defaultValue' )
62+ ) {
63+ editValue [ field ] = formItemProps . defaultValue ;
64+ }
65+ if ( editFormItemProps && Reflect . has ( editFormItemProps , 'defaultValue' ) ) {
66+ editValue [ field ] = editFormItemProps . defaultValue ;
67+ }
68+ } ) ;
69+ return editValue ;
70+ } ;
71+
4372 /**
4473 * @description 进入编辑行状态
4574 *
@@ -54,20 +83,7 @@ export const useEditable = ({ state, props }: UseTableMethodsContext) => {
5483 message . warn ( props . onlyOneLineEditorAlertMessage || '只能同时编辑一行' ) ;
5584 return false ;
5685 }
57- // 克隆当前行数据作为临时编辑的表单数据,避免直接修改原数据
58- const editValue = cloneDeep (
59- currentRow ?? tableData . value . find ( ( n ) => n [ String ( props . rowKey ) ] === recordKey ) ,
60- ) ;
61- // 用户设置的默认值优先
62- props . columns . forEach ( ( item ) => {
63- const field = ( item . dataIndex || item . key ) as string ;
64- if ( item . formItemProps && Reflect . has ( item . formItemProps , 'defaultValue' ) ) {
65- editValue [ field ] = item . formItemProps . defaultValue ;
66- }
67- if ( item . editFormItemProps && Reflect . has ( item . editFormItemProps , 'defaultValue' ) ) {
68- editValue [ field ] = item . editFormItemProps . defaultValue ;
69- }
70- } ) ;
86+ const editValue = getEditValue ( recordKey , currentRow , props . columns ) ;
7187 setEditFormModel ( recordKey , editValue ) ;
7288 editableRowKeys . value . add ( recordKey ) ;
7389 return true ;
@@ -76,23 +92,9 @@ export const useEditable = ({ state, props }: UseTableMethodsContext) => {
7692 /** 进入编辑单元格状态 */
7793 const startCellEditable = ( recordKey : Key , dataIndex : Key , currentRow ?: Recordable ) => {
7894 editableRowKeys . value . clear ( ) ;
79- // 克隆当前行数据作为临时编辑的表单数据,避免直接修改原数据
80- const editValue = cloneDeep (
81- currentRow ?? tableData . value . find ( ( n ) => n [ String ( props . rowKey ) ] === recordKey ) ,
82- ) ;
83- const targetColumn = props . columns . find ( ( n ) => n . dataIndex === dataIndex ) ;
84- if ( targetColumn ) {
85- const field = ( targetColumn . dataIndex || targetColumn . key ) as string ;
86- if ( targetColumn . formItemProps && Reflect . has ( targetColumn . formItemProps , 'defaultValue' ) ) {
87- editValue [ field ] = targetColumn . formItemProps . defaultValue ;
88- }
89- if (
90- targetColumn . editFormItemProps &&
91- Reflect . has ( targetColumn . editFormItemProps , 'defaultValue' )
92- ) {
93- editValue [ field ] = targetColumn . editFormItemProps . defaultValue ;
94- }
95- }
95+ const targetColumn = props . columns . filter ( ( n ) => n . dataIndex === dataIndex ) ;
96+ const editValue = getEditValue ( recordKey , currentRow , targetColumn ) ;
97+
9698 editableCellKeys . value . add ( `${ recordKey } .${ dataIndex } ` ) ;
9799 setEditFormModel ( recordKey , {
98100 ...( getEditFormModel ( recordKey ) || editValue ) ,
0 commit comments