@@ -3,15 +3,16 @@ import type {
33 StateChangeRequest ,
44} from "@/lib/types/model/callback" ;
55import { store } from "@/lib/store" ;
6- import type {
7- ComponentState ,
8- ContainerState ,
6+ import {
7+ type ComponentState ,
8+ type ContainerState ,
9+ isComponentState ,
10+ isContainerState ,
911} from "@/lib/types/state/component" ;
1012import type { ContribPoint } from "@/lib/types/model/extension" ;
1113import type { ContributionState } from "@/lib" ;
1214import { updateArray } from "@/lib/utils/updateArray" ;
13- import { isContainerState } from "@/lib/actions/helpers/isContainerState" ;
14- import { getValue , setValue } from "@/lib/utils/objPath" ;
15+ import { getValue , normalizeObjPath , setValue } from "@/lib/utils/objPath" ;
1516
1617export function applyStateChangeRequests (
1718 stateChangeRequests : StateChangeRequest [ ] ,
@@ -106,26 +107,37 @@ export function applyComponentStateChange(
106107 stateChange : StateChange ,
107108) : ComponentState {
108109 if ( component . id === stateChange . id ) {
109- const property = stateChange . property ;
110+ const property = normalizeObjPath ( stateChange . property ) ;
110111 const valueOld = getValue ( component , property ) ;
111112 const valueNew = stateChange . value ;
112- if ( valueOld !== valueNew ) {
113+ if (
114+ property [ property . length - 1 ] === "children" &&
115+ ! Array . isArray ( valueNew ) &&
116+ valueNew !== null &&
117+ valueNew !== undefined
118+ ) {
119+ // Special case if the value of "children" is changed:
120+ // convert scalar valueNew into one-element array
121+ return setValue ( component , property , [ valueNew ] ) ;
122+ } else if ( valueOld !== valueNew ) {
113123 return setValue ( component , property , valueNew ) ;
114124 }
115125 } else if ( isContainerState ( component ) ) {
116126 const containerOld : ContainerState = component ;
117127 let containerNew : ContainerState = containerOld ;
118- for ( let i = 0 ; i < containerOld . components . length ; i ++ ) {
119- const itemOld = containerOld . components [ i ] ;
120- const itemNew = applyComponentStateChange ( itemOld , stateChange ) ;
121- if ( itemNew !== itemOld ) {
122- if ( containerNew === containerOld ) {
123- containerNew = {
124- ...containerOld ,
125- components : [ ...containerOld . components ] ,
126- } ;
128+ for ( let i = 0 ; i < containerOld . children . length ; i ++ ) {
129+ const itemOld = containerOld . children [ i ] ;
130+ if ( isComponentState ( itemOld ) ) {
131+ const itemNew = applyComponentStateChange ( itemOld , stateChange ) ;
132+ if ( itemNew !== itemOld ) {
133+ if ( containerNew === containerOld ) {
134+ containerNew = {
135+ ...containerOld ,
136+ children : [ ...containerOld . children ] ,
137+ } ;
138+ }
139+ containerNew . children [ i ] = itemNew ;
127140 }
128- containerNew . components [ i ] = itemNew ;
129141 }
130142 }
131143 return containerNew ;
0 commit comments