Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep focus in the New chat search after clearing it

Yudhi Armyndharisaec70999577c
diff --git a/dashboard/src/components/chats/NewChatPanel.tsx b/dashboard/src/components/chats/NewChatPanel.tsx
index 6f39d7748..8703e5a34 100644
--- a/dashboard/src/components/chats/NewChatPanel.tsx
+++ b/dashboard/src/components/chats/NewChatPanel.tsx
@@ -85,6 +85,7 @@ function NewChatPanel({ sessionId, onOpenChat, findListedChat, onClose }: NewCha
// unmounts it). Its result then belongs to nothing on screen and is dropped. Setup sets the flag again:
// StrictMode replays setup after a cleanup on a panel that stays mounted.
const openRef = useRef(true);
+ const searchRef = useRef<HTMLInputElement | null>(null);
useEffect(() => {
openRef.current = true;
return () => {
@@ -258,6 +259,7 @@ function NewChatPanel({ sessionId, onOpenChat, findListedChat, onClose }: NewCha
<div className="chat-search-input new-chat-search">
<Search size={18} />
<input
+ ref={searchRef}
type="text"
placeholder={isGroupMode ? t('chats.newGroup.participantsPlaceholder') : t('chats.newChat.searchPlaceholder')}
aria-label={isGroupMode ? t('chats.newGroup.participantsPlaceholder') : t('chats.newChat.searchPlaceholder')}
@@ -275,7 +277,11 @@ function NewChatPanel({ sessionId, onOpenChat, findListedChat, onClose }: NewCha
<button
type="button"
className="search-clear"
- onClick={() => setQuery('')}
+ onClick={() => {
+ setQuery('');
+ // The button leaves the page with the text, and focus would go with it.
+ searchRef.current?.focus();
+ }}
aria-label={t('chats.clearSearch')}
title={t('chats.clearSearch')}
>
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 1c22b3356..5dea41431 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -453,6 +453,23 @@ for (const reactStrictMode of [false, true]) {
});
}
+test('the New chat search clear button returns focus to the field it cleared', async () => {
+ const { screen, fireEvent } = rtl;
+ renderPage();
+ await screen.findByText('Alice');
+ fireEvent.click(screen.getByRole('button', { name: 'New chat' }));
+ const field = screen.getByLabelText('Search name or number with country code') as HTMLInputElement;
+ fireEvent.change(field, { target: { value: 'Bo' } });
+ const clear = screen.getByRole('button', { name: 'Clear search' });
+ // A keyboard user reaches the button with Tab and presses Enter on it; the button then leaves the page.
+ clear.focus();
+ fireEvent.click(clear);
+
+ assert.equal(field.value, '');
+ assert.ok(!screen.queryByRole('button', { name: 'Clear search' }), 'the clear button stayed');
+ assert.equal(document.activeElement === field, true, `focus is on "${focusedName()}", not on the field`);
+});
+
// ── The number check answers a phone id, while a contact that moved to privacy-id addressing keeps its conversation
// under its @lid: starting a chat with that number has to find it. ──