uni-easyinput.vue 18 KB

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