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;
|
2018-03-06 19:14:41 +08:00
|
|
|
const ActionButtonProps = {
|
|
|
|
type: ButtonType,
|
|
|
|
actionFn: PropTypes.func,
|
|
|
|
closeModal: PropTypes.func,
|
2020-07-17 17:36:58 +08:00
|
|
|
autofocus: PropTypes.bool,
|
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
|
|
|
|
|
|
|
export default {
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: ActionButtonProps,
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
2018-03-06 19:14:41 +08:00
|
|
|
return {
|
|
|
|
loading: false,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-06 19:14:41 +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) {
|
2019-01-12 11:33:27 +08:00
|
|
|
let ret;
|
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(
|
|
|
|
(...args) => {
|
|
|
|
// It's unnecessary to set loading=false, for the Modal will be unmounted after close.
|
|
|
|
// this.setState({ loading: false });
|
|
|
|
closeModal(...args);
|
|
|
|
},
|
2020-03-07 19:45:13 +08:00
|
|
|
e => {
|
|
|
|
// 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;
|
2018-03-06 19:14:41 +08:00
|
|
|
return (
|
2019-01-12 11:33:27 +08:00
|
|
|
<Button type={type} onClick={this.onClick} loading={loading} {...buttonProps}>
|
2020-06-15 18:55:12 +08:00
|
|
|
{getSlot(this)}
|
2018-03-06 19:14:41 +08:00
|
|
|
</Button>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-06 19:14:41 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|