haiwei-ui 1.3.6 → 1.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,138 +1,142 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<el-form class="nm-form" ref="form" :model="model" :rules="rules" :label-width="labelWidth"
|
|
2
|
+
<el-form class="nm-form" ref="form" :model="model" :rules="rules" :label-width="labelWidth"
|
|
3
|
+
:label-position="labelPosition" :size="fontSize" :inline="inline" :disabled="disabled" v-loading="showLoading"
|
|
4
|
+
:element-loading-text="loadingText" :element-loading-background="loadingBackground"
|
|
5
|
+
:element-loading-spinner="loadingSpinner" @validate="onValidate">
|
|
3
6
|
<slot />
|
|
4
7
|
</el-form>
|
|
5
8
|
</template>
|
|
6
9
|
<script>
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
import loading from '../../mixins/components/loading'
|
|
11
|
+
export default {
|
|
12
|
+
name: 'Form',
|
|
13
|
+
mixins: [loading],
|
|
14
|
+
data() {
|
|
15
|
+
return {
|
|
16
|
+
loading_: false
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
props: {
|
|
20
|
+
/** 表单对象 */
|
|
21
|
+
model: {
|
|
22
|
+
type: Object,
|
|
23
|
+
required: true
|
|
15
24
|
},
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
/** 行内表单模式 */
|
|
27
|
-
inline: Boolean,
|
|
28
|
-
/** 是否显示成功提示消息 */
|
|
29
|
-
successMsg: {
|
|
30
|
-
type: Boolean,
|
|
31
|
-
default: true
|
|
32
|
-
},
|
|
33
|
-
/** 成功提示消息文本 */
|
|
34
|
-
successMsgText: {
|
|
35
|
-
type: String,
|
|
36
|
-
default: '保存成功'
|
|
37
|
-
},
|
|
38
|
-
/** 标签的宽度 */
|
|
39
|
-
labelWidth: {
|
|
40
|
-
type: String,
|
|
41
|
-
default: '100px'
|
|
42
|
-
},
|
|
43
|
-
/** 表单域标签的位置,如果值为 left 或者 right 时,则需要设置 label-width */
|
|
44
|
-
labelPosition: {
|
|
45
|
-
type: String,
|
|
46
|
-
default: 'right'
|
|
47
|
-
},
|
|
48
|
-
// 自定义验证
|
|
49
|
-
customValidate: Function,
|
|
50
|
-
/** 禁用表单 */
|
|
51
|
-
disabled: Boolean,
|
|
52
|
-
/** 显示加载动画 */
|
|
53
|
-
loading: Boolean,
|
|
54
|
-
/** 不显示加载动画 */
|
|
55
|
-
noLoading: Boolean,
|
|
56
|
-
/** 自定义重置操作 */
|
|
57
|
-
customResetFunction: Function
|
|
25
|
+
/** 验证规则 */
|
|
26
|
+
rules: Object,
|
|
27
|
+
/** 提交请求 */
|
|
28
|
+
action: Function,
|
|
29
|
+
/** 行内表单模式 */
|
|
30
|
+
inline: Boolean,
|
|
31
|
+
/** 是否显示成功提示消息 */
|
|
32
|
+
successMsg: {
|
|
33
|
+
type: Boolean,
|
|
34
|
+
default: true
|
|
58
35
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
36
|
+
/** 成功提示消息文本 */
|
|
37
|
+
successMsgText: {
|
|
38
|
+
type: String,
|
|
39
|
+
default: '保存成功'
|
|
63
40
|
},
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
} else {
|
|
101
|
-
this.$refs.form.resetFields()
|
|
102
|
-
this.resetChildren(this.$refs.form)
|
|
103
|
-
}
|
|
104
|
-
this.$emit('reset')
|
|
105
|
-
},
|
|
106
|
-
/** 重置子组件 */
|
|
107
|
-
resetChildren(vnode) {
|
|
108
|
-
if (vnode.$children && vnode.$children.length > 0) {
|
|
109
|
-
vnode.$children.forEach(item => {
|
|
110
|
-
if (item && item.reset && typeof item.reset === 'function') {
|
|
111
|
-
item.reset()
|
|
41
|
+
/** 标签的宽度 */
|
|
42
|
+
labelWidth: {
|
|
43
|
+
type: String,
|
|
44
|
+
default: '100px'
|
|
45
|
+
},
|
|
46
|
+
/** 表单域标签的位置,如果值为 left 或者 right 时,则需要设置 label-width */
|
|
47
|
+
labelPosition: {
|
|
48
|
+
type: String,
|
|
49
|
+
default: 'right'
|
|
50
|
+
},
|
|
51
|
+
// 自定义验证
|
|
52
|
+
customValidate: Function,
|
|
53
|
+
/** 禁用表单 */
|
|
54
|
+
disabled: Boolean,
|
|
55
|
+
/** 显示加载动画 */
|
|
56
|
+
loading: Boolean,
|
|
57
|
+
/** 不显示加载动画 */
|
|
58
|
+
noLoading: Boolean,
|
|
59
|
+
/** 自定义重置操作 */
|
|
60
|
+
customResetFunction: Function
|
|
61
|
+
},
|
|
62
|
+
computed: {
|
|
63
|
+
showLoading() {
|
|
64
|
+
return !this.noLoading && (this.loading_ || this.loading)
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
methods: {
|
|
68
|
+
/** 提交 */
|
|
69
|
+
submit() {
|
|
70
|
+
this.validate(() => {
|
|
71
|
+
this.openLoading()
|
|
72
|
+
|
|
73
|
+
this.action(this.model)
|
|
74
|
+
.then(data => {
|
|
75
|
+
if (this.successMsg === true) {
|
|
76
|
+
this._success(this.successMsgText)
|
|
112
77
|
}
|
|
113
|
-
this
|
|
78
|
+
this.$emit('success', data)
|
|
114
79
|
})
|
|
80
|
+
.catch(() => {
|
|
81
|
+
this.$emit('error')
|
|
82
|
+
})
|
|
83
|
+
.finally(() => {
|
|
84
|
+
this.closeLoading()
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
},
|
|
88
|
+
/** 表单验证 */
|
|
89
|
+
validate(callback) {
|
|
90
|
+
this.$refs.form.validate(async valid => {
|
|
91
|
+
// 自定义验证
|
|
92
|
+
if (valid && (!this.customValidate || this.customValidate() === true)) {
|
|
93
|
+
callback()
|
|
94
|
+
} else {
|
|
95
|
+
// 验证失败
|
|
96
|
+
this.$emit('validate-error')
|
|
115
97
|
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
98
|
+
})
|
|
99
|
+
},
|
|
100
|
+
/** 重置 */
|
|
101
|
+
reset() {
|
|
102
|
+
if (this.customResetFunction) {
|
|
103
|
+
this.customResetFunction()
|
|
104
|
+
} else {
|
|
105
|
+
this.$refs.form.resetFields()
|
|
106
|
+
this.resetChildren(this.$refs.form)
|
|
107
|
+
}
|
|
108
|
+
this.$emit('reset')
|
|
109
|
+
},
|
|
110
|
+
/** 重置子组件 */
|
|
111
|
+
resetChildren(vnode) {
|
|
112
|
+
if (vnode.$children && vnode.$children.length > 0) {
|
|
113
|
+
vnode.$children.forEach(item => {
|
|
114
|
+
if (item && item.reset && typeof item.reset === 'function') {
|
|
115
|
+
item.reset()
|
|
116
|
+
}
|
|
117
|
+
this.resetChildren(item)
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
/** 清除验证结果 */
|
|
122
|
+
clearValidate(props) {
|
|
123
|
+
this.$refs.form.clearValidate(props)
|
|
124
|
+
},
|
|
125
|
+
/** 打开加载中 */
|
|
126
|
+
openLoading() {
|
|
127
|
+
if (!this.noLoading) {
|
|
128
|
+
this.loading_ = true
|
|
135
129
|
}
|
|
130
|
+
},
|
|
131
|
+
/** 关闭加载中 */
|
|
132
|
+
closeLoading() {
|
|
133
|
+
if (!this.noLoading) {
|
|
134
|
+
this.loading_ = false
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
onValidate(prop, valid, msg) {
|
|
138
|
+
this.$emit('validate', prop, valid, msg)
|
|
136
139
|
}
|
|
137
140
|
}
|
|
141
|
+
}
|
|
138
142
|
</script>
|
|
@@ -150,35 +150,39 @@ export default {
|
|
|
150
150
|
}
|
|
151
151
|
},
|
|
152
152
|
methods: {
|
|
153
|
-
/** 提交 */
|
|
154
153
|
async submit() {
|
|
155
154
|
// 设置loading状态
|
|
156
155
|
this.loading_ = true
|
|
157
|
-
|
|
158
156
|
// 触发 before-submit 钩子
|
|
159
157
|
if (this.$listeners['before-submit']) {
|
|
160
158
|
try {
|
|
161
|
-
// 执行 before-submit
|
|
162
|
-
//
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
159
|
+
// 执行 before-submit 钩子
|
|
160
|
+
// $emit返回事件处理函数的返回值数组
|
|
161
|
+
const results = this.$emit('before-submit')
|
|
162
|
+
// 如果有返回值且是Promise,等待它
|
|
163
|
+
if (results && results.length > 0) {
|
|
164
|
+
const firstResult = results[0]
|
|
165
|
+
if (firstResult && typeof firstResult.then === 'function') {
|
|
166
|
+
const result = await firstResult
|
|
167
|
+
if (result === false) {
|
|
168
|
+
this.loading_ = false
|
|
169
|
+
return
|
|
170
|
+
}
|
|
171
|
+
} else if (firstResult === false) {
|
|
172
|
+
this.loading_ = false
|
|
173
|
+
return
|
|
174
|
+
}
|
|
169
175
|
}
|
|
170
|
-
// 返回true或undefined则继续提交
|
|
171
176
|
} catch (error) {
|
|
172
177
|
this.loading_ = false
|
|
173
178
|
this.$emit('error', error)
|
|
174
179
|
return
|
|
175
180
|
}
|
|
176
181
|
}
|
|
177
|
-
|
|
178
|
-
console.log('test',this.model)
|
|
179
|
-
// 继续原有提交逻辑
|
|
182
|
+
console.log('test', this.model)
|
|
180
183
|
this.$refs.form.submit()
|
|
181
184
|
},
|
|
185
|
+
|
|
182
186
|
/** 重置 */
|
|
183
187
|
reset() {
|
|
184
188
|
this.$nextTick(() => {
|