@@ -87,15 +87,16 @@ export class DsDynamicFormArrayComponent extends DynamicFormArrayComponent {
8787 }
8888
8989 moveSelection ( event : CdkDragDrop < Relationship > ) {
90+ const prevIndex = event . previousIndex ;
91+ const index = event . currentIndex ;
9092
9193 // prevent propagating events generated releasing on the same position
92- if ( event . previousIndex === event . currentIndex ) {
94+ if ( prevIndex === index ) {
9395 return ;
9496 }
9597
96- this . model . moveGroup ( event . previousIndex , event . currentIndex - event . previousIndex ) ;
97- const prevIndex = event . previousIndex ;
98- const index = event . currentIndex ;
98+ this . model . moveGroup ( prevIndex , index - prevIndex ) ;
99+ this . moveFormControlToPosition ( prevIndex , index ) ;
99100
100101 if ( hasValue ( this . model . groups [ index ] ) && hasValue ( ( this . control as any ) . controls [ index ] ) ) {
101102 this . onCustomEvent ( {
@@ -123,19 +124,6 @@ export class DsDynamicFormArrayComponent extends DynamicFormArrayComponent {
123124 return this . model . groups . length === 1 || ! this . model . isDraggable || this . model . notRepeatable ;
124125 }
125126
126- /**
127- * Gets the control of the specified group model. It adds the startingIndex property to the group model if it does not
128- * already have it. This ensures that the controls are always linked to the correct group model.
129- * @param groupModel The group model to get the control for.
130- * @returns The form control of the specified group model.
131- */
132- getControlOfGroup ( groupModel : any ) {
133- if ( ! groupModel . hasOwnProperty ( 'startingIndex' ) ) {
134- groupModel . startingIndex = groupModel . index ;
135- }
136- return this . control . get ( [ groupModel . startingIndex ] ) ;
137- }
138-
139127 /**
140128 * Toggles the keyboard drag and drop feature for the given sortable element.
141129 * @param event
@@ -198,6 +186,7 @@ export class DsDynamicFormArrayComponent extends DynamicFormArrayComponent {
198186
199187 if ( this . elementBeingSorted ) {
200188 this . model . moveGroup ( idx , newIndex - idx ) ;
189+ this . moveFormControlToPosition ( idx , newIndex ) ;
201190 if ( hasValue ( this . model . groups [ newIndex ] ) && hasValue ( ( this . control as any ) . controls [ newIndex ] ) ) {
202191 this . onCustomEvent ( {
203192 previousIndex : idx ,
@@ -226,6 +215,7 @@ export class DsDynamicFormArrayComponent extends DynamicFormArrayComponent {
226215
227216 cancelKeyboardDragAndDrop ( sortableElement : HTMLDivElement , index : number , length : number ) {
228217 this . model . moveGroup ( index , this . elementBeingSortedStartingIndex - index ) ;
218+ this . moveFormControlToPosition ( index , this . elementBeingSortedStartingIndex ) ;
229219 if ( hasValue ( this . model . groups [ this . elementBeingSortedStartingIndex ] ) && hasValue ( ( this . control as any ) . controls [ this . elementBeingSortedStartingIndex ] ) ) {
230220 this . onCustomEvent ( {
231221 previousIndex : index ,
@@ -280,4 +270,19 @@ export class DsDynamicFormArrayComponent extends DynamicFormArrayComponent {
280270 } ) ) ;
281271 }
282272 }
273+
274+ private moveFormControlToPosition ( fromIndex : number , toIndex : number ) {
275+ if ( ! hasValue ( fromIndex ) || ! hasValue ( toIndex ) ) {
276+ return ;
277+ }
278+
279+ const formArray = this . control as any ;
280+ if ( formArray && formArray . controls ) {
281+ const movedControl = formArray . at ( fromIndex ) ;
282+ if ( movedControl ) {
283+ formArray . removeAt ( fromIndex , { emitEvent : false } ) ;
284+ formArray . insert ( toIndex , movedControl , { emitEvent : false } ) ;
285+ }
286+ }
287+ }
283288}
0 commit comments