1- import { useEffect , useState } from 'react' ;
1+ import { useEffect , useState , useRef } from 'react' ;
22import { FieldExtensionComponentProps } from '@backstage/plugin-scaffolder-react' ;
33import { Typography , Box } from '@material-ui/core' ;
44import {
@@ -45,6 +45,10 @@ export const BuildWorkflowParameters = ({
4545 const [ loading , setLoading ] = useState ( false ) ;
4646 const [ error , setError ] = useState < string | null > ( null ) ;
4747
48+ // Track workflow changes to force Form remount only when workflow actually changes
49+ const [ resetKey , setResetKey ] = useState ( 0 ) ;
50+ const prevWorkflowRef = useRef < string | undefined > ( undefined ) ;
51+
4852 const discoveryApi = useApi ( discoveryApiRef ) ;
4953 const identityApi = useApi ( identityApiRef ) ;
5054
@@ -53,6 +57,18 @@ export const BuildWorkflowParameters = ({
5357 const selectedWorkflowName = formContext ?. formData ?. workflow_name ;
5458 const organizationName = formContext ?. formData ?. organization_name ;
5559
60+ // Increment resetKey only when workflow actually changes
61+ // This forces Form remount only on workflow change, not on every render
62+ useEffect ( ( ) => {
63+ if (
64+ prevWorkflowRef . current !== undefined &&
65+ prevWorkflowRef . current !== selectedWorkflowName
66+ ) {
67+ setResetKey ( prev => prev + 1 ) ;
68+ }
69+ prevWorkflowRef . current = selectedWorkflowName ;
70+ } , [ selectedWorkflowName ] ) ;
71+
5672 // Fetch workflow schema when workflow selection changes
5773 useEffect ( ( ) => {
5874 let ignore = false ;
@@ -124,20 +140,27 @@ export const BuildWorkflowParameters = ({
124140 } ;
125141 } , [ selectedWorkflowName , organizationName , discoveryApi , identityApi ] ) ;
126142
127- // Sync schema to formData when workflow schema changes
143+ // Sync schema to formData when workflow changes (not just when schema loads)
128144 useEffect ( ( ) => {
129- if ( workflowSchema ) {
130- // Update formData to include the schema for validation
145+ if ( workflowSchema && resetKey > 0 ) {
146+ // Clear old parameters only when workflow actually changes (resetKey increments)
147+ // This prevents clearing when user navigates back to this step
148+ onChange ( {
149+ parameters : { } ,
150+ schema : workflowSchema ,
151+ } ) ;
152+ } else if ( workflowSchema && resetKey === 0 && ! formData ?. schema ) {
153+ // First time loading: initialize with existing or empty parameters
131154 onChange ( {
132155 parameters : formData ?. parameters || { } ,
133156 schema : workflowSchema ,
134157 } ) ;
135158 }
136- // We intentionally only depend on workflowSchema to avoid infinite loop .
159+ // We intentionally only depend on workflowSchema and resetKey .
137160 // Adding onChange or formData would cause infinite re-renders:
138161 // onChange -> formData changes -> useEffect triggers -> onChange -> loop
139162 // eslint-disable-next-line react-hooks/exhaustive-deps
140- } , [ workflowSchema ] ) ;
163+ } , [ workflowSchema , resetKey ] ) ;
141164
142165 // Handle form data changes from RJSF
143166 const handleFormChange = ( changeEvent : any ) => {
@@ -190,6 +213,7 @@ export const BuildWorkflowParameters = ({
190213 Workflow Parameters
191214 </ Typography >
192215 < Form
216+ key = { resetKey }
193217 schema = { workflowSchema }
194218 uiSchema = { uiSchema }
195219 formData = { formData ?. parameters || { } }
0 commit comments