@@ -13,6 +13,7 @@ import { IconDeleteStroked, IconPlus } from "@douyinfe/semi-icons";
1313import { useUndoRedo , useTypes , useDiagram , useLayout } from "../../../hooks" ;
1414import TypeField from "./TypeField" ;
1515import { useTranslation } from "react-i18next" ;
16+ import { nanoid } from "nanoid" ;
1617
1718export default function TypeInfo ( { index, data } ) {
1819 const { layout } = useLayout ( ) ;
@@ -22,8 +23,11 @@ export default function TypeInfo({ index, data }) {
2223 const [ editField , setEditField ] = useState ( { } ) ;
2324 const { t } = useTranslation ( ) ;
2425
26+ // TODO: remove indexes, not a valid case after adding id to types
27+ const typeId = data . id ?? index ;
28+
2529 return (
26- < div id = { `scroll_type_${ index } ` } >
30+ < div id = { `scroll_type_${ typeId } ` } >
2731 < Collapse . Panel
2832 header = {
2933 < div className = "overflow-hidden text-ellipsis whitespace-nowrap" >
@@ -41,7 +45,7 @@ export default function TypeInfo({ index, data }) {
4145 placeholder = { t ( "name" ) }
4246 className = "ms-2"
4347 onChange = { ( value ) => {
44- updateType ( index , { name : value } ) ;
48+ updateType ( typeId , { name : value } ) ;
4549 tables . forEach ( ( table ) => {
4650 table . fields . forEach ( ( field ) => {
4751 if ( field . type . toLowerCase ( ) === data . name . toLowerCase ( ) ) {
@@ -71,7 +75,7 @@ export default function TypeInfo({ index, data }) {
7175 action : Action . EDIT ,
7276 element : ObjectType . TYPE ,
7377 component : "self" ,
74- tid : index ,
78+ tid : typeId ,
7579 undo : editField ,
7680 redo : { name : e . target . value } ,
7781 updatedFields,
@@ -103,7 +107,7 @@ export default function TypeInfo({ index, data }) {
103107 placeholder = { t ( "comment" ) }
104108 rows = { 1 }
105109 onChange = { ( value ) =>
106- updateType ( index , { comment : value } , false )
110+ updateType ( typeId , { comment : value } , false )
107111 }
108112 onFocus = { ( e ) => setEditField ( { comment : e . target . value } ) }
109113 onBlur = { ( e ) => {
@@ -114,7 +118,7 @@ export default function TypeInfo({ index, data }) {
114118 action : Action . EDIT ,
115119 element : ObjectType . TYPE ,
116120 component : "self" ,
117- tid : index ,
121+ tid : typeId ,
118122 undo : editField ,
119123 redo : { comment : e . target . value } ,
120124 message : t ( "edit_type" , {
@@ -135,28 +139,31 @@ export default function TypeInfo({ index, data }) {
135139 icon = { < IconPlus /> }
136140 disabled = { layout . readOnly }
137141 onClick = { ( ) => {
142+ const newField = {
143+ name : "" ,
144+ type : "" ,
145+ id : nanoid ( ) ,
146+ } ;
138147 setUndoStack ( ( prev ) => [
139148 ...prev ,
140149 {
141150 action : Action . EDIT ,
142151 element : ObjectType . TYPE ,
143152 component : "field_add" ,
144- tid : index ,
153+ data : {
154+ field : newField ,
155+ index : data . fields . length ,
156+ } ,
157+ tid : typeId ,
145158 message : t ( "edit_type" , {
146159 typeName : data . name ,
147160 extra : "[add field]" ,
148161 } ) ,
149162 } ,
150163 ] ) ;
151164 setRedoStack ( [ ] ) ;
152- updateType ( index , {
153- fields : [
154- ...data . fields ,
155- {
156- name : "" ,
157- type : "" ,
158- } ,
159- ] ,
165+ updateType ( typeId , {
166+ fields : [ ...data . fields , newField ] ,
160167 } ) ;
161168 } }
162169 block
@@ -170,7 +177,7 @@ export default function TypeInfo({ index, data }) {
170177 type = "danger"
171178 disabled = { layout . readOnly }
172179 icon = { < IconDeleteStroked /> }
173- onClick = { ( ) => deleteType ( index ) }
180+ onClick = { ( ) => deleteType ( typeId ) }
174181 >
175182 { t ( "delete" ) }
176183 </ Button >
0 commit comments