No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

IconButton.js 12 KiB

hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 _transitions = require('../styles/transitions');
  26. var _transitions2 = _interopRequireDefault(_transitions);
  27. var _propTypes3 = require('../utils/propTypes');
  28. var _propTypes4 = _interopRequireDefault(_propTypes3);
  29. var _EnhancedButton = require('../internal/EnhancedButton');
  30. var _EnhancedButton2 = _interopRequireDefault(_EnhancedButton);
  31. var _FontIcon = require('../FontIcon');
  32. var _FontIcon2 = _interopRequireDefault(_FontIcon);
  33. var _Tooltip = require('../internal/Tooltip');
  34. var _Tooltip2 = _interopRequireDefault(_Tooltip);
  35. var _childUtils = require('../utils/childUtils');
  36. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  37. function getStyles(props, context) {
  38. var baseTheme = context.muiTheme.baseTheme;
  39. return {
  40. root: {
  41. boxSizing: 'border-box',
  42. overflow: 'visible',
  43. transition: _transitions2.default.easeOut(),
  44. padding: baseTheme.spacing.iconSize / 2,
  45. width: baseTheme.spacing.iconSize * 2,
  46. height: baseTheme.spacing.iconSize * 2,
  47. fontSize: 0
  48. },
  49. tooltip: {
  50. boxSizing: 'border-box'
  51. },
  52. disabled: {
  53. color: baseTheme.palette.disabledColor,
  54. fill: baseTheme.palette.disabledColor,
  55. cursor: 'default'
  56. }
  57. };
  58. }
  59. var IconButton = function (_Component) {
  60. (0, _inherits3.default)(IconButton, _Component);
  61. function IconButton() {
  62. var _ref;
  63. var _temp, _this, _ret;
  64. (0, _classCallCheck3.default)(this, IconButton);
  65. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  66. args[_key] = arguments[_key];
  67. }
  68. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = IconButton.__proto__ || (0, _getPrototypeOf2.default)(IconButton)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  69. hovered: false,
  70. isKeyboardFocused: false,
  71. // Not to be confonded with the touch property.
  72. // This state is to determined if it's a mobile device.
  73. touch: false,
  74. tooltipShown: false
  75. }, _this.handleBlur = function (event) {
  76. _this.hideTooltip();
  77. if (_this.props.onBlur) {
  78. _this.props.onBlur(event);
  79. }
  80. }, _this.handleFocus = function (event) {
  81. _this.showTooltip();
  82. if (_this.props.onFocus) {
  83. _this.props.onFocus(event);
  84. }
  85. }, _this.handleMouseLeave = function (event) {
  86. if (!_this.button.isKeyboardFocused()) {
  87. _this.hideTooltip();
  88. }
  89. _this.setState({ hovered: false });
  90. if (_this.props.onMouseLeave) {
  91. _this.props.onMouseLeave(event);
  92. }
  93. }, _this.handleMouseOut = function (event) {
  94. if (_this.props.disabled) _this.hideTooltip();
  95. if (_this.props.onMouseOut) _this.props.onMouseOut(event);
  96. }, _this.handleMouseEnter = function (event) {
  97. _this.showTooltip();
  98. // Cancel hover styles for touch devices
  99. if (!_this.state.touch) {
  100. _this.setState({ hovered: true });
  101. }
  102. if (_this.props.onMouseEnter) {
  103. _this.props.onMouseEnter(event);
  104. }
  105. }, _this.handleTouchStart = function (event) {
  106. _this.setState({ touch: true });
  107. if (_this.props.onTouchStart) {
  108. _this.props.onTouchStart(event);
  109. }
  110. }, _this.handleKeyboardFocus = function (event, isKeyboardFocused) {
  111. var _this$props = _this.props,
  112. disabled = _this$props.disabled,
  113. onFocus = _this$props.onFocus,
  114. onBlur = _this$props.onBlur,
  115. onKeyboardFocus = _this$props.onKeyboardFocus;
  116. if (isKeyboardFocused && !disabled) {
  117. _this.showTooltip();
  118. if (onFocus) {
  119. onFocus(event);
  120. }
  121. } else {
  122. _this.hideTooltip();
  123. if (onBlur) {
  124. onBlur(event);
  125. }
  126. }
  127. _this.setState({ isKeyboardFocused: isKeyboardFocused });
  128. if (onKeyboardFocus) {
  129. onKeyboardFocus(event, isKeyboardFocused);
  130. }
  131. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  132. }
  133. (0, _createClass3.default)(IconButton, [{
  134. key: 'componentWillReceiveProps',
  135. value: function componentWillReceiveProps(nextProps) {
  136. if (nextProps.disabled) {
  137. this.setState({ hovered: false });
  138. }
  139. }
  140. }, {
  141. key: 'setKeyboardFocus',
  142. value: function setKeyboardFocus() {
  143. this.button.setKeyboardFocus();
  144. }
  145. }, {
  146. key: 'showTooltip',
  147. value: function showTooltip() {
  148. if (this.props.tooltip) {
  149. this.setState({ tooltipShown: true });
  150. }
  151. }
  152. }, {
  153. key: 'hideTooltip',
  154. value: function hideTooltip() {
  155. if (this.props.tooltip) this.setState({ tooltipShown: false });
  156. }
  157. }, {
  158. key: 'render',
  159. value: function render() {
  160. var _this2 = this;
  161. var _props = this.props,
  162. disabled = _props.disabled,
  163. hoveredStyle = _props.hoveredStyle,
  164. disableTouchRipple = _props.disableTouchRipple,
  165. children = _props.children,
  166. iconClassName = _props.iconClassName,
  167. style = _props.style,
  168. tooltip = _props.tooltip,
  169. tooltipPositionProp = _props.tooltipPosition,
  170. tooltipStyles = _props.tooltipStyles,
  171. touch = _props.touch,
  172. iconStyle = _props.iconStyle,
  173. other = (0, _objectWithoutProperties3.default)(_props, ['disabled', 'hoveredStyle', 'disableTouchRipple', 'children', 'iconClassName', 'style', 'tooltip', 'tooltipPosition', 'tooltipStyles', 'touch', 'iconStyle']);
  174. var fonticon = void 0;
  175. var styles = getStyles(this.props, this.context);
  176. var tooltipPosition = tooltipPositionProp.split('-');
  177. var hovered = (this.state.hovered || this.state.isKeyboardFocused) && !disabled;
  178. var mergedRootStyles = (0, _simpleAssign2.default)(styles.root, style, hovered ? hoveredStyle : {});
  179. var tooltipElement = tooltip ? _react2.default.createElement(_Tooltip2.default, {
  180. label: tooltip,
  181. show: this.state.tooltipShown,
  182. touch: touch,
  183. style: (0, _simpleAssign2.default)(styles.tooltip, tooltipStyles),
  184. verticalPosition: tooltipPosition[0],
  185. horizontalPosition: tooltipPosition[1]
  186. }) : null;
  187. if (iconClassName) {
  188. var iconHoverColor = iconStyle.iconHoverColor,
  189. iconStyleFontIcon = (0, _objectWithoutProperties3.default)(iconStyle, ['iconHoverColor']);
  190. fonticon = _react2.default.createElement(
  191. _FontIcon2.default,
  192. {
  193. className: iconClassName,
  194. hoverColor: disabled ? null : iconHoverColor,
  195. style: (0, _simpleAssign2.default)({}, disabled && styles.disabled, iconStyleFontIcon),
  196. color: this.context.muiTheme.baseTheme.palette.textColor
  197. },
  198. children
  199. );
  200. }
  201. var childrenStyle = disabled ? (0, _simpleAssign2.default)({}, iconStyle, styles.disabled) : iconStyle;
  202. return _react2.default.createElement(
  203. _EnhancedButton2.default,
  204. (0, _extends3.default)({
  205. ref: function ref(_ref2) {
  206. return _this2.button = _ref2;
  207. }
  208. }, other, {
  209. centerRipple: true,
  210. disabled: disabled,
  211. onTouchStart: this.handleTouchStart,
  212. style: mergedRootStyles,
  213. disableTouchRipple: disableTouchRipple,
  214. onBlur: this.handleBlur,
  215. onFocus: this.handleFocus,
  216. onMouseLeave: this.handleMouseLeave,
  217. onMouseEnter: this.handleMouseEnter,
  218. onMouseOut: this.handleMouseOut,
  219. onKeyboardFocus: this.handleKeyboardFocus
  220. }),
  221. tooltipElement,
  222. fonticon,
  223. (0, _childUtils.extendChildren)(children, {
  224. style: childrenStyle
  225. })
  226. );
  227. }
  228. }]);
  229. return IconButton;
  230. }(_react.Component);
  231. IconButton.muiName = 'IconButton';
  232. IconButton.defaultProps = {
  233. disabled: false,
  234. disableTouchRipple: false,
  235. iconStyle: {},
  236. tooltipPosition: 'bottom-center',
  237. touch: false
  238. };
  239. IconButton.contextTypes = {
  240. muiTheme: _propTypes2.default.object.isRequired
  241. };
  242. IconButton.propTypes = process.env.NODE_ENV !== "production" ? {
  243. /**
  244. * Can be used to pass a `FontIcon` element as the icon for the button.
  245. */
  246. children: _propTypes2.default.node,
  247. /**
  248. * The CSS class name of the root element.
  249. */
  250. className: _propTypes2.default.string,
  251. /**
  252. * If true, the element's ripple effect will be disabled.
  253. */
  254. disableTouchRipple: _propTypes2.default.bool,
  255. /**
  256. * If true, the element will be disabled.
  257. */
  258. disabled: _propTypes2.default.bool,
  259. /**
  260. * Override the inline-styles of the root element when the component is hovered.
  261. */
  262. hoveredStyle: _propTypes2.default.object,
  263. /**
  264. * The URL to link to when the button is clicked.
  265. */
  266. href: _propTypes2.default.string,
  267. /**
  268. * The CSS class name of the icon. Used for setting the icon with a stylesheet.
  269. */
  270. iconClassName: _propTypes2.default.string,
  271. /**
  272. * Override the inline-styles of the icon element.
  273. * Note: you can specify iconHoverColor as a String inside this object.
  274. */
  275. iconStyle: _propTypes2.default.object,
  276. /** @ignore */
  277. onBlur: _propTypes2.default.func,
  278. /**
  279. * Callback function fired when the button is clicked.
  280. *
  281. * @param {object} event Click event targeting the button.
  282. */
  283. onClick: _propTypes2.default.func,
  284. /** @ignore */
  285. onFocus: _propTypes2.default.func,
  286. /**
  287. * Callback function fired when the element is focused or blurred by the keyboard.
  288. *
  289. * @param {object} event `focus` or `blur` event targeting the element.
  290. * @param {boolean} keyboardFocused Indicates whether the element is focused.
  291. */
  292. onKeyboardFocus: _propTypes2.default.func,
  293. /** @ignore */
  294. onMouseEnter: _propTypes2.default.func,
  295. /** @ignore */
  296. onMouseLeave: _propTypes2.default.func,
  297. /** @ignore */
  298. onMouseOut: _propTypes2.default.func,
  299. /** @ignore */
  300. onTouchStart: _propTypes2.default.func,
  301. /**
  302. * Override the inline-styles of the root element.
  303. */
  304. style: _propTypes2.default.object,
  305. /**
  306. * The text to supply to the element's tooltip.
  307. */
  308. tooltip: _propTypes2.default.node,
  309. /**
  310. * The vertical and horizontal positions, respectively, of the element's tooltip.
  311. * Possible values are: "bottom-center", "top-center", "bottom-right", "top-right",
  312. * "bottom-left", and "top-left".
  313. */
  314. tooltipPosition: _propTypes4.default.cornersAndCenter,
  315. /**
  316. * Override the inline-styles of the tooltip element.
  317. */
  318. tooltipStyles: _propTypes2.default.object,
  319. /**
  320. * If true, increase the tooltip element's size. Useful for increasing tooltip
  321. * readability on mobile devices.
  322. */
  323. touch: _propTypes2.default.bool
  324. } : {};
  325. exports.default = IconButton;