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';
|
|
|
|
const ButtonType = buttonTypes().type;
|
2018-03-06 19:14:41 +08:00
|
|
|
const ActionButtonProps = {
|
|
|
|
type: ButtonType,
|
|
|
|
actionFn: PropTypes.func,
|
|
|
|
closeModal: PropTypes.func,
|
|
|
|
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() {
|
2018-03-06 19:14:41 +08:00
|
|
|
if (this.autoFocus) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.timeoutId = setTimeout(() => this.$el.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() {
|
|
|
|
const { type, $slots, 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}>
|
2018-03-06 19:14:41 +08:00
|
|
|
{$slots.default}
|
|
|
|
</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
|
|
|
};
|