Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

392 строки
13 KiB

  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 _colorManipulator = require('../utils/colorManipulator');
  28. var _EnhancedButton = require('../internal/EnhancedButton');
  29. var _EnhancedButton2 = _interopRequireDefault(_EnhancedButton);
  30. var _FontIcon = require('../FontIcon');
  31. var _FontIcon2 = _interopRequireDefault(_FontIcon);
  32. var _Paper = require('../Paper');
  33. var _Paper2 = _interopRequireDefault(_Paper);
  34. var _childUtils = require('../utils/childUtils');
  35. var _warning = require('warning');
  36. var _warning2 = _interopRequireDefault(_warning);
  37. var _propTypes3 = require('../utils/propTypes');
  38. var _propTypes4 = _interopRequireDefault(_propTypes3);
  39. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  40. function getStyles(props, context) {
  41. var floatingActionButton = context.muiTheme.floatingActionButton;
  42. var backgroundColor = props.backgroundColor || floatingActionButton.color;
  43. var iconColor = floatingActionButton.iconColor;
  44. if (props.disabled) {
  45. backgroundColor = props.disabledColor || floatingActionButton.disabledColor;
  46. iconColor = floatingActionButton.disabledTextColor;
  47. } else if (props.secondary) {
  48. backgroundColor = floatingActionButton.secondaryColor;
  49. iconColor = floatingActionButton.secondaryIconColor;
  50. }
  51. return {
  52. root: {
  53. transition: _transitions2.default.easeOut(),
  54. display: 'inline-block',
  55. backgroundColor: 'transparent'
  56. },
  57. container: {
  58. backgroundColor: backgroundColor,
  59. transition: _transitions2.default.easeOut(),
  60. height: floatingActionButton.buttonSize,
  61. width: floatingActionButton.buttonSize,
  62. padding: 0,
  63. overflow: 'hidden',
  64. borderRadius: '50%',
  65. textAlign: 'center',
  66. verticalAlign: 'bottom'
  67. },
  68. containerWhenMini: {
  69. height: floatingActionButton.miniSize,
  70. width: floatingActionButton.miniSize
  71. },
  72. overlay: {
  73. transition: _transitions2.default.easeOut(),
  74. top: 0
  75. },
  76. overlayWhenHovered: {
  77. backgroundColor: (0, _colorManipulator.fade)(iconColor, 0.4)
  78. },
  79. icon: {
  80. height: floatingActionButton.buttonSize,
  81. lineHeight: floatingActionButton.buttonSize + 'px',
  82. fill: iconColor,
  83. color: iconColor
  84. },
  85. iconWhenMini: {
  86. height: floatingActionButton.miniSize,
  87. lineHeight: floatingActionButton.miniSize + 'px'
  88. }
  89. };
  90. }
  91. var FloatingActionButton = function (_Component) {
  92. (0, _inherits3.default)(FloatingActionButton, _Component);
  93. function FloatingActionButton() {
  94. var _ref;
  95. var _temp, _this, _ret;
  96. (0, _classCallCheck3.default)(this, FloatingActionButton);
  97. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  98. args[_key] = arguments[_key];
  99. }
  100. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = FloatingActionButton.__proto__ || (0, _getPrototypeOf2.default)(FloatingActionButton)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  101. hovered: false,
  102. touch: false,
  103. zDepth: undefined
  104. }, _this.handleMouseDown = function (event) {
  105. // only listen to left clicks
  106. if (event.button === 0) {
  107. _this.setState({ zDepth: _this.props.zDepth + 1 });
  108. }
  109. if (_this.props.onMouseDown) _this.props.onMouseDown(event);
  110. }, _this.handleMouseUp = function (event) {
  111. _this.setState({ zDepth: _this.props.zDepth });
  112. if (_this.props.onMouseUp) {
  113. _this.props.onMouseUp(event);
  114. }
  115. }, _this.handleMouseLeave = function (event) {
  116. if (!_this.refs.container.isKeyboardFocused()) {
  117. _this.setState({ zDepth: _this.props.zDepth, hovered: false });
  118. }
  119. if (_this.props.onMouseLeave) {
  120. _this.props.onMouseLeave(event);
  121. }
  122. }, _this.handleMouseEnter = function (event) {
  123. if (!_this.refs.container.isKeyboardFocused() && !_this.state.touch) {
  124. _this.setState({ hovered: true });
  125. }
  126. if (_this.props.onMouseEnter) {
  127. _this.props.onMouseEnter(event);
  128. }
  129. }, _this.handleTouchStart = function (event) {
  130. _this.setState({
  131. touch: true,
  132. zDepth: _this.props.zDepth + 1
  133. });
  134. if (_this.props.onTouchStart) {
  135. _this.props.onTouchStart(event);
  136. }
  137. }, _this.handleTouchEnd = function (event) {
  138. _this.setState({
  139. touch: true,
  140. zDepth: _this.props.zDepth
  141. });
  142. if (_this.props.onTouchEnd) {
  143. _this.props.onTouchEnd(event);
  144. }
  145. }, _this.handleKeyboardFocus = function (event, keyboardFocused) {
  146. if (keyboardFocused && !_this.props.disabled) {
  147. _this.setState({ zDepth: _this.props.zDepth + 1 });
  148. _this.refs.overlay.style.backgroundColor = (0, _colorManipulator.fade)(getStyles(_this.props, _this.context).icon.color, 0.4);
  149. } else if (!_this.state.hovered) {
  150. _this.setState({ zDepth: _this.props.zDepth });
  151. _this.refs.overlay.style.backgroundColor = 'transparent';
  152. }
  153. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  154. }
  155. (0, _createClass3.default)(FloatingActionButton, [{
  156. key: 'componentWillMount',
  157. value: function componentWillMount() {
  158. this.setState({
  159. zDepth: this.props.disabled ? 0 : this.props.zDepth
  160. });
  161. }
  162. }, {
  163. key: 'componentDidMount',
  164. value: function componentDidMount() {
  165. process.env.NODE_ENV !== "production" ? (0, _warning2.default)(!this.props.iconClassName || !this.props.children, 'Material-UI: You have set both an iconClassName and a child icon. ' + 'It is recommended you use only one method when adding ' + 'icons to FloatingActionButtons.') : void 0;
  166. }
  167. }, {
  168. key: 'componentWillReceiveProps',
  169. value: function componentWillReceiveProps(nextProps) {
  170. var nextState = {};
  171. if (nextProps.disabled !== this.props.disabled) {
  172. nextState.zDepth = nextProps.disabled ? 0 : this.props.zDepth;
  173. }
  174. if (nextProps.disabled) {
  175. nextState.hovered = false;
  176. }
  177. this.setState(nextState);
  178. }
  179. }, {
  180. key: 'render',
  181. value: function render() {
  182. var _props = this.props,
  183. backgroundColor = _props.backgroundColor,
  184. className = _props.className,
  185. childrenProp = _props.children,
  186. disabled = _props.disabled,
  187. disabledColor = _props.disabledColor,
  188. mini = _props.mini,
  189. secondary = _props.secondary,
  190. iconStyle = _props.iconStyle,
  191. iconClassName = _props.iconClassName,
  192. zDepth = _props.zDepth,
  193. other = (0, _objectWithoutProperties3.default)(_props, ['backgroundColor', 'className', 'children', 'disabled', 'disabledColor', 'mini', 'secondary', 'iconStyle', 'iconClassName', 'zDepth']);
  194. var prepareStyles = this.context.muiTheme.prepareStyles;
  195. var styles = getStyles(this.props, this.context);
  196. var iconElement = void 0;
  197. if (iconClassName) {
  198. iconElement = _react2.default.createElement(_FontIcon2.default, {
  199. className: iconClassName,
  200. style: (0, _simpleAssign2.default)({}, styles.icon, mini && styles.iconWhenMini, iconStyle)
  201. });
  202. }
  203. var children = void 0;
  204. if (childrenProp) {
  205. children = (0, _childUtils.extendChildren)(childrenProp, function (child) {
  206. return {
  207. style: (0, _simpleAssign2.default)({}, styles.icon, mini && styles.iconWhenMini, iconStyle, child.props.style)
  208. };
  209. });
  210. }
  211. var buttonEventHandlers = disabled ? null : {
  212. onMouseDown: this.handleMouseDown,
  213. onMouseUp: this.handleMouseUp,
  214. onMouseLeave: this.handleMouseLeave,
  215. onMouseEnter: this.handleMouseEnter,
  216. onTouchStart: this.handleTouchStart,
  217. onTouchEnd: this.handleTouchEnd,
  218. onKeyboardFocus: this.handleKeyboardFocus
  219. };
  220. return _react2.default.createElement(
  221. _Paper2.default,
  222. {
  223. className: className,
  224. style: (0, _simpleAssign2.default)(styles.root, this.props.style),
  225. zDepth: this.state.zDepth,
  226. circle: true
  227. },
  228. _react2.default.createElement(
  229. _EnhancedButton2.default,
  230. (0, _extends3.default)({}, other, buttonEventHandlers, {
  231. ref: 'container',
  232. disabled: disabled,
  233. style: (0, _simpleAssign2.default)(styles.container, this.props.mini && styles.containerWhenMini, iconStyle),
  234. focusRippleColor: styles.icon.color,
  235. touchRippleColor: styles.icon.color
  236. }),
  237. _react2.default.createElement(
  238. 'div',
  239. {
  240. ref: 'overlay',
  241. style: prepareStyles((0, _simpleAssign2.default)(styles.overlay, this.state.hovered && !this.props.disabled && styles.overlayWhenHovered))
  242. },
  243. iconElement,
  244. children
  245. )
  246. )
  247. );
  248. }
  249. }]);
  250. return FloatingActionButton;
  251. }(_react.Component);
  252. FloatingActionButton.defaultProps = {
  253. disabled: false,
  254. mini: false,
  255. secondary: false,
  256. zDepth: 2
  257. };
  258. FloatingActionButton.contextTypes = {
  259. muiTheme: _propTypes2.default.object.isRequired
  260. };
  261. FloatingActionButton.propTypes = process.env.NODE_ENV !== "production" ? {
  262. /**
  263. * This value will override the default background color for the button.
  264. * However it will not override the default disabled background color.
  265. * This has to be set separately using the disabledColor attribute.
  266. */
  267. backgroundColor: _propTypes2.default.string,
  268. /**
  269. * This is what displayed inside the floating action button; for example, a SVG Icon.
  270. */
  271. children: _propTypes2.default.node,
  272. /**
  273. * The css class name of the root element.
  274. */
  275. className: _propTypes2.default.string,
  276. /**
  277. * Disables the button if set to true.
  278. */
  279. disabled: _propTypes2.default.bool,
  280. /**
  281. * This value will override the default background color for the button when it is disabled.
  282. */
  283. disabledColor: _propTypes2.default.string,
  284. /**
  285. * The URL to link to when the button is clicked.
  286. */
  287. href: _propTypes2.default.string,
  288. /**
  289. * The icon within the FloatingActionButton is a FontIcon component.
  290. * This property is the classname of the icon to be displayed inside the button.
  291. * An alternative to adding an iconClassName would be to manually insert a
  292. * FontIcon component or custom SvgIcon component or as a child of FloatingActionButton.
  293. */
  294. iconClassName: _propTypes2.default.string,
  295. /**
  296. * This is the equivalent to iconClassName except that it is used for
  297. * overriding the inline-styles of the FontIcon component.
  298. */
  299. iconStyle: _propTypes2.default.object,
  300. /**
  301. * If true, the button will be a small floating action button.
  302. */
  303. mini: _propTypes2.default.bool,
  304. /**
  305. * Callback function fired when the button is clicked.
  306. *
  307. * @param {object} event Click event targeting the button.
  308. */
  309. onClick: _propTypes2.default.func,
  310. /** @ignore */
  311. onMouseDown: _propTypes2.default.func,
  312. /** @ignore */
  313. onMouseEnter: _propTypes2.default.func,
  314. /** @ignore */
  315. onMouseLeave: _propTypes2.default.func,
  316. /** @ignore */
  317. onMouseUp: _propTypes2.default.func,
  318. /** @ignore */
  319. onTouchEnd: _propTypes2.default.func,
  320. /** @ignore */
  321. onTouchStart: _propTypes2.default.func,
  322. /**
  323. * If true, the button will use the secondary button colors.
  324. */
  325. secondary: _propTypes2.default.bool,
  326. /**
  327. * Override the inline-styles of the root element.
  328. */
  329. style: _propTypes2.default.object,
  330. /**
  331. * The zDepth of the underlying `Paper` component.
  332. */
  333. zDepth: _propTypes4.default.zDepth
  334. } : {};
  335. exports.default = FloatingActionButton;