uni-easyinput.vue 15 KB

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