Skip to content

fix: preserve parameter values when dynamic ordering changes #18270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

blink-so[bot]
Copy link
Contributor

@blink-so blink-so bot commented Jun 6, 2025

Problem

When creating a workspace from a template with dynamic parameter ordering, parameter values are not displaying correctly when the order changes. This occurs when a parameter's order value depends on another parameter's value.

Example scenario:

data "coder_parameter" "reorder" {
  name = "reorder"
  type = "bool"
  default = false
  order = 1
}

data "coder_parameter" "cpu" {
  order = data.coder_parameter.reorder.value ? 0 : 2
  name = "cpu"
  type = "number"
  default = 4
}

When the user toggles reorder from false to true, the cpu parameter moves from position 2 to position 0, but its value gets mixed up with the reorder parameter's value.

Root Cause

The issue was in CreateWorkspacePageViewExperimental.tsx where parameters were rendered using array indices instead of parameter names:

// Problematic code
const parameterField = `rich_parameter_values.${index}`;
const formValue = form.values?.rich_parameter_values?.[index]?.value || "";

When parameters are reordered:

  1. The parameters array order changes based on the new order values
  2. The form.values.rich_parameter_values array maintains the original order
  3. Array index-based lookup causes values to be mismatched

Solution

Implemented name-based lookup to ensure parameter values stay with their correct parameters:

// Find parameter value by name instead of index
const currentParameterValueIndex = form.values.rich_parameter_values?.findIndex(
  (p) => p.name === parameter.name
) ?? -1;

// Use the found index for form field mapping
const parameterFieldIndex = currentParameterValueIndex !== -1 ? currentParameterValueIndex : index;
const parameterField = `rich_parameter_values.${parameterFieldIndex}`;

// Get form value by name to ensure correct mapping
const formValue = currentParameterValueIndex !== -1 
  ? form.values?.rich_parameter_values?.[currentParameterValueIndex]?.value || ""
  : "";

Testing

  • ✅ Created test script that validates the fix works correctly
  • ✅ Tested with the provided template showing dynamic parameter ordering
  • ✅ Verified parameter values persist correctly during reordering
  • ✅ Confirmed no TypeScript compilation issues

Impact

This fix ensures that users can reliably use dynamic parameter ordering in their templates without losing parameter values when the order changes. This is particularly important for templates that use conditional parameter visibility and ordering based on user selections.

blink-so bot and others added 2 commits June 6, 2025 14:59
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.
@jaaydenh jaaydenh self-assigned this Jun 6, 2025
@jaaydenh jaaydenh requested a review from Emyrk June 6, 2025 15:54
Comment on lines +611 to +619
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}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaaydenh The overall approach looks correct to me. There might be some cleaner simplier syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy