2021-06-30 14:41:07 +08:00
|
|
|
import type { ExtractPropTypes, PropType } from 'vue';
|
2022-01-06 16:08:53 +08:00
|
|
|
import { onMounted, ref, defineComponent, onBeforeUnmount } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import Button from '../button';
|
2022-01-06 16:08:53 +08:00
|
|
|
import type { ButtonProps } from '../button';
|
2021-06-30 14:45:05 +08:00
|
|
|
import type { LegacyButtonType } from '../button/buttonTypes';
|
|
|
|
import { convertLegacyProps } from '../button/buttonTypes';
|
2022-03-12 09:56:32 +08:00
|
|
|
import useDestroyed from './hooks/useDestroyed';
|
2020-10-19 23:09:36 +08:00
|
|
|
|
2022-01-06 16:08:53 +08:00
|
|
|
const actionButtonProps = {
|
2021-06-30 10:03:17 +08:00
|
|
|
type: {
|
|
|
|
type: String as PropType<LegacyButtonType>,
|
|
|
|
},
|
2022-01-06 16:08:53 +08:00
|
|
|
actionFn: Function as PropType<(...args: any[]) => any | PromiseLike<any>>,
|
|
|
|
close: Function,
|
|
|
|
autofocus: Boolean,
|
|
|
|
prefixCls: String,
|
|
|
|
buttonProps: Object as PropType<ButtonProps>,
|
|
|
|
emitEvent: Boolean,
|
|
|
|
quitOnNullishReturnValue: Boolean,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-06 19:14:41 +08:00
|
|
|
|
2022-01-06 16:08:53 +08:00
|
|
|
export type ActionButtonProps = ExtractPropTypes<typeof actionButtonProps>;
|
|
|
|
|
|
|
|
function isThenable(thing?: PromiseLike<any>): boolean {
|
|
|
|
return !!(thing && !!thing.then);
|
|
|
|
}
|
2020-10-19 23:09:36 +08:00
|
|
|
|
|
|
|
export default defineComponent({
|
2022-01-06 16:08:53 +08:00
|
|
|
name: 'ActionButton',
|
|
|
|
props: actionButtonProps,
|
|
|
|
setup(props, { slots }) {
|
|
|
|
const clickedRef = ref<boolean>(false);
|
|
|
|
const buttonRef = ref();
|
|
|
|
const loading = ref(false);
|
|
|
|
let timeoutId: any;
|
2022-03-12 09:56:32 +08:00
|
|
|
const isDestroyed = useDestroyed();
|
2022-01-06 16:08:53 +08:00
|
|
|
onMounted(() => {
|
|
|
|
if (props.autofocus) {
|
|
|
|
timeoutId = setTimeout(() => buttonRef.value.$el?.focus());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
});
|
|
|
|
|
|
|
|
const handlePromiseOnOk = (returnValueOfOnOk?: PromiseLike<any>) => {
|
|
|
|
const { close } = props;
|
|
|
|
if (!isThenable(returnValueOfOnOk)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loading.value = true;
|
|
|
|
returnValueOfOnOk!.then(
|
|
|
|
(...args: any[]) => {
|
2022-03-12 09:56:32 +08:00
|
|
|
if (!isDestroyed.value) {
|
|
|
|
loading.value = false;
|
|
|
|
}
|
2022-01-06 16:08:53 +08:00
|
|
|
close(...args);
|
|
|
|
clickedRef.value = false;
|
|
|
|
},
|
|
|
|
(e: Error) => {
|
|
|
|
// Emit error when catch promise reject
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(e);
|
|
|
|
// See: https://github.com/ant-design/ant-design/issues/6183
|
2022-03-12 09:56:32 +08:00
|
|
|
if (!isDestroyed.value) {
|
|
|
|
loading.value = false;
|
|
|
|
}
|
2022-01-06 16:08:53 +08:00
|
|
|
clickedRef.value = false;
|
|
|
|
},
|
|
|
|
);
|
2020-10-19 23:09:36 +08:00
|
|
|
};
|
2022-01-06 16:08:53 +08:00
|
|
|
|
|
|
|
const onClick = (e: MouseEvent) => {
|
|
|
|
const { actionFn, close = () => {} } = props;
|
|
|
|
if (clickedRef.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
clickedRef.value = true;
|
|
|
|
if (!actionFn) {
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let returnValueOfOnOk;
|
|
|
|
if (props.emitEvent) {
|
|
|
|
returnValueOfOnOk = actionFn(e);
|
|
|
|
if (props.quitOnNullishReturnValue && !isThenable(returnValueOfOnOk)) {
|
|
|
|
clickedRef.value = false;
|
|
|
|
close(e);
|
|
|
|
return;
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2022-01-06 16:08:53 +08:00
|
|
|
} else if (actionFn.length) {
|
|
|
|
returnValueOfOnOk = actionFn(close);
|
|
|
|
// https://github.com/ant-design/ant-design/issues/23358
|
|
|
|
clickedRef.value = false;
|
2018-03-06 19:14:41 +08:00
|
|
|
} else {
|
2022-01-06 16:08:53 +08:00
|
|
|
returnValueOfOnOk = actionFn();
|
|
|
|
if (!returnValueOfOnOk) {
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2022-01-06 16:08:53 +08:00
|
|
|
handlePromiseOnOk(returnValueOfOnOk);
|
|
|
|
};
|
|
|
|
return () => {
|
|
|
|
const { type, prefixCls, buttonProps } = props;
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
{...convertLegacyProps(type)}
|
|
|
|
onClick={onClick}
|
|
|
|
loading={loading.value}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
{...buttonProps}
|
|
|
|
ref={buttonRef}
|
|
|
|
v-slots={slots}
|
|
|
|
></Button>
|
|
|
|
);
|
2020-09-07 13:49:12 +08:00
|
|
|
};
|
2018-03-06 19:14:41 +08:00
|
|
|
},
|
2020-10-19 23:09:36 +08:00
|
|
|
});
|