modo-mobile
Version:
A mobile UI toolkit, based on React
132 lines (122 loc) • 5.17 kB
JavaScript
import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
}return t;
};
import classnames from 'classnames';
import * as React from 'react';
import Icon from '../icon';
var rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
var isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
function isString(str) {
return typeof str === 'string';
}
// Insert one space between two chinese characters automatically.
function insertSpace(child, needInserted) {
// Check the child if is undefined or null.
if (child == null) {
return;
}
var SPACE = needInserted ? ' ' : '';
// strictNullChecks oops.
if (typeof child !== 'string' && typeof child !== 'number' && isString(child.type) && isTwoCNChar(child.props.children)) {
return React.cloneElement(child, {}, child.props.children.split('').join(SPACE));
}
if (typeof child === 'string') {
if (isTwoCNChar(child)) {
child = child.split('').join(SPACE);
}
return React.createElement(
'span',
null,
child
);
}
return child;
}
var Button = function (_React$PureComponent) {
_inherits(Button, _React$PureComponent);
function Button() {
_classCallCheck(this, Button);
var _this = _possibleConstructorReturn(this, (Button.__proto__ || Object.getPrototypeOf(Button)).apply(this, arguments));
_this.handleClick = function (e) {
var onClick = _this.props.onClick;
if (onClick) {
onClick(e);
}
};
return _this;
}
_createClass(Button, [{
key: 'isNeedInserted',
value: function isNeedInserted() {
var _props = this.props,
icon = _props.icon,
children = _props.children;
return React.Children.count(children) === 1 && !icon;
}
}, {
key: 'render',
value: function render() {
var _classnames,
_this2 = this;
var _a = this.props,
children = _a.children,
className = _a.className,
prefixCls = _a.prefixCls,
type = _a.type,
size = _a.size,
shape = _a.shape,
inline = _a.inline,
icon = _a.icon,
loading = _a.loading,
onClick = _a.onClick,
restProps = __rest(_a, ["children", "className", "prefixCls", "type", "size", "shape", "inline", "icon", "loading", "onClick"]);
var iconType = loading ? 'loading' : icon;
var wrapCls = classnames(prefixCls, className, (_classnames = {}, _defineProperty(_classnames, prefixCls + '-' + type, type), _defineProperty(_classnames, prefixCls + '-' + shape, shape), _defineProperty(_classnames, prefixCls + '-sm', size === 'small'), _defineProperty(_classnames, prefixCls + '-inline', inline || shape), _defineProperty(_classnames, prefixCls + '-loading', loading), _defineProperty(_classnames, prefixCls + '-icon', !!iconType), _classnames));
var iconNode = iconType ? React.createElement(Icon, { type: iconType }) : null;
var kids = children || children === 0 ? React.Children.map(children, function (child) {
return insertSpace(child, _this2.isNeedInserted());
}) : null;
if ('href' in restProps) {
return React.createElement(
'a',
_extends({}, restProps, { className: wrapCls, onClick: this.handleClick }),
iconNode,
kids
);
} else {
var htmlType = restProps.htmlType,
otherProps = __rest(restProps, ["htmlType"]);
return React.createElement(
'button',
_extends({}, otherProps, { type: htmlType || 'button', className: wrapCls, onClick: this.handleClick }),
React.createElement(
'div',
{ className: prefixCls + '-inner' },
iconNode,
kids
)
);
}
}
}]);
return Button;
}(React.PureComponent);
Button.defaultProps = {
disabled: false,
inline: false,
loading: false,
size: 'large',
prefixCls: 'm-button'
};
export default Button;