PcGlobalForm.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <div class="form global-form">
  3. <el-form class="form-list" :model="formData" :rules="rules" ref="ruleForm">
  4. <el-form-item
  5. v-for="item in formConfig"
  6. :prop="item.prop"
  7. :key="item.label"
  8. >
  9. <!-- 输入框 -->
  10. <el-input
  11. v-if="item.type === 'input'"
  12. v-model="formData[item.prop]"
  13. :disabled="item.disabled"
  14. :style="{ width: item.width }"
  15. @change="item.change && item.change(formData[item.prop])"
  16. :placeholder="item.placeholder"
  17. clearable
  18. ></el-input>
  19. <!-- 密码框 -->
  20. <el-input
  21. v-if="item.type === 'password'"
  22. :type="flag ? 'text' : 'password'"
  23. v-model="formData[item.prop]"
  24. autocomplete="off"
  25. :style="{ width: item.width }"
  26. @change="item.change && item.change(formData[item.prop])"
  27. :placeholder="item.placeholder"
  28. clearable
  29. >
  30. <i
  31. v-if="!flag"
  32. slot="suffix"
  33. class="el-input__icon iconfont"
  34. style="font-size: 22px;"
  35. @click="showPassword"
  36. >&#xebcc;</i
  37. >
  38. <i
  39. v-else
  40. slot="suffix"
  41. class="el-input__icon iconfont"
  42. style="font-size: 22px"
  43. @click="showPassword"
  44. >&#xebcd;</i
  45. >
  46. </el-input>
  47. <!-- 文本域 -->
  48. <el-input
  49. v-if="item.type === 'textarea'"
  50. type="textarea"
  51. v-model="formData[item.prop]"
  52. :disabled="item.disabled"
  53. :style="{ width: item.width }"
  54. :maxlength="item.maxlength"
  55. :rows="item.rows"
  56. :show-word-limit="item.showWordLimit"
  57. @change="item.change && item.change(formData[item.prop])"
  58. ></el-input>
  59. <!-- 下拉框 -->
  60. <el-select
  61. v-if="item.type === 'select'"
  62. v-model="formData[item.prop]"
  63. :disabled="item.disabled"
  64. :style="{ width: item.width }"
  65. @change="item.change && item.change(formData[item.prop])"
  66. clearable
  67. :multiple="multiple"
  68. :placeholder="item.placeholder">
  69. <el-option
  70. v-for="option in item.selectList"
  71. :label="option.name"
  72. :value="option.id"
  73. :key="option.id"
  74. ></el-option>
  75. </el-select>
  76. <!-- 下拉框: selectList为obj类型-->
  77. <el-select
  78. v-if="item.type === 'selectObj'"
  79. v-model="formData[item.prop]"
  80. :disabled="item.disabled"
  81. :style="{ width: item.width }"
  82. @change="item.change && item.change(formData[item.prop])"
  83. filterable
  84. clearable
  85. :placeholder="item.placeholder"
  86. >
  87. <el-option
  88. v-for="(item, key) in item.selectList"
  89. :key="parseInt(key)"
  90. :label="item"
  91. :value="parseInt(key)"
  92. ></el-option>
  93. </el-select>
  94. <!-- 单选框 -->
  95. <el-radio-group
  96. v-if="item.type === 'radio'"
  97. v-model="formData[item.prop]"
  98. :disabled="item.disabled"
  99. @change="item.change && item.change(formData[item.prop])"
  100. >
  101. <el-radio
  102. v-for="radio in item.radios"
  103. :label="radio.label"
  104. :key="radio.label"
  105. ></el-radio>
  106. </el-radio-group>
  107. <!-- 复选框 -->
  108. <el-checkbox-group
  109. v-if="item.type === 'checkbox'"
  110. v-model="formData[item.prop]"
  111. :disabled="item.disabled"
  112. @change="item.change && item.change(formData[item.prop])"
  113. >
  114. <el-checkbox
  115. v-for="item in item.checkboxs"
  116. :label="item.label"
  117. :key="item.label"
  118. >{{ item.label }}</el-checkbox
  119. >
  120. </el-checkbox-group>
  121. <!-- 开关 -->
  122. <el-switch
  123. v-if="item.type === 'switch'"
  124. v-model="formData[item.prop]"
  125. :disabled="item.disabled"
  126. @change="item.change && item.change(formData[item.prop])"
  127. ></el-switch>
  128. <!-- 插槽 -->
  129. <template v-if="item.type === 'slot'">
  130. <slot :name="item.slotName"></slot>
  131. </template>
  132. </el-form-item>
  133. </el-form>
  134. <div class="submit-btn">
  135. <el-button
  136. @click.native="submit"
  137. :loading="submitLoading"
  138. v-if="showLoginBtn"
  139. >
  140. LOGIN
  141. </el-button>
  142. <el-button
  143. @click.native="submit"
  144. :loading="submitLoading"
  145. v-if="showRegisterBtn"
  146. >
  147. REGISTER
  148. </el-button>
  149. <el-button @click.native="close" v-if="showDeleteBtn">取 消</el-button>
  150. </div>
  151. </div>
  152. </template>
  153. <script>
  154. export default {
  155. name: "GlobalForm",
  156. props: {
  157. showDeleteBtn: {
  158. type: Boolean,
  159. default: true,
  160. },
  161. showLoginBtn: {
  162. type: Boolean,
  163. default: true,
  164. },
  165. showRegisterBtn: {
  166. type: Boolean,
  167. default: true,
  168. },
  169. // 表单配置
  170. formConfig: {
  171. type: Array,
  172. required: true,
  173. },
  174. // 表单数据
  175. formData: {
  176. type: Object,
  177. required: true,
  178. },
  179. // 表单规则
  180. rules: {
  181. type: Object,
  182. },
  183. submitLoading: {
  184. type: Boolean,
  185. },
  186. },
  187. data() {
  188. return {
  189. flag: false,
  190. };
  191. },
  192. methods: {
  193. submit() {
  194. this.$refs.ruleForm.validate((valid) => {
  195. if (valid) {
  196. this.$emit("handleSubmit");
  197. } else {
  198. console.log("error submit!!");
  199. return false;
  200. }
  201. });
  202. },
  203. close() {
  204. this.$emit("handleClose");
  205. },
  206. showPassword() {
  207. this.flag = !this.flag;
  208. },
  209. },
  210. };
  211. </script>
  212. <style lang="scss" scope>
  213. .global-form {
  214. padding: 0 30px;
  215. margin-top: 20px;
  216. .el-input__inner {
  217. height: 50px;
  218. line-height: 50px;
  219. }
  220. .el-input__suffix .el-input__suffix-inner{
  221. margin-top: 5px;
  222. display: flex;
  223. justify-content: center;
  224. align-items: center;
  225. }
  226. .submit-btn {
  227. display: flex;
  228. justify-content: center;
  229. .el-button {
  230. background-color: #e90000;
  231. width: 156px;
  232. height: 50px;
  233. color: #fff;
  234. border-radius: 6px;
  235. font-weight: 600;
  236. }
  237. }
  238. }
  239. </style>