@@ -6,6 +6,7 @@ import { v4 as uuidv4 } from 'uuid';
66import { Context } from '../index' ;
77import { IColumnItemNew , IIndexIncludeColumnItem } from '@/typings' ;
88import i18n from '@/i18n' ;
9+ import { string } from 'sql-formatter/lib/src/lexer/regexFactory' ;
910
1011interface IProps {
1112 includedColumnList : IIndexIncludeColumnItem [ ] ;
@@ -14,29 +15,7 @@ interface IProps {
1415const createInitialData = ( ) => {
1516 return {
1617 key : uuidv4 ( ) ,
17- oldName : null ,
1818 name : null ,
19- tableName : null ,
20- columnType : null ,
21- dataType : null ,
22- defaultValue : null ,
23- autoIncrement : null ,
24- comment : null ,
25- primaryKey : null ,
26- schemaName : null ,
27- databaseName : null ,
28- typeName : null ,
29- columnSize : null ,
30- bufferLength : null ,
31- decimalDigits : null ,
32- numPrecRadix : null ,
33- nullableInt : null ,
34- sqlDataType : null ,
35- sqlDatetimeSub : null ,
36- charOctetLength : null ,
37- ordinalPosition : null ,
38- nullable : null ,
39- generatedColumn : null ,
4019 } ;
4120} ;
4221
@@ -49,46 +28,42 @@ const InitialDataSource = [createInitialData()];
4928const IncludeCol = forwardRef ( ( props : IProps , ref : ForwardedRef < IIncludeColRef > ) => {
5029 const { includedColumnList } = props ;
5130 const { columnListRef } = useContext ( Context ) ;
52- const [ dataSource , setDataSource ] = useState < IIndexIncludeColumnItem [ ] > ( InitialDataSource ) ;
31+ const [ dataSource , setDataSource ] = useState < any [ ] > ( InitialDataSource ) ;
5332 const [ form ] = Form . useForm ( ) ;
54- const [ editingKey , setEditingKey ] = useState ( dataSource [ 0 ] ?. key ) ;
33+ const [ editingKey , setEditingKey ] = useState < string | null > ( null ) ;
5534 const isEditing = ( record : IIndexIncludeColumnItem ) => record . key === editingKey ;
5635
5736 useEffect ( ( ) => {
5837 if ( includedColumnList . length ) {
5938 setDataSource (
6039 includedColumnList . map ( ( t ) => {
6140 return {
62- ...t ,
6341 key : uuidv4 ( ) ,
42+ name : t . name ,
6443 } ;
6544 } ) ,
6645 ) ;
6746 }
6847 } , [ includedColumnList ] ) ;
6948
7049 const columnList : IColumnItemNew [ ] = useMemo ( ( ) => {
71- const columnListInfo = columnListRef . current ?. getColumnListInfo ( ) ?. filter ( ( i ) => i . name ) ;
50+ const columnListInfo = columnListRef . current ?. getColumnListInfo ( ) ?. filter ( ( i ) => i . name !== null ) ;
7251 return columnListInfo || [ ] ;
7352 } , [ ] ) ;
7453
75- const edit = ( record : IIndexIncludeColumnItem ) => {
54+ const edit = ( record : any ) => {
7655 form . setFieldsValue ( { ...record } ) ;
77- setEditingKey ( record . key ) ;
56+ setEditingKey ( record . key || null ) ;
7857 } ;
7958
8059 const addData = ( ) => {
8160 const newData = createInitialData ( ) ;
8261 setDataSource ( [ ...dataSource , newData ] ) ;
62+ console . log ( [ ...dataSource , newData ] ) ;
8363 edit ( newData ) ;
8464 } ;
8565
8666 const deleteData = ( ) => {
87- // if (dataSource.length === 1) {
88- // message.warning('至少保留一条数据')
89- // return
90- // }
91-
9267 setDataSource ( dataSource . filter ( ( i ) => i . key !== editingKey ) ) ;
9368 } ;
9469
@@ -103,12 +78,12 @@ const IncludeCol = forwardRef((props: IProps, ref: ForwardedRef<IIncludeColRef>)
10378 } ,
10479 {
10580 title : i18n ( 'editTable.label.columnName' ) ,
106- dataIndex : 'columnName ' ,
81+ dataIndex : 'name ' ,
10782 // width: '45%',
10883 render : ( text : string , record : IIndexIncludeColumnItem ) => {
10984 const editable = isEditing ( record ) ;
11085 return editable ? (
111- < Form . Item name = "columnName " style = { { margin : 0 } } >
86+ < Form . Item name = "name " style = { { margin : 0 } } >
11287 < Select options = { columnList . map ( ( i ) => ( { label : i . name , value : i . name } ) ) } />
11388 </ Form . Item >
11489 ) : (
@@ -137,24 +112,28 @@ const IncludeCol = forwardRef((props: IProps, ref: ForwardedRef<IIncludeColRef>)
137112 // },
138113 ] ;
139114
140- const onValuesChange = ( changedValues : any , allValues : any ) => {
141- const newDataSource = dataSource ?. map ( ( i ) => {
142- if ( i . key === editingKey ) {
115+ const handelFieldsChange = ( field : any ) => {
116+ const { value } = field [ 0 ] ;
117+ const { name : nameList } = field [ 0 ] ;
118+ const name = nameList [ 0 ] ;
119+ const newData = dataSource . map ( ( item ) => {
120+ if ( item . key === editingKey ) {
143121 return {
144- ...i ,
145- ... allValues ,
122+ ...item ,
123+ [ name ] : value ,
146124 } ;
147125 }
148- return i ;
126+ return item ;
149127 } ) ;
150- setDataSource ( newDataSource ) ;
128+ setDataSource ( newData ) ;
151129 } ;
152130
153131 const getIncludeColInfo = ( ) => {
154132 const includeColInfo : IIndexIncludeColumnItem [ ] = [ ] ;
155133 dataSource . forEach ( ( t ) => {
156134 columnList . forEach ( ( columnItem ) => {
157135 if ( t . name === columnItem . name ) {
136+ delete columnItem . key ;
158137 includeColInfo . push ( {
159138 ...columnItem ,
160139 } ) ;
@@ -174,7 +153,7 @@ const IncludeCol = forwardRef((props: IProps, ref: ForwardedRef<IIncludeColRef>)
174153 < Button onClick = { addData } > { i18n ( 'editTable.button.add' ) } </ Button >
175154 < Button onClick = { deleteData } > { i18n ( 'editTable.button.delete' ) } </ Button >
176155 </ div >
177- < Form form = { form } onValuesChange = { onValuesChange } >
156+ < Form form = { form } onFieldsChange = { handelFieldsChange } >
178157 < Table pagination = { false } rowKey = "key" columns = { columns } dataSource = { dataSource } />
179158 </ Form >
180159 </ div >
0 commit comments