uni-easyinput.vue 17 KB

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