feat(site/src/pages/TemplateBuilder): keep Continue button enabled with inline validation (#28940)
## 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) |
|---|---|---|
|

| 
| 
|
Module settings step (`?step=module-settings`) — required field outlined
in red with the 12px message on Continue:

## 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.
<details>
<summary>Implementation notes</summary>
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)`.
</details>
---
_This PR was generated by Coder Agents on behalf of @chrifro._
---------
Co-authored-by: Andrew Aquino <dawneraq@gmail.com>
This commit is contained in:
1 parent
df4a7644fd
commit
d44f87eed0
6 files changed
+303
-12
No files matched your search
@@ -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<typeof BaseTemplateParametersStep> = {
|
||||
title: "pages/TemplateBuilder/BaseTemplateParametersStep",
|
||||
component: BaseTemplateParametersStep,
|
||||
args: {
|
||||
baseId,
|
||||
values: {},
|
||||
onChangeValues: fn(),
|
||||
},
|
||||
parameters: {
|
||||
queries: [
|
||||
{
|
||||
key: ["templateBuilder", "bases"],
|
||||
data: basesData,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof BaseTemplateParametersStep>;
|
||||
|
||||
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");
|
||||
},
|
||||
};
|
||||
@@ -22,6 +22,7 @@ interface BaseTemplateParametersStepProps {
|
||||
baseId: string;
|
||||
values: Record<string, string>;
|
||||
onChangeValues: (values: Record<string, string>) => 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 = <ConfigurationFieldLabel variable={variable} />;
|
||||
@@ -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 (
|
||||
<>
|
||||
|
||||
@@ -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<typeof ModuleSettingsStep> = {
|
||||
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<typeof ModuleSettingsStep>;
|
||||
|
||||
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");
|
||||
},
|
||||
};
|
||||
@@ -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 = <ConfigurationFieldLabel variable={variable} />;
|
||||
@@ -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<ModuleSettingsStepProps> = ({
|
||||
onChangeModuleVariables,
|
||||
onRemoveModule,
|
||||
registerModuleRef,
|
||||
showErrors = false,
|
||||
}) => {
|
||||
const { data } = useQuery(templateBuilderModules(baseId));
|
||||
const modules = data?.modules ?? [];
|
||||
@@ -134,13 +137,20 @@ export const ModuleSettingsStep: FC<ModuleSettingsStepProps> = ({
|
||||
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);
|
||||
|
||||
@@ -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<typeof TemplateBuilderPageView> = {
|
||||
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<typeof TemplateBuilderPageView>;
|
||||
|
||||
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(),
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -151,6 +151,19 @@ export const TemplateBuilderPageView: FC<TemplateBuilderPageViewProps> = ({
|
||||
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<TemplateBuilderPageViewProps> = ({
|
||||
};
|
||||
|
||||
const handleNext = () => {
|
||||
if (!canContinue) {
|
||||
setShowContinueError(true);
|
||||
return;
|
||||
}
|
||||
navigateToStep(nextIndex);
|
||||
};
|
||||
|
||||
@@ -310,6 +327,7 @@ export const TemplateBuilderPageView: FC<TemplateBuilderPageViewProps> = ({
|
||||
onRemoveModule={handleDeselectModule}
|
||||
registerModuleRef={registerModuleRef}
|
||||
onCreate={handleCreate}
|
||||
showValidationErrors={showContinueError}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -331,12 +349,16 @@ export const TemplateBuilderPageView: FC<TemplateBuilderPageViewProps> = ({
|
||||
Create Template
|
||||
</Button>
|
||||
) : (
|
||||
<Button onClick={handleNext} disabled={!canContinue}>
|
||||
Continue
|
||||
</Button>
|
||||
<Button onClick={handleNext}>Continue</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{showContinueError && !canContinue && (
|
||||
<p className="flex justify-end mt-2 mb-0 text-xs text-content-destructive">
|
||||
{getContinueErrorMessage(currentStep.id)}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{currentStep.id === "base-infra" && <TemplateAlternatives />}
|
||||
</div>
|
||||
|
||||
@@ -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<StepContentProps> = ({
|
||||
@@ -389,6 +412,7 @@ const StepContent: FC<StepContentProps> = ({
|
||||
onRemoveModule,
|
||||
registerModuleRef,
|
||||
onCreate,
|
||||
showValidationErrors,
|
||||
}) => {
|
||||
switch (stepId) {
|
||||
case "base-infra":
|
||||
@@ -407,6 +431,7 @@ const StepContent: FC<StepContentProps> = ({
|
||||
onChangeValues={(values) =>
|
||||
dispatch({ type: "SET_BASE_VARIABLES", values })
|
||||
}
|
||||
showErrors={showValidationErrors}
|
||||
/>
|
||||
);
|
||||
case "module-select":
|
||||
@@ -436,6 +461,7 @@ const StepContent: FC<StepContentProps> = ({
|
||||
}
|
||||
onRemoveModule={onRemoveModule}
|
||||
registerModuleRef={registerModuleRef}
|
||||
showErrors={showValidationErrors}
|
||||
/>
|
||||
);
|
||||
case "customizations":
|
||||
@@ -454,6 +480,23 @@ const StepContent: FC<StepContentProps> = ({
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 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,
|
||||
|
||||
Reference in new issue
Block a user