From 05f9b1033b9396ad576e79e8db4d92742e68bde2 Mon Sep 17 00:00:00 2001 From: cagataycivici Date: Wed, 19 Dec 2018 14:53:10 +0300 Subject: [PATCH] Fixed #690 - Improve Listbox Accessibility --- src/components/listbox/ListBox.js | 8 +++- src/components/listbox/ListBoxItem.js | 62 +++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/components/listbox/ListBox.js b/src/components/listbox/ListBox.js index 10acaa18c2..e5aeba81f8 100644 --- a/src/components/listbox/ListBox.js +++ b/src/components/listbox/ListBox.js @@ -22,6 +22,7 @@ export class ListBox extends Component { multiple: false, metaKeySelection: false, filter: false, + tabIndex: '0', tooltip: null, tooltipOptions: null, onChange: null @@ -40,6 +41,7 @@ export class ListBox extends Component { multiple: PropTypes.bool, metaKeySelection: PropTypes.bool, filter: PropTypes.bool, + tabIndex: PropTypes.string, tooltip: PropTypes.string, tooltipOptions: PropTypes.object, onChange: PropTypes.func @@ -264,8 +266,10 @@ export class ListBox extends Component { items = items.map((option, index) => { let optionLabel = this.getOptionLabel(option); - return this.onOptionTouchEnd(e, option, index)} />; + return ( + this.onOptionTouchEnd(e, option, index)} tabIndex={this.props.tabIndex} /> + ); }); } diff --git a/src/components/listbox/ListBoxItem.js b/src/components/listbox/ListBoxItem.js index 82cca9645b..dde690bb96 100644 --- a/src/components/listbox/ListBoxItem.js +++ b/src/components/listbox/ListBoxItem.js @@ -1,6 +1,7 @@ import React, {Component} from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; +import DomHandler from '../utils/DomHandler'; export class ListBoxItem extends Component { @@ -8,15 +9,17 @@ export class ListBoxItem extends Component { option: null, label: null, selected: false, + tabIndex: null, onClick: null, onTouchEnd: null, - template: null + template: null, } static propTypes = { option: PropTypes.any, label: PropTypes.string, selected: PropTypes.bool, + tabIndex: PropTypes.string, onClick: PropTypes.func, onTouchEnd: PropTypes.func, template: PropTypes.func @@ -26,6 +29,7 @@ export class ListBoxItem extends Component { super(); this.onClick = this.onClick.bind(this); this.onTouchEnd = this.onTouchEnd.bind(this); + this.onKeyDown = this.onKeyDown.bind(this); } onClick(event) { @@ -47,15 +51,65 @@ export class ListBoxItem extends Component { }); } } + + onKeyDown(event) { + let item = event.currentTarget; + + switch(event.which) { + //down + case 40: + var nextItem = this.findNextItem(item); + if(nextItem) { + nextItem.focus(); + } + + event.preventDefault(); + break; + + //up + case 38: + var prevItem = this.findPrevItem(item); + if(prevItem) { + prevItem.focus(); + } + + event.preventDefault(); + break; + + //enter + case 13: + this.onClick(event); + event.preventDefault(); + break; + } + } + + findNextItem(item) { + let nextItem = item.nextElementSibling; + + if (nextItem) + return DomHandler.hasClass(nextItem, 'p-disabled') ? this.findNextItem(nextItem) : nextItem; + else + return null; + } + + findPrevItem(item) { + let prevItem = item.previousElementSibling; + + if (prevItem) + return DomHandler.hasClass(prevItem, 'p-disabled') ? this.findPrevItem(prevItem) : prevItem; + else + return null; + } render() { let className = classNames('p-listbox-item', {'p-highlight': this.props.selected}); let content = this.props.template ? this.props.template(this.props.option) : this.props.label; return ( -
  • - {content} -
  • +
  • + {content} +
  • ); } } \ No newline at end of file