Skip to content

Commit

Permalink
feat(common): search input toggle key event (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyechan99 committed May 15, 2024
1 parent cf9911b commit 1944924
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/components/Portal/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export interface PortalProps extends React.ComponentProps<'div'> {

const Portal = ({ children, ref, width, onKeyDown, disabledAutoFocus, ...props }: PortalProps) => {
const portalRef = React.useRef<HTMLDivElement>(null);
const { showModal, toggleElement, reorgPos } = usePortal({ portalRef });
const { showModal, setShowModal, toggleElement, reorgPos } = usePortal({ portalRef });
const [items, setItems] = useState<HTMLElement[]>([]);

const ACTIONS: Record<string, (e: React.KeyboardEvent<HTMLInputElement>) => void> = {
ArrowDown: () => focus('next'),
ArrowUp: () => focus('prev'),
Tab: () => focus('next'),
Enter: () => select(),
Escape: () => setShowModal(false),
};

const handleOnKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/Search/SearchInputToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as React from 'react';

import { Input } from '@/components/Input';
import { PortalContext } from '@/components/Portal/PortalContext';
import useContext from '@/hooks/useContext';
import { composeEventHandlers } from '@/libs/event';
import { composeRefs } from '@/libs/ref';

import { Input } from '../..';

type ComponentPropsWithoutRef<E extends React.ElementType> = React.ComponentPropsWithoutRef<E>;

/**
Expand All @@ -29,9 +28,9 @@ export const SearchInputToggle = React.forwardRef<HTMLInputElement, ComponentPro
setShowModal(true);
};

const closeModal = () => {
const closeModal = React.useCallback(() => {
setShowModal(false);
};
}, []);

const ACTIONS: Record<string, (e: React.KeyboardEvent<HTMLInputElement>) => void> = {
ArrowDown: () => focus('next'),
Expand All @@ -42,6 +41,7 @@ export const SearchInputToggle = React.forwardRef<HTMLInputElement, ComponentPro
};

const handleOnKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
console.log(e);
const handler = ACTIONS[e.key];

if (handler) {
Expand Down

0 comments on commit 1944924

Please sign in to comment.