From 5e013860eee729c7131d5dcde47e4feff5b8037a Mon Sep 17 00:00:00 2001 From: Jani Mustonen Date: Fri, 13 Jan 2017 18:26:59 +0200 Subject: [PATCH] Definition for AccessibleButton --- .../views/elements/AccessibleButton.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/components/views/elements/AccessibleButton.js diff --git a/src/components/views/elements/AccessibleButton.js b/src/components/views/elements/AccessibleButton.js new file mode 100644 index 0000000000..91a9b99333 --- /dev/null +++ b/src/components/views/elements/AccessibleButton.js @@ -0,0 +1,44 @@ +/* + Copyright 2016 Aviral Dasgupta + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +import React from 'react'; + +export default function AccessibleButton(props) { + const {element, onClick, children, ...restProps} = props; + restProps.onClick = onClick; + restProps.onKeyDown = function(e) { + if (e.keyCode == 13 || e.keyCode == 32) return onClick(); + }; + restProps.tabIndex = restProps.tabIndex || "0"; + restProps.role = "button"; + if (Array.isArray(children)) { + return React.createElement(element, restProps, ...children); + } else { + return React.createElement(element, restProps, children); + } +} + +AccessibleButton.propTypes = { + children: React.PropTypes.node, + element: React.PropTypes.string, + onClick: React.PropTypes.func.isRequired, +}; + +AccessibleButton.defaultProps = { + element: 'div' +}; + +AccessibleButton.displayName = "AccessibleButton";