uni-easyinput.vue 15 KB

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