UNPKG

rat-button

Version:

button component for Rax.

222 lines (184 loc) 7.27 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _class, _temp; var _ratFrame = require('rat-frame'); var _ratProvider = require('rat-provider'); var _index = require('./styles/index.js'); var _index2 = _interopRequireDefault(_index); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function mapIconSize(size) { return { large: 'small', medium: 'xs', small: 'xs' }[size]; } /** Button */ var Button = (_temp = _class = function (_Component) { _inherits(Button, _Component); function Button(props) { _classCallCheck(this, Button); var _this = _possibleConstructorReturn(this, (Button.__proto__ || Object.getPrototypeOf(Button)).call(this, props)); _this.onMouseUp = function (e) { _this.button.blur(); if (_this.props.onMouseUp) { _this.props.onMouseUp(e); } }; _this.buttonRefHandler = function (button) { _this.button = button; }; _this.onMouseOver = function (e) { _this.setState({ hover: true }); }; _this.onMouseLeave = function (e) { _this.setState({ hover: false }); }; _this.state = { hover: false }; return _this; } _createClass(Button, [{ key: 'render', value: function render() { var _props = this.props, type = _props.type, size = _props.size, htmlType = _props.htmlType, loading = _props.loading, text = _props.text, warning = _props.warning, component = _props.component, iconSize = _props.iconSize, children = _props.children, disabled = _props.disabled, style = _props.style, styles = _props.themeStyle, others = _objectWithoutProperties(_props, ['type', 'size', 'htmlType', 'loading', 'text', 'warning', 'component', 'iconSize', 'children', 'disabled', 'style', 'themeStyle']); var hover = this.state.hover; var sizeStyle = styles[size]; var typeStyle = _extends({}, styles['btn'], styles[type]); var colStyle = {}; if (text) { typeStyle = _extends({}, styles['text-size'], styles['text' + type]); // styles['text' + type] } /*if (warning) { }*/ if (disabled) { typeStyle = styles.disabled; } // hover if (hover) { typeStyle = _extends({}, typeStyle, styles[type + '-hover']); } colStyle = _extends({}, sizeStyle, typeStyle, style); var count = _ratFrame.Children.count(children); var clonedChildren = _ratFrame.Children.map(children, function (child, index) { if (child && typeof child.type === 'function' && child.type._typeMark === 'icon') { return React.cloneElement(child, { size: iconSize || mapIconSize(size) }); } return child; }); var TagName = component; var tagAttrs = _extends({}, others, { type: htmlType, style: colStyle }); if (TagName === 'a') { delete tagAttrs.type; if (disabled) { delete tagAttrs.onClick; // a 标签的 onClick 浏览器默认不会禁用 tagAttrs.href && delete tagAttrs.href; // a 标签在禁用状态下无跳转 } } return (0, _ratFrame.createElement)( TagName, _extends({}, tagAttrs, { type: htmlType, onMouseOver: this.onMouseOver, onMouseLeave: this.onMouseLeave, onMouseUp: this.onMouseUp, ref: this.buttonRefHandler, role: 'button' }), clonedChildren ); } }]); return Button; }(_ratFrame.Component), _class.propTypes = { /** * 按钮的类型 */ type: _ratFrame.PropTypes.oneOf(['primary', 'secondary', 'normal']), /** * 按钮的尺寸 */ size: _ratFrame.PropTypes.oneOf(['small', 'medium', 'large']), /** * 按钮中 Icon 的尺寸,用于替代 Icon 的默认大小 */ iconSize: _ratFrame.PropTypes.oneOf(['xxs', 'xs', 'small', 'medium', 'large', 'xl', 'xxl', 'xxxl']), /** * 当 component = 'button' 时,设置 button 标签的 type 值 */ htmlType: _ratFrame.PropTypes.oneOf(['submit', 'reset', 'button']), /** * 设置标签类型 */ component: _ratFrame.PropTypes.oneOf(['button', 'a']), /** * 设置按钮的载入状态 */ loading: _ratFrame.PropTypes.bool, /** * 是否为文本按钮 */ text: _ratFrame.PropTypes.bool, /** * 是否为警告按钮 */ warning: _ratFrame.PropTypes.bool, /** * 是否禁用 */ disabled: _ratFrame.PropTypes.bool, /** * 点击按钮的回调 * @param {Object} e Event Object */ onClick: _ratFrame.PropTypes.func, onMouseUp: _ratFrame.PropTypes.func, children: _ratFrame.PropTypes.node }, _class.state = { hover: false }, _class.defaultProps = { type: 'normal', size: 'medium', htmlType: 'button', component: 'button', loading: false, text: false, warning: false, disabled: false, onClick: function onClick() {} }, _temp); Button.displayName = 'Button'; var StyledButton = (0, _ratProvider.ConnectStyle)(_index2.default)(Button); exports.default = StyledButton; module.exports = exports['default'];