UNPKG

9.34 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.default = undefined;
5
6var _extends2 = require('babel-runtime/helpers/extends');
7
8var _extends3 = _interopRequireDefault(_extends2);
9
10var _typeof2 = require('babel-runtime/helpers/typeof');
11
12var _typeof3 = _interopRequireDefault(_typeof2);
13
14var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
15
16var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
17
18var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
19
20var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
21
22var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
23
24var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
25
26var _inherits2 = require('babel-runtime/helpers/inherits');
27
28var _inherits3 = _interopRequireDefault(_inherits2);
29
30var _class, _temp2;
31
32var _react = require('react');
33
34var _react2 = _interopRequireDefault(_react);
35
36var _propTypes = require('prop-types');
37
38var _propTypes2 = _interopRequireDefault(_propTypes);
39
40var _classnames = require('classnames');
41
42var _classnames2 = _interopRequireDefault(_classnames);
43
44var _configProvider = require('../../config-provider');
45
46var _configProvider2 = _interopRequireDefault(_configProvider);
47
48var _util = require('../../util');
49
50function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
51
52function mapIconSize(size) {
53 return {
54 large: 'small',
55 medium: 'xs',
56 small: 'xs'
57 }[size];
58}
59
60/** Button */
61var Button = (_temp2 = _class = function (_Component) {
62 (0, _inherits3.default)(Button, _Component);
63
64 function Button() {
65 var _temp, _this, _ret;
66
67 (0, _classCallCheck3.default)(this, Button);
68
69 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
70 args[_key] = arguments[_key];
71 }
72
73 return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.onMouseUp = function (e) {
74 _this.button.blur();
75
76 if (_this.props.onMouseUp) {
77 _this.props.onMouseUp(e);
78 }
79 }, _this.buttonRefHandler = function (button) {
80 _this.button = button;
81 }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
82 }
83
84 Button.prototype.render = function render() {
85 var _btnClsObj;
86
87 var _props = this.props,
88 prefix = _props.prefix,
89 className = _props.className,
90 type = _props.type,
91 size = _props.size,
92 htmlType = _props.htmlType,
93 loading = _props.loading,
94 text = _props.text,
95 warning = _props.warning,
96 ghost = _props.ghost,
97 component = _props.component,
98 iconSize = _props.iconSize,
99 icons = _props.icons,
100 disabled = _props.disabled,
101 onClick = _props.onClick,
102 children = _props.children,
103 rtl = _props.rtl,
104 others = (0, _objectWithoutProperties3.default)(_props, ['prefix', 'className', 'type', 'size', 'htmlType', 'loading', 'text', 'warning', 'ghost', 'component', 'iconSize', 'icons', 'disabled', 'onClick', 'children', 'rtl']);
105
106 var ghostType = ['light', 'dark'].indexOf(ghost) >= 0 ? ghost : 'dark';
107
108 var btnClsObj = (_btnClsObj = {}, _btnClsObj[prefix + 'btn'] = true, _btnClsObj['' + prefix + size] = size, _btnClsObj[prefix + 'btn-' + type] = type && !ghost, _btnClsObj[prefix + 'btn-text'] = text, _btnClsObj[prefix + 'btn-warning'] = warning, _btnClsObj[prefix + 'btn-loading'] = loading, _btnClsObj[prefix + 'btn-ghost'] = ghost, _btnClsObj[prefix + 'btn-' + ghostType] = ghost, _btnClsObj[className] = className, _btnClsObj);
109
110 var loadingIcon = null;
111
112 // 如果传入了 loading 的 icons,使用该节点来渲染
113 if (icons && icons.loading && (0, _react.isValidElement)(icons.loading)) {
114 var _classNames;
115
116 if (loading) {
117 delete btnClsObj[prefix + 'btn-loading'];
118 btnClsObj[prefix + 'btn-custom-loading'] = true;
119 }
120
121 var loadingSize = iconSize || mapIconSize(size);
122 loadingIcon = _react2.default.cloneElement(icons.loading, {
123 className: (0, _classnames2.default)((_classNames = {}, _classNames[prefix + 'btn-custom-loading-icon'] = true, _classNames.show = loading, _classNames)),
124 size: loadingSize
125 });
126 }
127
128 var count = _react.Children.count(children);
129 var clonedChildren = _react.Children.map(children, function (child, index) {
130 if (child && ['function', 'object'].indexOf((0, _typeof3.default)(child.type)) > -1 && child.type._typeMark === 'icon') {
131 var _classNames2;
132
133 var iconCls = (0, _classnames2.default)((_classNames2 = {}, _classNames2[prefix + 'btn-icon'] = !iconSize, _classNames2[prefix + 'icon-first'] = count > 1 && index === 0, _classNames2[prefix + 'icon-last'] = count > 1 && index === count - 1, _classNames2[prefix + 'icon-alone'] = count === 1, _classNames2[child.props.className] = !!child.props.className, _classNames2));
134
135 if ('size' in child.props) {
136 _util.log.warning('The size of Icon will not take effect, when Icon is the [direct child element] of Button(<Button><Icon size="' + child.props.size + '" /></Button>), use <Button iconSize="' + child.props.size + '"> or <Button><div><Icon size="' + child.props.size + '" /></div></Button> instead of.');
137 }
138 return _react2.default.cloneElement(child, {
139 className: iconCls,
140 size: iconSize || mapIconSize(size)
141 });
142 }
143
144 if (!(0, _react.isValidElement)(child)) {
145 return _react2.default.createElement(
146 'span',
147 { className: prefix + 'btn-helper' },
148 child
149 );
150 }
151
152 return child;
153 });
154
155 var TagName = component;
156 var tagAttrs = (0, _extends3.default)({}, _util.obj.pickOthers(Object.keys(Button.propTypes), others), {
157 type: htmlType,
158 disabled: disabled,
159 onClick: onClick,
160 className: (0, _classnames2.default)(btnClsObj)
161 });
162
163 if (TagName !== 'button') {
164 delete tagAttrs.type;
165
166 if (tagAttrs.disabled) {
167 delete tagAttrs.onClick; // a 标签的 onClick 浏览器默认不会禁用
168 tagAttrs.href && delete tagAttrs.href; // a 标签在禁用状态下无跳转
169 }
170 }
171
172 return _react2.default.createElement(
173 TagName,
174 (0, _extends3.default)({}, tagAttrs, { dir: rtl ? 'rtl' : undefined, onMouseUp: this.onMouseUp, ref: this.buttonRefHandler }),
175 loadingIcon,
176 clonedChildren
177 );
178 };
179
180 return Button;
181}(_react.Component), _class.propTypes = (0, _extends3.default)({}, _configProvider2.default.propTypes, {
182 prefix: _propTypes2.default.string,
183 rtl: _propTypes2.default.bool,
184 /**
185 * 按钮的类型
186 */
187 type: _propTypes2.default.oneOf(['primary', 'secondary', 'normal']),
188 /**
189 * 按钮的尺寸
190 */
191 size: _propTypes2.default.oneOf(['small', 'medium', 'large']),
192 /**
193 * 按钮中可配置的 Icon,格式为 { loading: <Icon type="loading" /> }
194 */
195 icons: _propTypes2.default.shape({
196 loading: _propTypes2.default.node
197 }),
198 /**
199 * 按钮中 Icon 的尺寸,用于替代 Icon 的默认大小
200 */
201 iconSize: _propTypes2.default.oneOfType([_propTypes2.default.oneOf(['xxs', 'xs', 'small', 'medium', 'large', 'xl', 'xxl', 'xxxl', 'inherit']), _propTypes2.default.number]),
202 /**
203 * 当 component = 'button' 时,设置 button 标签的 type 值
204 */
205 htmlType: _propTypes2.default.oneOf(['submit', 'reset', 'button']),
206 /**
207 * 设置标签类型
208 */
209 component: _propTypes2.default.oneOf(['button', 'a', 'div', 'span']),
210 /**
211 * 设置按钮的载入状态
212 */
213 loading: _propTypes2.default.bool,
214 /**
215 * 是否为幽灵按钮
216 */
217 ghost: _propTypes2.default.oneOf([true, false, 'light', 'dark']),
218 /**
219 * 是否为文本按钮
220 */
221 text: _propTypes2.default.bool,
222 /**
223 * 是否为警告按钮
224 */
225 warning: _propTypes2.default.bool,
226 /**
227 * 是否禁用
228 */
229 disabled: _propTypes2.default.bool,
230 /**
231 * 点击按钮的回调
232 * @param {Object} e Event Object
233 */
234 onClick: _propTypes2.default.func,
235 className: _propTypes2.default.string,
236 onMouseUp: _propTypes2.default.func,
237 children: _propTypes2.default.node
238}), _class.defaultProps = {
239 prefix: 'next-',
240 type: 'normal',
241 size: 'medium',
242 icons: {},
243 htmlType: 'button',
244 component: 'button',
245 loading: false,
246 ghost: false,
247 text: false,
248 warning: false,
249 disabled: false,
250 onClick: function onClick() {}
251}, _temp2);
252Button.displayName = 'Button';
253exports.default = Button;
254module.exports = exports['default'];
\No newline at end of file