-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionButton.jsx
69 lines (65 loc) · 1.59 KB
/
ActionButton.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import PropTypes from '../_util/vue-types'
import Button from '../button'
import BaseMixin from '../_util/BaseMixin'
import buttonTypes from '../button/buttonTypes'
const ButtonType = buttonTypes().type
const ActionButtonProps = {
type: ButtonType,
actionFn: PropTypes.func,
closeModal: PropTypes.func,
autoFocus: PropTypes.bool,
}
export default {
mixins: [BaseMixin],
props: ActionButtonProps,
data () {
return {
loading: false,
}
},
mounted () {
if (this.autoFocus) {
this.timeoutId = setTimeout(() => this.$el.focus())
}
},
beforeDestroy () {
clearTimeout(this.timeoutId)
},
methods: {
onClick () {
const { actionFn, closeModal } = this
if (actionFn) {
let ret
if (actionFn.length) {
ret = actionFn(closeModal)
} else {
ret = actionFn()
if (!ret) {
closeModal()
}
}
if (ret && ret.then) {
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)
}, () => {
// See: https://github.com/ant-design/ant-design/issues/6183
this.setState({ loading: false })
})
}
} else {
closeModal()
}
},
},
render () {
const { type, $slots, loading } = this
return (
<Button type={type} onClick={this.onClick} loading={loading}>
{$slots.default}
</Button>
)
},
}