Skip to content

Commit 8ac2f60

Browse files
authored
Remove lag in search field (#29885)
* fix(search): remove search input lag * test(search): add search field update test
1 parent 64f0dfe commit 8ac2f60

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/components/views/right_panel/RoomSummaryCard.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ const RoomSummaryCard: React.FC<IProps> = ({
179179
onSearchChange,
180180
onSearchCancel,
181181
focusRoomSearch,
182-
searchTerm,
182+
searchTerm = "",
183183
}) => {
184184
const cli = useContext(MatrixClientContext);
185185

@@ -245,6 +245,13 @@ const RoomSummaryCard: React.FC<IProps> = ({
245245
}
246246
});
247247

248+
// The search field is controlled and onSearchChange is debounced in RoomView,
249+
// so we need to set the value of the input right away
250+
const [searchValue, setSearchValue] = useState(searchTerm);
251+
useEffect(() => {
252+
setSearchValue(searchTerm);
253+
}, [searchTerm]);
254+
248255
const alias = room.getCanonicalAlias() || room.getAltAliases()[0] || "";
249256
const roomInfo = (
250257
<header className="mx_RoomSummaryCard_container">
@@ -320,9 +327,10 @@ const RoomSummaryCard: React.FC<IProps> = ({
320327
placeholder={_t("room|search|placeholder")}
321328
name="room_message_search"
322329
onChange={(e) => {
330+
setSearchValue(e.currentTarget.value);
323331
onSearchChange(e.currentTarget.value);
324332
}}
325-
value={searchTerm}
333+
value={searchValue}
326334
className="mx_no_textinput"
327335
ref={searchInputRef}
328336
autoFocus={focusRoomSearch}

test/unit-tests/components/views/right_panel/RoomSummaryCard-test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { render, fireEvent, screen, waitFor } from "jest-matrix-react";
1111
import { EventType, MatrixEvent, Room, type MatrixClient, JoinRule } from "matrix-js-sdk/src/matrix";
1212
import { KnownMembership } from "matrix-js-sdk/src/types";
1313
import { mocked, type MockedObject } from "jest-mock";
14+
import userEvent from "@testing-library/user-event";
1415

1516
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
1617
import RoomSummaryCard from "../../../../../src/components/views/right_panel/RoomSummaryCard";
@@ -166,6 +167,20 @@ describe("<RoomSummaryCard />", () => {
166167
fireEvent.keyDown(getByPlaceholderText("Search messages…"), { key: "Escape" });
167168
expect(onSearchCancel).toHaveBeenCalled();
168169
});
170+
it("should update the search field value correctly", async () => {
171+
const user = userEvent.setup();
172+
173+
const onSearchChange = jest.fn();
174+
const { getByPlaceholderText } = getComponent({
175+
onSearchChange,
176+
});
177+
178+
const searchInput = getByPlaceholderText("Search messages…");
179+
await user.type(searchInput, "test query");
180+
181+
expect(onSearchChange).toHaveBeenCalledWith("test query");
182+
expect(searchInput).toHaveValue("test query");
183+
});
169184
});
170185

171186
it("opens room file panel on button click", () => {

0 commit comments

Comments
 (0)