Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Breakdown modal search shortcut (#4559)

RobertJoonasb1152fc3512c
diff --git a/assets/js/dashboard/components/search-input.tsx b/assets/js/dashboard/components/search-input.tsx
index 8618a773a1..06872f9755 100644
--- a/assets/js/dashboard/components/search-input.tsx
+++ b/assets/js/dashboard/components/search-input.tsx
@@ -1,6 +1,6 @@
/** @format */
-import React, { ChangeEventHandler, useCallback, useRef } from 'react'
+import React, { ChangeEventHandler, useCallback, useState, useRef } from 'react'
import { isModifierPressed, Keybind } from '../keybinding'
import { useDebounce } from '../custom-hooks'
import classNames from 'classnames'
@@ -13,6 +13,7 @@ export const SearchInput = ({
onSearch: (value: string) => void
}) => {
const searchBoxRef = useRef<HTMLInputElement>(null)
+ const [isFocused, setIsFocused] = useState(false)
const onSearchInputChange: ChangeEventHandler<HTMLInputElement> = useCallback(
(event) => {
@@ -22,13 +23,25 @@ export const SearchInput = ({
)
const debouncedOnSearchInputChange = useDebounce(onSearchInputChange)
- const blurSearchBox = useCallback((event: KeyboardEvent) => {
- const searchBox = searchBoxRef.current
- if (searchBox?.contains(event.target as HTMLElement)) {
- searchBox.blur()
- event.stopPropagation()
- }
- }, [])
+ const blurSearchBox = useCallback(
+ (event: KeyboardEvent) => {
+ if (isFocused) {
+ searchBoxRef.current?.blur()
+ event.stopPropagation()
+ }
+ },
+ [isFocused]
+ )
+
+ const focusSearchBox = useCallback(
+ (event: KeyboardEvent) => {
+ if (!isFocused) {
+ searchBoxRef.current?.focus()
+ event.stopPropagation()
+ }
+ },
+ [isFocused]
+ )
return (
<>
@@ -38,10 +51,18 @@ export const SearchInput = ({
handler={blurSearchBox}
shouldIgnoreWhen={[isModifierPressed]}
/>
+ <Keybind
+ keyboardKey="/"
+ type="keyup"
+ handler={focusSearchBox}
+ shouldIgnoreWhen={[isModifierPressed]}
+ />
<input
+ onBlur={() => setIsFocused(false)}
+ onFocus={() => setIsFocused(true)}
ref={searchBoxRef}
type="text"
- placeholder="Search"
+ placeholder={isFocused ? 'Search' : 'Press / to search'}
className={classNames(
'shadow-sm dark:bg-gray-900 dark:text-gray-100 focus:ring-indigo-500 focus:border-indigo-500 block sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800 w-48',
className
diff --git a/assets/js/dashboard/stats/modals/breakdown-modal.tsx b/assets/js/dashboard/stats/modals/breakdown-modal.tsx
index 24b686a2f7..1d56df1d82 100644
--- a/assets/js/dashboard/stats/modals/breakdown-modal.tsx
+++ b/assets/js/dashboard/stats/modals/breakdown-modal.tsx
@@ -170,7 +170,7 @@ export default function BreakdownModal<TListItem extends { name: string }>({
<BreakdownTable<TListItem>
title={reportInfo.title}
{...apiState}
- onSearch={setSearch}
+ onSearch={searchEnabled ? setSearch : undefined}
columns={columns}
/>
)