2020-10-13 10:04:02 +00:00
|
|
|
import { defineComponent, inject, Text, VNode } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Wave from '../_util/wave';
|
2020-03-23 10:42:39 +00:00
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
2019-01-12 03:33:27 +00:00
|
|
|
import buttonTypes from './buttonTypes';
|
2020-06-30 13:39:00 +00:00
|
|
|
import { getSlot, getComponent } from '../_util/props-util';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-03-23 10:42:39 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2019-03-13 01:38:54 +00:00
|
|
|
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
|
|
|
|
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
|
2019-01-12 03:33:27 +00:00
|
|
|
const props = buttonTypes();
|
2020-10-13 10:04:02 +00:00
|
|
|
export default defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'AButton',
|
2019-02-01 09:23:00 +00:00
|
|
|
inheritAttrs: false,
|
2018-01-11 10:53:51 +00:00
|
|
|
__ANT_BUTTON: true,
|
2019-01-02 12:18:13 +00:00
|
|
|
props,
|
2020-05-28 11:45:50 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-10-13 10:04:02 +00:00
|
|
|
children: [],
|
|
|
|
iconCom: undefined,
|
2020-10-19 08:43:10 +00:00
|
|
|
delayTimeout: undefined,
|
2020-05-28 11:45:50 +00:00
|
|
|
};
|
2019-03-13 01:38:54 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2017-10-26 07:18:08 +00:00
|
|
|
return {
|
|
|
|
sizeMap: {
|
|
|
|
large: 'lg',
|
|
|
|
small: 'sm',
|
|
|
|
},
|
2017-12-29 08:51:06 +00:00
|
|
|
sLoading: !!this.loading,
|
2018-04-06 12:56:19 +00:00
|
|
|
hasTwoCNChar: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2017-10-26 07:18:08 +00:00
|
|
|
},
|
2020-03-23 10:42:39 +00:00
|
|
|
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();
|
|
|
|
},
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2020-03-23 10:42:39 +00:00
|
|
|
if (this.delayTimeout) {
|
|
|
|
clearTimeout(this.delayTimeout);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getClasses() {
|
2019-01-12 03:33:27 +00:00
|
|
|
const {
|
2019-03-13 01:38:54 +00:00
|
|
|
prefixCls: customizePrefixCls,
|
2019-01-12 03:33:27 +00:00
|
|
|
type,
|
|
|
|
shape,
|
|
|
|
size,
|
|
|
|
hasTwoCNChar,
|
|
|
|
sLoading,
|
|
|
|
ghost,
|
|
|
|
block,
|
2020-05-28 11:45:50 +00:00
|
|
|
$attrs,
|
2020-10-19 08:43:10 +00:00
|
|
|
} = this;
|
2020-05-28 15:36:43 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-13 01:38:54 +00:00
|
|
|
const prefixCls = getPrefixCls('btn', customizePrefixCls);
|
|
|
|
const autoInsertSpace = this.configProvider.autoInsertSpaceInButton !== false;
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
// large => lg
|
|
|
|
// small => sm
|
|
|
|
let sizeCls = '';
|
|
|
|
switch (size) {
|
|
|
|
case 'large':
|
|
|
|
sizeCls = 'lg';
|
|
|
|
break;
|
|
|
|
case 'small':
|
|
|
|
sizeCls = 'sm';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-08-10 10:36:55 +00:00
|
|
|
const iconType = sLoading ? 'loading' : this.iconCom;
|
2017-10-26 07:18:08 +00:00
|
|
|
return {
|
2020-10-13 10:04:02 +00:00
|
|
|
[$attrs.class as string]: $attrs.class,
|
2017-10-26 07:18:08 +00:00
|
|
|
[`${prefixCls}`]: true,
|
|
|
|
[`${prefixCls}-${type}`]: type,
|
|
|
|
[`${prefixCls}-${shape}`]: shape,
|
|
|
|
[`${prefixCls}-${sizeCls}`]: sizeCls,
|
2020-06-11 07:58:41 +00:00
|
|
|
[`${prefixCls}-icon-only`]: this.children.length === 0 && iconType,
|
2017-12-29 08:51:06 +00:00
|
|
|
[`${prefixCls}-loading`]: sLoading,
|
2017-10-26 07:18:08 +00:00
|
|
|
[`${prefixCls}-background-ghost`]: ghost || type === 'ghost',
|
2019-03-13 01:38:54 +00:00
|
|
|
[`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && autoInsertSpace,
|
2018-09-05 13:28:54 +00:00
|
|
|
[`${prefixCls}-block`]: block,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2017-10-26 07:18:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
fixTwoCNChar() {
|
2018-04-06 12:56:19 +00:00
|
|
|
// Fix for HOC usage like <FormatMessage />
|
2020-10-13 10:04:02 +00:00
|
|
|
const node = this.$refs.buttonNode as HTMLElement;
|
2018-11-28 13:58:42 +00:00
|
|
|
if (!node) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-11-28 13:58:42 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
const buttonText = node.textContent;
|
2018-04-06 12:56:19 +00:00
|
|
|
if (this.isNeedInserted() && isTwoCNChar(buttonText)) {
|
|
|
|
if (!this.hasTwoCNChar) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.hasTwoCNChar = true;
|
2018-04-06 12:56:19 +00:00
|
|
|
}
|
|
|
|
} else if (this.hasTwoCNChar) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.hasTwoCNChar = false;
|
2018-04-06 12:56:19 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-13 10:04:02 +00:00
|
|
|
handleClick(event: Event) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const { sLoading } = this.$data;
|
2018-11-28 13:58:42 +00:00
|
|
|
if (sLoading) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-11-28 13:58:42 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
this.$emit('click', event);
|
2017-10-26 07:18:08 +00:00
|
|
|
},
|
2020-10-13 10:04:02 +00:00
|
|
|
insertSpace(child: VNode, needInserted: boolean) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const SPACE = needInserted ? ' ' : '';
|
2020-06-20 13:45:08 +00:00
|
|
|
if (child.type === Text) {
|
2020-10-13 10:04:02 +00:00
|
|
|
let text = (child.children as string).trim();
|
2017-12-29 08:51:06 +00:00
|
|
|
if (isTwoCNChar(text)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
text = text.split('').join(SPACE);
|
2017-12-29 08:51:06 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return <span>{text}</span>;
|
2017-12-29 08:51:06 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return child;
|
2017-10-26 07:18:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
isNeedInserted() {
|
2020-06-30 13:39:00 +00:00
|
|
|
const { iconCom, type } = this;
|
|
|
|
return this.children.length === 1 && !iconCom && type !== 'link';
|
2018-04-06 12:56:19 +00:00
|
|
|
},
|
2017-10-26 07:18:08 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-06-30 13:39:00 +00:00
|
|
|
this.iconCom = getComponent(this, 'icon');
|
2020-10-19 08:43:10 +00:00
|
|
|
const { type, htmlType, iconCom, disabled, handleClick, sLoading, href, title, $attrs } = this;
|
2020-06-30 13:39:00 +00:00
|
|
|
const children = getSlot(this);
|
2020-06-11 07:58:41 +00:00
|
|
|
this.children = children;
|
|
|
|
const classes = this.getClasses();
|
|
|
|
|
2017-12-29 08:51:06 +00:00
|
|
|
const buttonProps = {
|
2020-05-28 11:45:50 +00:00
|
|
|
...$attrs,
|
2020-10-15 09:58:05 +00:00
|
|
|
title,
|
2020-05-28 11:45:50 +00:00
|
|
|
disabled,
|
2018-12-25 03:58:32 +00:00
|
|
|
class: classes,
|
2020-05-28 11:45:50 +00:00
|
|
|
onClick: handleClick,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-06-30 13:39:00 +00:00
|
|
|
const iconNode = sLoading ? <LoadingOutlined /> : iconCom;
|
2020-06-11 07:58:41 +00:00
|
|
|
|
2020-05-28 11:45:50 +00:00
|
|
|
const autoInsertSpace = this.configProvider.autoInsertSpaceInButton !== false;
|
2019-05-28 03:37:38 +00:00
|
|
|
const kids = children.map(child =>
|
|
|
|
this.insertSpace(child, this.isNeedInserted() && autoInsertSpace),
|
|
|
|
);
|
2018-11-28 13:58:42 +00:00
|
|
|
|
2020-10-15 09:58:05 +00:00
|
|
|
if (href !== undefined) {
|
2018-09-05 13:28:54 +00:00
|
|
|
return (
|
2020-10-15 09:58:05 +00:00
|
|
|
<a {...buttonProps} href={href} ref="buttonNode">
|
2019-01-12 03:33:27 +00:00
|
|
|
{iconNode}
|
|
|
|
{kids}
|
2018-09-05 13:28:54 +00:00
|
|
|
</a>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2017-12-29 08:51:06 +00:00
|
|
|
}
|
2019-08-12 03:30:32 +00:00
|
|
|
|
|
|
|
const buttonNode = (
|
|
|
|
<button {...buttonProps} ref="buttonNode" type={htmlType || 'button'}>
|
|
|
|
{iconNode}
|
|
|
|
{kids}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (type === 'link') {
|
|
|
|
return buttonNode;
|
|
|
|
}
|
|
|
|
|
2020-07-29 07:34:19 +00:00
|
|
|
return <Wave ref="wave">{buttonNode}</Wave>;
|
2017-10-26 07:18:08 +00:00
|
|
|
},
|
2020-10-13 10:04:02 +00:00
|
|
|
});
|