uni-easyinput.vue 13 KB

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