u--form.vue 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <uvForm
  3. ref="uForm"
  4. :model="model"
  5. @update:model="$emit('update:model', $event)"
  6. :rules="rules"
  7. :errorType="errorType"
  8. :borderBottom="borderBottom"
  9. :labelPosition="labelPosition"
  10. :labelWidth="labelWidth"
  11. :labelAlign="labelAlign"
  12. :labelStyle="labelStyle"
  13. :customStyle="customStyle"
  14. >
  15. <slot />
  16. </uvForm>
  17. </template>
  18. <script>
  19. /**
  20. * 此组件存在的理由是,在nvue下,u-form被uni-app官方占用了,u-form在nvue中相当于form组件
  21. * 所以在nvue下,取名为u--form,内部其实还是u-form.vue,只不过做一层中转
  22. */
  23. import uvForm from '../u-form/u-form.vue';
  24. import props from '../u-form/props.js'
  25. import mixin from '../../libs/mixin/mixin'
  26. import mpMixin from '../../libs/mixin/mpMixin'
  27. /**
  28. * Form 表单
  29. * @description 此组件一般用于表单场景,可以配置Input输入框,Select弹出框,进行表单验证等。
  30. * @tutorial https://uview.d3u.cn/components/form.html
  31. * @property {Object} model 当前form的需要验证字段的集合
  32. * @property {Object | Function | Array} rules 验证规则
  33. * @property {String} errorType 错误的提示方式,见上方说明 ( 默认 message )
  34. * @property {Boolean} borderBottom 是否显示表单域的下划线边框 ( 默认 true )
  35. * @property {String} labelPosition 表单域提示文字的位置,left-左侧,top-上方 ( 默认 'left' )
  36. * @property {String | Number} labelWidth 提示文字的宽度,单位px ( 默认 45 )
  37. * @property {String} labelAlign lable字体的对齐方式 ( 默认 ‘left' )
  38. * @property {Object} labelStyle lable的样式,对象形式
  39. * @example <u--formlabelPosition="left" :model="model1" :rules="rules" ref="form1"></u--form>
  40. */
  41. export default {
  42. // #ifdef MP-WEIXIN
  43. name: 'u-form',
  44. // #endif
  45. // #ifndef MP-WEIXIN
  46. name: 'u--form',
  47. // #endif
  48. mixins: [mpMixin, mixin, props],
  49. components: {
  50. uvForm
  51. },
  52. created() {
  53. this.children = []
  54. },
  55. methods: {
  56. // 手动设置校验的规则,如果规则中有函数的话,微信小程序中会过滤掉,所以只能手动调用设置规则
  57. setRules(rules) {
  58. this.$refs.uForm.setRules(rules)
  59. },
  60. validate() {
  61. /**
  62. * 在微信小程序中,通过this.$parent拿到的父组件是u--form,而不是其内嵌的u-form
  63. * 导致在u-form组件中,拿不到对应的children数组,从而校验无效,所以这里每次调用u-form组件中的
  64. * 对应方法的时候,在小程序中都先将u--form的children赋值给u-form中的children
  65. */
  66. // #ifdef MP-WEIXIN
  67. this.setMpData()
  68. // #endif
  69. return this.$refs.uForm.validate()
  70. },
  71. validateField(value, callback, event) {
  72. // #ifdef MP-WEIXIN
  73. this.setMpData()
  74. // #endif
  75. return this.$refs.uForm.validateField(value, callback, event)
  76. },
  77. resetFields() {
  78. // #ifdef MP-WEIXIN
  79. this.setMpData()
  80. // #endif
  81. return this.$refs.uForm.resetFields()
  82. },
  83. clearValidate(props) {
  84. // #ifdef MP-WEIXIN
  85. this.setMpData()
  86. // #endif
  87. return this.$refs.uForm.clearValidate(props)
  88. },
  89. setMpData() {
  90. this.$refs.uForm.children = this.children
  91. }
  92. },
  93. }
  94. </script>