2021-06-30 10:03:17 +08:00
|
|
|
import {
|
|
|
|
computed,
|
|
|
|
defineComponent,
|
|
|
|
onBeforeUnmount,
|
|
|
|
onMounted,
|
|
|
|
onUpdated,
|
2023-04-05 22:03:02 +08:00
|
|
|
shallowRef,
|
2021-06-30 10:03:17 +08:00
|
|
|
Text,
|
|
|
|
watch,
|
|
|
|
watchEffect,
|
|
|
|
} from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Wave from '../_util/wave';
|
2022-03-26 22:52:54 +08:00
|
|
|
import buttonProps from './buttonTypes';
|
2022-03-19 15:21:32 +08:00
|
|
|
import { flattenChildren, initDefaultProps } from '../_util/props-util';
|
2023-01-27 16:00:17 +08:00
|
|
|
import useConfigInject from '../config-provider/hooks/useConfigInject';
|
2023-06-15 11:17:58 +08:00
|
|
|
import { useInjectDisabled } from '../config-provider/DisabledContext';
|
2021-06-30 10:03:17 +08:00
|
|
|
import devWarning from '../vc-util/devWarning';
|
2022-03-19 15:21:32 +08:00
|
|
|
import LoadingIcon from './LoadingIcon';
|
2023-01-26 21:42:27 +08:00
|
|
|
import useStyle from './style';
|
2021-06-30 10:03:17 +08:00
|
|
|
import type { ButtonType } from './buttonTypes';
|
2023-04-05 22:03:02 +08:00
|
|
|
import type { VNode } from 'vue';
|
2022-05-10 16:18:44 +08:00
|
|
|
import { GroupSizeContext } from './button-group';
|
2023-02-12 09:26:39 +08:00
|
|
|
import { useCompactItemContext } from '../space/Compact';
|
2023-05-18 16:22:12 +08:00
|
|
|
import type { CustomSlotsType } from '../_util/type';
|
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);
|
2021-06-30 10:03:17 +08:00
|
|
|
|
2022-05-10 16:18:44 +08:00
|
|
|
function isUnBorderedButtonType(type: ButtonType | undefined) {
|
2021-06-30 10:03:17 +08:00
|
|
|
return type === 'text' || type === 'link';
|
|
|
|
}
|
2022-03-26 22:52:54 +08:00
|
|
|
export { buttonProps };
|
2020-10-13 18:04:02 +08:00
|
|
|
export default defineComponent({
|
2022-09-26 21:33:41 +08:00
|
|
|
compatConfig: { MODE: 3 },
|
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,
|
2022-03-26 22:52:54 +08:00
|
|
|
props: initDefaultProps(buttonProps(), { type: 'default' }),
|
2023-05-18 16:22:12 +08:00
|
|
|
slots: Object as CustomSlotsType<{
|
|
|
|
icon: any;
|
|
|
|
default: any;
|
|
|
|
}>,
|
2022-03-26 22:52:54 +08:00
|
|
|
// emits: ['click', 'mousedown'],
|
2023-04-22 21:21:15 +08:00
|
|
|
setup(props, { slots, attrs, emit, expose }) {
|
2022-03-12 09:56:32 +08:00
|
|
|
const { prefixCls, autoInsertSpaceInButton, direction, size } = useConfigInject('btn', props);
|
2023-01-26 21:42:27 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2023-03-03 17:13:48 +08:00
|
|
|
const groupSizeContext = GroupSizeContext.useInject();
|
2023-06-15 11:17:58 +08:00
|
|
|
const disabledContext = useInjectDisabled();
|
|
|
|
const mergedDisabled = computed(() => props.disabled ?? disabledContext.value);
|
2023-04-05 22:03:02 +08:00
|
|
|
const buttonNodeRef = shallowRef<HTMLElement>(null);
|
|
|
|
const delayTimeoutRef = shallowRef(undefined);
|
2021-06-30 13:48:02 +08:00
|
|
|
let isNeedInserted = false;
|
2021-06-30 10:03:17 +08:00
|
|
|
|
2023-04-05 22:03:02 +08:00
|
|
|
const innerLoading = shallowRef<Loading>(false);
|
|
|
|
const hasTwoCNChar = shallowRef(false);
|
2021-06-30 10:03:17 +08:00
|
|
|
|
|
|
|
const autoInsertSpace = computed(() => autoInsertSpaceInButton.value !== false);
|
2023-02-12 09:26:39 +08:00
|
|
|
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
|
2021-06-30 10:03:17 +08:00
|
|
|
|
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(() => {
|
2022-03-12 09:56:32 +08:00
|
|
|
const { type, shape = 'default', ghost, block, danger } = props;
|
2021-06-30 13:48:02 +08:00
|
|
|
const pre = prefixCls.value;
|
2022-03-12 09:56:32 +08:00
|
|
|
|
|
|
|
const sizeClassNameMap = { large: 'lg', small: 'sm', middle: undefined };
|
2023-03-03 17:13:48 +08:00
|
|
|
const sizeFullname = compactSize.value || groupSizeContext?.size || size.value;
|
2022-03-12 09:56:32 +08:00
|
|
|
const sizeCls = sizeFullname ? sizeClassNameMap[sizeFullname] || '' : '';
|
|
|
|
|
2023-02-12 09:26:39 +08:00
|
|
|
return [
|
|
|
|
compactItemClassnames.value,
|
|
|
|
{
|
|
|
|
[hashId.value]: true,
|
|
|
|
[`${pre}`]: true,
|
|
|
|
[`${pre}-${shape}`]: shape !== 'default' && shape,
|
|
|
|
[`${pre}-${type}`]: type,
|
|
|
|
[`${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',
|
|
|
|
},
|
|
|
|
];
|
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
|
2023-06-15 11:17:58 +08:00
|
|
|
if (innerLoading.value || mergedDisabled.value) {
|
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);
|
|
|
|
};
|
2023-01-26 21:42:27 +08:00
|
|
|
const handleMousedown = (event: Event) => {
|
|
|
|
emit('mousedown', event);
|
|
|
|
};
|
2021-06-30 10:03:17 +08:00
|
|
|
|
|
|
|
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(
|
2022-05-10 16:18:44 +08:00
|
|
|
!(props.ghost && isUnBorderedButtonType(props.type)),
|
2021-06-30 10:03:17 +08:00
|
|
|
'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
|
|
|
});
|
|
|
|
|
2023-04-22 21:21:15 +08:00
|
|
|
const focus = () => {
|
|
|
|
buttonNodeRef.value?.focus();
|
|
|
|
};
|
|
|
|
const blur = () => {
|
|
|
|
buttonNodeRef.value?.blur();
|
|
|
|
};
|
|
|
|
expose({
|
|
|
|
focus,
|
|
|
|
blur,
|
|
|
|
});
|
|
|
|
|
2021-06-30 10:03:17 +08:00
|
|
|
return () => {
|
2022-03-19 15:21:32 +08:00
|
|
|
const { icon = slots.icon?.() } = props;
|
|
|
|
const children = flattenChildren(slots.default?.());
|
2021-06-30 13:48:02 +08:00
|
|
|
|
2022-05-10 16:18:44 +08:00
|
|
|
isNeedInserted = children.length === 1 && !icon && !isUnBorderedButtonType(props.type);
|
2021-06-30 10:03:17 +08:00
|
|
|
|
2023-06-15 11:17:58 +08:00
|
|
|
const { type, htmlType, href, title, target } = props;
|
2021-06-30 10:03:17 +08:00
|
|
|
|
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,
|
2023-06-15 11:17:58 +08:00
|
|
|
disabled: mergedDisabled.value,
|
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,
|
2023-01-26 21:42:27 +08:00
|
|
|
onMousedown: handleMousedown,
|
2021-06-30 10:03:17 +08:00
|
|
|
};
|
2021-11-27 15:23:08 +08:00
|
|
|
// https://github.com/vueComponent/ant-design-vue/issues/4930
|
2023-06-15 11:17:58 +08:00
|
|
|
if (!mergedDisabled.value) {
|
2021-11-27 15:23:08 +08:00
|
|
|
delete buttonProps.disabled;
|
|
|
|
}
|
2022-03-19 15:21:32 +08:00
|
|
|
const iconNode =
|
|
|
|
icon && !innerLoading.value ? (
|
|
|
|
icon
|
|
|
|
) : (
|
|
|
|
<LoadingIcon
|
|
|
|
existIcon={!!icon}
|
|
|
|
prefixCls={prefixCls.value}
|
|
|
|
loading={!!innerLoading.value}
|
|
|
|
/>
|
|
|
|
);
|
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) {
|
2023-01-26 21:42:27 +08:00
|
|
|
return wrapSSR(
|
2021-06-30 10:03:17 +08:00
|
|
|
<a {...buttonProps} href={href} target={target} ref={buttonNodeRef}>
|
|
|
|
{iconNode}
|
|
|
|
{kids}
|
2023-01-26 21:42:27 +08:00
|
|
|
</a>,
|
2021-06-30 10:03:17 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-26 21:42:27 +08:00
|
|
|
let buttonNode = (
|
2021-06-30 10:03:17 +08:00
|
|
|
<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
|
|
|
|
2023-01-26 21:42:27 +08:00
|
|
|
if (!isUnBorderedButtonType(type)) {
|
2023-02-12 09:26:39 +08:00
|
|
|
buttonNode = (
|
|
|
|
<Wave ref="wave" disabled={!!innerLoading.value}>
|
|
|
|
{buttonNode}
|
|
|
|
</Wave>
|
|
|
|
);
|
2021-06-30 10:03:17 +08:00
|
|
|
}
|
2019-08-12 11:30:32 +08:00
|
|
|
|
2023-02-02 18:08:36 +08:00
|
|
|
return wrapSSR(buttonNode);
|
2021-06-30 10:03:17 +08:00
|
|
|
};
|
2017-10-26 15:18:08 +08:00
|
|
|
},
|
2020-10-13 18:04:02 +08:00
|
|
|
});
|