Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

EnhancedButton.js 13 KiB

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends2 = require('babel-runtime/helpers/extends');
  6. var _extends3 = _interopRequireDefault(_extends2);
  7. var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
  8. var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
  9. var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  10. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  11. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  12. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  13. var _createClass2 = require('babel-runtime/helpers/createClass');
  14. var _createClass3 = _interopRequireDefault(_createClass2);
  15. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  16. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  17. var _inherits2 = require('babel-runtime/helpers/inherits');
  18. var _inherits3 = _interopRequireDefault(_inherits2);
  19. var _simpleAssign = require('simple-assign');
  20. var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
  21. var _react = require('react');
  22. var _react2 = _interopRequireDefault(_react);
  23. var _propTypes = require('prop-types');
  24. var _propTypes2 = _interopRequireDefault(_propTypes);
  25. var _events = require('../utils/events');
  26. var _events2 = _interopRequireDefault(_events);
  27. var _keycode = require('keycode');
  28. var _keycode2 = _interopRequireDefault(_keycode);
  29. var _FocusRipple = require('./FocusRipple');
  30. var _FocusRipple2 = _interopRequireDefault(_FocusRipple);
  31. var _TouchRipple = require('./TouchRipple');
  32. var _TouchRipple2 = _interopRequireDefault(_TouchRipple);
  33. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  34. var styleInjected = false;
  35. var listening = false;
  36. var tabPressed = false;
  37. function injectStyle() {
  38. if (!styleInjected) {
  39. // Remove inner padding and border in Firefox 4+.
  40. var style = document.createElement('style');
  41. style.innerHTML = '\n button::-moz-focus-inner,\n input::-moz-focus-inner {\n border: 0;\n padding: 0;\n }\n ';
  42. document.body.appendChild(style);
  43. styleInjected = true;
  44. }
  45. }
  46. function listenForTabPresses() {
  47. if (!listening) {
  48. _events2.default.on(window, 'keydown', function (event) {
  49. tabPressed = (0, _keycode2.default)(event) === 'tab';
  50. });
  51. listening = true;
  52. }
  53. }
  54. var EnhancedButton = function (_Component) {
  55. (0, _inherits3.default)(EnhancedButton, _Component);
  56. function EnhancedButton() {
  57. var _ref;
  58. var _temp, _this, _ret;
  59. (0, _classCallCheck3.default)(this, EnhancedButton);
  60. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  61. args[_key] = arguments[_key];
  62. }
  63. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = EnhancedButton.__proto__ || (0, _getPrototypeOf2.default)(EnhancedButton)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  64. isKeyboardFocused: false
  65. }, _this.handleKeyDown = function (event) {
  66. if (!_this.props.disabled && !_this.props.disableKeyboardFocus) {
  67. if ((0, _keycode2.default)(event) === 'esc' && _this.state.isKeyboardFocused) {
  68. _this.removeKeyboardFocus(event);
  69. }
  70. }
  71. _this.props.onKeyDown(event);
  72. }, _this.handleKeyUp = function (event) {
  73. _this.props.onKeyUp(event);
  74. }, _this.handleBlur = function (event) {
  75. _this.cancelFocusTimeout();
  76. _this.removeKeyboardFocus(event);
  77. _this.props.onBlur(event);
  78. }, _this.handleFocus = function (event) {
  79. if (event) event.persist();
  80. if (!_this.props.disabled && !_this.props.disableKeyboardFocus) {
  81. // setTimeout is needed because the focus event fires first
  82. // Wait so that we can capture if this was a keyboard focus
  83. // or touch focus
  84. _this.focusTimeout = setTimeout(function () {
  85. if (tabPressed) {
  86. _this.setKeyboardFocus(event);
  87. tabPressed = false;
  88. }
  89. }, 150);
  90. _this.props.onFocus(event);
  91. }
  92. }, _this.handleClick = function (event) {
  93. _this.cancelFocusTimeout();
  94. if (!_this.props.disabled) {
  95. tabPressed = false;
  96. _this.removeKeyboardFocus(event);
  97. _this.props.onClick(event);
  98. }
  99. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  100. }
  101. (0, _createClass3.default)(EnhancedButton, [{
  102. key: 'componentWillMount',
  103. value: function componentWillMount() {
  104. var _props = this.props,
  105. disabled = _props.disabled,
  106. disableKeyboardFocus = _props.disableKeyboardFocus,
  107. keyboardFocused = _props.keyboardFocused;
  108. if (!disabled && keyboardFocused && !disableKeyboardFocus) {
  109. this.setState({ isKeyboardFocused: true });
  110. }
  111. }
  112. }, {
  113. key: 'componentDidMount',
  114. value: function componentDidMount() {
  115. injectStyle();
  116. listenForTabPresses();
  117. if (this.state.isKeyboardFocused) {
  118. this.button.focus();
  119. this.props.onKeyboardFocus(null, true);
  120. }
  121. }
  122. }, {
  123. key: 'componentWillReceiveProps',
  124. value: function componentWillReceiveProps(nextProps) {
  125. if ((nextProps.disabled || nextProps.disableKeyboardFocus) && this.state.isKeyboardFocused) {
  126. this.setState({ isKeyboardFocused: false });
  127. if (nextProps.onKeyboardFocus) {
  128. nextProps.onKeyboardFocus(null, false);
  129. }
  130. }
  131. }
  132. }, {
  133. key: 'componentWillUnmount',
  134. value: function componentWillUnmount() {
  135. if (this.focusTimeout) {
  136. clearTimeout(this.focusTimeout);
  137. }
  138. }
  139. }, {
  140. key: 'isKeyboardFocused',
  141. value: function isKeyboardFocused() {
  142. return this.state.isKeyboardFocused;
  143. }
  144. }, {
  145. key: 'removeKeyboardFocus',
  146. value: function removeKeyboardFocus(event) {
  147. if (this.state.isKeyboardFocused) {
  148. this.setState({ isKeyboardFocused: false });
  149. this.props.onKeyboardFocus(event, false);
  150. }
  151. }
  152. }, {
  153. key: 'setKeyboardFocus',
  154. value: function setKeyboardFocus(event) {
  155. if (!this.state.isKeyboardFocused) {
  156. this.setState({ isKeyboardFocused: true });
  157. this.props.onKeyboardFocus(event, true);
  158. }
  159. }
  160. }, {
  161. key: 'cancelFocusTimeout',
  162. value: function cancelFocusTimeout() {
  163. if (this.focusTimeout) {
  164. clearTimeout(this.focusTimeout);
  165. this.focusTimeout = null;
  166. }
  167. }
  168. }, {
  169. key: 'createButtonChildren',
  170. value: function createButtonChildren() {
  171. var _props2 = this.props,
  172. centerRipple = _props2.centerRipple,
  173. children = _props2.children,
  174. disabled = _props2.disabled,
  175. disableFocusRipple = _props2.disableFocusRipple,
  176. disableKeyboardFocus = _props2.disableKeyboardFocus,
  177. disableTouchRipple = _props2.disableTouchRipple,
  178. focusRippleColor = _props2.focusRippleColor,
  179. focusRippleOpacity = _props2.focusRippleOpacity,
  180. touchRippleColor = _props2.touchRippleColor,
  181. touchRippleOpacity = _props2.touchRippleOpacity;
  182. var isKeyboardFocused = this.state.isKeyboardFocused;
  183. // Focus Ripple
  184. var focusRipple = isKeyboardFocused && !disabled && !disableFocusRipple && !disableKeyboardFocus ? _react2.default.createElement(_FocusRipple2.default, {
  185. color: focusRippleColor,
  186. opacity: focusRippleOpacity,
  187. show: isKeyboardFocused,
  188. style: {
  189. overflow: 'hidden'
  190. },
  191. key: 'focusRipple'
  192. }) : undefined;
  193. // Touch Ripple
  194. var touchRipple = !disabled && !disableTouchRipple ? _react2.default.createElement(
  195. _TouchRipple2.default,
  196. {
  197. centerRipple: centerRipple,
  198. color: touchRippleColor,
  199. opacity: touchRippleOpacity,
  200. key: 'touchRipple'
  201. },
  202. children
  203. ) : undefined;
  204. return [focusRipple, touchRipple, touchRipple ? undefined : children];
  205. }
  206. }, {
  207. key: 'render',
  208. value: function render() {
  209. var _this2 = this;
  210. var _props3 = this.props,
  211. centerRipple = _props3.centerRipple,
  212. children = _props3.children,
  213. containerElement = _props3.containerElement,
  214. disabled = _props3.disabled,
  215. disableFocusRipple = _props3.disableFocusRipple,
  216. disableKeyboardFocus = _props3.disableKeyboardFocus,
  217. disableTouchRipple = _props3.disableTouchRipple,
  218. focusRippleColor = _props3.focusRippleColor,
  219. focusRippleOpacity = _props3.focusRippleOpacity,
  220. href = _props3.href,
  221. keyboardFocused = _props3.keyboardFocused,
  222. touchRippleColor = _props3.touchRippleColor,
  223. touchRippleOpacity = _props3.touchRippleOpacity,
  224. onBlur = _props3.onBlur,
  225. onClick = _props3.onClick,
  226. onFocus = _props3.onFocus,
  227. onKeyUp = _props3.onKeyUp,
  228. onKeyDown = _props3.onKeyDown,
  229. onKeyboardFocus = _props3.onKeyboardFocus,
  230. style = _props3.style,
  231. tabIndex = _props3.tabIndex,
  232. type = _props3.type,
  233. other = (0, _objectWithoutProperties3.default)(_props3, ['centerRipple', 'children', 'containerElement', 'disabled', 'disableFocusRipple', 'disableKeyboardFocus', 'disableTouchRipple', 'focusRippleColor', 'focusRippleOpacity', 'href', 'keyboardFocused', 'touchRippleColor', 'touchRippleOpacity', 'onBlur', 'onClick', 'onFocus', 'onKeyUp', 'onKeyDown', 'onKeyboardFocus', 'style', 'tabIndex', 'type']);
  234. var _context$muiTheme = this.context.muiTheme,
  235. prepareStyles = _context$muiTheme.prepareStyles,
  236. enhancedButton = _context$muiTheme.enhancedButton;
  237. var mergedStyles = (0, _simpleAssign2.default)({
  238. border: 10,
  239. boxSizing: 'border-box',
  240. display: 'inline-block',
  241. fontFamily: this.context.muiTheme.baseTheme.fontFamily,
  242. WebkitTapHighlightColor: enhancedButton.tapHighlightColor, // Remove mobile color flashing (deprecated)
  243. cursor: disabled ? 'default' : 'pointer',
  244. textDecoration: 'none',
  245. margin: 0,
  246. padding: 0,
  247. outline: 'none',
  248. fontSize: 'inherit',
  249. fontWeight: 'inherit',
  250. position: 'relative', // This is needed so that ripples do not bleed past border radius.
  251. verticalAlign: href ? 'middle' : null
  252. }, style);
  253. // Passing both background:none & backgroundColor can break due to object iteration order
  254. if (!mergedStyles.backgroundColor && !mergedStyles.background) {
  255. mergedStyles.background = 'none';
  256. }
  257. if (disabled && href) {
  258. return _react2.default.createElement(
  259. 'span',
  260. (0, _extends3.default)({}, other, {
  261. style: mergedStyles
  262. }),
  263. children
  264. );
  265. }
  266. var buttonProps = (0, _extends3.default)({}, other, {
  267. style: prepareStyles(mergedStyles),
  268. ref: function ref(node) {
  269. return _this2.button = node;
  270. },
  271. disabled: disabled,
  272. onBlur: this.handleBlur,
  273. onFocus: this.handleFocus,
  274. onKeyUp: this.handleKeyUp,
  275. onKeyDown: this.handleKeyDown,
  276. onClick: this.handleClick,
  277. tabIndex: disabled || disableKeyboardFocus ? -1 : tabIndex
  278. });
  279. if (href) buttonProps.href = href;
  280. var buttonChildren = this.createButtonChildren();
  281. if (_react2.default.isValidElement(containerElement)) {
  282. return _react2.default.cloneElement(containerElement, buttonProps, buttonChildren);
  283. }
  284. if (!href && containerElement === 'button') {
  285. buttonProps.type = type;
  286. }
  287. return _react2.default.createElement(href ? 'a' : containerElement, buttonProps, buttonChildren);
  288. }
  289. }]);
  290. return EnhancedButton;
  291. }(_react.Component);
  292. EnhancedButton.defaultProps = {
  293. containerElement: 'button',
  294. onBlur: function onBlur() {},
  295. onClick: function onClick() {},
  296. onFocus: function onFocus() {},
  297. onKeyDown: function onKeyDown() {},
  298. onKeyUp: function onKeyUp() {},
  299. onKeyboardFocus: function onKeyboardFocus() {},
  300. tabIndex: 0,
  301. type: 'button'
  302. };
  303. EnhancedButton.contextTypes = {
  304. muiTheme: _propTypes2.default.object.isRequired
  305. };
  306. EnhancedButton.propTypes = process.env.NODE_ENV !== "production" ? {
  307. centerRipple: _propTypes2.default.bool,
  308. children: _propTypes2.default.node,
  309. containerElement: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.element]),
  310. disableFocusRipple: _propTypes2.default.bool,
  311. disableKeyboardFocus: _propTypes2.default.bool,
  312. disableTouchRipple: _propTypes2.default.bool,
  313. disabled: _propTypes2.default.bool,
  314. focusRippleColor: _propTypes2.default.string,
  315. focusRippleOpacity: _propTypes2.default.number,
  316. href: _propTypes2.default.string,
  317. keyboardFocused: _propTypes2.default.bool,
  318. onBlur: _propTypes2.default.func,
  319. onClick: _propTypes2.default.func,
  320. onFocus: _propTypes2.default.func,
  321. onKeyDown: _propTypes2.default.func,
  322. onKeyUp: _propTypes2.default.func,
  323. onKeyboardFocus: _propTypes2.default.func,
  324. style: _propTypes2.default.object,
  325. tabIndex: _propTypes2.default.number,
  326. touchRippleColor: _propTypes2.default.string,
  327. touchRippleOpacity: _propTypes2.default.number,
  328. type: _propTypes2.default.string
  329. } : {};
  330. exports.default = EnhancedButton;