forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.jsx
184 lines (178 loc) · 4.71 KB
/
button.jsx
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
import Wave from '../_util/wave';
import Icon from '../icon';
import buttonTypes from './buttonTypes';
import { filterEmpty, getListeners } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider';
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
const props = buttonTypes();
export default {
name: 'AButton',
inheritAttrs: false,
__ANT_BUTTON: true,
props,
inject: {
configProvider: { default: () => ConfigConsumerProps },
},
data() {
return {
sizeMap: {
large: 'lg',
small: 'sm',
},
sLoading: !!this.loading,
hasTwoCNChar: false,
};
},
computed: {
classes() {
const {
prefixCls: customizePrefixCls,
type,
shape,
size,
hasTwoCNChar,
sLoading,
ghost,
block,
icon,
$slots,
} = this;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('btn', customizePrefixCls);
const autoInsertSpace = this.configProvider.autoInsertSpaceInButton !== false;
// large => lg
// small => sm
let sizeCls = '';
switch (size) {
case 'large':
sizeCls = 'lg';
break;
case 'small':
sizeCls = 'sm';
break;
default:
break;
}
const iconType = sLoading ? 'loading' : icon;
const children = filterEmpty($slots.default);
return {
[`${prefixCls}`]: true,
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${shape}`]: shape,
[`${prefixCls}-${sizeCls}`]: sizeCls,
[`${prefixCls}-icon-only`]: children.length === 0 && iconType,
[`${prefixCls}-loading`]: sLoading,
[`${prefixCls}-background-ghost`]: ghost || type === 'ghost',
[`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && autoInsertSpace,
[`${prefixCls}-block`]: block,
};
},
},
watch: {
loading(val, preVal) {
if (preVal && typeof preVal !== 'boolean') {
clearTimeout(this.delayTimeout);
}
if (val && typeof val !== 'boolean' && val.delay) {
this.delayTimeout = setTimeout(() => {
this.sLoading = !!val;
}, val.delay);
} else {
this.sLoading = !!val;
}
},
},
mounted() {
this.fixTwoCNChar();
},
updated() {
this.fixTwoCNChar();
},
beforeDestroy() {
// if (this.timeout) {
// clearTimeout(this.timeout)
// }
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
},
methods: {
fixTwoCNChar() {
// Fix for HOC usage like <FormatMessage />
const node = this.$refs.buttonNode;
if (!node) {
return;
}
const buttonText = node.textContent;
if (this.isNeedInserted() && isTwoCNChar(buttonText)) {
if (!this.hasTwoCNChar) {
this.hasTwoCNChar = true;
}
} else if (this.hasTwoCNChar) {
this.hasTwoCNChar = false;
}
},
handleClick(event) {
const { sLoading } = this.$data;
if (sLoading) {
return;
}
this.$emit('click', event);
},
insertSpace(child, needInserted) {
const SPACE = needInserted ? ' ' : '';
if (typeof child.text === 'string') {
let text = child.text.trim();
if (isTwoCNChar(text)) {
text = text.split('').join(SPACE);
}
return <span>{text}</span>;
}
return child;
},
isNeedInserted() {
const { icon, $slots, type } = this;
return $slots.default && $slots.default.length === 1 && !icon && type !== 'link';
},
},
render() {
const { type, htmlType, classes, icon, disabled, handleClick, sLoading, $slots, $attrs } = this;
const buttonProps = {
attrs: {
...$attrs,
disabled,
},
class: classes,
on: {
...getListeners(this),
click: handleClick,
},
};
const iconType = sLoading ? 'loading' : icon;
const iconNode = iconType ? <Icon type={iconType} /> : $slots.icon;
const children = filterEmpty($slots.default);
const autoInsertSpace = this.configProvider.autoInsertSpaceInButton !== false;
const kids = children.map(child =>
this.insertSpace(child, this.isNeedInserted() && autoInsertSpace),
);
if ($attrs.href !== undefined) {
return (
<a {...buttonProps} ref="buttonNode">
{iconNode}
{kids}
</a>
);
}
const buttonNode = (
<button {...buttonProps} ref="buttonNode" type={htmlType || 'button'}>
{iconNode}
{kids}
</button>
);
if (type === 'link') {
return buttonNode;
}
return <Wave>{buttonNode}</Wave>;
},
};