uni-forms-item.vue 12 KB

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