2020-10-19 23:09:36 +08:00
|
|
|
import { defineComponent, ExtractPropTypes } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import Button from '../button';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import buttonTypes from '../button/buttonTypes';
|
2020-07-27 22:36:56 +08:00
|
|
|
import { getSlot, findDOMNode } from '../_util/props-util';
|
2019-01-12 11:33:27 +08:00
|
|
|
const ButtonType = buttonTypes().type;
|
2020-10-19 23:09:36 +08:00
|
|
|
|
2018-03-06 19:14:41 +08:00
|
|
|
const ActionButtonProps = {
|
|
|
|
type: ButtonType,
|
|
|
|
actionFn: PropTypes.func,
|
|
|
|
closeModal: PropTypes.func,
|
2020-10-10 18:16:28 +08:00
|
|
|
autofocus: PropTypes.looseBool,
|
2018-12-09 17:34:27 +08:00
|
|
|
buttonProps: PropTypes.object,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-06 19:14:41 +08:00
|
|
|
|
2020-10-19 23:09:36 +08:00
|
|
|
export type IActionButtonProps = ExtractPropTypes<typeof ActionButtonProps>;
|
|
|
|
|
|
|
|
export default defineComponent({
|
2018-03-06 19:14:41 +08:00
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: ActionButtonProps,
|
2020-11-07 15:37:17 +08:00
|
|
|
setup() {
|
2018-03-06 19:14:41 +08:00
|
|
|
return {
|
2020-11-07 15:37:17 +08:00
|
|
|
timeoutId: undefined,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-06 19:14:41 +08:00
|
|
|
},
|
2020-11-07 15:37:17 +08:00
|
|
|
data() {
|
2020-10-19 23:09:36 +08:00
|
|
|
return {
|
2020-11-07 15:37:17 +08:00
|
|
|
loading: false,
|
2020-10-19 23:09:36 +08:00
|
|
|
};
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
2020-07-17 17:36:58 +08:00
|
|
|
if (this.autofocus) {
|
2020-07-27 22:36:56 +08:00
|
|
|
this.timeoutId = setTimeout(() => findDOMNode(this).focus());
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
|
|
|
},
|
2020-06-11 16:13:09 +08:00
|
|
|
beforeUnmount() {
|
2019-01-12 11:33:27 +08:00
|
|
|
clearTimeout(this.timeoutId);
|
2018-03-06 19:14:41 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
onClick() {
|
|
|
|
const { actionFn, closeModal } = this;
|
2018-03-06 19:14:41 +08:00
|
|
|
if (actionFn) {
|
2020-10-25 23:06:03 +08:00
|
|
|
let ret: any;
|
2018-03-06 19:14:41 +08:00
|
|
|
if (actionFn.length) {
|
2019-01-12 11:33:27 +08:00
|
|
|
ret = actionFn(closeModal);
|
2018-03-06 19:14:41 +08:00
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
ret = actionFn();
|
2018-03-06 19:14:41 +08:00
|
|
|
if (!ret) {
|
2019-01-12 11:33:27 +08:00
|
|
|
closeModal();
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret && ret.then) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.setState({ loading: true });
|
|
|
|
ret.then(
|
2020-10-25 23:06:03 +08:00
|
|
|
(...args: any[]) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
// It's unnecessary to set loading=false, for the Modal will be unmounted after close.
|
|
|
|
// this.setState({ loading: false });
|
|
|
|
closeModal(...args);
|
|
|
|
},
|
2020-10-25 23:06:03 +08:00
|
|
|
(e: Event) => {
|
2020-03-07 19:45:13 +08:00
|
|
|
// Emit error when catch promise reject
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(e);
|
2019-01-12 11:33:27 +08:00
|
|
|
// See: https://github.com/ant-design/ant-design/issues/6183
|
|
|
|
this.setState({ loading: false });
|
|
|
|
},
|
|
|
|
);
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-12 11:33:27 +08:00
|
|
|
closeModal();
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2020-06-15 18:55:12 +08:00
|
|
|
const { type, loading, buttonProps } = this;
|
2020-09-07 13:49:12 +08:00
|
|
|
const props = {
|
|
|
|
type,
|
|
|
|
onClick: this.onClick,
|
|
|
|
loading,
|
|
|
|
...buttonProps,
|
|
|
|
};
|
|
|
|
return <Button {...props}>{getSlot(this)}</Button>;
|
2018-03-06 19:14:41 +08:00
|
|
|
},
|
2020-10-19 23:09:36 +08:00
|
|
|
});
|