uni-forms-item.vue 10 KB

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