button.js
8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _typeof from "@babel/runtime/helpers/typeof";
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 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
/* eslint-disable react/button-has-type */
import * as React from 'react';
import classNames from 'classnames';
import omit from 'omit.js';
import Group from './button-group';
import { ConfigContext } from '../config-provider';
import Wave from '../_util/wave';
import { tuple } from '../_util/type';
import devWarning from '../_util/devWarning';
import SizeContext from '../config-provider/SizeContext';
import LoadingIcon from './LoadingIcon';
import { cloneElement } from '../_util/reactNode';
var rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
var isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
function isString(str) {
return typeof str === 'string';
}
function isUnborderedButtonType(type) {
return type === 'text' || type === 'link';
} // 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 cloneElement(child, {
children: child.props.children.split('').join(SPACE)
});
}
if (typeof child === 'string') {
if (isTwoCNChar(child)) {
child = child.split('').join(SPACE);
}
return /*#__PURE__*/React.createElement("span", null, child);
}
return child;
}
function spaceChildren(children, needInserted) {
var isPrevChildPure = false;
var childList = [];
React.Children.forEach(children, function (child) {
var type = _typeof(child);
var isCurrentChildPure = type === 'string' || type === 'number';
if (isPrevChildPure && isCurrentChildPure) {
var lastIndex = childList.length - 1;
var lastChild = childList[lastIndex];
childList[lastIndex] = "".concat(lastChild).concat(child);
} else {
childList.push(child);
}
isPrevChildPure = isCurrentChildPure;
}); // Pass to React.Children.map to auto fill key
return React.Children.map(childList, function (child) {
return insertSpace(child, needInserted);
});
}
var ButtonTypes = tuple('default', 'primary', 'ghost', 'dashed', 'link', 'text');
var ButtonShapes = tuple('circle', 'round');
var ButtonHTMLTypes = tuple('submit', 'button', 'reset');
export function convertLegacyProps(type) {
if (type === 'danger') {
return {
danger: true
};
}
return {
type: type
};
}
var InternalButton = function InternalButton(props, ref) {
var _classNames;
var _props$loading = props.loading,
loading = _props$loading === void 0 ? false : _props$loading,
customizePrefixCls = props.prefixCls,
type = props.type,
danger = props.danger,
shape = props.shape,
customizeSize = props.size,
className = props.className,
children = props.children,
icon = props.icon,
_props$ghost = props.ghost,
ghost = _props$ghost === void 0 ? false : _props$ghost,
_props$block = props.block,
block = _props$block === void 0 ? false : _props$block,
_props$htmlType = props.htmlType,
htmlType = _props$htmlType === void 0 ? 'button' : _props$htmlType,
rest = __rest(props, ["loading", "prefixCls", "type", "danger", "shape", "size", "className", "children", "icon", "ghost", "block", "htmlType"]);
var size = React.useContext(SizeContext);
var _React$useState = React.useState(!!loading),
_React$useState2 = _slicedToArray(_React$useState, 2),
innerLoading = _React$useState2[0],
setLoading = _React$useState2[1];
var _React$useState3 = React.useState(false),
_React$useState4 = _slicedToArray(_React$useState3, 2),
hasTwoCNChar = _React$useState4[0],
setHasTwoCNChar = _React$useState4[1];
var _React$useContext = React.useContext(ConfigContext),
getPrefixCls = _React$useContext.getPrefixCls,
autoInsertSpaceInButton = _React$useContext.autoInsertSpaceInButton,
direction = _React$useContext.direction;
var buttonRef = ref || /*#__PURE__*/React.createRef();
var delayTimeoutRef = React.useRef();
var isNeedInserted = function isNeedInserted() {
return React.Children.count(children) === 1 && !icon && !isUnborderedButtonType(type);
};
var fixTwoCNChar = function fixTwoCNChar() {
// Fix for HOC usage like <FormatMessage />
if (!buttonRef || !buttonRef.current || autoInsertSpaceInButton === false) {
return;
}
var buttonText = buttonRef.current.textContent;
if (isNeedInserted() && isTwoCNChar(buttonText)) {
if (!hasTwoCNChar) {
setHasTwoCNChar(true);
}
} else if (hasTwoCNChar) {
setHasTwoCNChar(false);
}
}; // =============== Update Loading ===============
var loadingOrDelay;
if (_typeof(loading) === 'object' && loading.delay) {
loadingOrDelay = loading.delay || true;
} else {
loadingOrDelay = !!loading;
}
React.useEffect(function () {
clearTimeout(delayTimeoutRef.current);
if (typeof loadingOrDelay === 'number') {
delayTimeoutRef.current = window.setTimeout(function () {
setLoading(loadingOrDelay);
}, loadingOrDelay);
} else {
setLoading(loadingOrDelay);
}
}, [loadingOrDelay]);
React.useEffect(fixTwoCNChar, [buttonRef]);
var handleClick = function handleClick(e) {
var onClick = props.onClick;
if (innerLoading) {
return;
}
if (onClick) {
onClick(e);
}
};
devWarning(!(typeof icon === 'string' && icon.length > 2), 'Button', "`icon` is using ReactNode instead of string naming in v4. Please check `".concat(icon, "` at https://ant.design/components/icon"));
devWarning(!(ghost && isUnborderedButtonType(type)), 'Button', "`link` or `text` button can't be a `ghost` button.");
var prefixCls = getPrefixCls('btn', customizePrefixCls);
var autoInsertSpace = autoInsertSpaceInButton !== false; // large => lg
// small => sm
var sizeCls = '';
switch (customizeSize || size) {
case 'large':
sizeCls = 'lg';
break;
case 'small':
sizeCls = 'sm';
break;
default:
break;
}
var iconType = innerLoading ? 'loading' : icon;
var classes = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(type), type), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(shape), shape), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(sizeCls), sizeCls), _defineProperty(_classNames, "".concat(prefixCls, "-icon-only"), !children && children !== 0 && iconType), _defineProperty(_classNames, "".concat(prefixCls, "-background-ghost"), ghost && !isUnborderedButtonType(type)), _defineProperty(_classNames, "".concat(prefixCls, "-loading"), innerLoading), _defineProperty(_classNames, "".concat(prefixCls, "-two-chinese-chars"), hasTwoCNChar && autoInsertSpace), _defineProperty(_classNames, "".concat(prefixCls, "-block"), block), _defineProperty(_classNames, "".concat(prefixCls, "-dangerous"), !!danger), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames), className);
var iconNode = icon && !innerLoading ? icon : /*#__PURE__*/React.createElement(LoadingIcon, {
existIcon: !!icon,
prefixCls: prefixCls,
loading: !!innerLoading
});
var kids = children || children === 0 ? spaceChildren(children, isNeedInserted() && autoInsertSpace) : null;
var linkButtonRestProps = omit(rest, ['navigate']);
if (linkButtonRestProps.href !== undefined) {
return /*#__PURE__*/React.createElement("a", _extends({}, linkButtonRestProps, {
className: classes,
onClick: handleClick,
ref: buttonRef
}), iconNode, kids);
}
var buttonNode = /*#__PURE__*/React.createElement("button", _extends({}, rest, {
type: htmlType,
className: classes,
onClick: handleClick,
ref: buttonRef
}), iconNode, kids);
if (isUnborderedButtonType(type)) {
return buttonNode;
}
return /*#__PURE__*/React.createElement(Wave, null, buttonNode);
};
var Button = /*#__PURE__*/React.forwardRef(InternalButton);
Button.displayName = 'Button';
Button.Group = Group;
Button.__ANT_BUTTON = true;
export default Button;