Skip to content

[URH-55 ] useKeyCombination 신규 #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2024
Merged

Conversation

suhyeoonn
Copy link
Contributor

@suhyeoonn suhyeoonn commented Aug 23, 2024

👾 Pull Request

  • 상태: 신규 (테스트 코드는 아직 작성 중입니다!)

1️⃣ Spec

  • 지정된 키 조합이 눌렸을 때 콜백 함수를 호출하는 훅

2️⃣ 변경 사항

  • 없음

3️⃣ 예시 코드

import { useCallback, useRef, useState } from 'react';
import useKeyCombination from './hooks/useKeyCombination';

function App() {
  const [bold, setBold] = useState(false);
  const [isSave, setIsSave] = useState(false);
  const input = useRef<HTMLInputElement>(null);

  const toggleBold = useCallback(() => {
    setBold((state) => !state);
  }, [setBold]);

  const keyActions = {
    toggleBold: {
      shortcutKeys: ['ControlLeft', 'KeyB'],
      callback: toggleBold,
    },
    save: {
      shortcutKeys: ['MetaLeft', 'KeyS'],
      callback: useCallback(() => setIsSave((state) => !state), [setIsSave]),
      isPrevent: true,
    },
    search: {
      shortcutKeys: ['MetaLeft', 'KeyK'],
      callback: useCallback(() => input.current?.focus(), []),
    },
  };

  useKeyCombination(keyActions.toggleBold);
  useKeyCombination(keyActions.save);
  useKeyCombination(keyActions.search);

  return (
    <div>
      <input type="text" ref={input} placeholder="Press command + K" />
      <div>USE-REACT-HOOKS</div>
      <ul>
        <li style={{ fontWeight: bold ? 'bold' : 'normal' }}>
          command + B : Bold
        </li>
        <li>command + S: {isSave ? 'SAVE!' : 'Not saved yet'}</li>
      </ul>
    </div>
  );
}

export default App;

4️⃣ 관련 문서 (선택 사항)

Copy link
Collaborator

@foresec foresec left a comment

Choose a reason for hiding this comment

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

수고하셨습니다👍😊

Copy link
Member

@jeongbaebang jeongbaebang left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Collaborator

@foresec foresec left a comment

Choose a reason for hiding this comment

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

수고많으셨습니다😊👍

🩹 chore: 뗀 키만 map에서 제거되도록 수정

🐛 fix: macOS에서 Meta 키를 누른 상태에서 keyup 이벤트가 발생하지 않는 문제 해결

🩹 chore: 키 삭제 대신 false로 할당하도록 변경
✅ test: 키 반복 입력 테스트 코드 추가 및 리팩토링
@suhyeoonn suhyeoonn force-pushed the URH-55/use-key-combination branch from c35e01e to cae7050 Compare September 1, 2024 08:21
@suhyeoonn suhyeoonn merged commit 4e379a8 into master Sep 1, 2024
@suhyeoonn suhyeoonn deleted the URH-55/use-key-combination branch September 1, 2024 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants