Skip to content

Commit

Permalink
feat: enable left and right arrow functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rita committed May 6, 2024
1 parent fb7571a commit d35ffb2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
23 changes: 2 additions & 21 deletions components/menu/src/flyout-menu/flyout-menu.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { colors, elevations, spacers } from '@dhis2/ui-constants'
import PropTypes from 'prop-types'
import React, {
Children,
cloneElement,
isValidElement,
useState,
// useEffect,
} from 'react'
import React, { Children, cloneElement, isValidElement, useState } from 'react'
import { Menu } from '../index.js'

const FlyoutMenu = ({
Expand All @@ -17,25 +11,12 @@ const FlyoutMenu = ({
maxHeight,
maxWidth,
}) => {
const [openedSubMenu, setOpenedSubMenu] = useState(0)
const [openedSubMenu, setOpenedSubMenu] = useState(null)
const toggleSubMenu = (index) => {
const toggleValue = index === openedSubMenu ? null : index
setOpenedSubMenu(toggleValue)
}

// useEffect(() => {
// if (openedSubMenu) {
// menuItemRef.current.childNodes[0].focus()
// }
// }, [openedSubMenu])

// console.log(document.activeElement, "flyout menu active element")
// // const handleKeydown = () => {

// // }

console.log(openedSubMenu, 'opened submenu')

return (
<div className={className} data-test={dataTest}>
<Menu dense={dense}>
Expand Down
13 changes: 12 additions & 1 deletion components/menu/src/menu-item/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ const MenuItem = forwardRef(function MenuItem(
aria-disabled={disabled}
aria-checked={checkbox && checked}
aria-label={label}
aria-owns={
children &&
`popper-${label.split(' ').join('-').toLowerCase()}`
}
>
{icon && <span className="icon">{icon}</span>}

Expand All @@ -108,7 +112,14 @@ const MenuItem = forwardRef(function MenuItem(
</li>
{children && showSubMenu && (
<Portal>
<Popper placement="right-start" reference={menuItemRef}>
<Popper
placement="right-start"
reference={menuItemRef}
id={`popper-${label
.split(' ')
.join('-')
.toLowerCase()}`}
>
<FlyoutMenu dense={dense}>{children}</FlyoutMenu>
</Popper>
</Portal>
Expand Down
38 changes: 30 additions & 8 deletions components/menu/src/menu/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ const Menu = ({ children, className, dataTest, dense }) => {
return obj
}

const focusFirstFocusableItem = useCallback((e) => {
const focusableItems = findFocusableItems()

if (e.target === menuRef.current) {
setFocusedIndex(~~Object.keys(focusableItems)[0])
Object.values(focusableItems)[0].focus()
}
}, [])
const focusFirstFocusableItem = useCallback(
(e) => {
const focusableItems = findFocusableItems()

if (e.target === menuRef.current) {
if (focusedIndex === -1) {
setFocusedIndex(~~Object.keys(focusableItems)[0])
// Object.values(focusableItems)[0].focus()
}
}
},
[focusedIndex]
)

const setNextFocusableIndex = useCallback(({ elemIndex, action }) => {
const focusableItems = findFocusableItems()
Expand Down Expand Up @@ -99,6 +104,20 @@ const Menu = ({ children, className, dataTest, dense }) => {
break
case 'ArrowLeft':
event.preventDefault()
document
.querySelector(
`[aria-owns='${event.target.parentNode.parentNode.parentNode.parentNode.getAttribute(
'id'
)}']`
)
.focus()
document
.querySelector(
`[aria-owns='${event.target.parentNode.parentNode.parentNode.parentNode.getAttribute(
'id'
)}']`
)
.click()
break
case ' ':
case 'Enter':
Expand Down Expand Up @@ -156,6 +175,9 @@ const Menu = ({ children, className, dataTest, dense }) => {
: child.props.hideDivider,
tabIndex: focusedIndex === index ? 0 : -1,
active: focusedIndex === index,
showSubMenu: child.props.children
? child.props.showSubMenu
: null,
ref: (node) => {
const role = node?.getAttribute('role')
if (
Expand Down
3 changes: 3 additions & 0 deletions components/popper/src/popper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const flipPlacement = (placement) => {
}

const Popper = ({
id,
children,
className,
dataTest,
Expand Down Expand Up @@ -51,6 +52,7 @@ const Popper = ({

return (
<div
id={id}
className={className}
data-test={dataTest}
ref={setPopperElement}
Expand All @@ -74,6 +76,7 @@ Popper.propTypes = {
children: PropTypes.node.isRequired,
className: PropTypes.string,
dataTest: PropTypes.string,
id: PropTypes.string,
/** A property of the `createPopper` options. See [popper docs](https://popper.js.org/docs/v2/constructors/) */
modifiers: PropTypes.arrayOf(
PropTypes.shape({
Expand Down
1 change: 1 addition & 0 deletions components/popper/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type PopperReference = VirtualElement | Element
type ReferenceElement = PopperReference | React.RefObject<PopperReference>

export interface PopperProps {
id?: string
/**
* Content inside the Popper
*/
Expand Down

0 comments on commit d35ffb2

Please sign in to comment.