uni-forms-item.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <template>
  2. <view class="uni-forms-item" :class="{'uni-forms-item--border':border,'is-first-border':border&&isFirstBorder,'uni-forms-item-error':msg}">
  3. <view class="uni-forms-item__box">
  4. <view class="uni-forms-item__inner" :class="['is-direction-'+labelPos,]">
  5. <view v-if="label" class="uni-forms-item__label" :style="{width:labelWid+'px',justifyContent: justifyContent}">
  6. <slot name="left">
  7. <uni-icons v-if="leftIcon" class="label-icon" size="16" :type="leftIcon" :color="iconColor" />
  8. <text class="label-text">{{label}}</text>
  9. <text v-if="required" class="is-required">*</text>
  10. </slot>
  11. </view>
  12. <view class="uni-forms-item__content" :class="{'is-input-error-border': msg}">
  13. <slot></slot>
  14. </view>
  15. </view>
  16. <view v-if="msg" class="uni-error-message" :class="{'uni-error-msg--boeder':border}" :style="{
  17. paddingLeft: (labelPos === 'left'? Number(labelWid)+5:5) + 'px'
  18. }"><text class="uni-error-message-text">{{ showMsg === 'undertext' ? msg:'' }}</text></view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. /**
  24. * FormsItem 表单子组件
  25. * @description 此组件可以实现表单的输入与校验,包括 "text" 和 "textarea" 类型。
  26. * @tutorial https://ext.dcloud.net.cn/plugin?id=2773
  27. * @property {String} name 表单域的属性名,在使用校验规则时必填
  28. * @property {Boolean} required 左边显示红色"*"号,样式显示不会对校验规则产生效果(默认 false)
  29. * @property {String} validate-trigger = [bind|submit] 表单校验时机(默认 submit)
  30. * @value bind 数据发生变化时触发
  31. * @value submit 提交表单是触发
  32. * @property {String} left-icon label左边的图标,限uni-ui的图标名称
  33. * @property {String} icon-color 左边通过icon配置的图标的颜色 (默认 #606266)
  34. * @property {String} label 输入框左边的文字提示
  35. * @property {Number} label-width label的宽度,单位px
  36. * @property {String} label-align = [left|center|right] label的文字对齐方式(默认 left)
  37. * @value left 左对齐
  38. * @value center 居中对齐
  39. * @value right 右对齐
  40. * @property {String} label-position = [top|left] label的文字的位置(默认 left)
  41. * @value top 顶部显示 label
  42. * @value left 左侧显示 label
  43. * @property {String} error-message 显示的错误提示内容,如果为空字符串或者false,则不显示错误信息
  44. */
  45. export default {
  46. name: "uniFormsItem",
  47. props: {
  48. // 自定义内容
  49. custom: {
  50. type: Boolean,
  51. default: false
  52. },
  53. // 是否显示报错信息
  54. showMessage: {
  55. type: Boolean,
  56. default: true
  57. },
  58. name: String,
  59. required: Boolean,
  60. validateTrigger: {
  61. type: String,
  62. default: ''
  63. },
  64. leftIcon: String,
  65. iconColor: {
  66. type: String,
  67. default: '#606266'
  68. },
  69. label: String,
  70. // 左边标题的宽度单位px
  71. labelWidth: {
  72. type: [Number, String],
  73. default: ''
  74. },
  75. // 对齐方式,left|center|right
  76. labelAlign: {
  77. type: String,
  78. default: ''
  79. },
  80. // lable的位置,可选为 left-左边,top-上边
  81. labelPosition: {
  82. type: String,
  83. default: ''
  84. },
  85. errorMessage: {
  86. type: [String, Boolean],
  87. default: ''
  88. }
  89. },
  90. data() {
  91. return {
  92. errorTop: false,
  93. errorBottom: false,
  94. labelMarginBottom: '',
  95. errorWidth: '',
  96. errMsg: '',
  97. val: '',
  98. labelPos: '',
  99. labelWid: '',
  100. labelAli: '',
  101. showMsg: 'undertext',
  102. border: false,
  103. isFirstBorder: false
  104. };
  105. },
  106. computed: {
  107. msg() {
  108. return this.errorMessage || this.errMsg;
  109. },
  110. fieldStyle() {
  111. let style = {}
  112. if (this.labelPos == 'top') {
  113. style.padding = '0 0'
  114. this.labelMarginBottom = '6px'
  115. }
  116. if (this.labelPos == 'left' && this.msg !== false && this.msg != '') {
  117. style.paddingBottom = '0px'
  118. this.errorBottom = true
  119. this.errorTop = false
  120. } else if (this.labelPos == 'top' && this.msg !== false && this.msg != '') {
  121. this.errorBottom = false
  122. this.errorTop = true
  123. } else {
  124. // style.paddingBottom = ''
  125. this.errorTop = false
  126. this.errorBottom = false
  127. }
  128. return style
  129. },
  130. // uni不支持在computed中写style.justifyContent = 'center'的形式,故用此方法
  131. justifyContent() {
  132. if (this.labelAli === 'left') return 'flex-start';
  133. if (this.labelAli === 'center') return 'center';
  134. if (this.labelAli === 'right') return 'flex-end';
  135. }
  136. },
  137. watch: {
  138. validateTrigger(trigger) {
  139. this.formTrigger = trigger
  140. }
  141. },
  142. created() {
  143. this.form = this.getForm()
  144. this.group = this.getForm('uniGroup')
  145. this.formRules = []
  146. this.formTrigger = this.validateTrigger
  147. if (this.form) {
  148. this.form.childrens.push(this)
  149. }
  150. this.init()
  151. },
  152. destroyed() {
  153. if (this.form) {
  154. this.form.childrens.forEach((item, index) => {
  155. if (item === this) {
  156. this.form.childrens.splice(index, 1)
  157. delete this.form.formData[item.name]
  158. }
  159. })
  160. }
  161. },
  162. methods: {
  163. init() {
  164. if (this.form) {
  165. let {
  166. formRules,
  167. validator,
  168. formData,
  169. value,
  170. labelPosition,
  171. labelWidth,
  172. labelAlign,
  173. errShowType
  174. } = this.form
  175. this.labelPos = this.labelPosition ? this.labelPosition : labelPosition
  176. this.labelWid = this.label ? (this.labelWidth ? this.labelWidth : labelWidth):0
  177. this.labelAli = this.labelAlign ? this.labelAlign : labelAlign
  178. // 判断第一个 item
  179. if (!this.form.isFirstBorder) {
  180. this.form.isFirstBorder = true
  181. this.isFirstBorder = true
  182. }
  183. // 判断 group 里的第一个 item
  184. if (this.group) {
  185. if (!this.group.isFirstBorder) {
  186. this.group.isFirstBorder = true
  187. this.isFirstBorder = true
  188. }
  189. }
  190. this.border = this.form.border
  191. this.showMsg = errShowType
  192. if (formRules) {
  193. this.formRules = formRules[this.name] || {}
  194. }
  195. this.validator = validator
  196. } else {
  197. this.labelPos = this.labelPosition || 'left'
  198. this.labelWid = this.labelWidth || 65
  199. this.labelAli = this.labelAlign || 'left'
  200. }
  201. },
  202. /**
  203. * 获取父元素实例
  204. */
  205. getForm(name = 'uniForms') {
  206. let parent = this.$parent;
  207. let parentName = parent.$options.name;
  208. while (parentName !== name) {
  209. parent = parent.$parent;
  210. if (!parent) return false
  211. parentName = parent.$options.name;
  212. }
  213. return parent;
  214. },
  215. /**
  216. * 移除该表单项的校验结果
  217. */
  218. clearValidate() {
  219. this.errMsg = ''
  220. },
  221. setValue(value){
  222. if (this.name) {
  223. if(this.errMsg) this.errMsg = ''
  224. this.form.formData[this.name] = this.form._getValue(this.name, value)
  225. if(!this.formRules || (typeof(this.formRules) && JSON.stringify(this.formRules) === '{}')) return
  226. this.triggerCheck(this.form._getValue(this.name, value))
  227. }
  228. },
  229. /**
  230. * 校验规则
  231. * @param {Object} value
  232. */
  233. async triggerCheck(value, callback) {
  234. let promise = null;
  235. this.errMsg = ''
  236. // if no callback, return promise
  237. // if (callback && typeof callback !== 'function' && Promise) {
  238. // promise = new Promise((resolve, reject) => {
  239. // callback = function(valid) {
  240. // !valid ? resolve(valid) : reject(valid)
  241. // };
  242. // });
  243. // }
  244. // if (!this.validator) {
  245. // typeof callback === 'function' && callback(null);
  246. // if (promise) return promise
  247. // }
  248. if (!this.validator) return
  249. const isNoField = this.isRequired(this.formRules.rules || [])
  250. let isTrigger = this.isTrigger(this.formRules.validateTrigger, this.validateTrigger, this.form.validateTrigger)
  251. let result = null
  252. if (!(!isTrigger)) {
  253. result = await this.validator.validateUpdate({
  254. [this.name]: value
  255. }, this.form.formData)
  256. }
  257. // 判断是否必填,非必填,不填不校验,填写才校验
  258. if (!isNoField && (value === undefined || value === '')) {
  259. result = null
  260. }
  261. if (isTrigger && result && result.errorMessage) {
  262. const inputComp = this.form.inputChildrens.find(child => child.rename === this.name)
  263. if (inputComp) {
  264. inputComp.errMsg = result.errorMessage
  265. }
  266. if (this.form.errShowType === 'toast') {
  267. uni.showToast({
  268. title: result.errorMessage || '校验错误',
  269. icon: 'none'
  270. })
  271. }
  272. if (this.form.errShowType === 'modal') {
  273. uni.showModal({
  274. title: '提示',
  275. content: result.errorMessage || '校验错误'
  276. })
  277. }
  278. }
  279. this.errMsg = !result ? '' : result.errorMessage
  280. // 触发validate事件
  281. this.form.validateCheck(result ? result : null)
  282. // typeof callback === 'function' && callback(result ? result : null);
  283. // if (promise) return promise
  284. },
  285. /**
  286. * 触发时机
  287. * @param {Object} event
  288. */
  289. isTrigger(rule, itemRlue, parentRule) {
  290. let rl = true;
  291. // bind submit
  292. if (rule === 'submit' || !rule) {
  293. if (rule === undefined) {
  294. if (itemRlue !== 'bind') {
  295. if (!itemRlue) {
  296. return parentRule === 'bind' ? true : false
  297. }
  298. return false
  299. }
  300. return true
  301. }
  302. return false
  303. }
  304. return true;
  305. },
  306. // 是否有必填字段
  307. isRequired(rules) {
  308. let isNoField = false
  309. for (let i = 0; i < rules.length; i++) {
  310. const ruleData = rules[i]
  311. if (ruleData.required) {
  312. isNoField = true
  313. break
  314. }
  315. }
  316. return isNoField
  317. }
  318. }
  319. };
  320. </script>
  321. <style lang="scss" scoped>
  322. .uni-forms-item {
  323. position: relative;
  324. padding: 0px;
  325. text-align: left;
  326. color: #333;
  327. font-size: 14px;
  328. // margin-bottom: 22px;
  329. }
  330. .uni-forms-item__box {
  331. position: relative;
  332. }
  333. .uni-forms-item__inner {
  334. /* #ifndef APP-NVUE */
  335. display: flex;
  336. /* #endif */
  337. // flex-direction: row;
  338. // align-items: center;
  339. padding-bottom: 22px;
  340. // margin-bottom: 22px;
  341. }
  342. .is-direction-left {
  343. flex-direction: row;
  344. }
  345. .is-direction-top {
  346. flex-direction: column;
  347. }
  348. .uni-forms-item__label {
  349. /* #ifndef APP-NVUE */
  350. display: flex;
  351. flex-shrink: 0;
  352. box-sizing: border-box;
  353. /* #endif */
  354. flex-direction: row;
  355. align-items: center;
  356. width: 65px;
  357. // line-height: 2;
  358. // margin-top: 3px;
  359. padding: 5px 0;
  360. height: 36px;
  361. margin-right: 5px;
  362. .label-text {
  363. font-size: 14px;
  364. color: #333;
  365. }
  366. }
  367. .uni-forms-item__content {
  368. /* #ifndef APP-NVUE */
  369. width: 100%;
  370. box-sizing: border-box;
  371. min-height: 36px;
  372. /* #endif */
  373. flex: 1;
  374. }
  375. .label-icon {
  376. margin-right: 5px;
  377. margin-top: -1px;
  378. }
  379. // 必填
  380. .is-required {
  381. color: $uni-color-error;
  382. }
  383. .uni-error-message {
  384. position: absolute;
  385. bottom: 0px;
  386. left: 0;
  387. text-align: left;
  388. }
  389. .uni-error-message-text {
  390. line-height: 22px;
  391. color: $uni-color-error;
  392. font-size: 12px;
  393. }
  394. .uni-error-msg--boeder {
  395. position: relative;
  396. bottom: 0;
  397. line-height: 22px;
  398. }
  399. .is-input-error-border {
  400. border-color: $uni-color-error;
  401. }
  402. .uni-forms-item--border {
  403. margin-bottom: 0;
  404. padding: 10px 0;
  405. // padding-bottom: 0;
  406. border-top: 1px #eee solid;
  407. .uni-forms-item__inner {
  408. padding: 0;
  409. }
  410. }
  411. .uni-forms-item-error {
  412. padding-bottom: 0;
  413. }
  414. .is-first-border {
  415. /* #ifndef APP-NVUE */
  416. border: none;
  417. /* #endif */
  418. /* #ifdef APP-NVUE */
  419. border-width: 0;
  420. /* #endif */
  421. }
  422. </style>