Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

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

Yudhi Armyndharisce74a124c730
diff --git a/dashboard/src/components/chats/ChatSidebar.tsx b/dashboard/src/components/chats/ChatSidebar.tsx
index d6f7e7997..73fb93621 100644
--- a/dashboard/src/components/chats/ChatSidebar.tsx
+++ b/dashboard/src/components/chats/ChatSidebar.tsx
@@ -1,4 +1,4 @@
-import { useState, type ReactNode } from 'react';
+import { useRef, useState, type ReactNode } from 'react';
import { useTranslation } from 'react-i18next';
import {
AlertCircle,
@@ -102,6 +102,7 @@ function ChatSidebar({
// Posting a status needs an operator key; a read-only key would only reach a 403.
const { canWrite, scoped } = useRole();
const [showMessageSearch, setShowMessageSearch] = useState(false);
+ const searchRef = useRef<HTMLInputElement | null>(null);
const formatLastMessageSnippet = (chat: Chat) => chat.lastMessage || '';
@@ -242,6 +243,7 @@ function ChatSidebar({
<div className="chat-search-input">
<Search size={18} />
<input
+ ref={searchRef}
type="text"
placeholder={t('chats.searchPlaceholder')}
value={searchQuery}
@@ -251,7 +253,11 @@ function ChatSidebar({
<button
type="button"
className="search-clear"
- onClick={() => onSearchQueryChange('')}
+ onClick={() => {
+ onSearchQueryChange('');
+ // 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 4a36c955c..352a84a44 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -663,6 +663,21 @@ test('the New chat search clear button returns focus to the field it cleared', a
assert.equal(document.activeElement === field, true, `focus is on "${focusedName()}", not on the field`);
});
+test('the chat list search clear button returns focus to the field it cleared', async () => {
+ const { screen, fireEvent } = rtl;
+ const { container } = renderPage();
+ await screen.findByText('Alice');
+ const field = container.querySelector('.chat-search-input input') as HTMLInputElement;
+ fireEvent.change(field, { target: { value: 'Ali' } });
+ const clear = screen.getByRole('button', { name: 'Clear search' });
+ 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`);
+});
+
test('an unnamed chat is titled by its number, formatted and isolated left to right, in the header and the panel', async () => {
const { screen, fireEvent, waitFor, within } = rtl;
aliceNow.name = '';