|
| 1 | +import React, { useState } from "react" |
| 2 | +import { StandardTooltip } from "@/components/ui" |
| 3 | +import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react" |
| 4 | + |
| 5 | +interface RenameButtonProps { |
| 6 | + currentName: string |
| 7 | + onRename: (newName: string) => void |
| 8 | + className?: string |
| 9 | +} |
| 10 | + |
| 11 | +export const RenameButton: React.FC<RenameButtonProps> = ({ currentName, onRename, className = "" }) => { |
| 12 | + const [isEditing, setIsEditing] = useState(false) |
| 13 | + const [editValue, setEditValue] = useState(currentName) |
| 14 | + |
| 15 | + const handleStartEdit = (e: React.MouseEvent) => { |
| 16 | + e.stopPropagation() |
| 17 | + setEditValue(currentName) |
| 18 | + setIsEditing(true) |
| 19 | + } |
| 20 | + |
| 21 | + const handleSave = () => { |
| 22 | + const trimmedValue = editValue.trim() |
| 23 | + if (trimmedValue !== currentName) { |
| 24 | + onRename(trimmedValue) |
| 25 | + } |
| 26 | + setIsEditing(false) |
| 27 | + } |
| 28 | + |
| 29 | + const handleCancel = () => { |
| 30 | + setEditValue(currentName) |
| 31 | + setIsEditing(false) |
| 32 | + } |
| 33 | + |
| 34 | + const handleKeyDown = (e: React.KeyboardEvent) => { |
| 35 | + if (e.key === "Enter") { |
| 36 | + handleSave() |
| 37 | + } else if (e.key === "Escape") { |
| 38 | + handleCancel() |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (isEditing) { |
| 43 | + return ( |
| 44 | + <div className="flex items-center gap-1" onClick={(e) => e.stopPropagation()}> |
| 45 | + <VSCodeTextField |
| 46 | + value={editValue} |
| 47 | + onInput={(e) => setEditValue((e.target as HTMLInputElement).value)} |
| 48 | + onKeyDown={handleKeyDown} |
| 49 | + className="text-xs" |
| 50 | + style={{ minWidth: "120px" }} |
| 51 | + autoFocus |
| 52 | + data-testid="rename-input" |
| 53 | + /> |
| 54 | + <button |
| 55 | + onClick={handleSave} |
| 56 | + className="p-1 rounded hover:bg-vscode-toolbar-hoverBackground transition-colors" |
| 57 | + data-testid="rename-save"> |
| 58 | + <span className="codicon codicon-check text-xs text-green-400" /> |
| 59 | + </button> |
| 60 | + <button |
| 61 | + onClick={handleCancel} |
| 62 | + className="p-1 rounded hover:bg-vscode-toolbar-hoverBackground transition-colors" |
| 63 | + data-testid="rename-cancel"> |
| 64 | + <span className="codicon codicon-close text-xs text-red-400" /> |
| 65 | + </button> |
| 66 | + </div> |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <StandardTooltip content="Rename task"> |
| 72 | + <button |
| 73 | + onClick={handleStartEdit} |
| 74 | + className={`p-1 rounded hover:bg-vscode-toolbar-hoverBackground transition-colors ${className}`} |
| 75 | + data-testid="rename-button"> |
| 76 | + <span className="codicon codicon-edit text-sm text-vscode-descriptionForeground" /> |
| 77 | + </button> |
| 78 | + </StandardTooltip> |
| 79 | + ) |
| 80 | +} |
0 commit comments