@@ -180,31 +180,52 @@ export function fieldReducer(state: FormState, action: FieldAction): FormState {
180
180
181
181
case 'MOVE_ROW' : {
182
182
const { moveFromIndex, moveToIndex, path } = action
183
- const { remainingFields, rows } = separateRows ( path , state )
184
183
185
- // copy the row to move
186
- const copyOfMovingRow = rows [ moveFromIndex ]
187
- // delete the row by index
188
- rows . splice ( moveFromIndex , 1 )
189
- // insert row copyOfMovingRow back in
190
- rows . splice ( moveToIndex , 0 , copyOfMovingRow )
184
+ // Handle moving rows on the top-level, i.e. `array.0.text` -> `array.1.text`
185
+ const { remainingFields, rows : topLevelRows } = separateRows ( path , state )
186
+ const copyOfMovingRow = topLevelRows [ moveFromIndex ]
187
+ topLevelRows . splice ( moveFromIndex , 1 )
188
+ topLevelRows . splice ( moveToIndex , 0 , copyOfMovingRow )
191
189
192
190
// modify array/block internal row state (i.e. collapsed, blockType)
193
- const rowStateCopy = [ ...( state [ path ] ?. rows || [ ] ) ]
194
- const movingRowState = { ...rowStateCopy [ moveFromIndex ] }
195
- rowStateCopy . splice ( moveFromIndex , 1 )
196
- rowStateCopy . splice ( moveToIndex , 0 , movingRowState )
191
+ const rowsWithinField = [ ...( state [ path ] ?. rows || [ ] ) ]
192
+ const copyOfMovingRow2 = { ...rowsWithinField [ moveFromIndex ] }
193
+ rowsWithinField . splice ( moveFromIndex , 1 )
194
+ rowsWithinField . splice ( moveToIndex , 0 , copyOfMovingRow2 )
197
195
198
196
const newState = {
199
197
...remainingFields ,
200
- ...flattenRows ( path , rows ) ,
198
+ ...flattenRows ( path , topLevelRows ) ,
201
199
[ path ] : {
202
200
...state [ path ] ,
203
201
requiresRender : true ,
204
- rows : rowStateCopy ,
202
+ rows : rowsWithinField ,
205
203
} ,
206
204
}
207
205
206
+ // Do the same for custom components, i.e. `array.customComponents.RowLabels[0]` -> `array.customComponents.RowLabels[1]`
207
+ // Do this _after_ initializing `newState` to avoid adding the `customComponents` key to the state if it doesn't exist
208
+ if ( newState [ path ] ?. customComponents ?. RowLabels ) {
209
+ const customComponents = {
210
+ ...newState [ path ] . customComponents ,
211
+ RowLabels : [ ...newState [ path ] . customComponents . RowLabels ] ,
212
+ }
213
+
214
+ // Ensure the array grows if necessary
215
+ if ( moveToIndex >= customComponents . RowLabels . length ) {
216
+ customComponents . RowLabels . length = moveToIndex + 1
217
+ }
218
+
219
+ const copyOfMovingLabel = customComponents . RowLabels [ moveFromIndex ]
220
+
221
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
222
+ customComponents . RowLabels . splice ( moveFromIndex , 1 )
223
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
224
+ customComponents . RowLabels . splice ( moveToIndex , 0 , copyOfMovingLabel )
225
+
226
+ newState [ path ] . customComponents = customComponents
227
+ }
228
+
208
229
return newState
209
230
}
210
231
0 commit comments