UNPKG

4.96 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _defineProperty from 'babel-runtime/helpers/defineProperty';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _createClass from 'babel-runtime/helpers/createClass';
5import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
6import _inherits from 'babel-runtime/helpers/inherits';
7var __rest = this && this.__rest || function (s, e) {
8 var t = {};
9 for (var p in s) {
10 if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
11 }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
12 if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
13 }return t;
14};
15import classnames from 'classnames';
16import React from 'react';
17import TouchFeedback from 'rmc-feedback';
18import Icon from '../icon';
19var rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
20var isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
21function isString(str) {
22 return typeof str === 'string';
23}
24// Insert one space between two chinese characters automatically.
25function insertSpace(child) {
26 if (isString(child.type) && isTwoCNChar(child.props.children)) {
27 return React.cloneElement(child, {}, child.props.children.split('').join(' '));
28 }
29 if (isString(child)) {
30 if (isTwoCNChar(child)) {
31 child = child.split('').join(' ');
32 }
33 return React.createElement(
34 'span',
35 null,
36 child
37 );
38 }
39 return child;
40}
41
42var Button = function (_React$Component) {
43 _inherits(Button, _React$Component);
44
45 function Button() {
46 _classCallCheck(this, Button);
47
48 return _possibleConstructorReturn(this, (Button.__proto__ || Object.getPrototypeOf(Button)).apply(this, arguments));
49 }
50
51 _createClass(Button, [{
52 key: 'render',
53 value: function render() {
54 var _classnames;
55
56 var _a = this.props,
57 children = _a.children,
58 className = _a.className,
59 prefixCls = _a.prefixCls,
60 type = _a.type,
61 size = _a.size,
62 inline = _a.inline,
63 disabled = _a.disabled,
64 icon = _a.icon,
65 loading = _a.loading,
66 activeStyle = _a.activeStyle,
67 activeClassName = _a.activeClassName,
68 onClick = _a.onClick,
69 restProps = __rest(_a, ["children", "className", "prefixCls", "type", "size", "inline", "disabled", "icon", "loading", "activeStyle", "activeClassName", "onClick"]);
70 var iconType = loading ? 'loading' : icon;
71 var wrapCls = classnames(prefixCls, className, (_classnames = {}, _defineProperty(_classnames, prefixCls + '-primary', type === 'primary'), _defineProperty(_classnames, prefixCls + '-ghost', type === 'ghost'), _defineProperty(_classnames, prefixCls + '-warning', type === 'warning'), _defineProperty(_classnames, prefixCls + '-small', size === 'small'), _defineProperty(_classnames, prefixCls + '-inline', inline), _defineProperty(_classnames, prefixCls + '-disabled', disabled), _defineProperty(_classnames, prefixCls + '-loading', loading), _defineProperty(_classnames, prefixCls + '-icon', !!iconType), _classnames));
72 var kids = React.Children.map(children, insertSpace);
73 var iconEl = void 0;
74 if (typeof iconType === 'string') {
75 iconEl = React.createElement(Icon, { 'aria-hidden': 'true', type: iconType, size: size === 'small' ? 'xxs' : 'md', className: prefixCls + '-icon' });
76 } else if (iconType) {
77 var rawCls = iconType.props && iconType.props.className;
78 var cls = classnames('am-icon', prefixCls + '-icon', size === 'small' ? 'am-icon-xxs' : 'am-icon-md');
79 iconEl = React.cloneElement(iconType, {
80 className: rawCls ? rawCls + ' ' + cls : cls
81 });
82 }
83 // use div, button native is buggy @yiminghe
84 return React.createElement(
85 TouchFeedback
86 // tslint:disable-next-line:jsx-no-multiline-js
87 ,
88 { activeClassName: activeClassName || (activeStyle ? prefixCls + '-active' : undefined), disabled: disabled, activeStyle: activeStyle },
89 React.createElement(
90 'a',
91 _extends({ role: 'button', className: wrapCls }, restProps, { onClick: disabled ? undefined : onClick, 'aria-disabled': disabled }),
92 iconEl,
93 kids
94 )
95 );
96 }
97 }]);
98
99 return Button;
100}(React.Component);
101
102Button.defaultProps = {
103 prefixCls: 'am-button',
104 size: 'large',
105 inline: false,
106 disabled: false,
107 loading: false,
108 activeStyle: {}
109};
110export default Button;
\No newline at end of file