Skip to content

Commit ebb2ee1

Browse files
authored
Merge pull request #2854 from devtron-labs/feat/focus-search
feat: hook / shortcut to focus search bar
2 parents 6ab49cd + e77a976 commit ebb2ee1

File tree

8 files changed

+10
-77
lines changed

8 files changed

+10
-77
lines changed

src/components/ApplicationGroup/List/EnvironmentsList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const EnvironmentsList = ({ isSuperAdmin }: AppGroupAdminType) => {
118118
autoFocus: true,
119119
}}
120120
dataTestId="environment-search-box"
121+
keyboardShortcut="/"
121122
/>
122123
)
123124

src/components/ClusterNodes/ClusterList/ClusterListView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ const ClusterListView = (props: ClusterViewType) => {
108108
autoFocus: true,
109109
disabled: initialLoading,
110110
}}
111+
keyboardShortcut="/"
111112
/>
112113
{ClusterFilters && (
113114
<ClusterFilters clusterFilter={clusterFilter} setClusterFilter={setClusterFilter} />

src/components/Jobs/JobList/JobListFilters.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const JobListFilters = ({
7272
autoFocus: true,
7373
}}
7474
dataTestId="Search-by-job-name"
75+
keyboardShortcut="/"
7576
/>
7677
<div className="flexbox dc__gap-8 dc__align-items-center dc__zi-4">
7778
<FilterSelectPicker

src/components/ResourceBrowser/ResourceBrowser.scss

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,6 @@
7676
}
7777
}
7878
}
79-
80-
.resource-filter-options-container {
81-
&__search-box {
82-
width: 250px;
83-
84-
.resource-search-shortcut-key {
85-
right: 8px;
86-
top: 50%;
87-
transform: translateY(-50%);
88-
}
89-
}
90-
}
9179
}
9280
}
9381

src/components/ResourceBrowser/ResourceList/NodeListSearchFilter.tsx

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { KeyboardEvent, useEffect, useMemo, useRef } from 'react'
17+
import { useMemo } from 'react'
1818
import { useHistory, useLocation, useParams } from 'react-router-dom'
1919
import { parse as parseQueryString, ParsedQuery, stringify as stringifyQueryString } from 'query-string'
2020

21-
import {
22-
FilterChips,
23-
GroupedFilterSelectPicker,
24-
SearchBar,
25-
useAsync,
26-
useRegisterShortcut,
27-
} from '@devtron-labs/devtron-fe-common-lib'
21+
import { FilterChips, GroupedFilterSelectPicker, SearchBar, useAsync } from '@devtron-labs/devtron-fe-common-lib'
2822

2923
import { getClusterCapacity } from '@Components/ClusterNodes/clusterNodes.service'
3024

