UNPKG

material-ui

Version:

React Components that Implement Google's Material Design.

346 lines (298 loc) 12.6 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties'); var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _simpleAssign = require('simple-assign'); var _simpleAssign2 = _interopRequireDefault(_simpleAssign); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _transitions = require('../styles/transitions'); var _transitions2 = _interopRequireDefault(_transitions); var _colorManipulator = require('../utils/colorManipulator'); var _EnhancedButton = require('../internal/EnhancedButton'); var _EnhancedButton2 = _interopRequireDefault(_EnhancedButton); var _FlatButtonLabel = require('./FlatButtonLabel'); var _FlatButtonLabel2 = _interopRequireDefault(_FlatButtonLabel); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function validateLabel(props, propName, componentName) { if (process.env.NODE_ENV !== 'production') { if (!props.children && props.label !== 0 && !props.label && !props.icon) { return new Error('Required prop label or children or icon was not specified in ' + componentName + '.'); } } } var FlatButton = function (_Component) { (0, _inherits3.default)(FlatButton, _Component); function FlatButton() { var _ref; var _temp, _this, _ret; (0, _classCallCheck3.default)(this, FlatButton); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = FlatButton.__proto__ || (0, _getPrototypeOf2.default)(FlatButton)).call.apply(_ref, [this].concat(args))), _this), _this.state = { hovered: false, isKeyboardFocused: false, touch: false }, _this.handleKeyboardFocus = function (event, isKeyboardFocused) { _this.setState({ isKeyboardFocused: isKeyboardFocused }); _this.props.onKeyboardFocus(event, isKeyboardFocused); }, _this.handleMouseEnter = function (event) { // Cancel hover styles for touch devices if (!_this.state.touch) _this.setState({ hovered: true }); _this.props.onMouseEnter(event); }, _this.handleMouseLeave = function (event) { _this.setState({ hovered: false }); _this.props.onMouseLeave(event); }, _this.handleTouchStart = function (event) { _this.setState({ touch: true }); _this.props.onTouchStart(event); }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret); } (0, _createClass3.default)(FlatButton, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if (nextProps.disabled) { this.setState({ hovered: false }); } } }, { key: 'render', value: function render() { var _props = this.props, backgroundColor = _props.backgroundColor, children = _props.children, disabled = _props.disabled, fullWidth = _props.fullWidth, hoverColor = _props.hoverColor, icon = _props.icon, label = _props.label, labelStyle = _props.labelStyle, labelPosition = _props.labelPosition, primary = _props.primary, rippleColor = _props.rippleColor, secondary = _props.secondary, style = _props.style, other = (0, _objectWithoutProperties3.default)(_props, ['backgroundColor', 'children', 'disabled', 'fullWidth', 'hoverColor', 'icon', 'label', 'labelStyle', 'labelPosition', 'primary', 'rippleColor', 'secondary', 'style']); var _context$muiTheme = this.context.muiTheme, borderRadius = _context$muiTheme.borderRadius, _context$muiTheme$but = _context$muiTheme.button, buttonHeight = _context$muiTheme$but.height, buttonMinWidth = _context$muiTheme$but.minWidth, buttonTextTransform = _context$muiTheme$but.textTransform, _context$muiTheme$fla = _context$muiTheme.flatButton, buttonFilterColor = _context$muiTheme$fla.buttonFilterColor, buttonColor = _context$muiTheme$fla.color, disabledTextColor = _context$muiTheme$fla.disabledTextColor, fontSize = _context$muiTheme$fla.fontSize, fontWeight = _context$muiTheme$fla.fontWeight, primaryTextColor = _context$muiTheme$fla.primaryTextColor, secondaryTextColor = _context$muiTheme$fla.secondaryTextColor, textColor = _context$muiTheme$fla.textColor, _context$muiTheme$fla2 = _context$muiTheme$fla.textTransform, textTransform = _context$muiTheme$fla2 === undefined ? buttonTextTransform || 'uppercase' : _context$muiTheme$fla2; var defaultTextColor = disabled ? disabledTextColor : primary ? primaryTextColor : secondary ? secondaryTextColor : textColor; var defaultHoverColor = (0, _colorManipulator.fade)(buttonFilterColor, 0.2); var defaultRippleColor = buttonFilterColor; var buttonHoverColor = hoverColor || defaultHoverColor; var buttonRippleColor = rippleColor || defaultRippleColor; var buttonBackgroundColor = backgroundColor || buttonColor; var hovered = (this.state.hovered || this.state.isKeyboardFocused) && !disabled; var mergedRootStyles = (0, _simpleAssign2.default)({}, { height: buttonHeight, lineHeight: buttonHeight + 'px', minWidth: fullWidth ? '100%' : buttonMinWidth, color: defaultTextColor, transition: _transitions2.default.easeOut(), borderRadius: borderRadius, userSelect: 'none', overflow: 'hidden', backgroundColor: hovered ? buttonHoverColor : buttonBackgroundColor, padding: 0, margin: 0, textAlign: 'center' }, style); var iconCloned = void 0; var labelStyleIcon = {}; if (icon) { var iconStyles = (0, _simpleAssign2.default)({ verticalAlign: 'middle', marginLeft: label && labelPosition !== 'before' ? 12 : 0, marginRight: label && labelPosition === 'before' ? 12 : 0 }, icon.props.style); iconCloned = _react2.default.cloneElement(icon, { color: icon.props.color || mergedRootStyles.color, style: iconStyles, key: 'iconCloned' }); if (labelPosition === 'before') { labelStyleIcon.paddingRight = 8; } else { labelStyleIcon.paddingLeft = 8; } } var mergedLabelStyles = (0, _simpleAssign2.default)({ letterSpacing: 0, textTransform: textTransform, fontWeight: fontWeight, fontSize: fontSize }, labelStyleIcon, labelStyle); var labelElement = label ? _react2.default.createElement(_FlatButtonLabel2.default, { key: 'labelElement', label: label, style: mergedLabelStyles }) : undefined; // Place label before or after children. var enhancedButtonChildren = labelPosition === 'before' ? [labelElement, iconCloned, children] : [children, iconCloned, labelElement]; return _react2.default.createElement( _EnhancedButton2.default, (0, _extends3.default)({}, other, { disabled: disabled, focusRippleColor: buttonRippleColor, focusRippleOpacity: 0.3, onKeyboardFocus: this.handleKeyboardFocus, onMouseLeave: this.handleMouseLeave, onMouseEnter: this.handleMouseEnter, onTouchStart: this.handleTouchStart, style: mergedRootStyles, touchRippleColor: buttonRippleColor, touchRippleOpacity: 0.3 }), enhancedButtonChildren ); } }]); return FlatButton; }(_react.Component); FlatButton.muiName = 'FlatButton'; FlatButton.defaultProps = { disabled: false, fullWidth: false, labelStyle: {}, labelPosition: 'after', onKeyboardFocus: function onKeyboardFocus() {}, onMouseEnter: function onMouseEnter() {}, onMouseLeave: function onMouseLeave() {}, onTouchStart: function onTouchStart() {}, primary: false, secondary: false }; FlatButton.contextTypes = { muiTheme: _propTypes2.default.object.isRequired }; FlatButton.propTypes = process.env.NODE_ENV !== "production" ? { /** * Color of button when mouse is not hovering over it. */ backgroundColor: _propTypes2.default.string, /** * This is what will be displayed inside the button. * If a label is specified, the text within the label prop will * be displayed. Otherwise, the component will expect children * which will then be displayed. (In our example, * we are nesting an `<input type="file" />` and a `span` * that acts as our label to be displayed.) This only * applies to flat and raised buttons. */ children: _propTypes2.default.node, /** * The CSS class name of the root element. */ className: _propTypes2.default.string, /** * The element to use as the container for the FlatButton. Either a string to * use a DOM element or a ReactElement. This is useful for wrapping the * FlatButton in a custom Link component. If a ReactElement is given, ensure * that it passes all of its given props through to the underlying DOM * element and renders its children prop for proper integration. */ containerElement: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.element]), /** * If true, the element's ripple effect will be disabled. */ disableTouchRipple: _propTypes2.default.bool, /** * Disables the button if set to true. */ disabled: _propTypes2.default.bool, /** * If true, the button will take up the full width of its container. */ fullWidth: _propTypes2.default.bool, /** * Color of button when mouse hovers over. */ hoverColor: _propTypes2.default.string, /** * The URL to link to when the button is clicked. */ href: _propTypes2.default.string, /** * Use this property to display an icon. */ icon: _propTypes2.default.node, /** * Label for the button. */ label: validateLabel, /** * Place label before or after the passed children. */ labelPosition: _propTypes2.default.oneOf(['before', 'after']), /** * Override the inline-styles of the button's label element. */ labelStyle: _propTypes2.default.object, /** * Callback function fired when the button is clicked. * * @param {object} event Click event targeting the button. */ onClick: _propTypes2.default.func, /** * Callback function fired when the element is focused or blurred by the keyboard. * * @param {object} event `focus` or `blur` event targeting the element. * @param {boolean} isKeyboardFocused Indicates whether the element is focused. */ onKeyboardFocus: _propTypes2.default.func, /** @ignore */ onMouseEnter: _propTypes2.default.func, /** @ignore */ onMouseLeave: _propTypes2.default.func, /** @ignore */ onTouchStart: _propTypes2.default.func, /** * If true, colors button according to * primaryTextColor from the Theme. */ primary: _propTypes2.default.bool, /** * Color for the ripple after button is clicked. */ rippleColor: _propTypes2.default.string, /** * If true, colors button according to secondaryTextColor from the theme. * The primary prop has precendent if set to true. */ secondary: _propTypes2.default.bool, /** * Override the inline-styles of the root element. */ style: _propTypes2.default.object } : {}; exports.default = FlatButton;