2017-01-13 19:26:59 +03:00
|
|
|
/*
|
2017-01-13 20:34:53 +03:00
|
|
|
Copyright 2016 Jani Mustonen
|
2017-01-13 19:26:59 +03:00
|
|
|
|
|
|
|
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';
|
|
|
|
|
2017-01-13 20:34:53 +03:00
|
|
|
/**
|
2017-01-25 01:41:52 +03:00
|
|
|
* AccessibleButton is a generic wrapper for any element that should be treated
|
|
|
|
* as a button. Identifies the element as a button, setting proper tab
|
|
|
|
* indexing and keyboard activation behavior.
|
|
|
|
*
|
|
|
|
* @param {Object} props react element properties
|
|
|
|
* @returns {Object} rendered react
|
2017-01-13 20:34:53 +03:00
|
|
|
*/
|
2017-01-13 19:26:59 +03:00
|
|
|
export default function AccessibleButton(props) {
|
|
|
|
const {element, onClick, children, ...restProps} = props;
|
|
|
|
restProps.onClick = onClick;
|
2017-03-14 14:50:13 +03:00
|
|
|
restProps.onKeyUp = function(e) {
|
|
|
|
if (e.keyCode == 13 || e.keyCode == 32) return onClick(e);
|
2017-01-13 19:26:59 +03:00
|
|
|
};
|
2017-01-25 01:41:52 +03:00
|
|
|
restProps.tabIndex = restProps.tabIndex || "0";
|
2017-01-13 19:26:59 +03:00
|
|
|
restProps.role = "button";
|
2017-10-11 19:56:17 +03:00
|
|
|
restProps.className = (restProps.className ? restProps.className + " " : "") +
|
2017-04-15 02:30:48 +03:00
|
|
|
"mx_AccessibleButton";
|
2017-01-13 20:34:53 +03:00
|
|
|
return React.createElement(element, restProps, children);
|
2017-01-13 19:26:59 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:34:53 +03:00
|
|
|
/**
|
|
|
|
* children: React's magic prop. Represents all children given to the element.
|
|
|
|
* element: (optional) The base element type. "div" by default.
|
|
|
|
* onClick: (required) Event handler for button activation. Should be
|
|
|
|
* implemented exactly like a normal onClick handler.
|
|
|
|
*/
|
2017-01-13 19:26:59 +03:00
|
|
|
AccessibleButton.propTypes = {
|
|
|
|
children: React.PropTypes.node,
|
|
|
|
element: React.PropTypes.string,
|
|
|
|
onClick: React.PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
AccessibleButton.defaultProps = {
|
2017-01-25 01:41:52 +03:00
|
|
|
element: 'div',
|
2017-01-13 19:26:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
AccessibleButton.displayName = "AccessibleButton";
|