From d44f87eed0881da8ce8162ae1bf3c42aff354f06 Mon Sep 17 00:00:00 2001 From: christin <46345125+chrifro@users.noreply.github.com> Date: Wed, 9 Sep 2026 09:02:23 +0200 Subject: [PATCH] feat(site/src/pages/TemplateBuilder): keep Continue button enabled with inline validation (#28940) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary On the template builder (`/templates/new/builder`), the **Continue** button was disabled until the current step's requirements were met, leaving users with no explanation for why they could not advance. This PR keeps the **Continue** button enabled at all times. When the current step is incomplete, clicking Continue no longer advances; instead it: - shows a red validation message (12px) below the button explaining what is missing, and - outlines the required input fields that are blocking continuation in red. ## Changes - Removed the `disabled={!canContinue}` guard from the Continue button so it is always clickable. - `handleNext` blocks navigation when the step is incomplete and reveals the red validation message instead. - Added `continueErrorMessage(stepId)` with a step-specific reason (base template not selected, required parameters/module settings missing, no provisioner online). - Validation message uses a 12px (`text-xs`) font. - When validation is triggered, empty required fields in the base-parameters and module-settings steps are outlined in red (`aria-invalid` + `border-border-destructive`). Optional fields are unaffected. - The message and outlines clear automatically once the step's requirements are satisfied or the user moves to another step. - Added Storybook interaction tests: `TemplateBuilderPageView` (Continue stays enabled, message appears then clears), `BaseTemplateParametersStep` and `ModuleSettingsStep` (required field flagged invalid when empty). The final-step "Create Template" button keeps its existing provisioner-based disable behavior; this change is scoped to the Continue button as requested. ## Visual proof Base template step (`?step=base-infra`), no base selected: | Before (Continue disabled) | After (Continue enabled) | After (validation error on click) | |---|---|---| | ![before](https://raw.githubusercontent.com/coder/coder/pr-assets/template-builder-continue-btn/before-1-initial.png) | ![after enabled](https://raw.githubusercontent.com/coder/coder/pr-assets/template-builder-continue-btn/after-1-initial.png) | ![after error](https://raw.githubusercontent.com/coder/coder/pr-assets/template-builder-continue-btn/after-2-error.png) | Module settings step (`?step=module-settings`) — required field outlined in red with the 12px message on Continue: ![module outline](https://raw.githubusercontent.com/coder/coder/pr-assets/template-builder-continue-btn/after-3-module-outline.png) ## Testing - `pnpm check`, `pnpm lint`, `pnpm lint:types`, `pnpm format` pass. - Storybook interaction tests pass (`vitest --project=storybook` for the three step/page stories). - Verified manually against the running dev app on port 8080.
Implementation notes Requirement: "for the template builder (`/templates/new/builder`) always keep the continue button enabled. If the requirements to continue are not met, show the error in red." Follow-up: "use 12px for the font size for the error message and highlight the input fields that cause the error with a red outline if applicable." Approach: 1. Keep the Continue button always enabled by dropping the `disabled` prop. 2. Gate navigation in `handleNext`: when `canContinue` is false, set a local `showContinueError` flag and return instead of navigating. 3. Render a red (`text-content-destructive`), 12px (`text-xs`) validation message below the nav controls when `showContinueError && !canContinue`. 4. Pass `showContinueError` into the step renderers as `showErrors`; the base-parameters and module-settings steps mark each required-but-empty field's `field.error`, which `FormField` renders as `aria-invalid` plus a red border. 5. Clear the flag (and thus the message and outlines) when requirements become satisfied or on step change, so nothing lingers. 6. Message text is derived per step via `continueErrorMessage(stepId)`.
--- _This PR was generated by Coder Agents on behalf of @chrifro._ --------- Co-authored-by: Andrew Aquino --- .../BaseTemplateParametersStep.stories.tsx | 76 ++++++++++++++++ .../BaseTemplateParametersStep.tsx | 18 ++-- .../ModuleSettingsStep.stories.tsx | 69 +++++++++++++++ .../TemplateBuilder/ModuleSettingsStep.tsx | 16 +++- .../TemplateBuilderPageView.stories.tsx | 87 +++++++++++++++++++ .../TemplateBuilderPageView.tsx | 49 ++++++++++- 6 files changed, 303 insertions(+), 12 deletions(-) create mode 100644 site/src/pages/TemplateBuilder/BaseTemplateParametersStep.stories.tsx create mode 100644 site/src/pages/TemplateBuilder/ModuleSettingsStep.stories.tsx create mode 100644 site/src/pages/TemplateBuilder/TemplateBuilderPageView.stories.tsx diff --git a/site/src/pages/TemplateBuilder/BaseTemplateParametersStep.stories.tsx b/site/src/pages/TemplateBuilder/BaseTemplateParametersStep.stories.tsx new file mode 100644 index 00000000000..c0344c0cf85 --- /dev/null +++ b/site/src/pages/TemplateBuilder/BaseTemplateParametersStep.stories.tsx @@ -0,0 +1,76 @@ +import type { Meta, StoryObj } from "@storybook/react-vite"; +import { expect, fn, within } from "storybook/test"; +import type { TemplateBuilderBasesResponse } from "#/api/typesGenerated"; +import { BaseTemplateParametersStep } from "./BaseTemplateParametersStep"; + +const baseId = "aws-ec2"; + +const basesData: TemplateBuilderBasesResponse = { + bases: [ + { + id: baseId, + name: "AWS EC2", + description: "Provision workspaces as AWS EC2 instances.", + icon: "/icon/aws.svg", + os: "linux", + prerequisites: "", + agents: [], + variables: [ + { + name: "region", + type: "string", + description: "AWS region to deploy into.", + required: true, + sensitive: false, + }, + { + name: "instance_type", + type: "string", + description: "EC2 instance type.", + required: false, + sensitive: false, + }, + ], + }, + ], +}; + +const meta: Meta = { + title: "pages/TemplateBuilder/BaseTemplateParametersStep", + component: BaseTemplateParametersStep, + args: { + baseId, + values: {}, + onChangeValues: fn(), + }, + parameters: { + queries: [ + { + key: ["templateBuilder", "bases"], + data: basesData, + }, + ], + }, +}; + +export default meta; +type Story = StoryObj; + +export const Default: Story = {}; + +// With showErrors on and the required "region" field empty, the field is +// flagged invalid (red outline) while the optional field is not. +export const RequiredFieldError: Story = { + args: { + showErrors: true, + }, + play: async ({ canvasElement }) => { + const canvas = within(canvasElement); + const region = await canvas.findByRole("textbox", { name: /region/ }); + await expect(region).toHaveAttribute("aria-invalid", "true"); + const instanceType = canvas.getByRole("textbox", { + name: /instance_type/, + }); + await expect(instanceType).toHaveAttribute("aria-invalid", "false"); + }, +}; diff --git a/site/src/pages/TemplateBuilder/BaseTemplateParametersStep.tsx b/site/src/pages/TemplateBuilder/BaseTemplateParametersStep.tsx index e52846cf257..59535f57acc 100644 --- a/site/src/pages/TemplateBuilder/BaseTemplateParametersStep.tsx +++ b/site/src/pages/TemplateBuilder/BaseTemplateParametersStep.tsx @@ -22,6 +22,7 @@ interface BaseTemplateParametersStepProps { baseId: string; values: Record; onChangeValues: (values: Record) => void; + showErrors?: boolean; } function detailsUrl(baseId: string): string { @@ -36,6 +37,7 @@ function variableToField( variable: TemplateBuilderModuleVariable, value: string, onChange: (name: string, value: string) => void, + error: boolean, ): ConfigurationFieldDefinition { const id = `base-var-${variable.name}`; const label = ; @@ -68,7 +70,7 @@ function variableToField( value, onChange: (e) => onChange(variable.name, e.target.value), onBlur: () => {}, - error: false, + error, }, }; } @@ -98,7 +100,7 @@ export function baseParametersComplete( export const BaseTemplateParametersStep: FC< BaseTemplateParametersStepProps -> = ({ baseId, values, onChangeValues }) => { +> = ({ baseId, values, onChangeValues, showErrors = false }) => { const { data } = useQuery(templateBuilderBases()); const base = data?.bases.find((b) => b.id === baseId); const variables = base?.variables.filter((v) => !v.sensitive) ?? []; @@ -108,13 +110,17 @@ export const BaseTemplateParametersStep: FC< onChangeValues({ ...values, [name]: value }); }; - const fields: ConfigurationFieldDefinition[] = variables.map((v) => - variableToField( + const fields: ConfigurationFieldDefinition[] = variables.map((v) => { + const rawValue = values[v.name]; + const hasError = + showErrors && v.required && (rawValue === undefined || rawValue === ""); + return variableToField( v, values[v.name] ?? defaultPlaceholder(v.default) ?? "", handleChange, - ), - ); + hasError, + ); + }); return ( <> diff --git a/site/src/pages/TemplateBuilder/ModuleSettingsStep.stories.tsx b/site/src/pages/TemplateBuilder/ModuleSettingsStep.stories.tsx new file mode 100644 index 00000000000..3538437ffde --- /dev/null +++ b/site/src/pages/TemplateBuilder/ModuleSettingsStep.stories.tsx @@ -0,0 +1,69 @@ +import type { Meta, StoryObj } from "@storybook/react-vite"; +import { expect, fn, within } from "storybook/test"; +import type { TemplateBuilderModulesResponse } from "#/api/typesGenerated"; +import { ModuleSettingsStep } from "./ModuleSettingsStep"; + +const baseId = "docker"; + +const modulesData: TemplateBuilderModulesResponse = { + modules: [ + { + id: "jetbrains", + display_name: "JetBrains Toolbox", + description: "Add JetBrains IDE integrations to your Coder workspaces.", + icon: "/icon/jetbrains.svg", + category: "IDE", + version: "1.0.0", + compatible_os: ["linux"], + conflicts_with: [], + variables: [ + { + name: "folder", + type: "string", + description: "The directory to open in the IDE.", + required: true, + sensitive: false, + }, + ], + }, + ], +}; + +const meta: Meta = { + title: "pages/TemplateBuilder/ModuleSettingsStep", + component: ModuleSettingsStep, + args: { + baseId, + selectedModuleIds: ["jetbrains"], + moduleVariables: {}, + onChangeModuleVariables: fn(), + onRemoveModule: fn(), + registerModuleRef: fn(), + }, + parameters: { + queries: [ + { + key: ["templateBuilder", "modules", baseId], + data: modulesData, + }, + ], + }, +}; + +export default meta; +type Story = StoryObj; + +export const Default: Story = {}; + +// With showErrors on and the required "folder" field empty, the input is +// flagged invalid so it renders with a red outline. +export const RequiredFieldError: Story = { + args: { + showErrors: true, + }, + play: async ({ canvasElement }) => { + const canvas = within(canvasElement); + const folder = await canvas.findByRole("textbox", { name: /folder/ }); + await expect(folder).toHaveAttribute("aria-invalid", "true"); + }, +}; diff --git a/site/src/pages/TemplateBuilder/ModuleSettingsStep.tsx b/site/src/pages/TemplateBuilder/ModuleSettingsStep.tsx index 3fb4b51b6d9..70ddd238fea 100644 --- a/site/src/pages/TemplateBuilder/ModuleSettingsStep.tsx +++ b/site/src/pages/TemplateBuilder/ModuleSettingsStep.tsx @@ -27,6 +27,7 @@ interface ModuleSettingsStepProps { ) => void; onRemoveModule: (moduleId: string) => void; registerModuleRef: (moduleId: string, node: HTMLDivElement | null) => void; + showErrors?: boolean; } function variableToField( @@ -34,6 +35,7 @@ function variableToField( variable: TemplateBuilderModuleVariable, value: string, onChange: (name: string, value: string) => void, + error: boolean, ): ConfigurationFieldDefinition { const id = `mod-${moduleId}-${variable.name}`; const label = ; @@ -66,7 +68,7 @@ function variableToField( value, onChange: (e) => onChange(variable.name, e.target.value), onBlur: () => {}, - error: false, + error, }, }; } @@ -110,6 +112,7 @@ export const ModuleSettingsStep: FC = ({ onChangeModuleVariables, onRemoveModule, registerModuleRef, + showErrors = false, }) => { const { data } = useQuery(templateBuilderModules(baseId)); const modules = data?.modules ?? []; @@ -134,13 +137,20 @@ export const ModuleSettingsStep: FC = ({ const sensitiveVars = mod.variables.filter((v) => v.sensitive); const vars = moduleVariables[mod.id] ?? {}; - const toField = (v: TemplateBuilderModuleVariable) => - variableToField( + const toField = (v: TemplateBuilderModuleVariable) => { + const rawValue = vars[v.name]; + const hasError = + showErrors && + v.required && + (rawValue === undefined || rawValue === ""); + return variableToField( mod.id, v, vars[v.name] ?? defaultPlaceholder(v.default) ?? "", (name, val) => handleChange(mod.id, name, val), + hasError, ); + }; const requiredVars = configurableVars.filter((v) => v.required); const optionalVars = configurableVars.filter((v) => !v.required); diff --git a/site/src/pages/TemplateBuilder/TemplateBuilderPageView.stories.tsx b/site/src/pages/TemplateBuilder/TemplateBuilderPageView.stories.tsx new file mode 100644 index 00000000000..3fede1ee3fe --- /dev/null +++ b/site/src/pages/TemplateBuilder/TemplateBuilderPageView.stories.tsx @@ -0,0 +1,87 @@ +import type { Meta, StoryObj } from "@storybook/react-vite"; +import { expect, fn, userEvent, waitFor, within } from "storybook/test"; +import type { + TemplateBuilderBase, + TemplateBuilderBasesResponse, +} from "#/api/typesGenerated"; +import { TemplateBuilderPageView } from "./TemplateBuilderPageView"; + +const bases: TemplateBuilderBase[] = [ + { + id: "docker", + name: "Docker", + description: "Provision workspaces as Docker containers.", + icon: "/icon/docker.png", + os: "linux", + variables: [], + prerequisites: "", + agents: [], + }, + { + id: "kubernetes", + name: "Kubernetes", + description: "Provision workspaces as Kubernetes pods.", + icon: "/icon/k8s.png", + os: "linux", + variables: [], + prerequisites: "", + agents: [], + }, +]; + +const basesData: TemplateBuilderBasesResponse = { bases }; + +const meta: Meta = { + title: "pages/TemplateBuilder/TemplateBuilderPageView", + component: TemplateBuilderPageView, + args: { + error: null, + basesData, + onCreateTemplate: fn(), + createError: null, + isCreating: false, + onClearCreateError: fn(), + sessionId: "session-1", + }, + parameters: { + queries: [ + { + key: ["templateBuilder", "bases"], + data: basesData, + }, + { + key: ["templateBuilder", "modules", "docker"], + data: { modules: [] }, + }, + ], + }, +}; + +export default meta; +type Story = StoryObj; + +export const Default: Story = {}; + +// The Continue button stays enabled even when the step is incomplete. Clicking +// it with no base selected reveals a red validation message instead of +// advancing, and the message clears once a base is selected. +export const ContinueShowsValidationError: Story = { + play: async ({ canvasElement }) => { + const canvas = within(canvasElement); + + const continueButton = await canvas.findByRole("button", { + name: "Continue", + }); + await expect(continueButton).toBeEnabled(); + + await userEvent.click(continueButton); + await canvas.findByText("Select a base template to continue."); + + await userEvent.click(await canvas.findByRole("radio", { name: /Docker/ })); + await waitFor(() => + expect( + canvas.queryByText("Select a base template to continue."), + ).not.toBeInTheDocument(), + ); + }, +}; diff --git a/site/src/pages/TemplateBuilder/TemplateBuilderPageView.tsx b/site/src/pages/TemplateBuilder/TemplateBuilderPageView.tsx index aa97cefbde4..8c4b795bc5d 100644 --- a/site/src/pages/TemplateBuilder/TemplateBuilderPageView.tsx +++ b/site/src/pages/TemplateBuilder/TemplateBuilderPageView.tsx @@ -151,6 +151,19 @@ export const TemplateBuilderPageView: FC = ({ moduleVarMap, ); + const [showContinueError, setShowContinueError] = useState(false); + const [errorStepId, setErrorStepId] = useState(currentStep.id); + + // Hide the validation message once the step's requirements are satisfied + // or the user moves to a different step. Both are render-time state + // adjustments rather than effects. + if (errorStepId !== currentStep.id) { + setErrorStepId(currentStep.id); + setShowContinueError(false); + } else if (showContinueError && canContinue) { + setShowContinueError(false); + } + // Pushes a history entry so browser back/forward walks the steps. const navigateToStep = useCallback( (index: number) => { @@ -170,6 +183,10 @@ export const TemplateBuilderPageView: FC = ({ }; const handleNext = () => { + if (!canContinue) { + setShowContinueError(true); + return; + } navigateToStep(nextIndex); }; @@ -310,6 +327,7 @@ export const TemplateBuilderPageView: FC = ({ onRemoveModule={handleDeselectModule} registerModuleRef={registerModuleRef} onCreate={handleCreate} + showValidationErrors={showContinueError} /> @@ -331,12 +349,16 @@ export const TemplateBuilderPageView: FC = ({ Create Template ) : ( - + )} + {showContinueError && !canContinue && ( +

+ {getContinueErrorMessage(currentStep.id)} +

+ )} + {currentStep.id === "base-infra" && } @@ -377,6 +399,7 @@ interface StepContentProps { onRemoveModule: (moduleId: string) => void; registerModuleRef: (moduleId: string, node: HTMLDivElement | null) => void; onCreate: (values: CustomizationsFormValues) => void; + showValidationErrors: boolean; } const StepContent: FC = ({ @@ -389,6 +412,7 @@ const StepContent: FC = ({ onRemoveModule, registerModuleRef, onCreate, + showValidationErrors, }) => { switch (stepId) { case "base-infra": @@ -407,6 +431,7 @@ const StepContent: FC = ({ onChangeValues={(values) => dispatch({ type: "SET_BASE_VARIABLES", values }) } + showErrors={showValidationErrors} /> ); case "module-select": @@ -436,6 +461,7 @@ const StepContent: FC = ({ } onRemoveModule={onRemoveModule} registerModuleRef={registerModuleRef} + showErrors={showValidationErrors} /> ); case "customizations": @@ -454,6 +480,23 @@ const StepContent: FC = ({ } }; +/** + * Human-readable reason a step's requirements are not yet met, shown in red + * when the user clicks Continue on an incomplete step. + */ +function getContinueErrorMessage(stepId: StepId): string { + switch (stepId) { + case "base-infra": + return "Select a base template to continue."; + case "base-parameters": + return "Fill in all required parameters to continue."; + case "module-settings": + return "Fill in all required module settings to continue."; + default: + return "Complete this step to continue."; + } +} + function computeCanContinue( stepId: StepId, state: TemplateBuilderWizardState,