import { X } from "lucide-react";
import IconButton from "./IconButton";

export default function AddItemForm({ placeholder, value, onChange, onSave, onCancel, saving, px = 'px-3' }: {
  placeholder: string; value: string; onChange: (v: string) => void
  onSave: () => void; onCancel: () => void; saving: boolean; px?: string
}) {
  return (
    <div className={`flex items-center gap-2 ${px} py-2.5`}>
      <input
        autoFocus placeholder={placeholder} value={value}
        onChange={e => onChange(e.target.value)}
        onKeyDown={e => { if (e.key === 'Enter') onSave(); if (e.key === 'Escape') onCancel() }}
        className="flex-1 text-sm border border-blue-200 rounded-lg px-3 py-1 focus:outline-none focus:ring-2 focus:ring-blue-400"
      />
      <button onClick={onSave} disabled={saving}
        className="px-3 py-1 text-xs font-semibold text-white rounded-lg bg-blue-600 hover:bg-blue-700 disabled:opacity-60">
        {saving ? '…' : 'Save'}
      </button>
      <IconButton onClick={onCancel}><X className="w-3.5 h-3.5" /></IconButton>
    </div>
  )
}