2021-06-30 10:03:17 +08:00
|
|
|
import {
|
|
|
|
computed,
|
|
|
|
defineComponent,
|
|
|
|
onBeforeUnmount,
|
|
|
|
onMounted,
|
|
|
|
onUpdated,
|
|
|
|
ref,
|
|
|
|
Text,
|
|
|
|
watch,
|
|
|
|
watchEffect,
|
|
|
|
} from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Wave from '../_util/wave';
|
|
|
|
import buttonTypes from './buttonTypes';
|
2021-06-30 10:03:17 +08:00
|
|
|
import LoadingOutlined from '@ant-design/icons-vue/LoadingOutlined';
|
|
|
|
import { flattenChildren, getPropsSlot } from '../_util/props-util';
|
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import devWarning from '../vc-util/devWarning';
|
|
|
|
|
|
|
|
import type { ButtonType } from './buttonTypes';
|
2021-06-30 14:45:05 +08:00
|
|
|
import type { VNode, Ref } from 'vue';
|
2021-06-30 10:03:17 +08:00
|
|
|
|
2021-06-30 13:48:02 +08:00
|
|
|
type Loading = boolean | number;
|
|
|
|
|
2019-03-13 09:38:54 +08:00
|
|
|
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
|
|
|
|
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
|
2019-01-12 11:33:27 +08:00
|
|
|
const props = buttonTypes();
|
2021-06-30 10:03:17 +08:00
|
|
|
|
|
|
|
function isUnborderedButtonType(type: ButtonType | undefined) {
|
|
|
|
return type === 'text' || type === 'link';
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:04:02 +08:00
|
|
|
export default defineComponent({
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'AButton',
|
2019-02-01 17:23:00 +08:00
|
|
|
inheritAttrs: false,
|
2018-01-11 18:53:51 +08:00
|
|
|
__ANT_BUTTON: true,
|
2019-01-02 20:18:13 +08:00
|
|
|
props,
|
2021-06-30 10:03:17 +08:00
|
|
|
slots: ['icon'],
|
2021-12-14 23:50:22 +08:00
|
|
|
emits: ['click', 'mousedown'],
|
2021-06-30 10:03:17 +08:00
|
|
|
setup(props, { slots, attrs, emit }) {
|
|
|
|
const { prefixCls, autoInsertSpaceInButton, direction } = useConfigInject('btn', props);
|
|
|
|
|
|
|
|
const buttonNodeRef = ref<HTMLElement>(null);
|
2021-06-30 13:48:02 +08:00
|
|
|
const delayTimeoutRef = ref(undefined);
|
|
|
|
let isNeedInserted = false;
|
2021-06-30 10:03:17 +08:00
|
|
|
|
2021-06-30 13:48:02 +08:00
|
|
|
const innerLoading: Ref<Loading> = ref(false);
|
2021-06-30 10:03:17 +08:00
|
|
|
const hasTwoCNChar = ref(false);
|
|
|
|
|
|
|
|
const autoInsertSpace = computed(() => autoInsertSpaceInButton.value !== false);
|
|
|
|
|
2021-06-30 13:48:02 +08:00
|
|
|
// =============== Update Loading ===============
|
|
|
|
const loadingOrDelay = computed(() =>
|
|
|
|
typeof props.loading === 'object' && props.loading.delay
|
|
|
|
? props.loading.delay || true
|
|
|
|
: !!props.loading,
|
|
|
|
);
|
2019-03-13 09:38:54 +08:00
|
|
|
|
2021-06-30 13:48:02 +08:00
|
|
|
watch(
|
|
|
|
loadingOrDelay,
|
2021-06-30 14:41:07 +08:00
|
|
|
val => {
|
2021-06-30 13:48:02 +08:00
|
|
|
clearTimeout(delayTimeoutRef.value);
|
|
|
|
if (typeof loadingOrDelay.value === 'number') {
|
2021-11-29 17:53:09 +08:00
|
|
|
delayTimeoutRef.value = setTimeout(() => {
|
2021-06-30 13:48:02 +08:00
|
|
|
innerLoading.value = val;
|
|
|
|
}, loadingOrDelay.value);
|
|
|
|
} else {
|
|
|
|
innerLoading.value = val;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
immediate: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const classes = computed(() => {
|
|
|
|
const { type, shape, size, ghost, block, danger } = props;
|
|
|
|
const pre = prefixCls.value;
|
2020-03-07 19:45:13 +08:00
|
|
|
// large => lg
|
|
|
|
// small => sm
|
|
|
|
let sizeCls = '';
|
|
|
|
switch (size) {
|
|
|
|
case 'large':
|
|
|
|
sizeCls = 'lg';
|
|
|
|
break;
|
|
|
|
case 'small':
|
|
|
|
sizeCls = 'sm';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-10-26 15:18:08 +08:00
|
|
|
return {
|
2021-06-30 13:48:02 +08:00
|
|
|
[`${pre}`]: true,
|
|
|
|
[`${pre}-${type}`]: type,
|
|
|
|
[`${pre}-${shape}`]: shape,
|
|
|
|
[`${pre}-${sizeCls}`]: sizeCls,
|
|
|
|
[`${pre}-loading`]: innerLoading.value,
|
|
|
|
[`${pre}-background-ghost`]: ghost && !isUnborderedButtonType(type),
|
|
|
|
[`${pre}-two-chinese-chars`]: hasTwoCNChar.value && autoInsertSpace.value,
|
|
|
|
[`${pre}-block`]: block,
|
|
|
|
[`${pre}-dangerous`]: !!danger,
|
|
|
|
[`${pre}-rtl`]: direction.value === 'rtl',
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2021-06-30 13:48:02 +08:00
|
|
|
});
|
2021-06-30 10:03:17 +08:00
|
|
|
|
|
|
|
const fixTwoCNChar = () => {
|
2018-04-06 20:56:19 +08:00
|
|
|
// Fix for HOC usage like <FormatMessage />
|
2021-06-30 10:03:17 +08:00
|
|
|
const node = buttonNodeRef.value!;
|
|
|
|
if (!node || autoInsertSpaceInButton.value === false) {
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-11-28 21:58:42 +08:00
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
const buttonText = node.textContent;
|
2021-06-30 10:03:17 +08:00
|
|
|
|
2021-06-30 13:48:02 +08:00
|
|
|
if (isNeedInserted && isTwoCNChar(buttonText)) {
|
2021-06-30 10:03:17 +08:00
|
|
|
if (!hasTwoCNChar.value) {
|
|
|
|
hasTwoCNChar.value = true;
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
2021-06-30 10:03:17 +08:00
|
|
|
} else if (hasTwoCNChar.value) {
|
|
|
|
hasTwoCNChar.value = false;
|
2018-04-06 20:56:19 +08:00
|
|
|
}
|
2021-06-30 10:03:17 +08:00
|
|
|
};
|
|
|
|
const handleClick = (event: Event) => {
|
|
|
|
// https://github.com/ant-design/ant-design/issues/30207
|
2021-06-30 13:48:02 +08:00
|
|
|
if (innerLoading.value || props.disabled) {
|
2021-06-30 10:03:17 +08:00
|
|
|
event.preventDefault();
|
2019-01-12 11:33:27 +08:00
|
|
|
return;
|
2018-11-28 21:58:42 +08:00
|
|
|
}
|
2021-06-30 10:03:17 +08:00
|
|
|
emit('click', event);
|
|
|
|
};
|
|
|
|
|
|
|
|
const insertSpace = (child: VNode, needInserted: boolean) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const SPACE = needInserted ? ' ' : '';
|
2020-06-20 21:45:08 +08:00
|
|
|
if (child.type === Text) {
|
2020-10-13 18:04:02 +08:00
|
|
|
let text = (child.children as string).trim();
|
2017-12-29 16:51:06 +08:00
|
|
|
if (isTwoCNChar(text)) {
|
2019-01-12 11:33:27 +08:00
|
|
|
text = text.split('').join(SPACE);
|
2017-12-29 16:51:06 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return <span>{text}</span>;
|
2017-12-29 16:51:06 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return child;
|
|
|
|
};
|
2020-06-11 15:58:41 +08:00
|
|
|
|
2021-06-30 10:03:17 +08:00
|
|
|
watchEffect(() => {
|
|
|
|
devWarning(
|
|
|
|
!(props.ghost && isUnborderedButtonType(props.type)),
|
|
|
|
'Button',
|
|
|
|
"`link` or `text` button can't be a `ghost` button.",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(fixTwoCNChar);
|
|
|
|
onUpdated(fixTwoCNChar);
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
2021-06-30 13:48:02 +08:00
|
|
|
delayTimeoutRef.value && clearTimeout(delayTimeoutRef.value);
|
2021-06-30 10:03:17 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
2021-06-30 13:48:02 +08:00
|
|
|
const children = flattenChildren(getPropsSlot(slots, props));
|
|
|
|
|
|
|
|
const icon = getPropsSlot(slots, props, 'icon');
|
|
|
|
|
|
|
|
isNeedInserted = children.length === 1 && !icon && !isUnborderedButtonType(props.type);
|
2021-06-30 10:03:17 +08:00
|
|
|
|
|
|
|
const { type, htmlType, disabled, href, title, target } = props;
|
|
|
|
|
2021-06-30 13:48:02 +08:00
|
|
|
const iconType = innerLoading.value ? 'loading' : icon;
|
2021-06-30 10:03:17 +08:00
|
|
|
const buttonProps = {
|
|
|
|
...attrs,
|
|
|
|
title,
|
|
|
|
disabled,
|
2021-06-30 13:48:02 +08:00
|
|
|
class: [
|
|
|
|
classes.value,
|
|
|
|
attrs.class,
|
|
|
|
{ [`${prefixCls.value}-icon-only`]: children.length === 0 && !!iconType },
|
|
|
|
],
|
2021-06-30 10:03:17 +08:00
|
|
|
onClick: handleClick,
|
|
|
|
};
|
2021-11-27 15:23:08 +08:00
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/4930
|
|
|
|
if (!disabled) {
|
|
|
|
delete buttonProps.disabled;
|
|
|
|
}
|
2021-06-30 10:03:17 +08:00
|
|
|
|
2021-06-30 16:13:05 +08:00
|
|
|
const iconNode = innerLoading.value ? <LoadingOutlined /> : icon;
|
2021-06-30 13:48:02 +08:00
|
|
|
|
2021-06-30 14:41:07 +08:00
|
|
|
const kids = children.map(child =>
|
2021-06-30 13:48:02 +08:00
|
|
|
insertSpace(child, isNeedInserted && autoInsertSpace.value),
|
2021-06-30 10:03:17 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if (href !== undefined) {
|
|
|
|
return (
|
|
|
|
<a {...buttonProps} href={href} target={target} ref={buttonNodeRef}>
|
|
|
|
{iconNode}
|
|
|
|
{kids}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const buttonNode = (
|
|
|
|
<button {...buttonProps} ref={buttonNodeRef} type={htmlType}>
|
2019-01-12 11:33:27 +08:00
|
|
|
{iconNode}
|
|
|
|
{kids}
|
2021-06-30 10:03:17 +08:00
|
|
|
</button>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2019-08-12 11:30:32 +08:00
|
|
|
|
2021-06-30 10:03:17 +08:00
|
|
|
if (isUnborderedButtonType(type)) {
|
|
|
|
return buttonNode;
|
|
|
|
}
|
2019-08-12 11:30:32 +08:00
|
|
|
|
2021-06-30 10:03:17 +08:00
|
|
|
return <Wave ref="wave">{buttonNode}</Wave>;
|
|
|
|
};
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
2020-10-13 18:04:02 +08:00
|
|
|
});
|