uni-easyinput.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <template>
  2. <view class="uni-easyinput" :class="{'uni-easyinput-error':msg}" :style="{color:inputBorder && msg?'#dd524d':styles.color}">
  3. <view class="uni-easyinput__content" :class="{'is-input-border':inputBorder ,'is-input-error-border':inputBorder && msg,'is-textarea':type==='textarea','is-disabled':disabled}"
  4. :style="{'border-color':inputBorder && msg?'#dd524d':styles.borderColor,'background-color':disabled?styles.disableColor:''}">
  5. <uni-icons v-if="prefixIcon" class="content-clear-icon" :type="prefixIcon" color="#c0c4cc" @click="onClickIcon('prefix')"></uni-icons>
  6. <textarea v-if="type === 'textarea'" class="uni-easyinput__content-textarea" :class="{'input-padding':inputBorder}"
  7. :name="name" :value="val" :placeholder="placeholder" :placeholderStyle="placeholderStyle" :disabled="disabled"
  8. :maxlength="inputMaxlength" :focus="focused" :autoHeight="autoHeight" @input="onInput" @blur="onBlur" @focus="onFocus"
  9. @confirm="onConfirm"></textarea>
  10. <input v-else :type="type === 'password'?'text':type" class="uni-easyinput__content-input" :style="{
  11. 'padding-right':type === 'password' ||clearable || prefixIcon?'':'10px',
  12. 'padding-left':prefixIcon?'':'10px'
  13. }"
  14. :name="name" :value="val" :password="!showPassword && type === 'password'" :placeholder="placeholder"
  15. :placeholderStyle="placeholderStyle" :disabled="disabled" :maxlength="inputMaxlength" :focus="focused" :confirmType="confirmType" @focus="onFocus"
  16. @blur="onBlur" @input="onInput" @confirm="onConfirm" />
  17. <template v-if="type === 'password' && passwordIcon" >
  18. <uni-icons v-if="val != '' " class="content-clear-icon" :class="{'is-textarea-icon':type==='textarea'}" :type="showPassword?'eye-slash-filled':'eye-filled'"
  19. :size="18" color="#c0c4cc" @click="onEyes"></uni-icons>
  20. </template>
  21. <template v-else-if="suffixIcon">
  22. <uni-icons v-if="suffixIcon" class="content-clear-icon" :type="suffixIcon" color="#c0c4cc" @click="onClickIcon('suffix')"></uni-icons>
  23. </template>
  24. <template v-else>
  25. <uni-icons class="content-clear-icon" :class="{'is-textarea-icon':type==='textarea'}" type="clear" :size="clearSize"
  26. v-if="clearable && val && !disabled" color="#c0c4cc" @click="onClear"></uni-icons>
  27. </template>
  28. <slot name="right"></slot>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. // import {
  34. // debounce,
  35. // throttle
  36. // } from './common.js'
  37. /**
  38. * Easyinput 输入框
  39. * @description 此组件可以实现表单的输入与校验,包括 "text" 和 "textarea" 类型。
  40. * @tutorial https://ext.dcloud.net.cn/plugin?id=3455
  41. * @property {String} value 输入内容
  42. * @property {String } type 输入框的类型(默认text) password/text/textarea/..
  43. * @value text 文本输入键盘
  44. * @value textarea 多行文本输入键盘
  45. * @value password 密码输入键盘
  46. * @value number 数字输入键盘,注意iOS上app-vue弹出的数字键盘并非9宫格方式
  47. * @value idcard 身份证输入键盘,信、支付宝、百度、QQ小程序
  48. * @value digit 带小数点的数字键盘 ,App的nvue页面、微信、支付宝、百度、头条、QQ小程序支持
  49. * @property {Boolean} clearable 是否显示右侧清空内容的图标控件,点击可清空输入框内容(默认true)
  50. * @property {Boolean} autoHeight 是否自动增高输入区域,type为textarea时有效(默认true)
  51. * @property {String } placeholder 输入框的提示文字
  52. * @property {String } placeholderStyle placeholder的样式(内联样式,字符串),如"color: #ddd"
  53. * @property {Boolean} focus 是否自动获得焦点(默认false)
  54. * @property {Boolean} disabled 是否禁用(默认false)
  55. * @property {Number } maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
  56. * @property {String } confirmType 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
  57. * @property {Number } clearSize 清除图标的大小,单位px(默认15)
  58. * @property {String} prefixIcon 输入框头部图标
  59. * @property {String} suffixIcon 输入框尾部图标
  60. * @property {Boolean} trim 是否自动去除两端的空格
  61. * @value both 去除两端空格
  62. * @value left 去除左侧空格
  63. * @value right 去除右侧空格
  64. * @value start 去除左侧空格
  65. * @value end 去除右侧空格
  66. * @value all 去除全部空格
  67. * @value none 不去除空格
  68. * @property {Boolean} inputBorder 是否显示input输入框的边框(默认true)
  69. * @property {Boolean} passwordIcon type=password时是否显示小眼睛图标
  70. * @property {Object} styles 自定义颜色
  71. * @event {Function} input 输入框内容发生变化时触发
  72. * @event {Function} focus 输入框获得焦点时触发
  73. * @event {Function} blur 输入框失去焦点时触发
  74. * @event {Function} confirm 点击完成按钮时触发
  75. * @event {Function} iconClick 点击图标时触发
  76. * @example <uni-easyinput v-model="mobile"></uni-easyinput>
  77. */
  78. export default {
  79. name: 'uni-easyinput',
  80. emits:['click','iconClick','update:modelValue','input','focus','blur','confirm'],
  81. model:{
  82. prop:'modelValue',
  83. event:'update:modelValue'
  84. },
  85. props: {
  86. name: String,
  87. value: [Number, String],
  88. modelValue: [Number, String],
  89. type: {
  90. type: String,
  91. default: 'text'
  92. },
  93. clearable: {
  94. type: Boolean,
  95. default: true
  96. },
  97. autoHeight: {
  98. type: Boolean,
  99. default: false
  100. },
  101. placeholder: String,
  102. placeholderStyle: String,
  103. focus: {
  104. type: Boolean,
  105. default: false
  106. },
  107. disabled: {
  108. type: Boolean,
  109. default: false
  110. },
  111. maxlength: {
  112. type: [Number, String],
  113. default: 140
  114. },
  115. confirmType: {
  116. type: String,
  117. default: 'done'
  118. },
  119. clearSize: {
  120. type: [Number, String],
  121. default: 15
  122. },
  123. inputBorder: {
  124. type: Boolean,
  125. default: true
  126. },
  127. prefixIcon: {
  128. type: String,
  129. default: ''
  130. },
  131. suffixIcon: {
  132. type: String,
  133. default: ''
  134. },
  135. trim: {
  136. type: [Boolean, String],
  137. default: true
  138. },
  139. passwordIcon:{
  140. type: Boolean,
  141. default: true
  142. },
  143. styles: {
  144. type: Object,
  145. default () {
  146. return {
  147. color: '#333',
  148. disableColor: '#eee',
  149. borderColor: '#e5e5e5'
  150. }
  151. }
  152. },
  153. errorMessage:{
  154. type:[String,Boolean],
  155. default:''
  156. }
  157. },
  158. data() {
  159. return {
  160. focused: false,
  161. errMsg: '',
  162. val: '',
  163. showMsg: '',
  164. border: false,
  165. isFirstBorder: false,
  166. showClearIcon: false,
  167. showPassword: false
  168. };
  169. },
  170. computed: {
  171. msg() {
  172. return this.errorMessage || this.errMsg;
  173. },
  174. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  175. inputMaxlength() {
  176. return Number(this.maxlength);
  177. },
  178. },
  179. watch: {
  180. value(newVal) {
  181. if (this.errMsg) this.errMsg = ''
  182. this.val = newVal
  183. // fix by mehaotian is_reset 在 uni-forms 中定义
  184. if (this.form && this.formItem &&!this.is_reset) {
  185. this.is_reset = false
  186. this.formItem.setValue(newVal)
  187. }
  188. },
  189. modelValue(newVal) {
  190. if (this.errMsg) this.errMsg = ''
  191. this.val = newVal
  192. if (this.form && this.formItem &&!this.is_reset) {
  193. this.is_reset = false
  194. this.formItem.setValue(newVal)
  195. }
  196. },
  197. focus(newVal) {
  198. this.$nextTick(() => {
  199. this.focused = this.focus
  200. })
  201. }
  202. },
  203. created() {
  204. if(!this.value){
  205. this.val = this.modelValue
  206. }
  207. if(!this.modelValue){
  208. this.val = this.value
  209. }
  210. this.form = this.getForm('uniForms')
  211. this.formItem = this.getForm('uniFormsItem')
  212. if (this.form && this.formItem) {
  213. if (this.formItem.name) {
  214. if(!this.is_reset){
  215. this.is_reset = false
  216. this.formItem.setValue(this.val)
  217. }
  218. this.rename = this.formItem.name
  219. this.form.inputChildrens.push(this)
  220. }
  221. }
  222. },
  223. mounted() {
  224. // this.onInput = throttle(this.input, 500)
  225. this.$nextTick(() => {
  226. // setTimeout(()=>{
  227. // },1000)
  228. this.focused = this.focus
  229. })
  230. },
  231. methods: {
  232. /**
  233. * 初始化变量值
  234. */
  235. init() {
  236. },
  237. onClickIcon(type) {
  238. this.$emit('iconClick', type)
  239. },
  240. /**
  241. * 获取父元素实例
  242. */
  243. getForm(name = 'uniForms') {
  244. let parent = this.$parent;
  245. let parentName = parent.$options.name;
  246. while (parentName !== name) {
  247. parent = parent.$parent;
  248. if (!parent) return false;
  249. parentName = parent.$options.name;
  250. }
  251. return parent;
  252. },
  253. onEyes() {
  254. this.showPassword = !this.showPassword
  255. },
  256. onInput(event) {
  257. let value = event.detail.value;
  258. // 判断是否去除空格
  259. if (this.trim) {
  260. if (typeof(this.trim) === 'boolean' && this.trim) {
  261. value = this.trimStr(value)
  262. }
  263. if (typeof(this.trim) === 'string') {
  264. value = this.trimStr(value, this.trim)
  265. }
  266. };
  267. if (this.errMsg) this.errMsg = ''
  268. this.val = value
  269. // TODO 兼容 vue2
  270. this.$emit('input', value);
  271. // TODO 兼容 vue3
  272. this.$emit('update:modelValue',value)
  273. },
  274. onFocus(event) {
  275. // this.focused = true;
  276. this.$emit('focus', event);
  277. },
  278. onBlur(event) {
  279. let value = event.detail.value;
  280. // setTimeout(() => {
  281. // this.focused = false;
  282. // }, 100);
  283. this.$emit('blur', event);
  284. },
  285. onConfirm(e) {
  286. this.$emit('confirm', e.detail.value);
  287. },
  288. onClear(event) {
  289. this.val = '';
  290. // TODO 兼容 vue2
  291. this.$emit('input', '');
  292. // TODO 兼容 vue2
  293. // TODO 兼容 vue3
  294. this.$emit('update:modelValue','')
  295. },
  296. fieldClick() {
  297. this.$emit('click');
  298. },
  299. trimStr(str, pos = 'both') {
  300. if (pos === 'both') {
  301. return str.trim();
  302. } else if (pos === 'left') {
  303. return str.trimLeft();
  304. } else if (pos === 'right') {
  305. return str.trimRight();
  306. } else if (pos === 'start') {
  307. return str.trimStart()
  308. } else if (pos === 'end') {
  309. return str.trimEnd()
  310. } else if (pos === 'all') {
  311. return str.replace(/\s+/g, '');
  312. } else if (pos === 'none') {
  313. return str;
  314. }
  315. return str;
  316. }
  317. }
  318. };
  319. </script>
  320. <style lang="scss" scoped>
  321. .uni-easyinput {
  322. /* #ifndef APP-NVUE */
  323. width: 100%;
  324. /* #endif */
  325. flex: 1;
  326. position: relative;
  327. text-align: left;
  328. color: #333;
  329. font-size: 14px;
  330. }
  331. .uni-easyinput__content {
  332. flex: 1;
  333. /* #ifndef APP-NVUE */
  334. width: 100%;
  335. display: flex;
  336. box-sizing: border-box;
  337. min-height: 36px;
  338. /* #endif */
  339. flex-direction: row;
  340. align-items: center;
  341. }
  342. .uni-easyinput__content-input {
  343. /* #ifndef APP-NVUE */
  344. width: auto;
  345. /* #endif */
  346. position: relative;
  347. overflow: hidden;
  348. flex: 1;
  349. line-height: 1;
  350. font-size: 14px;
  351. }
  352. .is-textarea {
  353. align-items: flex-start;
  354. }
  355. .is-textarea-icon {
  356. margin-top: 5px;
  357. }
  358. .uni-easyinput__content-textarea {
  359. position: relative;
  360. overflow: hidden;
  361. flex: 1;
  362. line-height: 1.5;
  363. font-size: 14px;
  364. padding-top: 6px;
  365. padding-bottom: 10px;
  366. height: 80px;
  367. /* #ifndef APP-NVUE */
  368. min-height: 80px;
  369. width: auto;
  370. /* #endif */
  371. }
  372. .input-padding {
  373. padding-left: 10px;
  374. }
  375. .content-clear-icon {
  376. padding: 0 5px;
  377. }
  378. .label-icon {
  379. margin-right: 5px;
  380. margin-top: -1px;
  381. }
  382. // 显示边框
  383. .is-input-border {
  384. /* #ifndef APP-NVUE */
  385. display: flex;
  386. box-sizing: border-box;
  387. /* #endif */
  388. flex-direction: row;
  389. align-items: center;
  390. border: 1px solid $uni-border-color;
  391. border-radius: 4px;
  392. }
  393. .uni-easyinput__right {
  394. // margin-left: 5px;
  395. }
  396. // 必填
  397. .is-required {
  398. color: $uni-color-error;
  399. }
  400. .uni-error-message {
  401. position: absolute;
  402. bottom: -17px;
  403. left: 0;
  404. line-height: 12px;
  405. color: $uni-color-error;
  406. font-size: 12px;
  407. text-align: left;
  408. }
  409. .uni-error-msg--boeder {
  410. position: relative;
  411. bottom: 0;
  412. line-height: 22px;
  413. }
  414. .is-input-error-border {
  415. border-color: $uni-color-error;
  416. }
  417. .uni-easyinput--border {
  418. margin-bottom: 0;
  419. padding: 10px 15px;
  420. // padding-bottom: 0;
  421. border-top: 1px #eee solid;
  422. }
  423. .uni-easyinput-error {
  424. padding-bottom: 0;
  425. }
  426. .is-first-border {
  427. /* #ifndef APP-NVUE */
  428. border: none;
  429. /* #endif */
  430. /* #ifdef APP-NVUE */
  431. border-width: 0;
  432. /* #endif */
  433. }
  434. .is-disabled {
  435. background-color: #eee;
  436. }
  437. </style>