web/roomlist: add button and keyboard shortcut to clear search filter

This commit is contained in:
Tulir Asokan 2024-11-02 19:50:54 +02:00
parent ed45998248
commit a31c68fc5d
2 changed files with 49 additions and 14 deletions

View file

@ -15,13 +15,28 @@ div.room-list {
flex: 1; flex: 1;
} }
input.room-search { div.room-search-wrapper {
width: 100%; width: 100%;
padding: 1rem; display: flex;
box-sizing: border-box; align-items: center;
border: none; height: 3rem;
background-color: var(--room-list-search-background-overlay); background-color: var(--room-list-search-background-overlay);
outline: none;
> input {
padding: 0 0 0 1rem;
height: 3rem;
box-sizing: border-box;
border: none;
outline: none;
background-color: transparent;
flex: 1;
}
> button {
height: 3rem;
width: 3rem;
border-radius: 0;
}
} }
div.room-entry { div.room-entry {

View file

@ -19,7 +19,10 @@ import { useEventAsState } from "@/util/eventdispatcher.ts"
import reverseMap from "@/util/reversemap.ts" import reverseMap from "@/util/reversemap.ts"
import toSearchableString from "@/util/searchablestring.ts" import toSearchableString from "@/util/searchablestring.ts"
import ClientContext from "../ClientContext.ts" import ClientContext from "../ClientContext.ts"
import { keyToString } from "../keybindings.ts"
import Entry from "./Entry.tsx" import Entry from "./Entry.tsx"
import CloseIcon from "@/icons/close.svg?react"
import SearchIcon from "@/icons/search.svg?react"
import "./RoomList.css" import "./RoomList.css"
interface RoomListProps { interface RoomListProps {
@ -38,17 +41,34 @@ const RoomList = ({ activeRoomID }: RoomListProps) => {
client.store.currentRoomListFilter = toSearchableString(evt.target.value) client.store.currentRoomListFilter = toSearchableString(evt.target.value)
setRealRoomFilter(client.store.currentRoomListFilter) setRealRoomFilter(client.store.currentRoomListFilter)
}, [client]) }, [client])
const clearQuery = useCallback(() => {
setRoomFilter("")
client.store.currentRoomListFilter = ""
setRealRoomFilter("")
roomFilterRef.current?.focus()
}, [client])
const onKeyDown = useCallback((evt: React.KeyboardEvent<HTMLInputElement>) => {
if (keyToString(evt) === "Escape") {
clearQuery()
}
}, [clearQuery])
return <div className="room-list-wrapper"> return <div className="room-list-wrapper">
<input <div className="room-search-wrapper">
value={roomFilter} <input
onChange={updateRoomFilter} value={roomFilter}
className="room-search" onChange={updateRoomFilter}
type="text" onKeyDown={onKeyDown}
placeholder="Search rooms" className="room-search"
ref={roomFilterRef} type="text"
id="room-search" placeholder="Search rooms"
/> ref={roomFilterRef}
id="room-search"
/>
<button onClick={clearQuery} disabled={roomFilter === ""}>
{roomFilter !== "" ? <CloseIcon/> : <SearchIcon/>}
</button>
</div>
<div className="room-list"> <div className="room-list">
{reverseMap(roomList, room => {reverseMap(roomList, room =>
<Entry <Entry