uni-easyinput.vue 18 KB

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