[ML] Ensure applying/removing groups menu is keyboard accessible (#24212)

* add group list arrow nav

* up/down moved into own functions

* always prevent default on arrow key press
This commit is contained in:
Melissa Alvarez 2018-10-19 09:42:21 +01:00 committed by GitHub
parent db905c0da5
commit da26fcbcce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 1 deletions

View file

@ -12,6 +12,7 @@ import React, {
import {
EuiIcon,
keyCodes,
} from '@elastic/eui';
import './styles/main.less';
@ -47,22 +48,65 @@ export class GroupList extends Component {
this.state = {
groups: [],
};
// keep track of each of the group item refs
this.selectItems = [];
}
selectGroup = (group) => {
this.props.selectGroup(group);
}
moveUp = (event, index) => {
event.preventDefault();
if (index < 0) {
return;
} else if (index > 0) {
this.selectItems[index - 1].focus();
}
}
moveDown = (event, index) => {
event.preventDefault();
if (index < this.selectItems.length - 1) {
this.selectItems[index + 1].focus();
}
}
handleKeyDown = (event, group, index) => {
switch (event.keyCode) {
case keyCodes.ENTER:
this.selectGroup(group);
break;
case keyCodes.SPACE:
this.selectGroup(group);
break;
case keyCodes.DOWN:
this.moveDown(event, index);
break;
case keyCodes.UP:
this.moveUp(event, index);
break;
}
}
setRef = (ref, index) => {
this.selectItems[index] = ref;
}
render() {
const { selectedGroups, groups } = this.props;
return (
<div className="group-list">
{
groups.map(g => (
groups.map((g, index) => (
<div
tabIndex={'0'}
onKeyDown={(event) => this.handleKeyDown(event, g, index)}
key={g.id}
className="group-item"
onClick={() => this.selectGroup(g)}
ref={(ref) => this.setRef(ref, index)}
>
<Check group={g} selectedGroups={selectedGroups} />
<JobGroup name={g.id} />

View file

@ -8,6 +8,11 @@
border-bottom: 1px solid #eee;
cursor: pointer;
&:focus {
background-color: #eef6f9;
box-shadow: none;
}
.check {
width: 20px;
display: inline-block;