uni-field.vue 17 KB

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