uni-field.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <template>
  2. <view class="uni-field" :class="{ 'uni-border-top': borderTop, 'uni-border-bottom': borderBottom }" :style="[fieldStyle]">
  3. <view class="uni-field-inner" :class="[type == 'textarea' ? 'uni-textarea-inner' : '', 'uni-label-postion-' + labelPos]">
  4. <view :class="errorTop ? 'uni-error-in-label' : ''">
  5. <view class="uni-field-label" :class="[required ? 'uni-required' : '']" :style="{
  6. justifyContent: justifyContent,
  7. width: labelWid + 'px',
  8. marginBottom: labelMarginBottom
  9. }">
  10. <view class="uni-icon-wrap" v-if="leftIcon">
  11. <uni-icons size="16" :type="leftIcon" :color="iconColor" />
  12. </view>
  13. <slot name="leftIcon"></slot>
  14. <text class="uni-label-text" :class="[leftIcon ? 'uni-label-left-gap' : '']">{{ label }}</text>
  15. </view>
  16. <view v-if="errorTop" class="uni-error-message" :style="{ paddingLeft: '4px' }">{{ msg }}</view>
  17. </view>
  18. <view class="fild-body" :class="[inputBorder ? 'uni-input-border' : '']" :style="[borderEixstTextareaStyle]">
  19. <view class="uni-flex-1 uni-flex" :style="[inputWrapStyle]">
  20. <textarea v-if="type == 'textarea'" class="uni-flex-1 uni-textarea-class" :name="name" :value="value" :placeholder="placeholder"
  21. :placeholderStyle="placeholderStyle" :disabled="disabled" :maxlength="inputMaxlength" :focus="focus" :autoHeight="autoHeight"
  22. @input="onInput" @blur="onBlur" @focus="onFocus" @confirm="onConfirm" @tap="fieldClick" />
  23. <input
  24. v-else
  25. :type="type"
  26. class="uni-flex-1 uni-field__input-wrap"
  27. :name="name"
  28. :value="value"
  29. :password="password || this.type === 'password'"
  30. :placeholder="placeholder"
  31. :placeholderStyle="placeholderStyle"
  32. :disabled="disabled"
  33. :maxlength="inputMaxlength"
  34. :focus="focus"
  35. :confirmType="confirmType"
  36. @focus="onFocus"
  37. @blur="onBlur"
  38. @input="onInput"
  39. @confirm="onConfirm"
  40. @tap="fieldClick"
  41. />
  42. <uni-icons :size="clearSize" v-if="clearable && value != ''" type="clear" color="#c0c4cc" @click="onClear" class="uni-clear-icon" />
  43. </view>
  44. <view class="uni-button-wrap"><slot name="right" /></view>
  45. <uni-icons v-if="rightIcon" size="16" @click="rightIconClick" :type="rightIcon" color="#c0c4cc" :style="[rightIconStyle]" />
  46. </view>
  47. </view>
  48. <view
  49. v-if="errorBottom"
  50. class="uni-error-message"
  51. :style="{
  52. paddingLeft: Number(labelWid) + 4 + 'px'
  53. }"
  54. >
  55. {{ msg }}
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. /**
  61. * Field 输入框
  62. * @description 此组件可以实现表单的输入与校验,包括 "text" 和 "textarea" 类型。
  63. * @tutorial https://ext.dcloud.net.cn/plugin?id=21001
  64. * @property {String } type 输入框的类型(默认text)
  65. * @property {Boolean} required 是否必填,左边您显示红色"*"号(默认false)
  66. * @property {String } leftIcon label左边的图标,限uni-ui的图标名称
  67. * @property {String } iconColor 左边通过icon配置的图标的颜色(默认#606266)
  68. * @property {Boolean} rightIcon 输入框右边的图标名称,限uni-ui的图标名称(默认false)
  69. * @property {String } label 输入框左边的文字提示
  70. * @property {Number } labelWidth label的宽度,单位px(默认65)
  71. * @property {String } labelAlign label的文字对齐方式(默认left)
  72. * @property {String } labelPosition label的文字的位置(默认left)
  73. * @property {Boolean} clearable 是否显示右侧清空内容的图标控件(输入框有内容,且获得焦点时才显示),点击可清空输入框内容(默认true)
  74. * @property {String } placeholder 输入框的提示文字
  75. * @property {String } placeholderStyle placeholder的样式(内联样式,字符串),如"color: #ddd"
  76. * @property {Boolean} password 是否密码输入方式(用点替换文字),type为text时有效(默认false)
  77. * @property {Boolean} focus 是否自动获得焦点(默认false)
  78. * @property {Boolean} disabled 是否不可输入(默认false)
  79. * @property {Number } maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
  80. * @property {String } confirmType 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
  81. * @property {String } errorMessage 显示的错误提示内容,如果为空字符串或者false,则不显示错误信息
  82. * @property {Number } clearSize 清除图标的大小,单位px(默认15)
  83. * @property {Boolean} trim 是否自动去除两端的空格
  84. * @property {String } name 表单域的属性名,在使用校验规则时必填
  85. * @property {Array } rules 单行表单验证规则,接受一个数组
  86. * @property {Boolean} inputBorder 是否显示input输入框的边框(默认false)
  87. * @property {Boolean} border-bottom 是否显示field的下边框(默认true)
  88. * @property {Boolean} border-top 是否显示field的上边框(默认false)
  89. * @property {Boolean} auto-height 是否自动增高输入区域,type为textarea时有效(默认true)
  90. * @event {Function} input 输入框内容发生变化时触发
  91. * @event {Function} focus 输入框获得焦点时触发
  92. * @event {Function} blur 输入框失去焦点时触发
  93. * @event {Function} confirm 点击完成按钮时触发
  94. * @event {Function} right-icon-click 通过right-icon生成的图标被点击时触发
  95. * @event {Function} click 输入框被点击或者通过right-icon生成的图标被点击时触发,这样设计是考虑到传递右边的图标,一般都为需要弹出"picker"等操作时的场景,点击倒三角图标,理应发出此事件,见上方说明
  96. * @example <uni-field v-model="mobile" label="手机号" required :error-message="errorMessage"></uni-field>
  97. */
  98. export default {
  99. name: 'uni-field',
  100. props: {
  101. // rules:{
  102. // type:Array,
  103. // default(){
  104. // return []
  105. // }
  106. // },
  107. trigger: {
  108. type: String,
  109. default: ''
  110. },
  111. leftIcon: String,
  112. rightIcon: String,
  113. required: Boolean,
  114. label: String,
  115. password: Boolean,
  116. clearable: {
  117. type: Boolean,
  118. default: true
  119. },
  120. // 左边标题的宽度单位px
  121. labelWidth: {
  122. type: [Number, String],
  123. default: ''
  124. },
  125. // 对齐方式,left|center|right
  126. labelAlign: {
  127. type: String,
  128. default: ''
  129. },
  130. iconColor: {
  131. type: String,
  132. default: '#606266'
  133. },
  134. autoHeight: {
  135. type: Boolean,
  136. default: true
  137. },
  138. errorMessage: {
  139. type: [String, Boolean],
  140. default: ''
  141. },
  142. placeholder: String,
  143. placeholderStyle: String,
  144. focus: Boolean,
  145. name: String,
  146. value: [Number, String],
  147. type: {
  148. type: String,
  149. default: 'text'
  150. },
  151. disabled: {
  152. type: Boolean,
  153. default: false
  154. },
  155. maxlength: {
  156. type: [Number, String],
  157. default: 140
  158. },
  159. confirmType: {
  160. type: String,
  161. default: 'done'
  162. },
  163. // lable的位置,可选为 left-左边,top-上边
  164. labelPosition: {
  165. type: String,
  166. default: ''
  167. },
  168. // 清除按钮的大小
  169. clearSize: {
  170. type: [Number, String],
  171. default: 15
  172. },
  173. // 是否显示 input 边框
  174. inputBorder: {
  175. type: Boolean,
  176. default: false
  177. },
  178. // 是否显示上边框
  179. borderTop: {
  180. type: Boolean,
  181. default: false
  182. },
  183. // 是否显示下边框
  184. borderBottom: {
  185. type: Boolean,
  186. default: true
  187. },
  188. // 是否自动去除两端的空格
  189. trim: {
  190. type: Boolean,
  191. default: true
  192. }
  193. },
  194. data() {
  195. return {
  196. focused: false,
  197. itemIndex: 0,
  198. errorTop: false,
  199. errorBottom: false,
  200. labelMarginBottom: '',
  201. errorWidth: '',
  202. errMsg: '',
  203. errorBorderColor: false,
  204. val: '',
  205. labelPos: '',
  206. labelWid: '',
  207. labelAli: ''
  208. };
  209. },
  210. computed: {
  211. msg() {
  212. return this.errorMessage || this.errMsg;
  213. },
  214. fieldStyle() {
  215. let style = {};
  216. if (this.labelPos === 'top') {
  217. style.padding = '10px 14px';
  218. this.labelMarginBottom = '6px';
  219. }
  220. if (this.labelPos === 'left' && this.msg !== false && this.msg !== '') {
  221. style.paddingBottom = '0px';
  222. this.errorBottom = true;
  223. this.errorTop = false;
  224. } else if (this.labelPos === 'top' && this.msg !== false && this.msg !== '') {
  225. this.errorBottom = false;
  226. this.errorTop = true;
  227. } else {
  228. // style.paddingBottom = ''
  229. this.errorTop = false;
  230. this.errorBottom = false;
  231. }
  232. return style;
  233. },
  234. borderEixstTextareaStyle() {
  235. let style = {};
  236. if (this.inputBorder) {
  237. if (this.type === 'textarea') {
  238. style.minHeight = '60px';
  239. }
  240. if (this.msg !== false && this.msg != '') {
  241. style.borderColor = '#dd524d';
  242. }
  243. }
  244. return style;
  245. },
  246. inputWrapStyle() {
  247. let style = {};
  248. // 判断lable的位置,如果是left的话,让input左边两边有间隙
  249. if (this.labelPos == 'left') {
  250. style.margin = `0 4px`;
  251. } else {
  252. // 如果lable是top的,input的左边就没必要有间隙了
  253. style.marginRight = `4px`;
  254. // this.fieldStyle.style.padding = '10px 14px'
  255. }
  256. return style;
  257. },
  258. rightIconStyle() {
  259. let style = {};
  260. if (this.arrowDirection == 'top') style.transform = 'roate(-90deg)';
  261. if (this.arrowDirection == 'bottom') style.transform = 'roate(90deg)';
  262. else style.transform = 'roate(0deg)';
  263. return style;
  264. },
  265. labelStyle() {
  266. let style = {};
  267. if (this.labelAli == 'left') style.justifyContent = 'flext-start';
  268. if (this.labelAli == 'center') style.justifyContent = 'center';
  269. if (this.labelAli == 'right') style.justifyContent = 'flext-end';
  270. return style;
  271. },
  272. // uni不支持在computed中写style.justifyContent = 'center'的形式,故用此方法
  273. justifyContent() {
  274. if (this.labelAli == 'left') return 'flex-start';
  275. if (this.labelAli == 'center') return 'center';
  276. if (this.labelAli == 'right') return 'flex-end';
  277. },
  278. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  279. inputMaxlength() {
  280. return Number(this.maxlength);
  281. },
  282. // label的位置
  283. fieldInnerStyle() {
  284. let style = {};
  285. if (this.labelPos == 'left') {
  286. style.flexDirection = 'row';
  287. } else {
  288. style.flexDirection = 'column';
  289. }
  290. return style;
  291. }
  292. },
  293. watch: {
  294. trigger(trigger) {
  295. this.formTrigger = trigger;
  296. }
  297. },
  298. created() {
  299. this.form = this.getForm();
  300. this.formRules = [];
  301. this.formTrigger = this.trigger;
  302. this.init();
  303. },
  304. methods: {
  305. /**
  306. * 初始化变量值
  307. */
  308. init() {
  309. if (this.form) {
  310. this.form.childrens.push(this);
  311. this.labelPos = this.labelPosition ? this.labelPosition : this.form.labelPosition;
  312. this.labelWid = this.labelWidth ? this.labelWidth : this.form.labelWidth;
  313. this.labelAli = this.labelAlign ? this.labelAlign : this.form.labelAlign;
  314. if (this.form.formRules) {
  315. this.formRules = this.form.formRules[this.name];
  316. }
  317. this.validator = this.form.validator;
  318. if(this.name){
  319. this.form.formData[this.name] = this.value || '';
  320. }
  321. } else {
  322. this.labelPos = this.labelPosition || 'left';
  323. this.labelWid = this.labelWidth || 65;
  324. this.labelAli = this.labelAlign || 'left';
  325. }
  326. },
  327. /**
  328. * 获取父元素实例
  329. */
  330. getForm() {
  331. let parent = this.$parent;
  332. let parentName = parent.$options.name;
  333. while (parentName !== 'uniForms') {
  334. parent = parent.$parent;
  335. if (!parent) return false;
  336. parentName = parent.$options.name;
  337. }
  338. return parent;
  339. },
  340. /**
  341. * 移除该表单项的校验结果
  342. */
  343. clearValidate() {
  344. this.errMsg = '';
  345. },
  346. /**
  347. * 父组件处理函数
  348. * @param {Object} callback
  349. */
  350. parentVal(callback) {
  351. if (this.type === 'number') {
  352. this.val = this.val === '' ? this.val : Number(this.val);
  353. }
  354. typeof callback === 'function' &&
  355. callback(
  356. {
  357. [this.name]: this.val
  358. },
  359. this.name
  360. );
  361. },
  362. /**
  363. * 触发校验
  364. * @param {Object} trigger
  365. * @param {Object} value
  366. */
  367. triggerValidator(trigger, value) {
  368. let isValid = false;
  369. // 如果 name 不存在,则不开启校验
  370. this.formRules &&
  371. this.formRules.rules &&
  372. this.formRules.rules.forEach(item => {
  373. item.trigger = this.isTrigger(this.form.formTrigger, this.formTrigger, item.trigger);
  374. if (item.trigger !== trigger || item.trigger === 'submit') return;
  375. isValid = true;
  376. });
  377. isValid && this.triggerCheck(value);
  378. },
  379. /**
  380. * 校验规则
  381. * @param {Object} value
  382. */
  383. triggerCheck(value, item) {
  384. // 输入值为 number
  385. if (this.type === 'number') {
  386. value = value === '' ? value : Number(value);
  387. }
  388. const result = this.validator.validateUpdate({
  389. [this.name]: value
  390. });
  391. this.errMsg = !result ? '' : result.errorMessage;
  392. this.form.validateCheck(result);
  393. },
  394. /**
  395. * 触发时机
  396. * @param {Object} event
  397. */
  398. isTrigger(parentRule, itemRlue, rule) {
  399. let rl = 'none';
  400. if (rule) {
  401. rl = rule;
  402. } else if (itemRlue) {
  403. rl = itemRlue;
  404. } else if (parentRule) {
  405. rl = parentRule;
  406. } else {
  407. rl = 'blur';
  408. }
  409. return rl;
  410. },
  411. onInput(event) {
  412. let value = event.detail.value;
  413. // 判断是否去除空格
  414. if (this.trim) value = this.trimStr(value);
  415. this.form.formData[this.name] = value || '';
  416. this.val = value;
  417. this.$emit('input', value);
  418. // 校验输入
  419. this.triggerValidator('change', value);
  420. },
  421. onFocus(event) {
  422. this.focused = true;
  423. this.$emit('focus', event);
  424. },
  425. onBlur(event) {
  426. let value = event.detail.value;
  427. // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
  428. // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
  429. setTimeout(() => {
  430. this.focused = false;
  431. }, 100);
  432. this.$emit('blur', event);
  433. // 校验输入
  434. this.triggerValidator('blur', value);
  435. },
  436. onConfirm(e) {
  437. this.$emit('confirm', e.detail.value);
  438. },
  439. onClear(event) {
  440. this.val = '';
  441. this.$emit('input', '');
  442. this.clearValidate();
  443. },
  444. rightIconClick() {
  445. this.$emit('right-icon-click');
  446. this.$emit('click');
  447. },
  448. fieldClick() {
  449. this.$emit('click');
  450. },
  451. trimStr(str, pos = 'both') {
  452. if (pos == 'both') {
  453. return str.replace(/^\s+|\s+$/g, '');
  454. } else if (pos == 'left') {
  455. return str.replace(/^\s*/, '');
  456. } else if (pos == 'right') {
  457. return str.replace(/(\s*$)/g, '');
  458. } else if (pos == 'all') {
  459. return str.replace(/\s+/g, '');
  460. } else {
  461. return str;
  462. }
  463. }
  464. }
  465. };
  466. </script>
  467. <style lang="scss" scoped>
  468. .uni-field {
  469. padding: 16px 14px;
  470. text-align: left;
  471. color: #333;
  472. font-size: 14px;
  473. background-color: #fff;
  474. }
  475. .uni-field-inner {
  476. display: flex;
  477. align-items: center;
  478. }
  479. .uni-textarea-inner {
  480. align-items: flex-start;
  481. }
  482. .uni-textarea-class {
  483. min-height: 48px;
  484. width: auto;
  485. font-size: 14px;
  486. }
  487. .fild-body {
  488. width: 100%;
  489. display: flex;
  490. flex: 1;
  491. align-items: center;
  492. }
  493. .uni-arror-right {
  494. margin-left: 4px;
  495. }
  496. .uni-label-text {
  497. display: inline-block;
  498. }
  499. .uni-label-left-gap {
  500. margin-left: 3px;
  501. }
  502. .uni-label-postion-top {
  503. flex-direction: column;
  504. align-items: flex-start;
  505. flex: 1;
  506. }
  507. .uni-field-label {
  508. width: 65px;
  509. flex: 1 1 65px;
  510. text-align: left;
  511. position: relative;
  512. display: flex;
  513. align-items: center;
  514. }
  515. .uni-required::before {
  516. content: '*';
  517. position: absolute;
  518. left: -8px;
  519. font-size: 14px;
  520. color: $uni-color-error;
  521. height: 9px;
  522. line-height: 1;
  523. }
  524. .uni-field__input-wrap {
  525. position: relative;
  526. overflow: hidden;
  527. font-size: 14px;
  528. height: 24px;
  529. flex: 1;
  530. width: auto;
  531. }
  532. .uni-clear-icon {
  533. display: flex;
  534. align-items: center;
  535. }
  536. .uni-error-message {
  537. line-height: 12px;
  538. padding-top: 2px;
  539. padding-bottom: 2px;
  540. color: $uni-color-error;
  541. font-size: 12px;
  542. text-align: left;
  543. }
  544. .uni-input-error-border {
  545. border-color: $uni-color-error;
  546. }
  547. .placeholder-style {
  548. color: rgb(150, 151, 153);
  549. }
  550. .uni-input-class {
  551. font-size: 14px;
  552. }
  553. .uni-button-wrap {
  554. margin-left: 4px;
  555. }
  556. /* start--Retina 屏幕下的 1px 边框--start */
  557. .uni-border,
  558. .uni-border-bottom,
  559. .uni-border-left,
  560. .uni-border-right,
  561. .uni-border-top,
  562. .uni-border-top-bottom {
  563. position: relative;
  564. }
  565. .uni-border-bottom:after,
  566. .uni-border-left:after,
  567. .uni-border-right:after,
  568. .uni-border-top-bottom:after,
  569. .uni-border-top:after,
  570. .uni-border:after {
  571. /* #ifndef APP-NVUE */
  572. content: ' ';
  573. /* #endif */
  574. position: absolute;
  575. left: 0;
  576. top: 0;
  577. pointer-events: none;
  578. box-sizing: border-box;
  579. -webkit-transform-origin: 0 0;
  580. transform-origin: 0 0;
  581. // 多加0.1%,能解决有时候边框缺失的问题
  582. width: 199.8%;
  583. height: 199.7%;
  584. transform: scale(0.5, 0.5);
  585. border: 0 solid $uni-border-color;
  586. z-index: 2;
  587. }
  588. .uni-input-border {
  589. min-height: 34px;
  590. padding-left: 4px;
  591. border: 1px solid $uni-border-color;
  592. border-radius: 6px;
  593. box-sizing: border-box;
  594. }
  595. .uni-border-top:after {
  596. border-top-width: 1px;
  597. }
  598. .uni-border-left:after {
  599. border-left-width: 1px;
  600. }
  601. .uni-border-right:after {
  602. border-right-width: 1px;
  603. }
  604. .uni-border-bottom:after {
  605. border-bottom-width: 1px;
  606. }
  607. .uni-border-top-bottom:after {
  608. border-width: 1px 0;
  609. }
  610. .uni-border:after {
  611. border-width: 1px;
  612. }
  613. /* end--Retina 屏幕下的 1px 边框--end */
  614. .uni-icon-wrap {
  615. padding-left: 3px;
  616. padding-right: 3px;
  617. display: flex;
  618. align-items: center;
  619. justify-content: center;
  620. }
  621. .uni-button-wrap {
  622. display: flex;
  623. align-items: right;
  624. justify-content: center;
  625. }
  626. .uni-clear-icon {
  627. display: flex;
  628. align-items: center;
  629. margin-left: 4px;
  630. }
  631. .uni-flex {
  632. /* #ifndef APP-NVUE */
  633. display: flex;
  634. /* #endif */
  635. flex-direction: row;
  636. align-items: center;
  637. }
  638. .uni-flex-1 {
  639. flex: 1;
  640. }
  641. .uni-error-in-label {
  642. display: flex;
  643. flex-direction: row;
  644. }
  645. </style>