2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import Button from '../button';
|
|
|
|
import BaseMixin from '../_util/BaseMixin';
|
|
|
|
import buttonTypes from '../button/buttonTypes';
|
2020-07-27 14:36:56 +00:00
|
|
|
import { getSlot, findDOMNode } from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
const ButtonType = buttonTypes().type;
|
2018-03-06 11:14:41 +00:00
|
|
|
const ActionButtonProps = {
|
|
|
|
type: ButtonType,
|
|
|
|
actionFn: PropTypes.func,
|
|
|
|
closeModal: PropTypes.func,
|
2020-07-17 09:36:58 +00:00
|
|
|
autofocus: PropTypes.bool,
|
2018-12-09 09:34:27 +00:00
|
|
|
buttonProps: PropTypes.object,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-06 11:14:41 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: ActionButtonProps,
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
2018-03-06 11:14:41 +00:00
|
|
|
return {
|
|
|
|
loading: false,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-06 11:14:41 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2020-07-17 09:36:58 +00:00
|
|
|
if (this.autofocus) {
|
2020-07-27 14:36:56 +00:00
|
|
|
this.timeoutId = setTimeout(() => findDOMNode(this).focus());
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
|
|
|
},
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2019-01-12 03:33:27 +00:00
|
|
|
clearTimeout(this.timeoutId);
|
2018-03-06 11:14:41 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
onClick() {
|
|
|
|
const { actionFn, closeModal } = this;
|
2018-03-06 11:14:41 +00:00
|
|
|
if (actionFn) {
|
2019-01-12 03:33:27 +00:00
|
|
|
let ret;
|
2018-03-06 11:14:41 +00:00
|
|
|
if (actionFn.length) {
|
2019-01-12 03:33:27 +00:00
|
|
|
ret = actionFn(closeModal);
|
2018-03-06 11:14:41 +00:00
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
ret = actionFn();
|
2018-03-06 11:14:41 +00:00
|
|
|
if (!ret) {
|
2019-01-12 03:33:27 +00:00
|
|
|
closeModal();
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret && ret.then) {
|
2019-01-12 03:33:27 +00: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 11:45:13 +00:00
|
|
|
e => {
|
|
|
|
// Emit error when catch promise reject
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(e);
|
2019-01-12 03:33:27 +00:00
|
|
|
// See: https://github.com/ant-design/ant-design/issues/6183
|
|
|
|
this.setState({ loading: false });
|
|
|
|
},
|
|
|
|
);
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-12 03:33:27 +00:00
|
|
|
closeModal();
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-06-15 10:55:12 +00:00
|
|
|
const { type, loading, buttonProps } = this;
|
2020-09-07 05:49:12 +00:00
|
|
|
const props = {
|
|
|
|
type,
|
|
|
|
onClick: this.onClick,
|
|
|
|
loading,
|
|
|
|
...buttonProps,
|
|
|
|
};
|
|
|
|
return <Button {...props}>{getSlot(this)}</Button>;
|
2018-03-06 11:14:41 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|