@@ -53,25 +47,6 @@ const NodeListSearchFilter = ({
5347
const { search } = useLocation()
5448
const { push } = useHistory()
5549

56-
// REFS
57-
const searchInputRef = useRef<HTMLInputElement | null>(null)
58-
59-
const { registerShortcut, unregisterShortcut } = useRegisterShortcut()
60-
61-
useEffect(() => {
62-
const handleSearchFocus = () => {
63-
searchInputRef.current?.focus()
64-
}
65-
66-
if (registerShortcut) {
67-
registerShortcut({ keys: ['/'], callback: handleSearchFocus })
68-
}
69-
70-
return () => {
71-
unregisterShortcut(['/'])
72-
}
73-
}, [])
74-
7550
// CONSTANTS
7651
const isNodeSearchFilterApplied =
7752
searchParams[NODE_SEARCH_KEYS.LABEL] ||
@@ -120,12 +95,6 @@ const NodeListSearchFilter = ({
12095
push(`?${finalQueryString}`)
12196
}
12297

123-
const handleSearchInputKeyUp = (e: KeyboardEvent<HTMLInputElement>) => {
124-
if (e.key === 'Escape' || e.key === 'Esc') {
125-
searchInputRef.current?.blur()
126-
}
127-
}
128-
12998
const handleSearchFilterChange =
13099
(nodeSearchKey: NODE_SEARCH_KEYS | typeof NODE_K8S_VERSION_FILTER_KEY) =>
131100
(filtersToApply: NodeSearchListOptionType[]) => {
@@ -186,9 +155,7 @@ const NodeListSearchFilter = ({
186155
handleSearchChange={handleSearch}
187156
keyboardShortcut="/"
188157
inputProps={{
189-
ref: searchInputRef,
190158
placeholder: 'Search Nodes',
191-
onKeyUp: handleSearchInputKeyUp,
192159
}}
193160
containerClassName="w-250"
194161
/>

src/components/ResourceBrowser/ResourceList/ResourceFilterOptions.tsx

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ComponentProps, KeyboardEvent, useEffect, useMemo, useRef, useState } from 'react'
17+
import { ComponentProps, useEffect, useMemo, useRef, useState } from 'react'
1818
import { useParams } from 'react-router-dom'
1919
import { SelectInstance } from 'react-select'
2020

@@ -44,7 +44,6 @@ import { FILE_NAMES } from '@Components/common/ExportToCsv/constants'
4444
import ExportToCsv from '@Components/common/ExportToCsv/ExportToCsv'
4545

4646
import { convertToOptionsList, importComponentFromFELibrary } from '../../common'
47-
import { ShortcutKeyBadge } from '../../common/formFields/Widgets/Widgets'
4847
import { NAMESPACE_NOT_APPLICABLE_OPTION, NAMESPACE_NOT_APPLICABLE_TEXT } from '../Constants'
4948
import { namespaceListByClusterId } from '../ResourceBrowser.service'
5049
import { ResourceFilterOptionsProps } from '../Types'
@@ -79,15 +78,13 @@ const ResourceFilterOptions = ({
7978
const { registerShortcut, unregisterShortcut } = useRegisterShortcut()
8079
const { clusterId } = useParams<K8sResourceListURLParams>()
8180
const [showFilterModal, setShowFilterModal] = useState(false)
82-
const [isInputFocused, setIsInputFocused] = useState(false)
8381
const searchInputRef = useRef<HTMLInputElement>(null)
8482
const namespaceFilterRef = useRef<SelectInstance<SelectPickerOptionType>>(null)
8583

8684
const isEventListing = selectedResource?.gvk?.Kind === Nodes.Event
8785

8886
const { gvkOptions, areGVKOptionsLoading, reloadGVKOptions, gvkOptionsError } = gvkFilterConfig || {}
8987

90-
const showShortcutKey = !isInputFocused && !searchText
9188
const isResourceRecommender = selectedResource?.gvk?.Kind === Nodes.ResourceRecommender
9289

9390
const {
@@ -110,10 +107,6 @@ const ResourceFilterOptions = ({
110107
return NAMESPACE_NOT_APPLICABLE_OPTION
111108
}, [selectedNamespace, selectedResource?.namespaced, namespaceOptions])
112109

113-
const handleInputShortcut = () => {
114-
searchInputRef.current?.focus()
115-
}
116-
117110
const handleShowFilterModal = () => {
118111
setShowFilterModal(true)
119112
}
@@ -129,23 +122,15 @@ const ResourceFilterOptions = ({
129122

130123
useEffect(() => {
131124
if (registerShortcut) {
132-
registerShortcut({ keys: ['/'], callback: handleInputShortcut })
133125
registerShortcut({ keys: ['F'], callback: handleShowFilterModal })
134126
registerShortcut({ keys: ['N'], callback: handleFocusNamespaceFilter })
135127
}
136128
return (): void => {
137129
unregisterShortcut(['F'])
138-
unregisterShortcut(['/'])
139130
unregisterShortcut(['N'])
140131
}
141132
}, [])
142133

143-
const handleFilterKeyUp = (e: KeyboardEvent): void => {
144-
if (e.key === 'Escape' || e.key === 'Esc') {
145-
searchInputRef.current?.blur()
146-
}
147-
}
148-
149134
const handleOnChangeSearchText: ComponentProps<typeof SearchBar>['handleSearchChange'] = (text) => {
150135
setSearchText(text)
151136
if (!text) {
@@ -164,10 +149,6 @@ const ResourceFilterOptions = ({
164149
)
165150
}
166151

167-
const handleInputBlur = () => setIsInputFocused(false)
168-
169-
const handleInputFocus = () => setIsInputFocused(true)
170-
171152
const handleOnEventTypeChange: SegmentedControlProps['onChange'] = ({ value }) => {
172153
updateSearchParams({ eventType: value === 'normal' ? value : null })
173154
}
@@ -231,21 +212,12 @@ const ResourceFilterOptions = ({
231212
inputProps={{
232213
placeholder: searchPlaceholder || `Search ${selectedResource?.gvk?.Kind || ''}`,
233214
disabled: isSearchInputDisabled,
234-
onBlur: handleInputBlur,
235-
onFocus: handleInputFocus,
236215
ref: searchInputRef,
237-
onKeyUp: handleFilterKeyUp,
238216
}}
239217
handleSearchChange={handleOnChangeSearchText}
240218
initialSearchText={searchText}
219+
keyboardShortcut="/"
241220
/>
242-
{showShortcutKey && (
243-
<ShortcutKeyBadge
244-
shortcutKey="/"
245-
rootClassName="resource-search-shortcut-key"
246-
onClick={handleInputShortcut}
247-
/>
248-
)}
249221
</div>
250222
</div>
251223

@@ -312,6 +284,7 @@ const ResourceFilterOptions = ({
312284
icon={<NamespaceIcon className="fcn-6" />}
313285
disabledTippyContent={NAMESPACE_NOT_APPLICABLE_TEXT}
314286
shouldMenuAlignRight
287+
keyboardShortcut="N"
315288
/>
316289
</div>
317290

src/components/app/list-new/AppListFilters.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ const AppListFilters = ({
219219
}}
220220
handleEnter={handleSearch}
221221
size={ComponentSizeType.medium}
222+
keyboardShortcut="/"
222223
/>
223224
{window._env_.FEATURE_GROUPED_APP_LIST_FILTERS_ENABLE && (
224225
<div className="dc__position-rel">

src/components/charts/ChartHeaderFilters.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ const ChartHeaderFilter = ({
214214
autoFocus: true,
215215
}}
216216
dataTestId="chart-store-search-box"
217+
keyboardShortcut='/'
217218
/>
218219
</div>
219220
<div className="pl-12 pr-12 flexbox-col flex-grow-1 dc__overflow-auto">

0 commit comments

Comments
 (0)