uni-easyinput.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. };
  205. },
  206. computed: {
  207. // 输入框内是否有值
  208. isVal() {
  209. const val = this.val
  210. // fixed by mehaotian 处理值为0的情况,字符串0不在处理范围
  211. if (val || val === 0) {
  212. return true
  213. }
  214. return false
  215. },
  216. msg() {
  217. // console.log('computed', this.form, this.formItem);
  218. // if (this.form) {
  219. // return this.errorMessage || this.formItem.errMsg;
  220. // }
  221. // TODO 处理头条 formItem 中 errMsg 不更新的问题
  222. return this.localMsg || this.errorMessage
  223. },
  224. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,用户可以传入字符串数值
  225. inputMaxlength() {
  226. return Number(this.maxlength);
  227. },
  228. // 处理外层样式的style
  229. boxStyle() {
  230. return `color:${this.inputBorder && this.msg?'#e43d33':this.styles.color};`
  231. },
  232. // input 内容的类和样式处理
  233. inputContentClass() {
  234. return obj2strClass({
  235. 'is-input-border': this.inputBorder,
  236. 'is-input-error-border': this.inputBorder && this.msg,
  237. 'is-textarea': this.type === 'textarea',
  238. 'is-disabled': this.disabled
  239. })
  240. },
  241. inputContentStyle() {
  242. const focusColor = this.focusShow ? '#2979ff' : this.styles.borderColor
  243. const borderColor = this.inputBorder && this.msg ? '#dd524d' : focusColor
  244. return obj2strStyle({
  245. 'border-color': borderColor || '#e5e5e5',
  246. 'background-color': this.disabled ? this.styles.disableColor : '#fff'
  247. })
  248. },
  249. // input右侧样式
  250. inputStyle() {
  251. const paddingRight = this.type === 'password' || this.clearable || this.prefixIcon ? '' : '10px'
  252. return obj2strStyle({
  253. 'padding-right': paddingRight,
  254. 'padding-left': this.prefixIcon ? '' : '10px'
  255. })
  256. }
  257. },
  258. watch: {
  259. value(newVal) {
  260. this.val = newVal
  261. },
  262. modelValue(newVal) {
  263. this.val = newVal
  264. },
  265. focus(newVal) {
  266. this.$nextTick(() => {
  267. this.focused = this.focus
  268. this.focusShow = this.focus
  269. })
  270. }
  271. },
  272. created() {
  273. this.init()
  274. // TODO 处理头条vue3 computed 不监听 inject 更改的问题(formItem.errMsg)
  275. if (this.form && this.formItem) {
  276. this.$watch('formItem.errMsg', (newVal) => {
  277. this.localMsg = newVal
  278. })
  279. }
  280. },
  281. mounted() {
  282. this.$nextTick(() => {
  283. this.focused = this.focus
  284. this.focusShow = this.focus
  285. })
  286. },
  287. methods: {
  288. /**
  289. * 初始化变量值
  290. */
  291. init() {
  292. if (this.value || this.value === 0) {
  293. this.val = this.value
  294. } else if (this.modelValue || this.modelValue === 0) {
  295. this.val = this.modelValue
  296. } else {
  297. this.val = null
  298. }
  299. },
  300. /**
  301. * 点击图标时触发
  302. * @param {Object} type
  303. */
  304. onClickIcon(type) {
  305. this.$emit('iconClick', type)
  306. },
  307. /**
  308. * 显示隐藏内容,密码框时生效
  309. */
  310. onEyes() {
  311. this.showPassword = !this.showPassword
  312. this.$emit('eyes', this.showPassword)
  313. },
  314. /**
  315. * 输入时触发
  316. * @param {Object} event
  317. */
  318. onInput(event) {
  319. let value = event.detail.value;
  320. // 判断是否去除空格
  321. if (this.trim) {
  322. if (typeof(this.trim) === 'boolean' && this.trim) {
  323. value = this.trimStr(value)
  324. }
  325. if (typeof(this.trim) === 'string') {
  326. value = this.trimStr(value, this.trim)
  327. }
  328. };
  329. if (this.errMsg) this.errMsg = ''
  330. this.val = value
  331. // TODO 兼容 vue2
  332. this.$emit('input', value);
  333. // TODO 兼容 vue3
  334. this.$emit('update:modelValue', value)
  335. },
  336. /**
  337. * 外部调用方法
  338. * 获取焦点时触发
  339. * @param {Object} event
  340. */
  341. onFocus() {
  342. this.$nextTick(() => {
  343. this.focused = true
  344. })
  345. this.$emit('focus', null);
  346. },
  347. _Focus(event) {
  348. this.focusShow = true
  349. this.$emit('focus', event);
  350. },
  351. /**
  352. * 外部调用方法
  353. * 失去焦点时触发
  354. * @param {Object} event
  355. */
  356. onBlur() {
  357. this.focused = false
  358. this.$emit('focus', null);
  359. },
  360. _Blur(event) {
  361. let value = event.detail.value;
  362. this.focusShow = false
  363. this.$emit('blur', event);
  364. // 根据类型返回值,在event中获取的值理论上讲都是string
  365. this.$emit('change', this.val)
  366. // 失去焦点时参与表单校验
  367. if (this.form && this.formItem) {
  368. const {
  369. validateTrigger
  370. } = this.form
  371. if (validateTrigger === 'blur') {
  372. this.formItem.onFieldChange()
  373. }
  374. }
  375. },
  376. /**
  377. * 按下键盘的发送键
  378. * @param {Object} e
  379. */
  380. onConfirm(e) {
  381. this.$emit('confirm', this.val);
  382. this.$emit('change', this.val)
  383. },
  384. /**
  385. * 清理内容
  386. * @param {Object} event
  387. */
  388. onClear(event) {
  389. this.val = '';
  390. // TODO 兼容 vue2
  391. this.$emit('input', '');
  392. // TODO 兼容 vue2
  393. // TODO 兼容 vue3
  394. this.$emit('update:modelValue', '')
  395. // 点击叉号触发
  396. this.$emit('clear')
  397. },
  398. /**
  399. * 去除空格
  400. */
  401. trimStr(str, pos = 'both') {
  402. if (pos === 'both') {
  403. return str.trim();
  404. } else if (pos === 'left') {
  405. return str.trimLeft();
  406. } else if (pos === 'right') {
  407. return str.trimRight();
  408. } else if (pos === 'start') {
  409. return str.trimStart()
  410. } else if (pos === 'end') {
  411. return str.trimEnd()
  412. } else if (pos === 'all') {
  413. return str.replace(/\s+/g, '');
  414. } else if (pos === 'none') {
  415. return str;
  416. }
  417. return str;
  418. }
  419. }
  420. };
  421. </script>
  422. <style lang="scss">
  423. $uni-error: #e43d33;
  424. $uni-border-1: #DCDFE6 !default;
  425. .uni-easyinput {
  426. /* #ifndef APP-NVUE */
  427. width: 100%;
  428. /* #endif */
  429. flex: 1;
  430. position: relative;
  431. text-align: left;
  432. color: #333;
  433. font-size: 14px;
  434. }
  435. .uni-easyinput__content {
  436. flex: 1;
  437. /* #ifndef APP-NVUE */
  438. width: 100%;
  439. display: flex;
  440. box-sizing: border-box;
  441. // min-height: 36px;
  442. /* #endif */
  443. flex-direction: row;
  444. align-items: center;
  445. // 处理border动画刚开始显示黑色的问题
  446. border-color: #fff;
  447. transition-property: border-color;
  448. transition-duration: 0.3s;
  449. }
  450. .uni-easyinput__content-input {
  451. /* #ifndef APP-NVUE */
  452. width: auto;
  453. /* #endif */
  454. position: relative;
  455. overflow: hidden;
  456. flex: 1;
  457. line-height: 1;
  458. font-size: 14px;
  459. height: 35px;
  460. // min-height: 36px;
  461. }
  462. .uni-easyinput__placeholder-class {
  463. color: #999;
  464. font-size: 12px;
  465. // font-weight: 200;
  466. }
  467. .is-textarea {
  468. align-items: flex-start;
  469. }
  470. .is-textarea-icon {
  471. margin-top: 5px;
  472. }
  473. .uni-easyinput__content-textarea {
  474. position: relative;
  475. overflow: hidden;
  476. flex: 1;
  477. line-height: 1.5;
  478. font-size: 14px;
  479. margin: 6px;
  480. margin-left: 0;
  481. height: 80px;
  482. min-height: 80px;
  483. /* #ifndef APP-NVUE */
  484. min-height: 80px;
  485. width: auto;
  486. /* #endif */
  487. }
  488. .input-padding {
  489. padding-left: 10px;
  490. }
  491. .content-clear-icon {
  492. padding: 0 5px;
  493. }
  494. .label-icon {
  495. margin-right: 5px;
  496. margin-top: -1px;
  497. }
  498. // 显示边框
  499. .is-input-border {
  500. /* #ifndef APP-NVUE */
  501. display: flex;
  502. box-sizing: border-box;
  503. /* #endif */
  504. flex-direction: row;
  505. align-items: center;
  506. border: 1px solid $uni-border-1;
  507. border-radius: 4px;
  508. /* #ifdef MP-ALIPAY */
  509. overflow: hidden;
  510. /* #endif */
  511. }
  512. .uni-error-message {
  513. position: absolute;
  514. bottom: -17px;
  515. left: 0;
  516. line-height: 12px;
  517. color: $uni-error;
  518. font-size: 12px;
  519. text-align: left;
  520. }
  521. .uni-error-msg--boeder {
  522. position: relative;
  523. bottom: 0;
  524. line-height: 22px;
  525. }
  526. .is-input-error-border {
  527. border-color: $uni-error;
  528. .uni-easyinput__placeholder-class {
  529. color: mix(#fff, $uni-error, 50%);
  530. ;
  531. }
  532. }
  533. .uni-easyinput--border {
  534. margin-bottom: 0;
  535. padding: 10px 15px;
  536. // padding-bottom: 0;
  537. border-top: 1px #eee solid;
  538. }
  539. .uni-easyinput-error {
  540. padding-bottom: 0;
  541. }
  542. .is-first-border {
  543. /* #ifndef APP-NVUE */
  544. border: none;
  545. /* #endif */
  546. /* #ifdef APP-NVUE */
  547. border-width: 0;
  548. /* #endif */
  549. }
  550. .is-disabled {
  551. background-color: #F7F6F6;
  552. color: #D5D5D5;
  553. .uni-easyinput__placeholder-class {
  554. color: #D5D5D5;
  555. font-size: 12px;
  556. }
  557. }
  558. </style>