Skip to content

Commit fa0d717

Browse files
committed
feat: add support for command + enter in filter select picker
1 parent 98df963 commit fa0d717

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

src/Common/Hooks/UseRegisterShortcut/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const KEYBOARD_KEYS_MAP = {
2626
R: 'R',
2727
K: 'K',
2828
Escape: 'Escape',
29-
Enter: 'Enter',
29+
Enter: '',
3030
} as const
3131

3232
export type SupportedKeyboardKeysType = keyof typeof KEYBOARD_KEYS_MAP

src/Shared/Components/SelectPicker/FilterSelectPicker.tsx

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { useEffect, useMemo, useState } from 'react'
66
import { ReactComponent as ICFilter } from '@Icons/ic-filter.svg'
77
import { ReactComponent as ICFilterApplied } from '@Icons/ic-filter-applied.svg'
88
import { ComponentSizeType } from '@Shared/constants'
9+
import { useRegisterShortcut, UseRegisterShortcutProvider } from '@Common/Hooks'
10+
import { IS_PLATFORM_MAC_OS } from '@Common/Constants'
11+
import { SupportedKeyboardKeysType } from '@Common/Hooks/UseRegisterShortcut/types'
912
import SelectPicker from './SelectPicker.component'
1013
import { FilterSelectPickerProps, SelectPickerOptionType, SelectPickerProps } from './type'
1114
import { Button } from '../Button'
1215

16+
const APPLY_FILTER_SHORTCUT_KEYS: SupportedKeyboardKeysType[] = [IS_PLATFORM_MAC_OS ? 'Meta' : 'Control', 'Enter']
17+
1318
const FilterSelectPicker = ({
1419
appliedFilterOptions,
1520
handleApplyFilter,
@@ -23,6 +28,8 @@ const FilterSelectPicker = ({
2328
structuredClone(appliedFilterOptions ?? []),
2429
)
2530

31+
const { registerShortcut, unregisterShortcut } = useRegisterShortcut()
32+
2633
const appliedFiltersCount = appliedFilterOptions?.length ?? 0
2734

2835
useEffect(() => {
@@ -52,24 +59,39 @@ const FilterSelectPicker = ({
5259
setSelectedOptions(structuredClone(appliedFilterOptions ?? []))
5360
}
5461

55-
const renderApplyButton = () => {
56-
const handleApplyClick = () => {
57-
handleApplyFilter(selectedOptions)
58-
closeMenu()
62+
const handleApplyClick = () => {
63+
handleApplyFilter(selectedOptions)
64+
closeMenu()
65+
}
66+
67+
const renderApplyButton = () => (
68+
<div className="p-8 dc__border-top-n1">
69+
<Button
70+
text="Apply"
71+
dataTestId="filter-select-picker-apply"
72+
onClick={handleApplyClick}
73+
size={ComponentSizeType.small}
74+
fullWidth
75+
showTooltip
76+
tooltipProps={{
77+
shortcutKeyCombo: {
78+
text: 'Apply filter',
79+
combo: APPLY_FILTER_SHORTCUT_KEYS,
80+
},
81+
}}
82+
/>
83+
</div>
84+
)
85+
86+
useEffect(() => {
87+
if (isMenuOpen) {
88+
registerShortcut({ keys: APPLY_FILTER_SHORTCUT_KEYS, callback: handleApplyClick })
5989
}
6090

61-
return (
62-
<div className="p-8 dc__border-top-n1">
63-
<Button
64-
text="Apply"
65-
dataTestId="filter-select-picker-apply"
66-
onClick={handleApplyClick}
67-
size={ComponentSizeType.small}
68-
fullWidth
69-
/>
70-
</div>
71-
)
72-
}
91+
return () => {
92+
unregisterShortcut(APPLY_FILTER_SHORTCUT_KEYS)
93+
}
94+
}, [handleApplyClick, isMenuOpen])
7395

7496
return (
7597
<div className="dc__mxw-250">
@@ -96,4 +118,10 @@ const FilterSelectPicker = ({
96118
)
97119
}
98120

99-
export default FilterSelectPicker
121+
const FilterSelectPickerWrapper = (props: FilterSelectPickerProps) => (
122+
<UseRegisterShortcutProvider ignoreTags={[]}>
123+
<FilterSelectPicker {...props} />
124+
</UseRegisterShortcutProvider>
125+
)
126+
127+
export default FilterSelectPickerWrapper

src/Shared/Components/SelectPicker/SelectPicker.component.tsx

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

17-
import { GroupHeadingProps, MultiValueProps, OptionProps, ValueContainerProps, MenuPlacement } from 'react-select'
17+
import {
18+
GroupHeadingProps,
19+
MultiValueProps,
20+
OptionProps,
21+
ValueContainerProps,
22+
MenuPlacement,
23+
Props as ReactSelectProps,
24+
} from 'react-select'
1825
import CreatableSelect from 'react-select/creatable'
1926
import { ReactElement, useCallback, useMemo } from 'react'
2027

@@ -308,6 +315,13 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
308315
}
309316
}
310317

318+
const handleKeyDown: ReactSelectProps['onKeyDown'] = (e) => {
319+
// Prevent the option from being selected if meta or control key is pressed
320+
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
321+
e.preventDefault()
322+
}
323+
}
324+
311325
const commonProps = useMemo(
312326
() => ({
313327
name: name || inputId,
@@ -392,6 +406,7 @@ const SelectPicker = <OptionValue, IsMulti extends boolean>({
392406
onInputChange={onInputChange}
393407
icon={icon}
394408
showSelectedOptionIcon={shouldShowSelectedOptionIcon}
409+
onKeyDown={handleKeyDown}
395410
/>
396411
</div>
397412
</ConditionalWrap>

0 commit comments

Comments
 (0)