324 lines
11 KiB
JavaScript
324 lines
11 KiB
JavaScript
'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 _transitions = require('../styles/transitions');
|
|
|
|
var _transitions2 = _interopRequireDefault(_transitions);
|
|
|
|
var _childUtils = require('../utils/childUtils');
|
|
|
|
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,
|
|
children = _props.children,
|
|
disabled = _props.disabled,
|
|
hoverColor = _props.hoverColor,
|
|
backgroundColor = _props.backgroundColor,
|
|
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, ['children', 'disabled', 'hoverColor', 'backgroundColor', 'icon', 'label', 'labelStyle', 'labelPosition', 'primary', 'rippleColor', 'secondary', 'style']);
|
|
var _context$muiTheme = this.context.muiTheme,
|
|
_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: buttonMinWidth,
|
|
color: defaultTextColor,
|
|
transition: _transitions2.default.easeOut(),
|
|
borderRadius: 2,
|
|
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
|
|
});
|
|
|
|
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, { label: label, style: mergedLabelStyles }) : undefined;
|
|
|
|
// Place label before or after children.
|
|
var childrenFragment = labelPosition === 'before' ? {
|
|
labelElement: labelElement,
|
|
iconCloned: iconCloned,
|
|
children: children
|
|
} : {
|
|
children: children,
|
|
iconCloned: iconCloned,
|
|
labelElement: labelElement
|
|
};
|
|
|
|
var enhancedButtonChildren = (0, _childUtils.createChildFragment)(childrenFragment);
|
|
|
|
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,
|
|
labelStyle: {},
|
|
labelPosition: 'after',
|
|
onKeyboardFocus: function onKeyboardFocus() {},
|
|
onMouseEnter: function onMouseEnter() {},
|
|
onMouseLeave: function onMouseLeave() {},
|
|
onTouchStart: function onTouchStart() {},
|
|
primary: false,
|
|
secondary: false
|
|
};
|
|
FlatButton.contextTypes = {
|
|
muiTheme: _react.PropTypes.object.isRequired
|
|
};
|
|
process.env.NODE_ENV !== "production" ? FlatButton.propTypes = {
|
|
/**
|
|
* Color of button when mouse is not hovering over it.
|
|
*/
|
|
backgroundColor: _react.PropTypes.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: _react.PropTypes.node,
|
|
/**
|
|
* Disables the button if set to true.
|
|
*/
|
|
disabled: _react.PropTypes.bool,
|
|
/**
|
|
* Color of button when mouse hovers over.
|
|
*/
|
|
hoverColor: _react.PropTypes.string,
|
|
/**
|
|
* The URL to link to when the button is clicked.
|
|
*/
|
|
href: _react.PropTypes.string,
|
|
/**
|
|
* Use this property to display an icon.
|
|
*/
|
|
icon: _react.PropTypes.node,
|
|
/**
|
|
* Label for the button.
|
|
*/
|
|
label: validateLabel,
|
|
/**
|
|
* Place label before or after the passed children.
|
|
*/
|
|
labelPosition: _react.PropTypes.oneOf(['before', 'after']),
|
|
/**
|
|
* Override the inline-styles of the button's label element.
|
|
*/
|
|
labelStyle: _react.PropTypes.object,
|
|
/**
|
|
* 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: _react.PropTypes.func,
|
|
/** @ignore */
|
|
onMouseEnter: _react.PropTypes.func,
|
|
/** @ignore */
|
|
onMouseLeave: _react.PropTypes.func,
|
|
/** @ignore */
|
|
onTouchStart: _react.PropTypes.func,
|
|
/**
|
|
* If true, colors button according to
|
|
* primaryTextColor from the Theme.
|
|
*/
|
|
primary: _react.PropTypes.bool,
|
|
/**
|
|
* Color for the ripple after button is clicked.
|
|
*/
|
|
rippleColor: _react.PropTypes.string,
|
|
/**
|
|
* If true, colors button according to secondaryTextColor from the theme.
|
|
* The primary prop has precendent if set to true.
|
|
*/
|
|
secondary: _react.PropTypes.bool,
|
|
/**
|
|
* Override the inline-styles of the root element.
|
|
*/
|
|
style: _react.PropTypes.object
|
|
} : void 0;
|
|
exports.default = FlatButton; |