@@ -12,7 +12,7 @@ import {
1212 TacoButton ,
1313} from "lowcoder-design" ;
1414import _ from "lodash" ;
15- import { useEffect , useState } from "react" ;
15+ import { useEffect , useState , useCallback } from "react" ;
1616import { useDispatch , useSelector } from "react-redux" ;
1717import { AppState } from "redux/reducers" ;
1818import { fetchDatasourceStructure } from "redux/reduxActions/datasourceActions" ;
@@ -552,13 +552,22 @@ const CreateFormBody = (props: { onCreate: CreateHandler }) => {
552552 const dataSourceId : string | undefined = Form . useWatch ( "dataSourceId" , form ) ;
553553 const dataSourceItems = useDataSourceItems ( ) ;
554554 const dataSourceItem = dataSourceItems . find ( ( t ) => t . dataSource . id === dataSourceId ) ;
555+
556+ // Cleanup form on unmount
557+ useEffect ( ( ) => {
558+ return ( ) => {
559+ form . resetFields ( ) ;
560+ } ;
561+ } , [ form ] ) ;
562+
555563 // default to the first item
556564 useEffect ( ( ) => {
557565 if ( ! dataSourceItem ) {
558566 const id = dataSourceItems . length > 0 ? dataSourceItems [ 0 ] . dataSource . id : undefined ;
559567 form . setFieldsValue ( { dataSourceId : id } ) ;
560568 }
561- } , [ dataSourceItems ] ) ;
569+ } , [ dataSourceItems , dataSourceItem , form ] ) ;
570+
562571 // Refetch when changed
563572 const dispatch = useDispatch ( ) ;
564573 useEffect ( ( ) => {
@@ -570,25 +579,27 @@ const CreateFormBody = (props: { onCreate: CreateHandler }) => {
570579 const tableName : string | undefined = Form . useWatch ( "tableName" , form ) ;
571580 const tableStructures = useTableStructures ( dataSourceId ) ;
572581 const tableStructure = tableStructures . find ( ( t ) => t . name === tableName ) ;
582+
573583 // default to the first one
574584 useEffect ( ( ) => {
575585 if ( ! tableStructure ) {
576586 const name = tableStructures . length > 0 ? tableStructures [ 0 ] . name : undefined ;
577587 form . setFieldsValue ( { tableName : name } ) ;
578588 }
579- } , [ tableStructures ] ) ;
589+ } , [ tableStructures , tableStructure , form ] ) ;
590+
580591 // Columns of the data table, saved to support drag and drop
581592 const [ items , setItems ] = useState < RowItem [ ] > ( [ ] ) ;
582593 const dataSourceTypeConfig = dataSourceItem ?. typeConfig ;
594+
583595 useEffect ( ( ) => {
584596 const { initItems, initColumns } = getInitItemsAndColumns ( dataSourceTypeConfig , tableStructure ) ;
585597 // Set the initial value by the method. Because if another table has the same column name, setting via initialValue is invalid.
586598 form . setFieldsValue ( { columns : initColumns } ) ;
587599 setItems ( initItems ) ;
588- } , [ dataSourceTypeConfig , tableStructure ] ) ;
600+ } , [ dataSourceTypeConfig , tableStructure , form ] ) ;
589601
590- const handleDragEnd = ( e : { active : { id : string } ; over : { id : string } | null } ) => {
591- console . log ( 'handleDragEnd' , e ) ;
602+ const handleDragEnd = useCallback ( ( e : { active : { id : string } ; over : { id : string } | null } ) => {
592603 if ( ! e . over ) {
593604 return ;
594605 }
@@ -603,7 +614,7 @@ const CreateFormBody = (props: { onCreate: CreateHandler }) => {
603614 newData . splice ( toIndex , 0 , movedItem ) ;
604615
605616 setItems ( newData ) ;
606- } ;
617+ } , [ items ] ) ;
607618
608619 const emptyText = getEmptyText ( dataSourceItems . length , tableStructures . length , items . length ) ;
609620
@@ -688,27 +699,40 @@ const CreateFormBody = (props: { onCreate: CreateHandler }) => {
688699
689700export const CreateForm = ( props : { onCreate : CreateHandler } ) => {
690701 const [ visible , setVisible ] = useState ( false ) ;
702+
703+ const handleMouseDown = useCallback ( ( e : React . MouseEvent ) => {
704+ setVisible ( true ) ;
705+ e . stopPropagation ( ) ;
706+ } , [ ] ) ;
707+
708+ const handleKeyDown = useCallback ( ( e : React . KeyboardEvent ) => {
709+ e . stopPropagation ( ) ;
710+ } , [ ] ) ;
711+
712+ const handleClick = useCallback ( ( e : React . MouseEvent ) => {
713+ e . stopPropagation ( ) ;
714+ } , [ ] ) ;
715+
716+ const handleCancel = useCallback ( ( ) => {
717+ setVisible ( false ) ;
718+ } , [ ] ) ;
719+
691720 return (
692721 < >
693- < OpenDialogButton
694- onMouseDown = { ( e ) => {
695- setVisible ( true ) ;
696- e . stopPropagation ( ) ;
697- } }
698- >
722+ < OpenDialogButton onMouseDown = { handleMouseDown } >
699723 { trans ( "formComp.openDialogButton" ) }
700724 </ OpenDialogButton >
701725 < div
702- onKeyDown = { ( e ) => e . stopPropagation ( ) }
703- onMouseDown = { ( e ) => e . stopPropagation ( ) }
704- onClick = { ( e ) => e . stopPropagation ( ) }
726+ onKeyDown = { handleKeyDown }
727+ onMouseDown = { handleMouseDown }
728+ onClick = { handleClick }
705729 >
706730 < CustomModal
707731 open = { visible }
708732 destroyOnClose = { true }
709733 title = { trans ( "formComp.generateForm" ) }
710734 footer = { null }
711- onCancel = { ( ) => setVisible ( false ) }
735+ onCancel = { handleCancel }
712736 width = "600px"
713737 children = { < CreateFormBody { ...props } /> }
714738 styles = { { body : { padding : 0 } } }
0 commit comments