All files / src/components/field/InlineEdit InlineEdit.tsx

44.44% Statements 8/18
52.63% Branches 10/19
16.66% Functions 1/6
47.05% Lines 8/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                        3x                 62x 62x   62x         62x         62x 62x                                                             62x                                                                                            
import { Check, Edit2, Loader2 } from "lucide-react";
import React, { useState } from "react";
 
export interface InlineEditProps {
  value: string;
  placeholder?: string;
  multiline?: boolean;
  /** Rows for the multiline editor (default 3). */
  rows?: number;
  /** Render the value and editor in a monospace block (e.g. system prompts). */
  mono?: boolean;
  onSave: (v: string) => void;
  saving?: boolean;
}
 
/**
 * Click-to-edit text field: renders the value with a pencil affordance and
 * swaps to an input/textarea with Save/Cancel when clicked. Only calls
 * onSave when the trimmed draft actually differs from the current value.
 */
const InlineEdit: React.FC<InlineEditProps> = ({
  value,
  placeholder,
  multiline,
  rows = 3,
  mono,
  onSave,
  saving,
}) => {
  const [editing, setEditing] = useState(false);
  const [draft, setDraft] = useState(value);
 
  const commit = () => {
    setEditing(false);
    if (draft.trim() !== value) onSave(draft.trim());
  };
 
  const cancel = () => {
    setEditing(false);
    setDraft(value);
  };
 
  Eif (!editing) {
    return (
      <button
        type="button"
        onClick={() => {
          setDraft(value);
          setEditing(true);
        }}
        className="group flex items-start gap-1.5 text-left w-full"
      >
        {mono && value ? (
          <pre className="text-sm text-gray-700 dark:text-gray-300 whitespace-pre-wrap font-mono leading-relaxed bg-gray-50 dark:bg-gray-700/50 rounded-lg px-4 py-3 max-h-40 overflow-y-auto flex-1 min-w-0">
            {value}
          </pre>
        ) : (
          <span
            className={
              value
                ? "text-sm text-gray-800 dark:text-gray-200"
                : "text-sm text-gray-400 italic"
            }
          >
            {value || placeholder}
          </span>
        )}
        <Edit2 className="size-3 text-gray-300 dark:text-gray-600 group-hover:text-gray-400 dark:group-hover:text-gray-400 mt-0.5 shrink-0 transition-colors" />
      </button>
    );
  }
 
  const inputClass = `w-full text-sm px-2 py-1 rounded-md border border-violet-300 dark:border-violet-600 bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200 focus:outline-none focus:ring-1 focus:ring-violet-400${mono ? " font-mono" : ""}`;
 
  return (
    <div className="space-y-1.5 w-full">
      {multiline ? (
        <textarea
          autoFocus
          rows={rows}
          value={draft}
          onChange={(e) => setDraft(e.target.value)}
          className={inputClass + " resize-none"}
        />
      ) : (
        <input
          autoFocus
          type="text"
          value={draft}
          onChange={(e) => setDraft(e.target.value)}
          className={inputClass}
        />
      )}
      <div className="flex gap-1.5">
        <button
          type="button"
          onClick={commit}
          disabled={saving}
          className="flex items-center gap-1 px-2.5 py-1 rounded text-xs font-medium bg-gray-900 dark:bg-white text-white dark:text-gray-900 hover:opacity-80 disabled:opacity-50 transition-opacity"
        >
          {saving ? (
            <Loader2 className="size-3 animate-spin" />
          ) : (
            <Check className="size-3" />
          )}
          Save
        </button>
        <button
          type="button"
          onClick={cancel}
          className="px-2.5 py-1 rounded text-xs text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
        >
          Cancel
        </button>
      </div>
    </div>
  );
};
 
export default InlineEdit;