cl-form.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <view
  3. :class="[
  4. 'cl-form',
  5. {
  6. 'is-border': border,
  7. },
  8. ]"
  9. >
  10. <slot></slot>
  11. </view>
  12. </template>
  13. <script>
  14. import AsyncValidator from "../../utils/async-validator";
  15. import Emitter from "../../mixins/emitter";
  16. import { isArray, isObject, isString, isBoolean, isEmpty, isNumber, cloneDeep } from "../../utils";
  17. /**
  18. * form 表单
  19. * @description 表单,基于 async-validator 的验证
  20. * @tutorial https://docs.cool-js.com/uni/components/form/rules.html
  21. * @property {Object} model 表单数据对象
  22. * @property {Object} rules 表单验证规则
  23. * @property {Boolean} border 是否带有边框
  24. * @property {Boolean} disabled 是否禁用
  25. * @property {Boolean} showMessage 是否显示消息提示
  26. * @property {String} labelWidth 表单域标签的宽度,默认150rpx
  27. * @property {String} labelPosition 表单域标签的位置,默认right
  28. * @property {Boolean} validateOnRuleChange 是否在 rules 属性改变后立即触发一次验证,默认true
  29. * @example <cl-form model="" rules=""></cl-form>
  30. */
  31. export default {
  32. name: "cl-form",
  33. componentName: "ClForm",
  34. props: {
  35. // 表单数据对象
  36. model: {
  37. type: Object,
  38. default: () => {
  39. return {};
  40. },
  41. },
  42. // 表单验证规则
  43. rules: Object,
  44. // 是否带有边框
  45. border: Boolean,
  46. // 是否禁用
  47. disabled: Boolean,
  48. // 是否显示消息提示
  49. showMessage: {
  50. type: Boolean,
  51. default: true,
  52. },
  53. // 表单域标签的宽度
  54. labelWidth: {
  55. type: String,
  56. default: "150rpx",
  57. },
  58. // 表单域标签的位置
  59. labelPosition: {
  60. type: String,
  61. default: "right",
  62. },
  63. // 是否在 rules 属性改变后立即触发一次验证
  64. validateOnRuleChange: {
  65. type: Boolean,
  66. default: false,
  67. },
  68. },
  69. mixins: [Emitter],
  70. data() {
  71. return {
  72. rules2: {},
  73. props: [],
  74. _form: {},
  75. lock: false,
  76. };
  77. },
  78. watch: {
  79. model2: {
  80. deep: true,
  81. handler(newV, oldV) {
  82. if (!this.lock) {
  83. // 数据变化时,通知 form-item 验证
  84. this.publish("validate", {
  85. props: this.checkProps(newV, oldV),
  86. });
  87. }
  88. this.lock = false;
  89. },
  90. },
  91. rules: {
  92. immediate: true,
  93. handler(val) {
  94. this.setRules(val);
  95. },
  96. },
  97. rules2: {
  98. deep: true,
  99. immediate: true,
  100. handler(val) {
  101. // 设置字段
  102. this.props = Object.keys(val);
  103. // 通知 form-item 修改验证规则
  104. this.publish("change-rule", { rules: val });
  105. },
  106. },
  107. },
  108. computed: {
  109. model2() {
  110. // 避免 newValue 与 oldValue 重复
  111. return cloneDeep(this.model);
  112. },
  113. },
  114. mounted() {
  115. // 设置默认数据
  116. this._form = cloneDeep(this.model);
  117. },
  118. methods: {
  119. // 弥补 props 方法不能传递的问题
  120. setRules(rules) {
  121. this.rules2 = rules || {};
  122. },
  123. // 校验表单
  124. validate(callback) {
  125. this.validateField(this.props, callback);
  126. },
  127. // 根据字段校验表单
  128. validateField(props, callback) {
  129. if (!props) {
  130. console.warn(`Props 为空`);
  131. return false;
  132. }
  133. if (!isArray(props)) {
  134. props = [props];
  135. }
  136. let rules = {};
  137. for (let i in this.rules2) {
  138. if (props.includes(i)) {
  139. rules[i] = this.rules2[i];
  140. }
  141. }
  142. const validator = new AsyncValidator(rules);
  143. let form = {};
  144. props.forEach((e) => {
  145. form[e] = this.model[e];
  146. });
  147. // 验证所有prop
  148. validator.validate(form, (errors, fields) => {
  149. this.publish("validate", {
  150. props,
  151. });
  152. if (callback) {
  153. callback(!errors, errors);
  154. }
  155. });
  156. },
  157. // 重置表单
  158. resetFields() {
  159. this.lock = true;
  160. this.form = cloneDeep(this._form);
  161. this.$emit("update:model", this.form);
  162. this.clearValidate();
  163. },
  164. // 移除表单校验结果
  165. clearValidate() {
  166. this.publish("clearValidate");
  167. },
  168. // 移除字段
  169. removeField(prop) {
  170. this.props.splice(this.props.indexOf(prop), 1);
  171. },
  172. // 发布消息
  173. publish(action, options) {
  174. let { rules, props = this.props, model = this.model2 } = options || {};
  175. this.broadcast("ClFormItem", "form.event", {
  176. rules,
  177. props,
  178. model,
  179. action,
  180. });
  181. },
  182. // 检测是哪个prop引起的变化
  183. checkProps(d1, d2) {
  184. const deep = (d1, d2) => {
  185. if (isArray(d2)) {
  186. if (isArray(d1)) {
  187. if (d2.length === d1.length) {
  188. return !d2.some((v, i) => {
  189. return !deep(d2[i], d1[i]);
  190. });
  191. }
  192. }
  193. return false;
  194. } else if (isObject(d2)) {
  195. if (isObject(d1)) {
  196. let flag = true;
  197. for (let i in d2) {
  198. flag = deep(d2[i], d1[i]);
  199. if (!flag) {
  200. return false;
  201. }
  202. }
  203. return true;
  204. }
  205. return false;
  206. } else if (isString(d2)) {
  207. return d1 === d2;
  208. } else if (isNumber(d2)) {
  209. return d1 === d2;
  210. } else if (isBoolean(d2)) {
  211. return d1 === d2;
  212. } else {
  213. return true;
  214. }
  215. };
  216. return this.props.filter((k) => {
  217. return !deep(d1[k], d2[k]);
  218. });
  219. },
  220. },
  221. };
  222. </script>