uni-easyinput.vue 15 KB

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