bjx-form-item.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <view class="bjx-form-item" :style="{width: theWidth}">
  3. <view :class="'label-' + theLabelType" :style="{alignItems: theVerticalAlign}">
  4. <view class="item-label" :style="theLabelStyle">
  5. <view class="item-required" v-show="thePromptType&&(thePromptType==2||theRequired)" :style="{color: theForm.msgColor,opacity:thePromptType&&theRequired?1:0}">
  6. {{theForm.prompt}}
  7. </view>
  8. <view class="label-con">
  9. <slot name="label" >
  10. <text class="label-text">{{label}}</text>
  11. <text class="right" v-if="theLabelType=='block'&&labelRight">{{labelRight}}</text>
  12. </slot>
  13. </view>
  14. </view>
  15. <view class="item-con">
  16. <slot />
  17. </view>
  18. </view>
  19. <scroll-view scroll-x='true' class="item-msg" v-if="theForm.msgType=='in'">
  20. <view :style="{color: theForm.msgColor}">{{msg}}</view>
  21. </scroll-view>
  22. </view>
  23. </template>
  24. <script>
  25. import {fromCheck} from './bjx-validate.js'
  26. export default {
  27. name: 'BjxFormItem',
  28. props: {
  29. // 字段名称
  30. label: String,
  31. // 表单字段
  32. prop: String,
  33. width: {
  34. type: String,
  35. default: 'auto'
  36. },
  37. labelWidth: {
  38. type: String,
  39. default: ''
  40. },
  41. // block 独占一行 inlie与内容共一行
  42. labelType: String,
  43. // label 文本水平对齐方式
  44. align: String,
  45. labelRight: {
  46. type: String,
  47. default: '' // 当labelType 为 block 时 label 右侧显示文字
  48. },
  49. required: {
  50. type: Boolean,
  51. default: false // 字段名左侧* 是否显示 默认由 校验规则 中的 required 控制
  52. },
  53. verticalAlign: {
  54. type: String,
  55. default: 'center' // label 文本垂直对齐方式
  56. },
  57. // 提示符占位,0:不显示, 1:非必要项不显示不占位 2:占位,用透明度控制显示隐藏使label文字对齐
  58. promptType: Number,
  59. authCheck: {
  60. type: Boolean,
  61. default: null // 动态校验,即值一改变就对数据进行校验
  62. },
  63. },
  64. data() {
  65. return {
  66. formField: ['labelType', 'labelWidth', 'align', 'msgType','form','rules','msgColor','prompt','promptType','authCheck'],
  67. msg: ''
  68. }
  69. },
  70. watch: {
  71. value() { // 监听value 检查是否做动态校验
  72. if(this.authCheck||this.authCheck===null&& this.theForm.authCheck){
  73. this.validate()
  74. }
  75. }
  76. },
  77. computed: {
  78. theRequired() {
  79. if(this.required || this.theForm.rules && this.theForm.rules[this.prop] && this.theForm.rules[this.prop].required) {
  80. return true
  81. }
  82. return false
  83. },
  84. theForm() {
  85. let parent = this.$parent;
  86. let parentName = parent.$options.name;
  87. while (parentName !== null && parentName !== 'BjxForm') {
  88. parent = parent ? parent.$parent : null;
  89. parentName = parent ? parent.$options.name : null;
  90. }
  91. let theForm = {}
  92. if(parent) {
  93. this.formField.forEach(key => {
  94. theForm[key] = parent[key]
  95. })
  96. }
  97. return theForm;
  98. },
  99. theWidth() {
  100. return !isNaN(Number(this.width)) ? this.width + 'rpx' : this.width
  101. },
  102. theLabelWidth(){
  103. let width = 'auto'
  104. if(!this.$slots.label) {
  105. width = this.labelWidth || this.theForm.labelWidth
  106. if(!isNaN(Number(width))) {
  107. width += 'rpx'
  108. }
  109. }
  110. return width
  111. },
  112. theLabelType() {
  113. let labelType = this.labelType || this.theForm.labelType
  114. return labelType
  115. },
  116. theLabelStyle() {
  117. let width = this.theLabelType!='block' ? this.theLabelWidth : 'auto'
  118. let aligns = {left: 'flex-start', right: 'flex-end', center: 'center',between: 'space-between'}
  119. let align = this.align || this.theForm.align
  120. return `width: ${width}; justify-content: ${aligns[align]}; vertical-align: ${this.verticalAlign};`
  121. },
  122. theVerticalAlign() {
  123. let type = {top: 'flex-start', bottom: 'flex-end', center: 'center'}
  124. return type[this.verticalAlign]
  125. },
  126. value() {
  127. let form = this.theForm.form
  128. let value = form ? form[this.prop] : ''
  129. return value
  130. },
  131. thePromptType() {
  132. return this.promptType > -1 ? this.promptType :this.theForm.promptType
  133. }
  134. },
  135. methods: {
  136. // 规则校验
  137. validate() {
  138. if(!this.theForm.rules) return
  139. let rule = this.theForm.rules[this.prop]
  140. this.msg = ''
  141. if(rule) {
  142. if(rule.required && (this.value == null || this.value == '')) {
  143. // 是否必填
  144. this.msg = rule.msg || this.label + '不能为空'
  145. }else if(rule.validator) {
  146. // 自定义规则校验函数
  147. let bol = rule.validator(this.value,rule)
  148. if(typeof bol == 'string') {
  149. this.msg = bol
  150. }else if(!bol) {
  151. this.msg = rule.message || this.label + '不符合规则'
  152. }
  153. }else if(rule.rule && this.value != null && this.value != '') {
  154. // 默认校验规则
  155. let result = fromCheck(this.value, rule.rule, this.theForm.form)
  156. if(result !== true) {
  157. this.msg = this.label + result.msg
  158. if(rule.message) {
  159. if(typeof rule.message === 'string') {
  160. this.msg = typeof rule.message
  161. } else if(rule.message[result.rule]) {
  162. this.msg = rule.message[result.rule]
  163. }
  164. }
  165. }
  166. }
  167. }
  168. if(this.msg != '') {
  169. if(this.theForm.msgType == 'out') {
  170. // 弹框
  171. uni.showModal({ content: this.msg })
  172. }else if(this.theForm.msgType == 'msg'){
  173. // 消息框
  174. uni.showToast({
  175. icon: 'none',
  176. title: this.msg
  177. })
  178. }
  179. }
  180. return !this.msg
  181. }
  182. },
  183. }
  184. </script>
  185. <style lang="scss" scoped>
  186. .bjx-form-item {
  187. padding: 3px 0;
  188. .label-block{
  189. .item-label {
  190. display:flex;
  191. margin-bottom: 2px;
  192. .label-con{
  193. flex:1;
  194. }
  195. .right {
  196. float: right;
  197. }
  198. }
  199. }
  200. .label-inline{
  201. display:flex;
  202. align-items: flex-start;
  203. .item-label {
  204. display:flex;
  205. margin-right: 10upx;
  206. }
  207. .item-con {
  208. flex:1;
  209. }
  210. }
  211. .item-msg {
  212. padding:2px;margin-bottom: 5px;font-size: 15px;height: 22px;
  213. view{width: max-content;}
  214. }
  215. }
  216. </style>