uni-forms-item.vue 13 KB

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