uni-easyinput.vue 18 KB

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