1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = undefined;
|
5 |
|
6 | var _extends2 = require('babel-runtime/helpers/extends');
|
7 |
|
8 | var _extends3 = _interopRequireDefault(_extends2);
|
9 |
|
10 | var _typeof2 = require('babel-runtime/helpers/typeof');
|
11 |
|
12 | var _typeof3 = _interopRequireDefault(_typeof2);
|
13 |
|
14 | var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
|
15 |
|
16 | var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
|
17 |
|
18 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
19 |
|
20 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
21 |
|
22 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
23 |
|
24 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
25 |
|
26 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
27 |
|
28 | var _inherits3 = _interopRequireDefault(_inherits2);
|
29 |
|
30 | var _class, _temp2;
|
31 |
|
32 | var _react = require('react');
|
33 |
|
34 | var _react2 = _interopRequireDefault(_react);
|
35 |
|
36 | var _propTypes = require('prop-types');
|
37 |
|
38 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
39 |
|
40 | var _classnames = require('classnames');
|
41 |
|
42 | var _classnames2 = _interopRequireDefault(_classnames);
|
43 |
|
44 | var _configProvider = require('../../config-provider');
|
45 |
|
46 | var _configProvider2 = _interopRequireDefault(_configProvider);
|
47 |
|
48 | var _util = require('../../util');
|
49 |
|
50 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
51 |
|
52 | function mapIconSize(size) {
|
53 | return {
|
54 | large: 'small',
|
55 | medium: 'xs',
|
56 | small: 'xs'
|
57 | }[size];
|
58 | }
|
59 |
|
60 |
|
61 | var 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 |
|
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;
|
168 | tagAttrs.href && delete tagAttrs.href;
|
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 |
|
194 |
|
195 | icons: _propTypes2.default.shape({
|
196 | loading: _propTypes2.default.node
|
197 | }),
|
198 | |
199 |
|
200 |
|
201 | iconSize: _propTypes2.default.oneOfType([_propTypes2.default.oneOf(['xxs', 'xs', 'small', 'medium', 'large', 'xl', 'xxl', 'xxxl', 'inherit']), _propTypes2.default.number]),
|
202 | |
203 |
|
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 |
|
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);
|
252 | Button.displayName = 'Button';
|
253 | exports.default = Button;
|
254 | module.exports = exports['default']; |
\ | No newline at end of file |