Skip to content
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

feat: useKeyPress callback passes in the triggered shortcut key #2170

Merged
merged 25 commits into from
Jun 27, 2023

Conversation

Simon-He95
Copy link
Contributor

@Simon-He95 Simon-He95 commented Apr 24, 2023

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 Related issue link

close: #1404

💡 Background and solution

📝 Changelog

Language Changelog
🇺🇸 English
🇨🇳 Chinese

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@liuyib liuyib self-requested a review April 24, 2023 08:57
@liuyib
Copy link
Collaborator

liuyib commented Apr 26, 2023

几个问题:

  1. 补充个 demo,展示下这个改进给用户带来的方便之处

  2. 收集 e.key 再通过回调传给用户,我认为不是个好的选择。原因是按照这个 PR 的代码,执行:

    useKeyPress("ctrl.alt.c", (e, key) => {
      console.log(key);
    });

    你会发现打印了:['ctrl', 'alt', 'ç']注意数组最后一项,不是英文字母 c,而是 mac 的废键字符,这样的话,用户还要知道 mac 废键这个东西,甚至其他系统也可能有一些特殊快捷键能打印出一些特殊字符。

    总之,使用心智负担比较重,而且不是跨平台兼容的。

    我的建议:

    用户传来哪些快捷键,触发回调时就再传给用户哪些,比如:

    useKeyPress(
      ["a", "b", "ctrl.alt.c"],
      // 注意 code 要兼容 exactMatch 参数
      (e, key) => {
        console.log(key);
        // 可能的打印如下:
        // a
        // b
        // ctrl.alt.c
      },
      {
        exactMatch: true,
      }
    );

    按照我这样更改,想实现特定键触发特定事件,可以这样做:

    const callbackMap = {
      a: () => {
        console.log("a");
      },
      "ctrl.a": () => {
        console.log("ctrl.a");
      },
      "shift.b": () => {
        console.log("shift.b");
      },
    };
    
    useKeyPress(
      ["a", "ctrl.a", "shift.b"],
      (e, key) => {
        // 指定快捷键触发指定事件
        callbackMap[key]();
      },
      {
        exactMatch: true,
      }
    );

@yunsii 这样是不是满足你的需求并且用法较为方便了?

@hchlq @miracles1919 @li-jia-nan @crazylxr 看下这样的思路可以吗?

@Simon-He95
Copy link
Contributor Author

做了下调整,现在code是匹配keyFilter中的值传入了

@liuyib
Copy link
Collaborator

liuyib commented Apr 26, 2023

做了下调整,现在code是匹配keyFilter中的值传入了

有空了看哈

@yunsii
Copy link

yunsii commented Apr 27, 2023

@liuyib 是这个意思,根据用户输入返回,很完美

@hchlq
Copy link
Collaborator

hchlq commented Apr 27, 2023

+1 ,感觉没啥问题

@crazylxr
Copy link
Collaborator

LGTM

@crazylxr crazylxr merged commit a4b8753 into alibaba:master Jun 27, 2023
askwuxue pushed a commit to askwuxue/hooks that referenced this pull request Jul 29, 2023
…aba#2170)

* feat: useKeyPress callback passes in the triggered shortcut key

* chore: update md

* chore: update

* fix: test

* chore: update md

* chore: update md

* chore: update

* style: change name to pascal

* docs: fix type in eventHandler

* style: rename 'code' to 'key'

* chore: add new demo

* test: rename & remove useless option

* refactor: 当 keyFilter 传函数时保持原来的用法

* fix: can't pass ci when keyFilter is array

* fix: second callback is not be called

* chore: firedKey when functional keyFilter

* chore: update

* test: better case

* test: update

* test: remove useless

* refactor: keyFilter 传函数的情况,不用再处理 exactMatch

---------

Co-authored-by: liuyib <[email protected]>
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.

[Feature request] useKeyPress 回调传入触发的快捷键
5 participants