You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.3 KiB
120 lines
3.3 KiB
3 years ago
|
import type { ExtractPropTypes, PropType } from 'vue';
|
||
3 years ago
|
import { onMounted, ref, defineComponent, onBeforeUnmount } from 'vue';
|
||
6 years ago
|
import Button from '../button';
|
||
3 years ago
|
import type { ButtonProps } from '../button';
|
||
3 years ago
|
import type { LegacyButtonType } from '../button/buttonTypes';
|
||
|
import { convertLegacyProps } from '../button/buttonTypes';
|
||
3 years ago
|
import useDestroyed from './hooks/useDestroyed';
|
||
4 years ago
|
|
||
3 years ago
|
const actionButtonProps = {
|
||
3 years ago
|
type: {
|
||
|
type: String as PropType<LegacyButtonType>,
|
||
|
},
|
||
3 years ago
|
actionFn: Function as PropType<(...args: any[]) => any | PromiseLike<any>>,
|
||
|
close: Function,
|
||
|
autofocus: Boolean,
|
||
|
prefixCls: String,
|
||
|
buttonProps: Object as PropType<ButtonProps>,
|
||
|
emitEvent: Boolean,
|
||
|
quitOnNullishReturnValue: Boolean,
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
3 years ago
|
export type ActionButtonProps = ExtractPropTypes<typeof actionButtonProps>;
|
||
|
|
||
|
function isThenable(thing?: PromiseLike<any>): boolean {
|
||
|
return !!(thing && !!thing.then);
|
||
|
}
|
||
4 years ago
|
|
||
|
export default defineComponent({
|
||
3 years ago
|
name: 'ActionButton',
|
||
|
props: actionButtonProps,
|
||
|
setup(props, { slots }) {
|
||
|
const clickedRef = ref<boolean>(false);
|
||
|
const buttonRef = ref();
|
||
|
const loading = ref(false);
|
||
|
let timeoutId: any;
|
||
3 years ago
|
const isDestroyed = useDestroyed();
|
||
3 years ago
|
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[]) => {
|
||
3 years ago
|
if (!isDestroyed.value) {
|
||
|
loading.value = false;
|
||
|
}
|
||
3 years ago
|
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
|
||
3 years ago
|
if (!isDestroyed.value) {
|
||
|
loading.value = false;
|
||
|
}
|
||
3 years ago
|
clickedRef.value = false;
|
||
|
},
|
||
|
);
|
||
4 years ago
|
};
|
||
3 years ago
|
|
||
|
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;
|
||
7 years ago
|
}
|
||
3 years ago
|
} else if (actionFn.length) {
|
||
|
returnValueOfOnOk = actionFn(close);
|
||
|
// https://github.com/ant-design/ant-design/issues/23358
|
||
|
clickedRef.value = false;
|
||
7 years ago
|
} else {
|
||
3 years ago
|
returnValueOfOnOk = actionFn();
|
||
|
if (!returnValueOfOnOk) {
|
||
|
close();
|
||
|
return;
|
||
|
}
|
||
7 years ago
|
}
|
||
3 years ago
|
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>
|
||
|
);
|
||
4 years ago
|
};
|
||
7 years ago
|
},
|
||
4 years ago
|
});
|