-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 019 — PigDetail card redesign #21
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { render, screen } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import type { Focus } from "../types/focus"; | ||
| import { PigDetail } from "./PigDetail"; | ||
|
|
||
| const baseFocus: Focus = { | ||
| id: "pig-a", | ||
| title: "Ship it", | ||
| description: "", | ||
| tasks: [], | ||
| }; | ||
|
|
||
| function renderDetail(overrides?: Partial<React.ComponentProps<typeof PigDetail>>) { | ||
| const props = { | ||
| focus: baseFocus, | ||
| pigX: 100, | ||
| pigY: 100, | ||
| viewportW: 1920, | ||
| viewportH: 1080, | ||
| onClose: vi.fn(), | ||
| onClearTask: vi.fn(), | ||
| onAddTask: vi.fn(), | ||
| ...overrides, | ||
| }; | ||
| render(<PigDetail {...props} />); | ||
| return props; | ||
| } | ||
|
|
||
| describe("PigDetail add-task input", () => { | ||
| it("renders an add task input with placeholder", () => { | ||
| renderDetail(); | ||
| expect(screen.getByPlaceholderText("Add task…")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("Enter calls onAddTask with trimmed text", async () => { | ||
| const { onAddTask } = renderDetail(); | ||
| const input = screen.getByPlaceholderText("Add task…"); | ||
| await userEvent.type(input, "fix the thing{Enter}"); | ||
| expect(onAddTask).toHaveBeenCalledWith("fix the thing"); | ||
| }); | ||
|
|
||
| it("Enter clears the input field", async () => { | ||
| renderDetail(); | ||
| const input = screen.getByPlaceholderText("Add task…"); | ||
| await userEvent.type(input, "some task{Enter}"); | ||
| expect(input).toHaveValue(""); | ||
| }); | ||
|
|
||
| it("Enter with empty text does not call onAddTask", async () => { | ||
| const { onAddTask } = renderDetail(); | ||
| const input = screen.getByPlaceholderText("Add task…"); | ||
| await userEvent.type(input, " {Enter}"); | ||
| expect(onAddTask).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("Escape closes the card", async () => { | ||
| const { onClose } = renderDetail(); | ||
| await userEvent.keyboard("{Escape}"); | ||
| expect(onClose).toHaveBeenCalled(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { useEffect } from "react"; | ||
| import { useEffect, useState } from "react"; | ||
| import { PIG_SIZE } from "../hooks/usePigMovement"; | ||
| import type { Focus } from "../types/focus"; | ||
|
|
||
|
|
@@ -10,9 +10,10 @@ export interface PigDetailProps { | |
| readonly viewportH: number; | ||
| readonly onClose: () => void; | ||
| readonly onClearTask: (index: number) => void; | ||
| readonly onAddTask: (text: string) => void; | ||
| } | ||
|
|
||
| const CARD_W = 210; | ||
| const CARD_W = 340; | ||
| const CARD_OFFSET_X = PIG_SIZE + 8; | ||
|
|
||
| export function PigDetail({ | ||
|
|
@@ -23,7 +24,10 @@ export function PigDetail({ | |
| viewportH, | ||
| onClose, | ||
| onClearTask, | ||
| onAddTask, | ||
| }: PigDetailProps) { | ||
| const [taskInput, setTaskInput] = useState(""); | ||
|
|
||
|
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reset draft input when the selected focus changes.
Proposed fix const [taskInput, setTaskInput] = useState("");
+
+ useEffect(() => {
+ setTaskInput("");
+ }, [focus.id]);Also applies to: 74-85 🤖 Prompt for AI Agents |
||
| useEffect(() => { | ||
| const handler = (e: KeyboardEvent) => { | ||
| if (e.key === "Escape") onClose(); | ||
|
|
@@ -67,6 +71,18 @@ export function PigDetail({ | |
| ))} | ||
| </ul> | ||
| )} | ||
| <input | ||
| className="pig-detail-add-task" | ||
| placeholder="Add task…" | ||
| value={taskInput} | ||
| onChange={(e) => setTaskInput(e.target.value)} | ||
| onKeyDown={(e) => { | ||
| if (e.key === "Enter" && taskInput.trim()) { | ||
| onAddTask(taskInput.trim()); | ||
| setTaskInput(""); | ||
| } | ||
| }} | ||
| /> | ||
| </div> | ||
| </> | ||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | 🏗️ Heavy lift
Move add-task persistence out of the component layer.
At Line 37,
Appperforms direct I/O viafocusWriter.appendTask(...). This violates the component-layer boundary and should be delegated to a non-UI layer (e.g., hook/controller/service), withAppconsuming a UI-safe callback.As per coding guidelines,
src/components/**/*.{ts,tsx}: Frontend React components insrc/components/should be view-only with nofetchand no direct I/O.Also applies to: 65-65
🤖 Prompt for AI Agents