Skip to content

Commit

Permalink
feat(markup search): updated SearchPopup
Browse files Browse the repository at this point in the history
  • Loading branch information
makhnatkin committed Jul 25, 2024
1 parent 91bdf5d commit 919e98f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/markup/codemirror/search-plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const SearchPanelPlugin = ViewPlugin.fromClass(
.facet(ReactRendererFacet)
.createItem('cm-search', () =>
React.createElement(PortalWithPopup, {
open: true,
anchor: this.anchor,
onChange: this.handleChange,
onClose: this.handleClose,
Expand Down
39 changes: 13 additions & 26 deletions src/markup/codemirror/search-plugin/view/SearchPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import {
Icon,
PopoverAnchorRef,
Popup,
Portal,
TextInput,
TextInputProps,
sp,
} from '@gravity-ui/uikit';

import {cn} from '../../../../classname';
import {i18n} from '../../../../i18n/search';
import {Key} from '../../../../shortcuts';
import {combinedKeyHandler} from '../../../../utils/handlers';
import {enterKeyHandler} from '../../../../utils/handlers';

import './SearchPopup.scss';

Expand Down Expand Up @@ -100,10 +98,7 @@ export const SearchCard: React.FC<SearchCardProps> = ({
setInputFocus();
};

const handleSearchKeyPress: TextInputProps['onKeyPress'] = combinedKeyHandler({
[Key.Enter]: handleNext,
[Key.Esc]: handleClose,
});
const handleSearchKeyPress: TextInputProps['onKeyPress'] = enterKeyHandler(handleNext);

return (
<Card className={b()}>
Expand Down Expand Up @@ -157,14 +152,19 @@ export interface SearchPopupProps {
open: boolean;
}

export const SearchPopup: React.FC<SearchPopupProps> = ({open, anchor, ...props}) => {
export const SearchPopup: React.FC<SearchPopupProps> = ({open, anchor, onClose, ...props}) => {
const anchorRef = useRef<HTMLElement>(anchor);

return (
<>
{anchorRef && (
<Popup open={open} anchorRef={anchorRef as PopoverAnchorRef} placement="bottom-end">
<SearchCard {...props} />
<Popup
onEscapeKeyDown={onClose}
open={open}
anchorRef={anchorRef as PopoverAnchorRef}
placement="bottom-end"
>
<SearchCard onClose={onClose} {...props} />
</Popup>
)}
</>
Expand All @@ -173,24 +173,11 @@ export const SearchPopup: React.FC<SearchPopupProps> = ({open, anchor, ...props}

SearchPopup.displayName = 'SearchPopup';

interface PopupWithRefProps extends Omit<SearchPopupProps, 'open' | 'anchor'> {
interface PopupWithRefProps extends Omit<SearchPopupProps, 'anchor'> {
anchor: HTMLElement | null;
}
export const PortalWithPopup: React.FC<PopupWithRefProps> = ({anchor, onClose, ...props}) => {
const [open, setOpen] = useState(true);

const handleClose = () => {
setOpen(false);
onClose();
};

export const PortalWithPopup: React.FC<PopupWithRefProps> = ({anchor, open, onClose, ...props}) => {
return (
<>
{anchor && (
<Portal container={anchor}>
<SearchPopup open={open} onClose={handleClose} anchor={anchor} {...props} />
</Portal>
)}
</>
<>{anchor && <SearchPopup open={open} onClose={onClose} anchor={anchor} {...props} />}</>
);
};

0 comments on commit 919e98f

Please sign in to comment.