Skip to content

Commit

Permalink
#5639 for TabMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitfindikli committed Dec 22, 2023
1 parent 2d5c97b commit e9b890a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 14 deletions.
4 changes: 2 additions & 2 deletions components/doc/tabmenu/basicdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function BasicDoc(props) {
<TabMenu model={items} />
`,
javascript: `
import React from 'react';
import React from 'react';
import { TabMenu } from 'primereact/tabmenu';
export default function BasicDemo() {
Expand All @@ -36,7 +36,7 @@ export default function BasicDemo() {
}
`,
typescript: `
import React from 'react';
import React from 'react';
import { TabMenu } from 'primereact/tabmenu';
import { MenuItem } from 'primereact/menuitem';
Expand Down
119 changes: 113 additions & 6 deletions components/lib/tabmenu/TabMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,107 @@ export const TabMenu = React.memo(
updateInkBar();
});

const onKeyDownItem = (event, item, index) => {
switch (event.code) {
case 'ArrowRight':
navigateToNextItem(event.target);
event.preventDefault();
break;

case 'ArrowLeft':
navigateToPrevItem(event.target);
event.preventDefault();
break;

case 'Home':
navigateToFirstItem(event.target);
event.preventDefault();
break;

case 'End':
navigateToLastItem(event.target);
event.preventDefault();
break;

case 'Space':
case 'Enter':
itemClick(event, item, index);
event.preventDefault();
break;

case 'Tab':
onTabKey();
break;

default:
break;
}
};

const navigateToNextItem = (target) => {
const nextItem = findNextItem(target);

nextItem && setFocusToMenuitem(target, nextItem);
};

const navigateToPrevItem = (target) => {
const prevItem = findPrevItem(target);

prevItem && setFocusToMenuitem(target, prevItem);
};

const navigateToFirstItem = (target) => {
const firstItem = findFirstItem(target);

firstItem && setFocusToMenuitem(target, firstItem);
};

const navigateToLastItem = (target) => {
const lastItem = findLastItem(target);

lastItem && setFocusToMenuitem(target, lastItem);
};

const findNextItem = (item) => {
const nextItem = item.parentElement.nextElementSibling;

return nextItem ? (DomHandler.getAttribute(nextItem, 'data-p-disabled') === true ? findNextItem(nextItem.children[0]) : nextItem.children[0]) : null;
};

const findPrevItem = (item) => {
const prevItem = item.parentElement.previousElementSibling;

return prevItem ? (DomHandler.getAttribute(prevItem, 'data-p-disabled') === true ? findPrevItem(prevItem.children[0]) : prevItem.children[0]) : null;
};

const findFirstItem = () => {
const firstSibling = DomHandler.findSingle(navRef.current, '[data-pc-section="menuitem"][data-p-disabled="false"]');

return firstSibling ? firstSibling.children[0] : null;
};

const findLastItem = () => {
const siblings = DomHandler.find(navRef.current, '[data-pc-section="menuitem"][data-p-disabled="false"]');

return siblings ? siblings[siblings.length - 1].children[0] : null;
};

const setFocusToMenuitem = (target, focusableItem) => {
target.tabIndex = '-1';
focusableItem.tabIndex = '0';
focusableItem.focus();
};

const onTabKey = () => {
const activeItem = DomHandler.findSingle(navRef.current, '[data-pc-section="menuitem"][data-p-disabled="false"][data-p-highlight="true"]');
const focusedItem = DomHandler.findSingle(navRef.current, '[data-pc-section="action"][tabindex="0"]');

if (focusedItem !== activeItem.children[0]) {
activeItem && (activeItem.children[0].tabIndex = '0');
focusedItem.tabIndex = '-1';
}
};

const createMenuItem = (item, index) => {
if (item.visible === false) {
return null;
Expand Down Expand Up @@ -125,10 +226,11 @@ export const TabMenu = React.memo(
const actionProps = mergeProps(
{
href: url || '#',
role: 'menuitem',
tabIndex: active ? '0' : '-1',
className: cx('action'),
target: target,
onClick: (event) => itemClick(event, item, index),
role: 'presentation'
onClick: (event) => itemClick(event, item, index)
},
getPTOptions('action', item, index)
);
Expand Down Expand Up @@ -162,11 +264,13 @@ export const TabMenu = React.memo(
ref: tabsRef.current[`tab_${index}`],
id: key,
key,
onKeyDown: (event) => onKeyDownItem(event, item, index),
className: cx('menuitem', { _className, active, disabled }),
style: style,
role: 'tab',
'aria-selected': active,
'aria-expanded': active,
role: 'presentation',
'aria-label': _label,
'data-p-highlight': active,
'data-p-disabled': disabled || false,
'aria-disabled': disabled
},
getPTOptions('menuitem', item, index)
Expand All @@ -185,15 +289,18 @@ export const TabMenu = React.memo(
const inkbarProps = mergeProps(
{
ref: inkbarRef,
role: 'none',
className: cx('inkbar')
},
ptm('inkbar')
);
const menuProps = mergeProps(
{
ref: navRef,
'aria-label': props.ariaLabel,
'aria-labelledby': props.ariaLabelledBy,
className: cx('menu'),
role: 'tablist'
role: 'menubar'
},
ptm('menu')
);
Expand Down
14 changes: 8 additions & 6 deletions components/lib/tabmenu/TabMenuBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ const styles = `
.p-tabmenu {
overflow-x: auto;
}
.p-tabmenu-nav {
display: flex;
margin: 0;
padding: 0;
list-style-type: none;
flex-wrap: nowrap;
}
.p-tabmenu-nav a {
cursor: pointer;
user-select: none;
Expand All @@ -43,20 +43,20 @@ const styles = `
text-decoration: none;
overflow: hidden;
}
.p-tabmenu-nav a:focus {
z-index: 1;
}
.p-tabmenu-nav .p-menuitem-text {
line-height: 1;
}
.p-tabmenu-ink-bar {
display: none;
z-index: 1;
}
.p-tabmenu::-webkit-scrollbar {
display: none;
}
Expand All @@ -69,6 +69,8 @@ export const TabMenuBase = ComponentBase.extend({
id: null,
model: null,
activeIndex: 0,
ariaLabel: null,
ariaLabelledBy: null,
style: null,
className: null,
onTabChange: null,
Expand Down

0 comments on commit e9b890a

Please sign in to comment.