Fix issue #144: Set cursor focus to IP address field instead of username field

When adding or editing a host, the cursor now automatically focuses on the IP address field, which is the first field in the connection details section. This improves UX by allowing users to immediately start typing the most logical first piece of information.

Changes:
- Added useRef for IP address input field
- Added useEffect to focus IP field when component mounts or editingHost changes
- Uses setTimeout to ensure DOM is ready before focusing
- Works for both adding new hosts and editing existing hosts
This commit is contained in:
ZacharyZcR
2025-09-07 15:21:22 +08:00
parent 71c54fccc5
commit f9891b8099
@@ -61,6 +61,9 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [authTab, setAuthTab] = useState<'password' | 'key' | 'credential'>('password'); const [authTab, setAuthTab] = useState<'password' | 'key' | 'credential'>('password');
// Ref for the IP address input to manage focus
const ipInputRef = useRef<HTMLInputElement>(null);
useEffect(() => { useEffect(() => {
const fetchData = async () => { const fetchData = async () => {
@@ -251,6 +254,18 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
} }
}, [editingHost, form]); }, [editingHost, form]);
// Focus the IP address field when the component mounts or when editingHost changes
useEffect(() => {
// Use setTimeout to ensure the input is rendered before focusing
const focusTimer = setTimeout(() => {
if (ipInputRef.current) {
ipInputRef.current.focus();
}
}, 100);
return () => clearTimeout(focusTimer);
}, [editingHost]);
const onSubmit = async (data: any) => { const onSubmit = async (data: any) => {
try { try {
const formData = data as FormData; const formData = data as FormData;
@@ -427,7 +442,11 @@ export function HostManagerHostEditor({editingHost, onFormSubmit}: SSHManagerHos
<FormItem className="col-span-5"> <FormItem className="col-span-5">
<FormLabel>{t('hosts.ipAddress')}</FormLabel> <FormLabel>{t('hosts.ipAddress')}</FormLabel>
<FormControl> <FormControl>
<Input placeholder={t('placeholders.ipAddress')} {...field} /> <Input
ref={ipInputRef}
placeholder={t('placeholders.ipAddress')}
{...field}
/>
</FormControl> </FormControl>
</FormItem> </FormItem>
)} )}