@@ -79,10 +79,97 @@ export interface FlowTransformOptions {
7979 resolveTranslations ?: boolean ;
8080}
8181
82+ /**
83+ * Create a mapping from ref to identifier based on data.inputs array.
84+ * This handles cases where meta.components use 'ref' to reference inputs,
85+ * and data.inputs contain the actual 'identifier' field.
86+ *
87+ * @param response - The flow response object
88+ * @returns Map of ref to identifier
89+ */
90+ const createInputRefMapping = ( response : any ) : Map < string , string > => {
91+ const mapping = new Map < string , string > ( ) ;
92+
93+ if ( response ?. data ?. inputs && Array . isArray ( response . data . inputs ) ) {
94+ response . data . inputs . forEach ( ( input : any ) => {
95+ if ( input . ref && input . identifier ) {
96+ mapping . set ( input . ref , input . identifier ) ;
97+ }
98+ } ) ;
99+ }
100+
101+ return mapping ;
102+ } ;
103+
104+ /**
105+ * Create a mapping from action ref to nextNode based on data.actions array.
106+ * This handles cases where meta.components reference actions by ref,
107+ * and data.actions contain the actual nextNode field for routing.
108+ *
109+ * @param response - The flow response object
110+ * @returns Map of action ref to nextNode
111+ */
112+ const createActionRefMapping = ( response : any ) : Map < string , string > => {
113+ const mapping = new Map < string , string > ( ) ;
114+
115+ if ( response ?. data ?. actions && Array . isArray ( response . data . actions ) ) {
116+ response . data . actions . forEach ( ( action : any ) => {
117+ if ( action . ref && action . nextNode ) {
118+ mapping . set ( action . ref , action . nextNode ) ;
119+ }
120+ } ) ;
121+ }
122+
123+ return mapping ;
124+ } ;
125+
126+ /**
127+ * Apply input ref mapping to components recursively.
128+ * This ensures that component.ref values are mapped to the correct identifier
129+ * from data.inputs, enabling proper form submission.
130+ *
131+ * @param components - Array of components to transform
132+ * @param refMapping - Map of ref to identifier
133+ * @param actionMapping - Map of action ref to nextNode
134+ * @returns Transformed components with correct identifiers and action references
135+ */
136+ const applyInputRefMapping = (
137+ components : EmbeddedFlowComponent [ ] ,
138+ refMapping : Map < string , string > ,
139+ actionMapping : Map < string , string > ,
140+ ) : EmbeddedFlowComponent [ ] => {
141+ return components . map ( component => {
142+ const transformedComponent = { ...component } as EmbeddedFlowComponent & { actionRef ?: string } ;
143+
144+ // If this component has a ref that maps to an identifier, update it
145+ if ( transformedComponent . ref && refMapping . has ( transformedComponent . ref ) ) {
146+ transformedComponent . ref = refMapping . get ( transformedComponent . ref ) ;
147+ }
148+
149+ // If this is an action component, map its id to the nextNode
150+ // Store the nextNode reference as actionRef property for later use
151+ if ( transformedComponent . type === 'ACTION' && transformedComponent . id && actionMapping . has ( transformedComponent . id ) ) {
152+ transformedComponent . actionRef = actionMapping . get ( transformedComponent . id ) ;
153+ }
154+
155+ // Recursively apply to nested components
156+ if ( transformedComponent . components && Array . isArray ( transformedComponent . components ) ) {
157+ transformedComponent . components = applyInputRefMapping (
158+ transformedComponent . components ,
159+ refMapping ,
160+ actionMapping ,
161+ ) ;
162+ }
163+
164+ return transformedComponent ;
165+ } ) ;
166+ } ;
167+
82168/**
83169 * Transform and resolve translations in components from flow response.
84170 * This function extracts components from the response meta structure and optionally resolves
85- * any translation strings within them.
171+ * any translation strings within them. It also handles mapping of input refs to identifiers
172+ * and action refs to nextNode values.
86173 *
87174 * @param response - The flow response object containing components in meta structure
88175 * @param t - Translation function from useTranslation hook
@@ -98,7 +185,18 @@ export const transformComponents = (
98185 return [ ] ;
99186 }
100187
101- const components : EmbeddedFlowComponent [ ] = response . data . meta . components ;
188+ let components : EmbeddedFlowComponent [ ] = response . data . meta . components ;
189+
190+ // Create mapping from ref to identifier based on data.inputs
191+ const refMapping = createInputRefMapping ( response ) ;
192+
193+ // Create mapping from action ref to nextNode based on data.actions
194+ const actionMapping = createActionRefMapping ( response ) ;
195+
196+ // Apply ref and action mapping if there are any mappings
197+ if ( refMapping . size > 0 || actionMapping . size > 0 ) {
198+ components = applyInputRefMapping ( components , refMapping , actionMapping ) ;
199+ }
102200
103201 return resolveTranslations ? resolveTranslationsInArray ( components , t ) : components ;
104202} ;
0 commit comments