Skip to content

Commit 14a9cfe

Browse files
fix: enter to submit workspace create form (#142)
1 parent d471fd0 commit 14a9cfe

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

src/features/workspace/components/__tests__/workspace-creation.test.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@ test("create workspace", async () => {
2020

2121
expect(screen.getByText(/name/i)).toBeVisible();
2222

23-
screen.logTestingPlaygroundURL();
2423
await userEvent.type(screen.getByRole("textbox"), "workspaceA");
2524
await userEvent.click(screen.getByRole("button", { name: /create/i }));
2625
await waitFor(() => expect(mockNavigate).toBeCalled());
2726
});
27+
28+
test("create workspace with enter button", async () => {
29+
render(<WorkspaceCreation />);
30+
31+
expect(screen.getByText(/name/i)).toBeVisible();
32+
33+
await userEvent.type(screen.getByRole("textbox"), "workspaceA{enter}");
34+
await waitFor(() => expect(mockNavigate).toBeCalled());
35+
});

src/features/workspace/components/workspace-creation.tsx

+33-25
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,53 @@ import {
44
Card,
55
CardBody,
66
CardFooter,
7+
Form,
78
Input,
89
Label,
910
LinkButton,
1011
TextField,
1112
} from "@stacklok/ui-kit";
12-
import { useState } from "react";
13+
import { FormEvent, useState } from "react";
1314

1415
export function WorkspaceCreation() {
1516
const [workspaceName, setWorkspaceName] = useState("");
1617
const { mutate, isPending, error } = useCreateWorkspace();
1718
const errorMsg = error?.detail ? `${error?.detail}` : "";
1819

19-
const handleCreateWorkspace = () => {
20+
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
21+
e.preventDefault();
2022
mutate({ body: { name: workspaceName } });
2123
};
2224

2325
return (
24-
<Card>
25-
<CardBody className="w-full">
26-
<TextField
27-
aria-label="Workspace name"
28-
validationBehavior="aria"
29-
isRequired
30-
onChange={setWorkspaceName}
31-
>
32-
<Label>Name</Label>
33-
<Input value={workspaceName} />
34-
{errorMsg && <div className="p-1 text-red-700">{errorMsg}</div>}
35-
</TextField>
36-
</CardBody>
37-
<CardFooter className="justify-end gap-2">
38-
<LinkButton variant="secondary">Cancel</LinkButton>
39-
<Button
40-
isDisabled={isPending || workspaceName === ""}
41-
onPress={() => handleCreateWorkspace()}
42-
>
43-
Create
44-
</Button>
45-
</CardFooter>
46-
</Card>
26+
<Form onSubmit={handleSubmit} validationBehavior="aria">
27+
<Card>
28+
<CardBody className="w-full">
29+
<TextField
30+
aria-label="Workspace name"
31+
name="Workspace name"
32+
validationBehavior="aria"
33+
isRequired
34+
onChange={setWorkspaceName}
35+
>
36+
<Label>Name</Label>
37+
<Input value={workspaceName} />
38+
{errorMsg && <div className="p-1 text-red-700">{errorMsg}</div>}
39+
</TextField>
40+
</CardBody>
41+
<CardFooter className="justify-end gap-2">
42+
<LinkButton variant="secondary" href="/workspaces">
43+
Cancel
44+
</LinkButton>
45+
<Button
46+
isDisabled={workspaceName === ""}
47+
isPending={isPending}
48+
type="submit"
49+
>
50+
Create
51+
</Button>
52+
</CardFooter>
53+
</Card>
54+
</Form>
4755
);
4856
}

0 commit comments

Comments
 (0)