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

Issue Fix: use-focus-trap doesn't handle Radio Groups well #4856

Merged
merged 11 commits into from
Oct 3, 2023

Conversation

Shresth72
Copy link
Contributor

@Shresth72 Shresth72 commented Sep 24, 2023

Fixes #4678

import { findTabbableDescendants } from './tabbable';

export function scopeTab(node: HTMLElement, event: KeyboardEvent) {
  const tabbable = findTabbableDescendants(node);
  if (!tabbable.length) {
    event.preventDefault();
    return;
  }
  const finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1];
  const root = node.getRootNode() as unknown as DocumentOrShadowRoot;
  let leavingFinalTabbable = finalTabbable === root.activeElement || node === root.activeElement;

  // Handle the case of the active element being in a RadioGroup with the finalTabbable element
  const activeElement = root.activeElement as Element;
  const activeElementIsRadio =
    activeElement.tagName === 'INPUT' && activeElement.getAttribute('type') === 'radio';
  if (activeElementIsRadio) {
    const activeRadioGroup = tabbable.filter(
      (element) =>
        element.getAttribute('type') === 'radio' &&
        element.getAttribute('name') === activeElement.getAttribute('name')
    );
    leavingFinalTabbable = activeRadioGroup.includes(finalTabbable);
  }

  if (!leavingFinalTabbable) {
    return;
  }

  event.preventDefault();

  const target = tabbable[event.shiftKey ? tabbable.length - 1 : 0];

  if (target) {
    target.focus();
  }
}

@Shresth72 Shresth72 closed this Sep 30, 2023
@Shresth72 Shresth72 reopened this Sep 30, 2023
@Shresth72 Shresth72 closed this Sep 30, 2023
@Shresth72 Shresth72 reopened this Sep 30, 2023
@@ -6048,30 +6048,10 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001449:
Copy link
Member

Choose a reason for hiding this comment

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

Please do not downgrade caniuse-lite.

Copy link
Contributor Author

@Shresth72 Shresth72 Sep 30, 2023

Choose a reason for hiding this comment

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

This was a previous commit, it was resolved earlier. It's now same as the master repo

@rtivital
Copy link
Member

rtivital commented Oct 2, 2023

Please resolve conflicts

@rtivital rtivital merged commit 34e52af into mantinedev:master Oct 3, 2023
1 check passed
@rtivital
Copy link
Member

rtivital commented Oct 3, 2023

Thanks!

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.

use-focus-trap doesn't handle Radio Groups well
2 participants