uni-easyinput.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <template>
  2. <view class="uni-easyinput" :class="{ 'uni-easyinput-error': msg }" :style="boxStyle">
  3. <view class="uni-easyinput__content" :class="inputContentClass" :style="inputContentStyle">
  4. <uni-icons v-if="prefixIcon" class="content-clear-icon" :type="prefixIcon" color="#c0c4cc" @click="onClickIcon('prefix')" size="22"></uni-icons>
  5. <textarea
  6. v-if="type === 'textarea'"
  7. class="uni-easyinput__content-textarea"
  8. :class="{ 'input-padding': inputBorder }"
  9. :name="name"
  10. :value="val"
  11. :placeholder="placeholder"
  12. :placeholderStyle="placeholderStyle"
  13. :disabled="disabled"
  14. placeholder-class="uni-easyinput__placeholder-class"
  15. :maxlength="inputMaxlength"
  16. :focus="focused"
  17. :autoHeight="autoHeight"
  18. :cursor-spacing="cursorSpacing"
  19. @input="onInput"
  20. @blur="_Blur"
  21. @focus="_Focus"
  22. @confirm="onConfirm"
  23. @keyboardheightchange="onkeyboardheightchange"
  24. ></textarea>
  25. <input
  26. v-else
  27. :type="type === 'password' ? 'text' : type"
  28. class="uni-easyinput__content-input"
  29. :style="inputStyle"
  30. :name="name"
  31. :value="val"
  32. :password="!showPassword && type === 'password'"
  33. :placeholder="placeholder"
  34. :placeholderStyle="placeholderStyle"
  35. placeholder-class="uni-easyinput__placeholder-class"
  36. :disabled="disabled"
  37. :maxlength="inputMaxlength"
  38. :focus="focused"
  39. :confirmType="confirmType"
  40. :cursor-spacing="cursorSpacing"
  41. @focus="_Focus"
  42. @blur="_Blur"
  43. @input="onInput"
  44. @confirm="onConfirm"
  45. @keyboardheightchange="onkeyboardheightchange"
  46. />
  47. <template v-if="type === 'password' && passwordIcon">
  48. <!-- 开启密码时显示小眼睛 -->
  49. <uni-icons
  50. v-if="isVal"
  51. class="content-clear-icon"
  52. :class="{ 'is-textarea-icon': type === 'textarea' }"
  53. :type="showPassword ? 'eye-slash-filled' : 'eye-filled'"
  54. :size="22"
  55. :color="focusShow ? primaryColor : '#c0c4cc'"
  56. @click="onEyes"
  57. ></uni-icons>
  58. </template>
  59. <template v-else-if="suffixIcon">
  60. <uni-icons v-if="suffixIcon" class="content-clear-icon" :type="suffixIcon" color="#c0c4cc" @click="onClickIcon('suffix')" size="22"></uni-icons>
  61. </template>
  62. <template v-else>
  63. <uni-icons
  64. v-if="clearable && isVal && !disabled && type !== 'textarea'"
  65. class="content-clear-icon"
  66. :class="{ 'is-textarea-icon': type === 'textarea' }"
  67. type="clear"
  68. :size="clearSize"
  69. :color="msg ? '#dd524d' : focusShow ? primaryColor : '#c0c4cc'"
  70. @click="onClear"
  71. ></uni-icons>
  72. </template>
  73. <slot name="right"></slot>
  74. </view>
  75. </view>
  76. </template>
  77. <script>
  78. /**
  79. * Easyinput 输入框
  80. * @description 此组件可以实现表单的输入与校验,包括 "text" 和 "textarea" 类型。
  81. * @tutorial https://ext.dcloud.net.cn/plugin?id=3455
  82. * @property {String} value 输入内容
  83. * @property {String } type 输入框的类型(默认text) password/text/textarea/..
  84. * @value text 文本输入键盘
  85. * @value textarea 多行文本输入键盘
  86. * @value password 密码输入键盘
  87. * @value number 数字输入键盘,注意iOS上app-vue弹出的数字键盘并非9宫格方式
  88. * @value idcard 身份证输入键盘,信、支付宝、百度、QQ小程序
  89. * @value digit 带小数点的数字键盘 ,App的nvue页面、微信、支付宝、百度、头条、QQ小程序支持
  90. * @property {Boolean} clearable 是否显示右侧清空内容的图标控件,点击可清空输入框内容(默认true)
  91. * @property {Boolean} autoHeight 是否自动增高输入区域,type为textarea时有效(默认true)
  92. * @property {String } placeholder 输入框的提示文字
  93. * @property {String } placeholderStyle placeholder的样式(内联样式,字符串),如"color: #ddd"
  94. * @property {Boolean} focus 是否自动获得焦点(默认false)
  95. * @property {Boolean} disabled 是否禁用(默认false)
  96. * @property {Number } maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
  97. * @property {String } confirmType 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
  98. * @property {Number } clearSize 清除图标的大小,单位px(默认15)
  99. * @property {String} prefixIcon 输入框头部图标
  100. * @property {String} suffixIcon 输入框尾部图标
  101. * @property {String} primaryColor 设置主题色(默认#2979ff)
  102. * @property {Boolean} trim 是否自动去除两端的空格
  103. * @property {Boolean} cursorSpacing 指定光标与键盘的距离,单位 px
  104. * @value both 去除两端空格
  105. * @value left 去除左侧空格
  106. * @value right 去除右侧空格
  107. * @value start 去除左侧空格
  108. * @value end 去除右侧空格
  109. * @value all 去除全部空格
  110. * @value none 不去除空格
  111. * @property {Boolean} inputBorder 是否显示input输入框的边框(默认true)
  112. * @property {Boolean} passwordIcon type=password时是否显示小眼睛图标
  113. * @property {Object} styles 自定义颜色
  114. * @event {Function} input 输入框内容发生变化时触发
  115. * @event {Function} focus 输入框获得焦点时触发
  116. * @event {Function} blur 输入框失去焦点时触发
  117. * @event {Function} confirm 点击完成按钮时触发
  118. * @event {Function} iconClick 点击图标时触发
  119. * @example <uni-easyinput v-model="mobile"></uni-easyinput>
  120. */
  121. function obj2strClass(obj) {
  122. let classess = '';
  123. for (let key in obj) {
  124. const val = obj[key];
  125. if (val) {
  126. classess += `${key} `;
  127. }
  128. }
  129. return classess;
  130. }
  131. function obj2strStyle(obj) {
  132. let style = '';
  133. for (let key in obj) {
  134. const val = obj[key];
  135. style += `${key}:${val};`;
  136. }
  137. return style;
  138. }
  139. export default {
  140. name: 'uni-easyinput',
  141. emits: ['click', 'iconClick', 'update:modelValue', 'input', 'focus', 'blur', 'confirm', 'clear', 'eyes', 'change', 'keyboardheightchange'],
  142. model: {
  143. prop: 'modelValue',
  144. event: 'update:modelValue'
  145. },
  146. options: {
  147. virtualHost: true
  148. },
  149. inject: {
  150. form: {
  151. from: 'uniForm',
  152. default: null
  153. },
  154. formItem: {
  155. from: 'uniFormItem',
  156. default: null
  157. }
  158. },
  159. props: {
  160. name: String,
  161. value: [Number, String],
  162. modelValue: [Number, String],
  163. type: {
  164. type: String,
  165. default: 'text'
  166. },
  167. clearable: {
  168. type: Boolean,
  169. default: true
  170. },
  171. autoHeight: {
  172. type: Boolean,
  173. default: false
  174. },
  175. placeholder: {
  176. type: String,
  177. default: ' '
  178. },
  179. placeholderStyle: String,
  180. focus: {
  181. type: Boolean,
  182. default: false
  183. },
  184. disabled: {
  185. type: Boolean,
  186. default: false
  187. },
  188. maxlength: {
  189. type: [Number, String],
  190. default: 140
  191. },
  192. confirmType: {
  193. type: String,
  194. default: 'done'
  195. },
  196. clearSize: {
  197. type: [Number, String],
  198. default: 24
  199. },
  200. inputBorder: {
  201. type: Boolean,
  202. default: true
  203. },
  204. prefixIcon: {
  205. type: String,
  206. default: ''
  207. },
  208. suffixIcon: {
  209. type: String,
  210. default: ''
  211. },
  212. trim: {
  213. type: [Boolean, String],
  214. default: false
  215. },
  216. cursorSpacing: {
  217. type: Number,
  218. default: 0
  219. },
  220. passwordIcon: {
  221. type: Boolean,
  222. default: true
  223. },
  224. primaryColor: {
  225. type: String,
  226. default: '#2979ff'
  227. },
  228. styles: {
  229. type: Object,
  230. default() {
  231. return {
  232. color: '#333',
  233. backgroundColor: '#fff',
  234. disableColor: '#F7F6F6',
  235. borderColor: '#e5e5e5'
  236. };
  237. }
  238. },
  239. errorMessage: {
  240. type: [String, Boolean],
  241. default: ''
  242. }
  243. },
  244. data() {
  245. return {
  246. focused: false,
  247. val: '',
  248. showMsg: '',
  249. border: false,
  250. isFirstBorder: false,
  251. showClearIcon: false,
  252. showPassword: false,
  253. focusShow: false,
  254. localMsg: '',
  255. isEnter: false // 用于判断当前是否是使用回车操作
  256. };
  257. },
  258. computed: {
  259. // 输入框内是否有值
  260. isVal() {
  261. const val = this.val;
  262. // fixed by mehaotian 处理值为0的情况,字符串0不在处理范围
  263. if (val || val === 0) {
  264. return true;
  265. }
  266. return false;
  267. },
  268. msg() {
  269. // console.log('computed', this.form, this.formItem);
  270. // if (this.form) {
  271. // return this.errorMessage || this.formItem.errMsg;
  272. // }
  273. // TODO 处理头条 formItem 中 errMsg 不更新的问题
  274. return this.localMsg || this.errorMessage;
  275. },
  276. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,用户可以传入字符串数值
  277. inputMaxlength() {
  278. return Number(this.maxlength);
  279. },
  280. // 处理外层样式的style
  281. boxStyle() {
  282. return `color:${this.inputBorder && this.msg ? '#e43d33' : this.styles.color};`;
  283. },
  284. // input 内容的类和样式处理
  285. inputContentClass() {
  286. return obj2strClass({
  287. 'is-input-border': this.inputBorder,
  288. 'is-input-error-border': this.inputBorder && this.msg,
  289. 'is-textarea': this.type === 'textarea',
  290. 'is-disabled': this.disabled,
  291. 'is-focused': this.focusShow
  292. });
  293. },
  294. inputContentStyle() {
  295. const focusColor = this.focusShow ? this.primaryColor : this.styles.borderColor;
  296. const borderColor = this.inputBorder && this.msg ? '#dd524d' : focusColor;
  297. return obj2strStyle({
  298. 'border-color': borderColor || '#e5e5e5',
  299. 'background-color': this.disabled ? this.styles.disableColor : this.styles.backgroundColor
  300. });
  301. },
  302. // input右侧样式
  303. inputStyle() {
  304. const paddingRight = this.type === 'password' || this.clearable || this.prefixIcon ? '' : '10px';
  305. return obj2strStyle({
  306. 'padding-right': paddingRight,
  307. 'padding-left': this.prefixIcon ? '' : '10px'
  308. });
  309. }
  310. },
  311. watch: {
  312. value(newVal) {
  313. this.val = newVal;
  314. },
  315. modelValue(newVal) {
  316. this.val = newVal;
  317. },
  318. focus(newVal) {
  319. this.$nextTick(() => {
  320. this.focused = this.focus;
  321. this.focusShow = this.focus;
  322. });
  323. }
  324. },
  325. created() {
  326. this.init();
  327. // TODO 处理头条vue3 computed 不监听 inject 更改的问题(formItem.errMsg)
  328. if (this.form && this.formItem) {
  329. this.$watch('formItem.errMsg', newVal => {
  330. this.localMsg = newVal;
  331. });
  332. }
  333. },
  334. mounted() {
  335. this.$nextTick(() => {
  336. this.focused = this.focus;
  337. this.focusShow = this.focus;
  338. });
  339. },
  340. methods: {
  341. /**
  342. * 初始化变量值
  343. */
  344. init() {
  345. if (this.value || this.value === 0) {
  346. this.val = this.value;
  347. } else if (this.modelValue || this.modelValue === 0 || this.modelValue === '') {
  348. this.val = this.modelValue;
  349. } else {
  350. this.val = null;
  351. }
  352. },
  353. /**
  354. * 点击图标时触发
  355. * @param {Object} type
  356. */
  357. onClickIcon(type) {
  358. this.$emit('iconClick', type);
  359. },
  360. /**
  361. * 显示隐藏内容,密码框时生效
  362. */
  363. onEyes() {
  364. this.showPassword = !this.showPassword;
  365. this.$emit('eyes', this.showPassword);
  366. },
  367. /**
  368. * 输入时触发
  369. * @param {Object} event
  370. */
  371. onInput(event) {
  372. let value = event.detail.value;
  373. // 判断是否去除空格
  374. if (this.trim) {
  375. if (typeof this.trim === 'boolean' && this.trim) {
  376. value = this.trimStr(value);
  377. }
  378. if (typeof this.trim === 'string') {
  379. value = this.trimStr(value, this.trim);
  380. }
  381. }
  382. if (this.errMsg) this.errMsg = '';
  383. this.val = value;
  384. // TODO 兼容 vue2
  385. this.$emit('input', value);
  386. // TODO 兼容 vue3
  387. this.$emit('update:modelValue', value);
  388. },
  389. /**
  390. * 外部调用方法
  391. * 获取焦点时触发
  392. * @param {Object} event
  393. */
  394. onFocus() {
  395. this.$nextTick(() => {
  396. this.focused = true;
  397. });
  398. this.$emit('focus', null);
  399. },
  400. _Focus(event) {
  401. this.focusShow = true;
  402. this.$emit('focus', event);
  403. },
  404. /**
  405. * 外部调用方法
  406. * 失去焦点时触发
  407. * @param {Object} event
  408. */
  409. onBlur() {
  410. this.focused = false;
  411. this.$emit('focus', null);
  412. },
  413. _Blur(event) {
  414. let value = event.detail.value;
  415. this.focusShow = false;
  416. this.$emit('blur', event);
  417. // 根据类型返回值,在event中获取的值理论上讲都是string
  418. if (this.isEnter === false) {
  419. this.$emit('change', this.val);
  420. }
  421. // 失去焦点时参与表单校验
  422. if (this.form && this.formItem) {
  423. const { validateTrigger } = this.form;
  424. if (validateTrigger === 'blur') {
  425. this.formItem.onFieldChange();
  426. }
  427. }
  428. },
  429. /**
  430. * 按下键盘的发送键
  431. * @param {Object} e
  432. */
  433. onConfirm(e) {
  434. this.$emit('confirm', this.val);
  435. this.isEnter = true;
  436. this.$emit('change', this.val);
  437. this.$nextTick(() => {
  438. this.isEnter = false;
  439. });
  440. },
  441. /**
  442. * 清理内容
  443. * @param {Object} event
  444. */
  445. onClear(event) {
  446. this.val = '';
  447. // TODO 兼容 vue2
  448. this.$emit('input', '');
  449. // TODO 兼容 vue2
  450. // TODO 兼容 vue3
  451. this.$emit('update:modelValue', '');
  452. // 点击叉号触发
  453. this.$emit('clear');
  454. },
  455. /**
  456. * 键盘高度发生变化的时候触发此事件
  457. * 兼容性:微信小程序2.7.0+、App 3.1.0+
  458. * @param {Object} event
  459. */
  460. onkeyboardheightchange(event) {
  461. this.$emit("keyboardheightchange",event);
  462. },
  463. /**
  464. * 去除空格
  465. */
  466. trimStr(str, pos = 'both') {
  467. if (pos === 'both') {
  468. return str.trim();
  469. } else if (pos === 'left') {
  470. return str.trimLeft();
  471. } else if (pos === 'right') {
  472. return str.trimRight();
  473. } else if (pos === 'start') {
  474. return str.trimStart();
  475. } else if (pos === 'end') {
  476. return str.trimEnd();
  477. } else if (pos === 'all') {
  478. return str.replace(/\s+/g, '');
  479. } else if (pos === 'none') {
  480. return str;
  481. }
  482. return str;
  483. }
  484. }
  485. };
  486. </script>
  487. <style lang="scss">
  488. $uni-error: #e43d33;
  489. $uni-border-1: #dcdfe6 !default;
  490. .uni-easyinput {
  491. /* #ifndef APP-NVUE */
  492. width: 100%;
  493. /* #endif */
  494. flex: 1;
  495. position: relative;
  496. text-align: left;
  497. color: #333;
  498. font-size: 14px;
  499. }
  500. .uni-easyinput__content {
  501. flex: 1;
  502. /* #ifndef APP-NVUE */
  503. width: 100%;
  504. display: flex;
  505. box-sizing: border-box;
  506. // min-height: 36px;
  507. /* #endif */
  508. flex-direction: row;
  509. align-items: center;
  510. // 处理border动画刚开始显示黑色的问题
  511. border-color: #fff;
  512. transition-property: border-color;
  513. transition-duration: 0.3s;
  514. }
  515. .uni-easyinput__content-input {
  516. /* #ifndef APP-NVUE */
  517. width: auto;
  518. /* #endif */
  519. position: relative;
  520. overflow: hidden;
  521. flex: 1;
  522. line-height: 1;
  523. font-size: 14px;
  524. height: 35px;
  525. // min-height: 36px;
  526. }
  527. .uni-easyinput__placeholder-class {
  528. color: #999;
  529. font-size: 12px;
  530. // font-weight: 200;
  531. }
  532. .is-textarea {
  533. align-items: flex-start;
  534. }
  535. .is-textarea-icon {
  536. margin-top: 5px;
  537. }
  538. .uni-easyinput__content-textarea {
  539. position: relative;
  540. overflow: hidden;
  541. flex: 1;
  542. line-height: 1.5;
  543. font-size: 14px;
  544. margin: 6px;
  545. margin-left: 0;
  546. height: 80px;
  547. min-height: 80px;
  548. /* #ifndef APP-NVUE */
  549. min-height: 80px;
  550. width: auto;
  551. /* #endif */
  552. }
  553. .input-padding {
  554. padding-left: 10px;
  555. }
  556. .content-clear-icon {
  557. padding: 0 5px;
  558. }
  559. .label-icon {
  560. margin-right: 5px;
  561. margin-top: -1px;
  562. }
  563. // 显示边框
  564. .is-input-border {
  565. /* #ifndef APP-NVUE */
  566. display: flex;
  567. box-sizing: border-box;
  568. /* #endif */
  569. flex-direction: row;
  570. align-items: center;
  571. border: 1px solid $uni-border-1;
  572. border-radius: 4px;
  573. /* #ifdef MP-ALIPAY */
  574. overflow: hidden;
  575. /* #endif */
  576. }
  577. .uni-error-message {
  578. position: absolute;
  579. bottom: -17px;
  580. left: 0;
  581. line-height: 12px;
  582. color: $uni-error;
  583. font-size: 12px;
  584. text-align: left;
  585. }
  586. .uni-error-msg--boeder {
  587. position: relative;
  588. bottom: 0;
  589. line-height: 22px;
  590. }
  591. .is-input-error-border {
  592. border-color: $uni-error;
  593. .uni-easyinput__placeholder-class {
  594. color: mix(#fff, $uni-error, 50%);
  595. }
  596. }
  597. .uni-easyinput--border {
  598. margin-bottom: 0;
  599. padding: 10px 15px;
  600. // padding-bottom: 0;
  601. border-top: 1px #eee solid;
  602. }
  603. .uni-easyinput-error {
  604. padding-bottom: 0;
  605. }
  606. .is-first-border {
  607. /* #ifndef APP-NVUE */
  608. border: none;
  609. /* #endif */
  610. /* #ifdef APP-NVUE */
  611. border-width: 0;
  612. /* #endif */
  613. }
  614. .is-disabled {
  615. background-color: #f7f6f6;
  616. color: #d5d5d5;
  617. .uni-easyinput__placeholder-class {
  618. color: #d5d5d5;
  619. font-size: 12px;
  620. }
  621. }
  622. </style>