uni-easyinput.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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" @focus="onFocus"
  16. @blur="onBlur" @input="onInput" @confirm="onConfirm" />
  17. <template v-if="type === 'password'">
  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 " 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 {Object} styles 自定义颜色
  70. * @event {Function} input 输入框内容发生变化时触发
  71. * @event {Function} focus 输入框获得焦点时触发
  72. * @event {Function} blur 输入框失去焦点时触发
  73. * @event {Function} confirm 点击完成按钮时触发
  74. * @event {Function} iconClick 点击图标时触发
  75. * @example <uni-easyinput v-model="mobile"></uni-easyinput>
  76. */
  77. export default {
  78. name: 'uni-easyinput',
  79. props: {
  80. name: String,
  81. value: [Number, String],
  82. type: {
  83. type: String,
  84. default: 'text'
  85. },
  86. clearable: {
  87. type: Boolean,
  88. default: true
  89. },
  90. autoHeight: {
  91. type: Boolean,
  92. default: false
  93. },
  94. placeholder: String,
  95. placeholderStyle: String,
  96. focus: {
  97. type: Boolean,
  98. default: false
  99. },
  100. disabled: {
  101. type: Boolean,
  102. default: false
  103. },
  104. maxlength: {
  105. type: [Number, String],
  106. default: 140
  107. },
  108. confirmType: {
  109. type: String,
  110. default: 'done'
  111. },
  112. // 清除按钮的大小
  113. clearSize: {
  114. type: [Number, String],
  115. default: 15
  116. },
  117. // 是否显示 input 边框
  118. inputBorder: {
  119. type: Boolean,
  120. default: true
  121. },
  122. prefixIcon: {
  123. type: String,
  124. default: ''
  125. },
  126. suffixIcon: {
  127. type: String,
  128. default: ''
  129. },
  130. // 是否自动去除两端的空格
  131. trim: {
  132. type: [Boolean, String],
  133. default: true
  134. },
  135. // 自定义样式
  136. styles: {
  137. type: Object,
  138. default () {
  139. return {
  140. color: '#333',
  141. disableColor: '#eee',
  142. borderColor: '#e5e5e5'
  143. }
  144. }
  145. }
  146. },
  147. data() {
  148. return {
  149. focused: false,
  150. errMsg: '',
  151. val: '',
  152. showMsg: '',
  153. border: false,
  154. isFirstBorder: false,
  155. showClearIcon: false,
  156. showPassword: false
  157. };
  158. },
  159. computed: {
  160. msg() {
  161. return this.errorMessage || this.errMsg;
  162. },
  163. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  164. inputMaxlength() {
  165. return Number(this.maxlength);
  166. },
  167. },
  168. watch: {
  169. value(newVal) {
  170. if (this.errMsg) this.errMsg = ''
  171. this.val = newVal
  172. if (this.form && this.formItem) {
  173. this.formItem.setValue(newVal)
  174. }
  175. },
  176. focus(newVal) {
  177. this.$nextTick(() => {
  178. this.focused = this.focus
  179. })
  180. }
  181. },
  182. created() {
  183. this.val = this.value
  184. this.form = this.getForm('uniForms')
  185. this.formItem = this.getForm('uniFormsItem')
  186. if (this.form && this.formItem) {
  187. if (this.formItem.name) {
  188. this.rename = this.formItem.name
  189. this.form.inputChildrens.push(this)
  190. }
  191. }
  192. },
  193. mounted() {
  194. // this.onInput = throttle(this.input, 500)
  195. this.$nextTick(() => {
  196. // setTimeout(()=>{
  197. // },1000)
  198. this.focused = this.focus
  199. })
  200. },
  201. methods: {
  202. /**
  203. * 初始化变量值
  204. */
  205. init() {
  206. },
  207. onClickIcon(type) {
  208. this.$emit('iconClick', type)
  209. },
  210. /**
  211. * 获取父元素实例
  212. */
  213. getForm(name = 'uniForms') {
  214. let parent = this.$parent;
  215. let parentName = parent.$options.name;
  216. while (parentName !== name) {
  217. parent = parent.$parent;
  218. if (!parent) return false;
  219. parentName = parent.$options.name;
  220. }
  221. return parent;
  222. },
  223. onEyes() {
  224. this.showPassword = !this.showPassword
  225. },
  226. onInput(event) {
  227. let value = event.detail.value;
  228. // 判断是否去除空格
  229. if (this.trim) {
  230. if (typeof(this.trim) === 'boolean' && this.trim) {
  231. value = this.trimStr(value)
  232. }
  233. if (typeof(this.trim) === 'string') {
  234. value = this.trimStr(value, this.trim)
  235. }
  236. };
  237. if (this.errMsg) this.errMsg = ''
  238. this.val = value
  239. this.$emit('input', value);
  240. },
  241. onFocus(event) {
  242. // this.focused = true;
  243. this.$emit('focus', event);
  244. },
  245. onBlur(event) {
  246. let value = event.detail.value;
  247. // setTimeout(() => {
  248. // this.focused = false;
  249. // }, 100);
  250. this.$emit('blur', event);
  251. },
  252. onConfirm(e) {
  253. this.$emit('confirm', e.detail.value);
  254. },
  255. onClear(event) {
  256. this.val = '';
  257. this.$emit('input', '');
  258. },
  259. fieldClick() {
  260. this.$emit('click');
  261. },
  262. trimStr(str, pos = 'both') {
  263. if (pos === 'both') {
  264. return str.trim();
  265. } else if (pos === 'left') {
  266. return str.trimLeft();
  267. } else if (pos === 'right') {
  268. return str.trimRight();
  269. } else if (pos === 'start') {
  270. return str.trimStart()
  271. } else if (pos === 'end') {
  272. return str.trimEnd()
  273. } else if (pos === 'all') {
  274. return str.replace(/\s+/g, '');
  275. } else if (pos === 'none') {
  276. return str;
  277. }
  278. return str;
  279. }
  280. }
  281. };
  282. </script>
  283. <style lang="scss" scoped>
  284. .uni-easyinput {
  285. /* #ifndef APP-NVUE */
  286. width: 100%;
  287. /* #endif */
  288. flex: 1;
  289. position: relative;
  290. text-align: left;
  291. color: #333;
  292. font-size: 14px;
  293. }
  294. .uni-easyinput__content {
  295. flex: 1;
  296. /* #ifndef APP-NVUE */
  297. width: 100%;
  298. display: flex;
  299. box-sizing: border-box;
  300. /* #endif */
  301. min-height: 36px;
  302. flex-direction: row;
  303. align-items: center;
  304. }
  305. .uni-easyinput__content-input {
  306. /* #ifndef APP-NVUE */
  307. width: auto;
  308. /* #endif */
  309. position: relative;
  310. overflow: hidden;
  311. flex: 1;
  312. line-height: 1;
  313. font-size: 14px;
  314. }
  315. .is-textarea {
  316. align-items: flex-start;
  317. }
  318. .is-textarea-icon {
  319. margin-top: 5px;
  320. }
  321. .uni-easyinput__content-textarea {
  322. position: relative;
  323. overflow: hidden;
  324. flex: 1;
  325. line-height: 1.5;
  326. font-size: 14px;
  327. padding-top: 6px;
  328. padding-bottom: 10px;
  329. height: 80px;
  330. /* #ifndef APP-NVUE */
  331. min-height: 80px;
  332. width: auto;
  333. /* #endif */
  334. }
  335. .input-padding {
  336. padding-left: 10px;
  337. }
  338. .content-clear-icon {
  339. padding: 0 5px;
  340. }
  341. .label-icon {
  342. margin-right: 5px;
  343. margin-top: -1px;
  344. }
  345. // 显示边框
  346. .is-input-border {
  347. /* #ifndef APP-NVUE */
  348. display: flex;
  349. box-sizing: border-box;
  350. /* #endif */
  351. flex-direction: row;
  352. align-items: center;
  353. border: 1px solid $uni-border-color;
  354. border-radius: 4px;
  355. }
  356. .uni-easyinput__right {
  357. // margin-left: 5px;
  358. }
  359. // 必填
  360. .is-required {
  361. color: $uni-color-error;
  362. }
  363. .uni-error-message {
  364. position: absolute;
  365. bottom: -17px;
  366. left: 0;
  367. line-height: 12px;
  368. color: $uni-color-error;
  369. font-size: 12px;
  370. text-align: left;
  371. }
  372. .uni-error-msg--boeder {
  373. position: relative;
  374. bottom: 0;
  375. line-height: 22px;
  376. }
  377. .is-input-error-border {
  378. border-color: $uni-color-error;
  379. }
  380. .uni-easyinput--border {
  381. margin-bottom: 0;
  382. padding: 10px 15px;
  383. // padding-bottom: 0;
  384. border-top: 1px #eee solid;
  385. }
  386. .uni-easyinput-error {
  387. padding-bottom: 0;
  388. }
  389. .is-first-border {
  390. /* #ifndef APP-NVUE */
  391. border: none;
  392. /* #endif */
  393. /* #ifdef APP-NVUE */
  394. border-width: 0;
  395. /* #endif */
  396. }
  397. .is-disabled {
  398. background-color: #eee;
  399. }
  400. </style>