bjx-form.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view>
  3. <form :report-submit="reportSubmit" @submit="formSubmit" @reset="formReset">
  4. <slot />
  5. </form>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. name: 'BjxForm',
  11. props: {
  12. form: {
  13. type: Object,
  14. default: function (){
  15. return {}
  16. }
  17. },
  18. rules: {
  19. type: Object,
  20. default: function (){
  21. return {}
  22. }
  23. },
  24. // 提示模式 1. out 弹框提提示 2.in item 页面内 item 文字提示 3 msg 消息框提示
  25. msgType: {
  26. type: String,
  27. default: 'out'
  28. },
  29. // block 独占一行 inlie与内容共一行
  30. labelType: {
  31. type: String,
  32. default: 'block'
  33. },
  34. labelWidth: {
  35. type: String,
  36. default: 'auto'
  37. },
  38. align: {
  39. type: String,
  40. default: 'left'
  41. },
  42. reportSubmit: {
  43. type: Boolean,
  44. default: false
  45. },
  46. submit: Function,
  47. reset: Function,
  48. msgColor: {
  49. type: String,
  50. default: '#F56C6C' // 提示符、提示文本字体颜色
  51. },
  52. prompt: {
  53. type: String,
  54. default: '*' // 提示符
  55. },
  56. promptType: {
  57. type: Number,
  58. default: 1 // 提示符占位,0:不显示, 1:非必要项不显示不占位 2:占位,用透明度控制显示隐藏使label文字对齐
  59. },
  60. authCheck: {
  61. type: Boolean,
  62. default: false // 动态校验,即值一改变就对数据进行校验
  63. },
  64. },
  65. methods: {
  66. formSubmit(e) {
  67. this.$emit('submit',e)
  68. },
  69. formReset(e) {
  70. this.$emit('reset',e)
  71. },
  72. // 规则校验
  73. validate(callback) {
  74. let vb = true
  75. let item = this.getItem(this.$children)
  76. for(let i = 0; i < item.length; ++i) {
  77. // 对表单下的子组件 form-item 做数据校验
  78. let bol = item[i].validate ? item[i].validate() : true
  79. if(vb && !bol) {
  80. vb = false
  81. }
  82. if(this.msgType != 'in' && !bol) {
  83. break
  84. }
  85. }
  86. callback && callback(vb)
  87. },
  88. // 递归遍历子元素 筛选 form-item
  89. getItem(children,item) {
  90. item = item || []
  91. children.forEach(it => {
  92. if(it.$options.name && it.$options.name === 'BjxFormItem' ||
  93. it.$options._componentTag && it.$options._componentTag === 'bjx-form-item'){
  94. item.push(it)
  95. } else if(it.$children.length){
  96. item = this.getItem(it.$children,item)
  97. }
  98. })
  99. return item
  100. }
  101. },
  102. }
  103. </script>
  104. <style lang="scss">
  105. </style>