ant-design-vue/components/modal/ActionButton.tsx

114 lines
3.1 KiB
Vue
Raw Normal View History

2021-06-30 14:41:07 +08:00
import type { ExtractPropTypes, PropType } from 'vue';
import { onMounted, ref, defineComponent, onBeforeUnmount } from 'vue';
2019-01-12 11:33:27 +08:00
import Button from '../button';
import type { ButtonProps } from '../button';
2021-06-30 14:45:05 +08:00
import type { LegacyButtonType } from '../button/buttonTypes';
import { convertLegacyProps } from '../button/buttonTypes';
2020-10-19 23:09:36 +08:00
const actionButtonProps = {
type: {
type: String as PropType<LegacyButtonType>,
},
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
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({
name: 'ActionButton',
props: actionButtonProps,
setup(props, { slots }) {
const clickedRef = ref<boolean>(false);
const buttonRef = ref();
const loading = ref(false);
let timeoutId: any;
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[]) => {
loading.value = false;
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
loading.value = false;
clickedRef.value = false;
},
);
2020-10-19 23:09:36 +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
}
} 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 {
returnValueOfOnOk = actionFn();
if (!returnValueOfOnOk) {
close();
return;
}
2018-03-06 19:14:41 +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
});