From 290f11b9db698458ac1ac676061c463ba30604b3 Mon Sep 17 00:00:00 2001 From: "blink-so[bot]" <211532188+blink-so[bot]@users.noreply.github.com> Date: Fri, 6 Jun 2025 14:59:08 +0000 Subject: [PATCH 1/2] fix: preserve parameter values when dynamic ordering changes When parameters are reordered dynamically (e.g., when a parameter's order depends on another parameter's value), the parameter values were not displaying correctly. This was because the rendering logic used array indices instead of parameter names to map form values. The issue occurred when: 1. Parameters are initially rendered in one order 2. A parameter value changes, causing dynamic reordering 3. The parameter array order changes but form values array stays the same 4. Values get mismatched due to index-based lookup This fix implements name-based lookup to ensure parameter values persist correctly regardless of ordering changes: - Find parameter value by name instead of array index - Use the found index for form field mapping - Fallback to current index for new parameters Fixes parameter value persistence in dynamic parameter ordering scenarios. --- .../CreateWorkspacePageViewExperimental.tsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx b/site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx index 1527e084503d4..b2500ad2537fe 100644 --- a/site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx +++ b/site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx @@ -608,7 +608,15 @@ export const CreateWorkspacePageViewExperimental: FC<
{parameters.map((parameter, index) => { - const parameterField = `rich_parameter_values.${index}`; + // Find the current parameter's value by name instead of index + // to ensure values persist correctly when parameter order changes + const currentParameterValueIndex = form.values.rich_parameter_values?.findIndex( + (p) => p.name === parameter.name + ) ?? -1; + + // Use the found index for the parameter field, or fallback to current index + const parameterFieldIndex = currentParameterValueIndex !== -1 ? currentParameterValueIndex : index; + const parameterField = `rich_parameter_values.${parameterFieldIndex}`; const isPresetParameter = presetParameterNames.includes( parameter.name, ); @@ -629,8 +637,10 @@ export const CreateWorkspacePageViewExperimental: FC< return null; } - const formValue = - form.values?.rich_parameter_values?.[index]?.value || ""; + // Get the form value by parameter name to ensure correct value mapping + const formValue = currentParameterValueIndex !== -1 + ? form.values?.rich_parameter_values?.[currentParameterValueIndex]?.value || "" + : ""; return ( Date: Fri, 6 Jun 2025 15:53:36 +0000 Subject: [PATCH 2/2] fix: format --- .../CreateWorkspacePageViewExperimental.tsx | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx b/site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx index b2500ad2537fe..c2b6807a5833f 100644 --- a/site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx +++ b/site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx @@ -608,15 +608,15 @@ export const CreateWorkspacePageViewExperimental: FC<
{parameters.map((parameter, index) => { - // Find the current parameter's value by name instead of index - // to ensure values persist correctly when parameter order changes - const currentParameterValueIndex = form.values.rich_parameter_values?.findIndex( - (p) => p.name === parameter.name - ) ?? -1; - - // Use the found index for the parameter field, or fallback to current index - const parameterFieldIndex = currentParameterValueIndex !== -1 ? currentParameterValueIndex : index; - const parameterField = `rich_parameter_values.${parameterFieldIndex}`; + const currentParameterValueIndex = + form.values.rich_parameter_values?.findIndex( + (p) => p.name === parameter.name, + ) ?? -1; + const parameterFieldIndex = + currentParameterValueIndex !== -1 + ? currentParameterValueIndex + : index; + const parameterField = `rich_parameter_values.${parameterFieldIndex}`; const isPresetParameter = presetParameterNames.includes( parameter.name, ); @@ -638,9 +638,12 @@ export const CreateWorkspacePageViewExperimental: FC< } // Get the form value by parameter name to ensure correct value mapping - const formValue = currentParameterValueIndex !== -1 - ? form.values?.rich_parameter_values?.[currentParameterValueIndex]?.value || "" - : ""; + const formValue = + currentParameterValueIndex !== -1 + ? form.values?.rich_parameter_values?.[ + currentParameterValueIndex + ]?.value || "" + : ""; return (