import { useState, useRef } from "react"; import { LuPlus, LuArrowLeft } from "react-icons/lu"; import { InputFieldWithLabel } from "@/components/InputField"; import { Button } from "@/components/Button"; interface AddDeviceFormProps { onAddDevice: (name: string, macAddress: string) => void; setShowAddForm: (show: boolean) => void; errorMessage: string | null; setErrorMessage: (errorMessage: string | null) => void; } export default function AddDeviceForm({ setShowAddForm, onAddDevice, errorMessage, setErrorMessage, }: AddDeviceFormProps) { const [isDeviceNameValid, setIsDeviceNameValid] = useState(false); const [isMacAddressValid, setIsMacAddressValid] = useState(false); const nameInputRef = useRef(null); const macInputRef = useRef(null); return (
{ setIsDeviceNameValid(e.target.validity.valid); setErrorMessage(null); }} maxLength={30} /> e.stopPropagation()} required pattern="^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$" error={errorMessage} onChange={e => { setIsMacAddressValid(e.target.validity.valid); setErrorMessage(null); }} minLength={17} maxLength={17} onKeyDown={e => { if (isMacAddressValid || isDeviceNameValid) { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); const deviceName = nameInputRef.current?.value || ""; const macAddress = macInputRef.current?.value || ""; onAddDevice(deviceName, macAddress); } else if (e.key === "Escape") { e.preventDefault(); setShowAddForm(false); } } }} />
); }