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: focus item when focusing menu #464

Merged
merged 5 commits into from
May 6, 2022
Merged

feat: focus item when focusing menu #464

merged 5 commits into from
May 6, 2022

Conversation

MadCcc
Copy link
Member

@MadCcc MadCcc commented May 5, 2022

之前通过 ref focus 只能聚焦到 ul 元素上,从用户角度看根本不知道有聚焦过这回事。这里修改成:

  1. 如果有 active 状态的 child ,聚焦 active 的 child
  2. 如果没有 active 的 child,聚焦第一个 不是 disabled 状态的 child

ref: ant-design/ant-design#35391 (comment)

@codecov
Copy link

codecov bot commented May 5, 2022

Codecov Report

Merging #464 (8df0ee3) into master (51ad071) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #464   +/-   ##
=======================================
  Coverage   99.85%   99.85%           
=======================================
  Files          25       25           
  Lines         675      677    +2     
  Branches      175      178    +3     
=======================================
+ Hits          674      676    +2     
  Misses          1        1           
Impacted Files Coverage Δ
src/Menu.tsx 100.00% <100.00%> (ø)

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

src/Menu.tsx Outdated
if (shouldFocusIndex > 0) {
containerRef.current
?.querySelectorAll('li')
[shouldFocusIndex]?.focus(options);
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

@MadCcc MadCcc May 5, 2022

Choose a reason for hiding this comment

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

通过控制 activeKey 实现和 dom 的 focus 感觉上有些差异,是否提供另一个方法比如 activate 来自动设置 activeKey 好些?focus 可以保留 dom 的 focus 行为

Copy link
Member Author

Choose a reason for hiding this comment

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

Menu 和 Select 还是有差别,需要 focus 才能用键盘交互,Select 就没有 focus。

Copy link
Member

@afc163 afc163 May 6, 2022

Choose a reason for hiding this comment

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

主要现在的实现是始终 focus 第一个,是不是有用户回车选择后需要保持 active 项的需求(而不是每次打开重置回第一个),所以我感觉内部需要维护一个 activeItemIndex 的 state 状态。

Copy link
Member Author

Choose a reason for hiding this comment

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

目前 active 有 activeKey 控制,可以加入判断。

@MadCcc
Copy link
Member Author

MadCcc commented May 6, 2022

@zombieJ 讨论了下,Dropdown 下面的 menu 和正常的导航 Menu 对 accessibility 的处理应当是不一样的,前者和 Select 类似,后者应该是 focus 实现的。目前看没什么太好的办法让这两种实现融合,最佳方案还是能把两种 Menu 分开比较好,但是对用户习惯改动会比较大。当前情况下的最优解还是用 focus 实现。

@zombieJ
Copy link
Member

zombieJ commented May 6, 2022

desc 里加点描述说明一下,方便以后好查看前因后果。

src/Menu.tsx Outdated
}
if (shouldFocusIndex > 0) {
containerRef.current
?.querySelectorAll('li')
Copy link
Member

Choose a reason for hiding this comment

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

感觉这么查有点问题,如果我就是 MenuItem 里再加 li,你这个 Selector 的顺序和 findIndex 是不匹配的

@MadCcc MadCcc merged commit 4d4a6ad into master May 6, 2022
@MadCcc MadCcc deleted the feat/focus-item branch May 6, 2022 10:59
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