Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

346 rader
12 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 _FlatButtonLabel = require('./FlatButtonLabel');
  31. var _FlatButtonLabel2 = _interopRequireDefault(_FlatButtonLabel);
  32. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  33. function validateLabel(props, propName, componentName) {
  34. if (process.env.NODE_ENV !== 'production') {
  35. if (!props.children && props.label !== 0 && !props.label && !props.icon) {
  36. return new Error('Required prop label or children or icon was not specified in ' + componentName + '.');
  37. }
  38. }
  39. }
  40. var FlatButton = function (_Component) {
  41. (0, _inherits3.default)(FlatButton, _Component);
  42. function FlatButton() {
  43. var _ref;
  44. var _temp, _this, _ret;
  45. (0, _classCallCheck3.default)(this, FlatButton);
  46. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  47. args[_key] = arguments[_key];
  48. }
  49. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = FlatButton.__proto__ || (0, _getPrototypeOf2.default)(FlatButton)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  50. hovered: false,
  51. isKeyboardFocused: false,
  52. touch: false
  53. }, _this.handleKeyboardFocus = function (event, isKeyboardFocused) {
  54. _this.setState({ isKeyboardFocused: isKeyboardFocused });
  55. _this.props.onKeyboardFocus(event, isKeyboardFocused);
  56. }, _this.handleMouseEnter = function (event) {
  57. // Cancel hover styles for touch devices
  58. if (!_this.state.touch) _this.setState({ hovered: true });
  59. _this.props.onMouseEnter(event);
  60. }, _this.handleMouseLeave = function (event) {
  61. _this.setState({ hovered: false });
  62. _this.props.onMouseLeave(event);
  63. }, _this.handleTouchStart = function (event) {
  64. _this.setState({ touch: true });
  65. _this.props.onTouchStart(event);
  66. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  67. }
  68. (0, _createClass3.default)(FlatButton, [{
  69. key: 'componentWillReceiveProps',
  70. value: function componentWillReceiveProps(nextProps) {
  71. if (nextProps.disabled) {
  72. this.setState({
  73. hovered: false
  74. });
  75. }
  76. }
  77. }, {
  78. key: 'render',
  79. value: function render() {
  80. var _props = this.props,
  81. backgroundColor = _props.backgroundColor,
  82. children = _props.children,
  83. disabled = _props.disabled,
  84. fullWidth = _props.fullWidth,
  85. hoverColor = _props.hoverColor,
  86. icon = _props.icon,
  87. label = _props.label,
  88. labelStyle = _props.labelStyle,
  89. labelPosition = _props.labelPosition,
  90. primary = _props.primary,
  91. rippleColor = _props.rippleColor,
  92. secondary = _props.secondary,
  93. style = _props.style,
  94. other = (0, _objectWithoutProperties3.default)(_props, ['backgroundColor', 'children', 'disabled', 'fullWidth', 'hoverColor', 'icon', 'label', 'labelStyle', 'labelPosition', 'primary', 'rippleColor', 'secondary', 'style']);
  95. var _context$muiTheme = this.context.muiTheme,
  96. borderRadius = _context$muiTheme.borderRadius,
  97. _context$muiTheme$but = _context$muiTheme.button,
  98. buttonHeight = _context$muiTheme$but.height,
  99. buttonMinWidth = _context$muiTheme$but.minWidth,
  100. buttonTextTransform = _context$muiTheme$but.textTransform,
  101. _context$muiTheme$fla = _context$muiTheme.flatButton,
  102. buttonFilterColor = _context$muiTheme$fla.buttonFilterColor,
  103. buttonColor = _context$muiTheme$fla.color,
  104. disabledTextColor = _context$muiTheme$fla.disabledTextColor,
  105. fontSize = _context$muiTheme$fla.fontSize,
  106. fontWeight = _context$muiTheme$fla.fontWeight,
  107. primaryTextColor = _context$muiTheme$fla.primaryTextColor,
  108. secondaryTextColor = _context$muiTheme$fla.secondaryTextColor,
  109. textColor = _context$muiTheme$fla.textColor,
  110. _context$muiTheme$fla2 = _context$muiTheme$fla.textTransform,
  111. textTransform = _context$muiTheme$fla2 === undefined ? buttonTextTransform || 'uppercase' : _context$muiTheme$fla2;
  112. var defaultTextColor = disabled ? disabledTextColor : primary ? primaryTextColor : secondary ? secondaryTextColor : textColor;
  113. var defaultHoverColor = (0, _colorManipulator.fade)(buttonFilterColor, 0.2);
  114. var defaultRippleColor = buttonFilterColor;
  115. var buttonHoverColor = hoverColor || defaultHoverColor;
  116. var buttonRippleColor = rippleColor || defaultRippleColor;
  117. var buttonBackgroundColor = backgroundColor || buttonColor;
  118. var hovered = (this.state.hovered || this.state.isKeyboardFocused) && !disabled;
  119. var mergedRootStyles = (0, _simpleAssign2.default)({}, {
  120. height: buttonHeight,
  121. lineHeight: buttonHeight + 'px',
  122. minWidth: fullWidth ? '100%' : buttonMinWidth,
  123. color: defaultTextColor,
  124. transition: _transitions2.default.easeOut(),
  125. borderRadius: borderRadius,
  126. userSelect: 'none',
  127. overflow: 'hidden',
  128. backgroundColor: hovered ? buttonHoverColor : buttonBackgroundColor,
  129. padding: 0,
  130. margin: 0,
  131. textAlign: 'center'
  132. }, style);
  133. var iconCloned = void 0;
  134. var labelStyleIcon = {};
  135. if (icon) {
  136. var iconStyles = (0, _simpleAssign2.default)({
  137. verticalAlign: 'middle',
  138. marginLeft: label && labelPosition !== 'before' ? 12 : 0,
  139. marginRight: label && labelPosition === 'before' ? 12 : 0
  140. }, icon.props.style);
  141. iconCloned = _react2.default.cloneElement(icon, {
  142. color: icon.props.color || mergedRootStyles.color,
  143. style: iconStyles,
  144. key: 'iconCloned'
  145. });
  146. if (labelPosition === 'before') {
  147. labelStyleIcon.paddingRight = 8;
  148. } else {
  149. labelStyleIcon.paddingLeft = 8;
  150. }
  151. }
  152. var mergedLabelStyles = (0, _simpleAssign2.default)({
  153. letterSpacing: 0,
  154. textTransform: textTransform,
  155. fontWeight: fontWeight,
  156. fontSize: fontSize
  157. }, labelStyleIcon, labelStyle);
  158. var labelElement = label ? _react2.default.createElement(_FlatButtonLabel2.default, { key: 'labelElement', label: label, style: mergedLabelStyles }) : undefined;
  159. // Place label before or after children.
  160. var enhancedButtonChildren = labelPosition === 'before' ? [labelElement, iconCloned, children] : [children, iconCloned, labelElement];
  161. return _react2.default.createElement(
  162. _EnhancedButton2.default,
  163. (0, _extends3.default)({}, other, {
  164. disabled: disabled,
  165. focusRippleColor: buttonRippleColor,
  166. focusRippleOpacity: 0.3,
  167. onKeyboardFocus: this.handleKeyboardFocus,
  168. onMouseLeave: this.handleMouseLeave,
  169. onMouseEnter: this.handleMouseEnter,
  170. onTouchStart: this.handleTouchStart,
  171. style: mergedRootStyles,
  172. touchRippleColor: buttonRippleColor,
  173. touchRippleOpacity: 0.3
  174. }),
  175. enhancedButtonChildren
  176. );
  177. }
  178. }]);
  179. return FlatButton;
  180. }(_react.Component);
  181. FlatButton.muiName = 'FlatButton';
  182. FlatButton.defaultProps = {
  183. disabled: false,
  184. fullWidth: false,
  185. labelStyle: {},
  186. labelPosition: 'after',
  187. onKeyboardFocus: function onKeyboardFocus() {},
  188. onMouseEnter: function onMouseEnter() {},
  189. onMouseLeave: function onMouseLeave() {},
  190. onTouchStart: function onTouchStart() {},
  191. primary: false,
  192. secondary: false
  193. };
  194. FlatButton.contextTypes = {
  195. muiTheme: _propTypes2.default.object.isRequired
  196. };
  197. FlatButton.propTypes = process.env.NODE_ENV !== "production" ? {
  198. /**
  199. * Color of button when mouse is not hovering over it.
  200. */
  201. backgroundColor: _propTypes2.default.string,
  202. /**
  203. * This is what will be displayed inside the button.
  204. * If a label is specified, the text within the label prop will
  205. * be displayed. Otherwise, the component will expect children
  206. * which will then be displayed. (In our example,
  207. * we are nesting an `<input type="file" />` and a `span`
  208. * that acts as our label to be displayed.) This only
  209. * applies to flat and raised buttons.
  210. */
  211. children: _propTypes2.default.node,
  212. /**
  213. * The CSS class name of the root element.
  214. */
  215. className: _propTypes2.default.string,
  216. /**
  217. * The element to use as the container for the FlatButton. Either a string to
  218. * use a DOM element or a ReactElement. This is useful for wrapping the
  219. * FlatButton in a custom Link component. If a ReactElement is given, ensure
  220. * that it passes all of its given props through to the underlying DOM
  221. * element and renders its children prop for proper integration.
  222. */
  223. containerElement: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.element]),
  224. /**
  225. * If true, the element's ripple effect will be disabled.
  226. */
  227. disableTouchRipple: _propTypes2.default.bool,
  228. /**
  229. * Disables the button if set to true.
  230. */
  231. disabled: _propTypes2.default.bool,
  232. /**
  233. * If true, the button will take up the full width of its container.
  234. */
  235. fullWidth: _propTypes2.default.bool,
  236. /**
  237. * Color of button when mouse hovers over.
  238. */
  239. hoverColor: _propTypes2.default.string,
  240. /**
  241. * The URL to link to when the button is clicked.
  242. */
  243. href: _propTypes2.default.string,
  244. /**
  245. * Use this property to display an icon.
  246. */
  247. icon: _propTypes2.default.node,
  248. /**
  249. * Label for the button.
  250. */
  251. label: validateLabel,
  252. /**
  253. * Place label before or after the passed children.
  254. */
  255. labelPosition: _propTypes2.default.oneOf(['before', 'after']),
  256. /**
  257. * Override the inline-styles of the button's label element.
  258. */
  259. labelStyle: _propTypes2.default.object,
  260. /**
  261. * Callback function fired when the button is clicked.
  262. *
  263. * @param {object} event Click event targeting the button.
  264. */
  265. onClick: _propTypes2.default.func,
  266. /**
  267. * Callback function fired when the element is focused or blurred by the keyboard.
  268. *
  269. * @param {object} event `focus` or `blur` event targeting the element.
  270. * @param {boolean} isKeyboardFocused Indicates whether the element is focused.
  271. */
  272. onKeyboardFocus: _propTypes2.default.func,
  273. /** @ignore */
  274. onMouseEnter: _propTypes2.default.func,
  275. /** @ignore */
  276. onMouseLeave: _propTypes2.default.func,
  277. /** @ignore */
  278. onTouchStart: _propTypes2.default.func,
  279. /**
  280. * If true, colors button according to
  281. * primaryTextColor from the Theme.
  282. */
  283. primary: _propTypes2.default.bool,
  284. /**
  285. * Color for the ripple after button is clicked.
  286. */
  287. rippleColor: _propTypes2.default.string,
  288. /**
  289. * If true, colors button according to secondaryTextColor from the theme.
  290. * The primary prop has precendent if set to true.
  291. */
  292. secondary: _propTypes2.default.bool,
  293. /**
  294. * Override the inline-styles of the root element.
  295. */
  296. style: _propTypes2.default.object
  297. } : {};
  298. exports.default = FlatButton;