Allow keyboard control even without a screen reader

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2019-09-26 13:52:09 +01:00
parent d588e709e5
commit 14e3cb8736
2 changed files with 29 additions and 4 deletions

View file

@ -1,6 +1,7 @@
/* /*
Copyright 2017 Travis Ralston Copyright 2017 Travis Ralston
Copyright 2019 New Vector Ltd Copyright 2019 New Vector Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -17,6 +18,7 @@ limitations under the License.
import * as React from "react"; import * as React from "react";
import {_t} from '../../languageHandler'; import {_t} from '../../languageHandler';
import {KeyCode} from "../../Keyboard";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
/** /**
@ -81,6 +83,13 @@ export class TabbedView extends React.Component {
} }
const onClickHandler = () => this._setActiveTab(tab); const onClickHandler = () => this._setActiveTab(tab);
const onKeyDownHandler = (e) => {
e.stopPropagation();
e.preventDefault();
if (e.keyCode === KeyCode.ENTER || e.keyCode === KeyCode.SPACE) {
this._setActiveTab(tab);
}
};
const label = _t(tab.label); const label = _t(tab.label);
return ( return (
@ -88,6 +97,7 @@ export class TabbedView extends React.Component {
className={classes} className={classes}
key={"tab_label_" + tab.label} key={"tab_label_" + tab.label}
onClick={onClickHandler} onClick={onClickHandler}
onKeyDown={onKeyDownHandler}
role="button" role="button"
aria-label={label} aria-label={label}
tabIndex={0} tabIndex={0}

View file

@ -17,6 +17,7 @@ limitations under the License.
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classNames from "classnames"; import classNames from "classnames";
import {KeyCode} from "../../../Keyboard";
export default class ToggleSwitch extends React.Component { export default class ToggleSwitch extends React.Component {
static propTypes = { static propTypes = {
@ -44,10 +45,7 @@ export default class ToggleSwitch extends React.Component {
} }
} }
_onClick = (e) => { _toggle = () => {
e.stopPropagation();
e.preventDefault();
if (this.props.disabled) return; if (this.props.disabled) return;
const newState = !this.state.checked; const newState = !this.state.checked;
@ -57,6 +55,22 @@ export default class ToggleSwitch extends React.Component {
} }
}; };
_onClick = (e) => {
e.stopPropagation();
e.preventDefault();
this._toggle();
};
_onKeyDown = (e) => {
e.stopPropagation();
e.preventDefault();
if (e.keyCode === KeyCode.ENTER || e.keyCode === KeyCode.SPACE) {
this._toggle();
}
};
render() { render() {
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const {checked, disabled, onChange, ...props} = this.props; const {checked, disabled, onChange, ...props} = this.props;
@ -71,6 +85,7 @@ export default class ToggleSwitch extends React.Component {
{...props} {...props}
className={classes} className={classes}
onClick={this._onClick} onClick={this._onClick}
onKeyDown={this._onKeyDown}
role="checkbox" role="checkbox"
aria-checked={this.state.checked} aria-checked={this.state.checked}
aria-disabled={disabled} aria-disabled={disabled}