u-form.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <view class="u-form">
  3. <slot />
  4. </view>
  5. </template>
  6. <script>
  7. /**
  8. * form 表单
  9. * @description 此组件一般用于表单场景,可以配置Input输入框,Select弹出框,进行表单验证等。
  10. * @tutorial https://vkuviewdoc.fsq.pub/components/form.html
  11. * @property {Object} model 表单数据对象
  12. * @property {Boolean} border-bottom 是否显示表单域的下划线边框
  13. * @property {String} label-position 表单域提示文字的位置,left-左侧,top-上方
  14. * @property {String Number} label-width 提示文字的宽度,单位rpx(默认90)
  15. * @property {Object} label-style lable的样式,对象形式
  16. * @property {String} label-align lable的对齐方式
  17. * @property {Object} rules 通过ref设置,见官网说明
  18. * @property {Array} error-type 错误的提示方式,数组形式,见上方说明(默认['message'])
  19. * @example <u-form :model="form" ref="uForm"></u-form>
  20. */
  21. export default {
  22. name: 'u-form',
  23. props: {
  24. // 当前form的需要验证字段的集合
  25. model: {
  26. type: Object,
  27. default () {
  28. return {};
  29. }
  30. },
  31. // 验证规则
  32. // rules: {
  33. // type: [Object, Function, Array],
  34. // default() {
  35. // return {};
  36. // }
  37. // },
  38. // 有错误时的提示方式,message-提示信息,border-如果input设置了边框,变成呈红色,
  39. // border-bottom-下边框呈现红色,none-无提示
  40. errorType: {
  41. type: Array,
  42. default () {
  43. return ['message', 'toast']
  44. }
  45. },
  46. // 是否显示表单域的下划线边框
  47. borderBottom: {
  48. type: Boolean,
  49. default: true
  50. },
  51. // label的位置,left-左边,top-上边
  52. labelPosition: {
  53. type: String,
  54. default: 'left'
  55. },
  56. // label的宽度,单位rpx
  57. labelWidth: {
  58. type: [String, Number],
  59. default: 90
  60. },
  61. // lable字体的对齐方式
  62. labelAlign: {
  63. type: String,
  64. default: 'left'
  65. },
  66. // lable的样式,对象形式
  67. labelStyle: {
  68. type: Object,
  69. default () {
  70. return {}
  71. }
  72. },
  73. // 表单内所有input的inputAlign属性的值
  74. inputAlign: {
  75. type: String,
  76. default: 'left'
  77. },
  78. // 表单内所有input的clearable属性的值
  79. clearable: {
  80. type: Boolean,
  81. default: true
  82. }
  83. },
  84. provide() {
  85. return {
  86. uForm: this
  87. };
  88. },
  89. data() {
  90. return {
  91. rules: {}
  92. };
  93. },
  94. created() {
  95. // 存储当前form下的所有u-form-item的实例
  96. // 不能定义在data中,否则微信小程序会造成循环引用而报错
  97. this.fields = [];
  98. },
  99. methods: {
  100. setRules(rules) {
  101. this.rules = rules;
  102. },
  103. // 清空所有u-form-item组件的内容,本质上是调用了u-form-item组件中的resetField()方法
  104. resetFields() {
  105. this.fields.map(field => {
  106. field.resetField();
  107. });
  108. },
  109. // 校验全部数据
  110. validate(callback) {
  111. return new Promise(resolve => {
  112. // 对所有的u-form-item进行校验
  113. let valid = true; // 默认通过
  114. let count = 0; // 用于标记是否检查完毕
  115. let errorArr = []; // 存放错误信息
  116. let errorObjArr = []; // 存放错误信息对象
  117. if (!this.fields.length || typeof callback != 'function') return callback(valid);
  118. this.fields.map(field => {
  119. // 调用每一个u-form-item实例的validation的校验方法
  120. field.validation('', (errorMsg, errObj) => {
  121. // 如果任意一个u-form-item校验不通过,就意味着整个表单不通过
  122. if (errorMsg) {
  123. valid = false;
  124. errorArr.push(errorMsg);
  125. errorObjArr.push(errObj)
  126. }
  127. // 当历遍了所有的u-form-item时,调用promise的then方法
  128. if (++count === this.fields.length) {
  129. resolve(valid, errorObjArr[0]); // 进入promise的then方法
  130. // 判断是否设置了toast的提示方式,只提示最前面的表单域的第一个错误信息
  131. if (this.errorType.indexOf('none') === -1 && this.errorType
  132. .indexOf('toast') >= 0 && errorArr.length) {
  133. this.$u.toast(errorArr[0]);
  134. }
  135. // 调用回调方法
  136. if (typeof callback == 'function') callback(valid, errorObjArr[0]);
  137. }
  138. });
  139. });
  140. });
  141. }
  142. }
  143. };
  144. </script>
  145. <style scoped lang="scss">
  146. @import "../../libs/css/style.components.scss";
  147. </style>