Skip to content

Commit

Permalink
feat(Dropdown): handle keyboard navigation and styling for Link child…
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcfaul authored and tlabaj committed Jan 16, 2019
1 parent 2bf5b55 commit 0996cfd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import ReactDOM from 'react-dom';
import styles from '@patternfly/patternfly-next/components/Dropdown/dropdown.css';
import { css } from '@patternfly/react-styles';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -61,7 +62,10 @@ class DropdownItem extends React.Component {
} else if (event.key === 'ArrowDown') {
this.props.context.keyHandler(this.props.index, 'down');
} else if (event.key === 'Enter') {
this.ref.current.click && this.ref.current.click();
if (!this.ref.current.getAttribute) ReactDOM.findDOMNode(this.ref.current).click();
else {
this.ref.current.click && this.ref.current.click();
}
}
};

Expand Down Expand Up @@ -97,7 +101,14 @@ class DropdownItem extends React.Component {
isHovered && styles.modifiers.hover,
className
)} ${child.props.className}`,
ref: this.ref
ref: this.ref,
onKeyDown: this.onKeyDown,
onClick: event => {
if (!isDisabled) {
if (Component === 'button') onClick && onClick(event);
onSelect && onSelect(event);
}
}
})
)
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { css } from '@patternfly/react-styles';
import PropTypes from 'prop-types';
import { componentShape } from '../../internal/componentShape';
import { DropdownPosition, DropdownContext, DropdownArrowContext } from './dropdownConstants';
import ReactDOM from 'react-dom';

const propTypes = {
/** Anything which can be rendered as dropdown items */
Expand Down Expand Up @@ -52,7 +53,9 @@ class DropdownMenu extends React.Component {
this.keyHandler(nextIndex, position, custom);
} else {
custom
? this.refsCollection[`item-${nextIndex}`].current.focus()
? (this.refsCollection[`item-${nextIndex}`].current.focus &&
this.refsCollection[`item-${nextIndex}`].current.focus()) ||
ReactDOM.findDOMNode(this.refsCollection[`item-${nextIndex}`].current).focus()
: this.refsCollection[`item-${nextIndex}`].focus();
}
};
Expand All @@ -61,13 +64,16 @@ class DropdownMenu extends React.Component {
if (this.props.openedOnEnter) {
if (this.props.component === 'ul') this.refsCollection['item-0'].focus();
else {
this.refsCollection['item-0'].current.focus();
(this.refsCollection['item-0'].current.focus && this.refsCollection['item-0'].current.focus()) ||
ReactDOM.findDOMNode(this.refsCollection['item-0'].current).focus();
}
}
}

sendRef = (index, node, isDisabled) => {
if (isDisabled || node.getAttribute('role') === 'separator') {
if (!node.getAttribute) {
this.refsCollection[`item-${index}`] = ReactDOM.findDOMNode(node);
} else if (isDisabled || node.getAttribute('role') === 'separator') {
this.refsCollection[`item-${index}`] = null;
} else {
this.refsCollection[`item-${index}`] = node;
Expand All @@ -86,6 +92,11 @@ class DropdownMenu extends React.Component {
const mappedChildren = React.Children.map(this.props.children, (child, index) => {
const mappedChild = React.cloneElement(child, {
ref: React.createRef(),
className: `${css(
child.props.isDisabled && styles.modifiers.disabled,
child.props.isHovered && styles.modifiers.hover,
styles.dropdownMenuItem
)}${child.props.className ? child.props.className : ''}`,
tabIndex: -1,
onKeyDown: event => {
if (event.key === 'Tab') return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ exports[`KebabToggle basic 1`] = `
line-height: 1.5;
background-color: transparent;
}
.pf-c-dropdown__menu-item {
display: block;
width: 100%;
padding: 0.5rem 1rem 0.5rem 1rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #282d33;
text-align: left;
white-space: nowrap;
text-decoration: none;
background-color: #ededed;
}
.pf-c-dropdown__menu {
display: block;
position: absolute;
Expand Down Expand Up @@ -139,6 +152,7 @@ exports[`KebabToggle basic 1`] = `
onClick={[Function]}
>
<div
className="pf-c-dropdown__menu-item"
key=".0"
onKeyDown={[Function]}
tabIndex={-1}
Expand Down Expand Up @@ -1813,6 +1827,19 @@ exports[`dropdown basic 1`] = `
line-height: 1.5;
background-color: transparent;
}
.pf-c-dropdown__menu-item {
display: block;
width: 100%;
padding: 0.5rem 1rem 0.5rem 1rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #282d33;
text-align: left;
white-space: nowrap;
text-decoration: none;
background-color: #ededed;
}
.pf-c-dropdown__menu {
display: block;
position: absolute;
Expand Down Expand Up @@ -1945,6 +1972,7 @@ exports[`dropdown basic 1`] = `
onClick={[Function]}
>
<div
className="pf-c-dropdown__menu-item"
key=".0"
onKeyDown={[Function]}
tabIndex={-1}
Expand Down

0 comments on commit 0996cfd

Please sign in to comment.