uni-forms-item.vue 12 KB

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