uni-forms-item.vue 12 KB

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