Skip to content

Commit

Permalink
Add focuslock (#45)
Browse files Browse the repository at this point in the history
* Add focuslock

Also trying to execute the callback on enter key, but for unknown reason
it's not working :\

* Fix enter callback

* Improve comment

* Close form on enter keypress

---------

Co-authored-by: selankon <[email protected]>
  • Loading branch information
elboletaire and selankon committed Jul 8, 2024
1 parent 192ad59 commit a1220a3
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions src/components/Layout/Inputs.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Button,
ButtonProps,
FocusLock,
HStack,
IconButton,
Input,
Expand All @@ -12,37 +13,50 @@ import {
PopoverContent,
PopoverTrigger,
} from '@chakra-ui/react'
import React, { ChangeEvent, useState } from 'react'
import { ChangeEvent, KeyboardEvent, useState } from 'react'
import { Trans } from 'react-i18next'
import { BiSearchAlt } from 'react-icons/bi'
import { debounce } from '~utils/debounce'

export const PopoverInputSearch = ({ input, button }: { input?: InputSearchProps; button?: ButtonProps }) => {
return (
<Popover>
{({ isOpen, onClose }) => (
<>
<PopoverTrigger>
<IconButton aria-label='TODO' icon={<BiSearchAlt />} />
</PopoverTrigger>
<PopoverContent>
<PopoverBody>
<HStack>
<InputSearch {...input} />
<Button
{...button}
onClick={(e) => {
if (button?.onClick) button.onClick(e)
onClose()
}}
>
<Trans i18nKey={'filter.goto'}>Go to</Trans>
</Button>
</HStack>
</PopoverBody>
</PopoverContent>
</>
)}
{({ onClose }) => {
const onClick = () => {
if (button?.onClick) {
// Suppressed because by default the ButtonProps.onClick is wants an event argument which is ignored by this CB
// @ts-ignore
button.onClick()
}
onClose()
}
return (
<>
<PopoverTrigger>
<IconButton aria-label='TODO' icon={<BiSearchAlt />} />
</PopoverTrigger>
<PopoverContent>
<PopoverBody>
<FocusLock>
<HStack>
<InputSearch
onKeyUp={(event: KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter') {
onClick()
}
}}
{...input}
/>
<Button {...button} onClick={onClick}>
<Trans i18nKey={'filter.goto'}>Go to</Trans>
</Button>
</HStack>
</FocusLock>
</PopoverBody>
</PopoverContent>
</>
)
}}
</Popover>
)
}
Expand Down

2 comments on commit a1220a3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.