uni-forms-item.vue 11 KB

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