feat: add search keyboard shortcut based on the current platform (#3267)

This commit is contained in:
Humberto Rodríguez A. 2024-09-26 16:26:54 +02:00 committed by GitHub
parent aeb5525bb0
commit 4381656a6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,30 @@
import { useEffect } from 'react'
import { getOS } from '@/utils/genericHelper'
const isMac = getOS() === 'macos'
const useSearchShorcut = (inputRef) => {
useEffect(() => {
const component = inputRef.current
const handleKeyDown = (event) => {
if ((isMac && event.metaKey && event.key === 'f') || (!isMac && event.ctrlKey && event.key === 'f')) {
event.preventDefault()
component.focus()
}
}
const handleInputEscape = (event) => {
if (event.key === 'Escape') component.blur()
}
inputRef.current.addEventListener('keydown', handleInputEscape)
document.addEventListener('keydown', handleKeyDown)
return () => {
component.addEventListener('keydown', handleInputEscape)
document.removeEventListener('keydown', handleKeyDown)
}
})
}
export default useSearchShorcut

View File

@ -1,4 +1,5 @@
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { useRef } from 'react'
// material-ui // material-ui
import { IconButton, Box, OutlinedInput, Toolbar, Typography } from '@mui/material' import { IconButton, Box, OutlinedInput, Toolbar, Typography } from '@mui/material'
@ -8,6 +9,8 @@ import { StyledFab } from '@/ui-component/button/StyledFab'
// icons // icons
import { IconSearch, IconArrowLeft, IconEdit } from '@tabler/icons-react' import { IconSearch, IconArrowLeft, IconEdit } from '@tabler/icons-react'
import useSearchShorcut from '@/hooks/useSearchShortcut'
const ViewHeader = ({ const ViewHeader = ({
children, children,
filters = null, filters = null,
@ -22,6 +25,8 @@ const ViewHeader = ({
onEdit onEdit
}) => { }) => {
const theme = useTheme() const theme = useTheme()
const searchInputRef = useRef()
useSearchShorcut(searchInputRef)
return ( return (
<Box sx={{ flexGrow: 1, py: 1.25, width: '100%' }}> <Box sx={{ flexGrow: 1, py: 1.25, width: '100%' }}>
@ -85,6 +90,7 @@ const ViewHeader = ({
<Box sx={{ height: 40, display: 'flex', alignItems: 'center', gap: 1 }}> <Box sx={{ height: 40, display: 'flex', alignItems: 'center', gap: 1 }}>
{search && ( {search && (
<OutlinedInput <OutlinedInput
inputRef={searchInputRef}
size='small' size='small'
sx={{ sx={{
width: '280px', width: '280px',