Skip to content

Commit d104cd6

Browse files
authored
fix: display validation error for workspace name (#17564)
- Display form validation error for workspace name - Scroll to the workspace name field if there is a validation error
1 parent 6bafe35 commit d104cd6

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

site/src/pages/CreateWorkspacePage/CreateWorkspacePageViewExperimental.tsx

+18-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
useContext,
2929
useEffect,
3030
useId,
31+
useRef,
3132
useState,
3233
} from "react";
3334
import { getFormHelpers, nameValidator } from "utils/formUtils";
@@ -103,6 +104,7 @@ export const CreateWorkspacePageViewExperimental: FC<
103104
);
104105
const [showPresetParameters, setShowPresetParameters] = useState(false);
105106
const id = useId();
107+
const workspaceNameInputRef = useRef<HTMLInputElement>(null);
106108
const rerollSuggestedName = useCallback(() => {
107109
setSuggestedName(() => generateWorkspaceName());
108110
}, []);
@@ -140,10 +142,15 @@ export const CreateWorkspacePageViewExperimental: FC<
140142
}
141143
}, [error]);
142144

143-
const getFieldHelpers = getFormHelpers<TypesGen.CreateWorkspaceRequest>(
144-
form,
145-
error,
146-
);
145+
useEffect(() => {
146+
if (form.submitCount > 0 && form.errors) {
147+
workspaceNameInputRef.current?.scrollIntoView({
148+
behavior: "smooth",
149+
block: "center",
150+
});
151+
workspaceNameInputRef.current?.focus();
152+
}
153+
}, [form.submitCount, form.errors]);
147154

148155
const [presetOptions, setPresetOptions] = useState([
149156
{ label: "None", value: "" },
@@ -333,16 +340,22 @@ export const CreateWorkspacePageViewExperimental: FC<
333340
<Label className="text-sm" htmlFor={`${id}-workspace-name`}>
334341
Workspace name
335342
</Label>
336-
<div>
343+
<div className="flex flex-col">
337344
<Input
338345
id={`${id}-workspace-name`}
346+
ref={workspaceNameInputRef}
339347
value={form.values.name}
340348
onChange={(e) => {
341349
form.setFieldValue("name", e.target.value.trim());
342350
resetMutation();
343351
}}
344352
disabled={creatingWorkspace}
345353
/>
354+
{form.touched.name && form.errors.name && (
355+
<div className="text-content-destructive text-xs mt-2">
356+
{form.errors.name}
357+
</div>
358+
)}
346359
<div className="flex gap-2 text-xs text-content-secondary items-center">
347360
Need a suggestion?
348361
<Button
@@ -477,7 +490,6 @@ export const CreateWorkspacePageViewExperimental: FC<
477490

478491
return (
479492
<DynamicParameter
480-
{...getFieldHelpers(parameterInputName)}
481493
key={parameter.name}
482494
parameter={parameter}
483495
onChange={(value) =>

0 commit comments

Comments
 (0)