uni-forms-item.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <view class="uni-forms-item"
  3. :class="{'uni-forms-item--border':border,'is-first-border':border&&isFirstBorder,'uni-forms-item-error':msg}">
  4. <view class="uni-forms-item__inner" :class="['is-direction-'+labelPos,]">
  5. <view v-if="label" class="uni-forms-item__label"
  6. :style="{width:labelWid+'px',justifyContent: justifyContent}">
  7. <slot name="left">
  8. <uni-icons v-if="leftIcon" class="label-icon" size="16" :type="leftIcon" :color="iconColor" />
  9. <text>{{label}}</text>
  10. <text v-if="required" class="is-required">*</text>
  11. </slot>
  12. </view>
  13. <view class="uni-forms-item__content" :class="{'is-input-error-border': msg}">
  14. <slot></slot>
  15. </view>
  16. </view>
  17. <view class="uni-error-message" :class="{'uni-error-msg--boeder':border}" :style="{
  18. paddingLeft: (labelPos === 'left'? Number(labelWid)+5:5) + 'px'
  19. }">{{ showMsg === 'undertext' ? msg:'' }}</view>
  20. </view>
  21. </template>
  22. <script>
  23. /**
  24. * Field 输入框
  25. * @description 此组件可以实现表单的输入与校验,包括 "text" 和 "textarea" 类型。
  26. * @tutorial https://ext.dcloud.net.cn/plugin?id=21001
  27. * @property {Boolean} required 是否必填,左边显示红色"*"号(默认false)
  28. * @property {String} validateTrigger = [bind|submit] 校验触发器方式 默认 submit 可选
  29. * @value bind 发生变化时触发
  30. * @value submit 提交时触发
  31. * @property {String } leftIcon label左边的图标,限 uni-ui 的图标名称
  32. * @property {String } iconColor 左边通过icon配置的图标的颜色(默认#606266)
  33. * @property {String } label 输入框左边的文字提示
  34. * @property {Number } labelWidth label的宽度,单位px(默认65)
  35. * @property {String } labelAlign = [left|center|right] label的文字对齐方式(默认left)
  36. * @value left label 左侧显示
  37. * @value center label 居中
  38. * @value right label 右侧对齐
  39. * @property {String } labelPosition = [top|left] label的文字的位置(默认left)
  40. * @value top 顶部显示 label
  41. * @value left 左侧显示 label
  42. * @property {String } errorMessage 显示的错误提示内容,如果为空字符串或者false,则不显示错误信息
  43. * @property {String } name 表单域的属性名,在使用校验规则时必填
  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
  251. .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 scoped>
  323. .uni-forms-item {
  324. position: relative;
  325. text-align: left;
  326. color: #333;
  327. font-size: 14px;
  328. background-color: #fff;
  329. }
  330. .uni-forms-item__inner {
  331. /* #ifndef APP-NVUE */
  332. display: flex;
  333. /* #endif */
  334. }
  335. .is-direction-left {
  336. flex-direction: row;
  337. }
  338. .is-direction-top {
  339. flex-direction: column;
  340. }
  341. .uni-forms-item__label {
  342. /* #ifndef APP-NVUE */
  343. display: flex;
  344. flex-shrink: 0;
  345. /* #endif */
  346. flex-direction: row;
  347. align-items: center;
  348. font-size: 14px;
  349. color: #333;
  350. width: 65px;
  351. padding: 5px 0;
  352. box-sizing: border-box;
  353. height: 36px;
  354. margin-right: 5px;
  355. }
  356. .uni-forms-item__content {
  357. /* #ifndef APP-NVUE */
  358. width: 100%;
  359. /* #endif */
  360. box-sizing: border-box;
  361. min-height: 36px;
  362. }
  363. .label-icon {
  364. margin-right: 5px;
  365. margin-top: -1px;
  366. }
  367. .is-required {
  368. color: #dd524d;
  369. }
  370. .uni-error-message {
  371. position: absolute;
  372. bottom: -17px;
  373. left: 0;
  374. line-height: 12px;
  375. color: #dd524d;
  376. font-size: 12px;
  377. text-align: left;
  378. }
  379. .uni-error-msg--boeder {
  380. position: relative;
  381. bottom: 0;
  382. line-height: 22px;
  383. }
  384. .is-input-error-border {
  385. border-color: #dd524d;
  386. }
  387. .uni-forms-item--border {
  388. margin-bottom: 0;
  389. border-top: 1px #eee solid;
  390. }
  391. .uni-forms-item-error {
  392. padding-bottom: 0;
  393. }
  394. .is-first-border {
  395. border: none;
  396. }
  397. </style>