u-input.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <view class="u-input" :class="{
  3. 'u-input--border': border,
  4. 'u-input--error': validateState
  5. }" :style="{
  6. padding: `0 ${border ? 20 : 0}rpx`,
  7. borderColor: borderColor,
  8. textAlign: inputAlign
  9. }" @tap.stop="inputClick">
  10. <textarea v-if="type == 'textarea'" class="u-input__input u-input__textarea" :style="[getStyle]"
  11. :value="defaultValue" :placeholder="placeholder" :placeholderStyle="placeholderStyle" :disabled="disabled"
  12. :maxlength="inputMaxlength" :fixed="fixed" :focus="focus" :autoHeight="autoHeight"
  13. :selection-end="uSelectionEnd" :selection-start="uSelectionStart" :cursor-spacing="getCursorSpacing"
  14. :show-confirm-bar="showConfirmbar" @input="handleInput" @blur="handleBlur" @focus="onFocus"
  15. @confirm="onConfirm" />
  16. <input v-else class="u-input__input" :type="type == 'password' ? 'text' : type" :style="[getStyle]"
  17. :value="defaultValue" :password="type == 'password' && !showPassword" :placeholder="placeholder"
  18. :placeholderStyle="placeholderStyle" :disabled="disabled || type === 'select'" :maxlength="inputMaxlength"
  19. :focus="focus" :confirmType="confirmType" :cursor-spacing="getCursorSpacing" :selection-end="uSelectionEnd"
  20. :selection-start="uSelectionStart" :show-confirm-bar="showConfirmbar" @focus="onFocus" @blur="handleBlur"
  21. @input="handleInput" @confirm="onConfirm" />
  22. <view class="u-input__right-icon u-flex">
  23. <view class="u-input__right-icon__clear u-input__right-icon__item" @tap="onClear"
  24. v-if="clearable && value != '' && focused">
  25. <u-icon size="32" name="close-circle-fill" color="#c0c4cc" />
  26. </view>
  27. <view class="u-input__right-icon__clear u-input__right-icon__item"
  28. v-if="passwordIcon && type == 'password'">
  29. <u-icon size="32" :name="!showPassword ? 'eye' : 'eye-fill'" color="#c0c4cc"
  30. @click="showPassword = !showPassword" />
  31. </view>
  32. <view class="u-input__right-icon--select u-input__right-icon__item" v-if="type == 'select'" :class="{
  33. 'u-input__right-icon--select--reverse': selectOpen
  34. }">
  35. <u-icon name="arrow-down-fill" size="26" color="#c0c4cc"></u-icon>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import Emitter from '../../libs/util/emitter.js';
  42. /**
  43. * input 输入框
  44. * @description 此组件为一个输入框,默认没有边框和样式,是专门为配合表单组件u-form而设计的,利用它可以快速实现表单验证,输入内容,下拉选择等功能。
  45. * @tutorial http://uviewui.com/components/input.html
  46. * @property {String} type 模式选择,见官网说明
  47. * @property {Boolean} clearable 是否显示右侧的清除图标(默认true)
  48. * @property {} v-model 用于双向绑定输入框的值
  49. * @property {String} input-align 输入框文字的对齐方式(默认left)
  50. * @property {String} placeholder placeholder显示值(默认 '请输入内容')
  51. * @property {Boolean} disabled 是否禁用输入框(默认false)
  52. * @property {String Number} maxlength 输入框的最大可输入长度(默认140)
  53. * @property {String Number} selection-start 光标起始位置,自动聚焦时有效,需与selection-end搭配使用(默认-1)
  54. * @property {String Number} maxlength 光标结束位置,自动聚焦时有效,需与selection-start搭配使用(默认-1)
  55. * @property {String Number} cursor-spacing 指定光标与键盘的距离,单位px(默认0)
  56. * @property {String} placeholderStyle placeholder的样式,字符串形式,如"color: red;"(默认 "color: #c0c4cc;")
  57. * @property {String} confirm-type 设置键盘右下角按钮的文字,仅在type为text时生效(默认done)
  58. * @property {Object} custom-style 自定义输入框的样式,对象形式
  59. * @property {Boolean} focus 是否自动获得焦点(默认false)
  60. * @property {Boolean} fixed 如果type为textarea,且在一个"position:fixed"的区域,需要指明为true(默认false)
  61. * @property {Boolean} password-icon type为password时,是否显示右侧的密码查看图标(默认true)
  62. * @property {Boolean} border 是否显示边框(默认false)
  63. * @property {String} border-color 输入框的边框颜色(默认#dcdfe6)
  64. * @property {Boolean} auto-height 是否自动增高输入区域,type为textarea时有效(默认true)
  65. * @property {String Number} height 高度,单位rpx(text类型时为70,textarea时为100)
  66. * @example <u-input v-model="value" :type="type" :border="border" />
  67. */
  68. export default {
  69. name: 'u-input',
  70. mixins: [Emitter],
  71. props: {
  72. value: {
  73. type: [String, Number],
  74. default: ''
  75. },
  76. // 输入框的类型,textarea,text,number
  77. type: {
  78. type: String,
  79. default: 'text'
  80. },
  81. inputAlign: {
  82. type: String,
  83. default: 'left'
  84. },
  85. placeholder: {
  86. type: String,
  87. default: '请输入内容'
  88. },
  89. disabled: {
  90. type: Boolean,
  91. default: false
  92. },
  93. maxlength: {
  94. type: [Number, String],
  95. default: 140
  96. },
  97. placeholderStyle: {
  98. type: String,
  99. default: 'color: #c0c4cc;'
  100. },
  101. confirmType: {
  102. type: String,
  103. default: 'done'
  104. },
  105. // 输入框的自定义样式
  106. customStyle: {
  107. type: Object,
  108. default () {
  109. return {};
  110. }
  111. },
  112. // 如果 textarea 是在一个 position:fixed 的区域,需要显示指定属性 fixed 为 true
  113. fixed: {
  114. type: Boolean,
  115. default: false
  116. },
  117. // 是否自动获得焦点
  118. focus: {
  119. type: Boolean,
  120. default: false
  121. },
  122. // 密码类型时,是否显示右侧的密码图标
  123. passwordIcon: {
  124. type: Boolean,
  125. default: true
  126. },
  127. // input|textarea是否显示边框
  128. border: {
  129. type: Boolean,
  130. default: false
  131. },
  132. // 输入框的边框颜色
  133. borderColor: {
  134. type: String,
  135. default: '#dcdfe6'
  136. },
  137. autoHeight: {
  138. type: Boolean,
  139. default: true
  140. },
  141. // type=select时,旋转右侧的图标,标识当前处于打开还是关闭select的状态
  142. // open-打开,close-关闭
  143. selectOpen: {
  144. type: Boolean,
  145. default: false
  146. },
  147. // 高度,单位rpx
  148. height: {
  149. type: [Number, String],
  150. default: ''
  151. },
  152. // 是否可清空
  153. clearable: {
  154. type: Boolean,
  155. default: true
  156. },
  157. // 指定光标与键盘的距离,单位 px
  158. cursorSpacing: {
  159. type: [Number, String],
  160. default: 0
  161. },
  162. // 光标起始位置,自动聚焦时有效,需与selection-end搭配使用
  163. selectionStart: {
  164. type: [Number, String],
  165. default: -1
  166. },
  167. // 光标结束位置,自动聚焦时有效,需与selection-start搭配使用
  168. selectionEnd: {
  169. type: [Number, String],
  170. default: -1
  171. },
  172. // 是否自动去除两端的空格
  173. trim: {
  174. type: Boolean,
  175. default: true
  176. },
  177. // 是否显示键盘上方带有”完成“按钮那一栏
  178. showConfirmbar: {
  179. type: Boolean,
  180. default: true
  181. }
  182. },
  183. data() {
  184. return {
  185. defaultValue: this.value,
  186. inputHeight: 70, // input的高度
  187. textareaHeight: 100, // textarea的高度
  188. validateState: false, // 当前input的验证状态,用于错误时,边框是否改为红色
  189. focused: false, // 当前是否处于获得焦点的状态
  190. showPassword: false, // 是否预览密码
  191. lastValue: '', // 用于头条小程序,判断@input中,前后的值是否发生了变化,因为头条中文下,按下键没有输入内容,也会触发@input时间
  192. };
  193. },
  194. watch: {
  195. value(nVal, oVal) {
  196. this.defaultValue = nVal;
  197. // 当值发生变化,且为select类型时(此时input被设置为disabled,不会触发@input事件),模拟触发@input事件
  198. if (nVal != oVal && this.type == 'select') this.handleInput({
  199. detail: {
  200. value: nVal
  201. }
  202. })
  203. },
  204. },
  205. computed: {
  206. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  207. inputMaxlength() {
  208. return Number(this.maxlength);
  209. },
  210. getStyle() {
  211. let style = {};
  212. // 如果没有自定义高度,就根据type为input还是textare来分配一个默认的高度
  213. style.minHeight = this.height ? this.height + 'rpx' : this.type == 'textarea' ?
  214. this.textareaHeight + 'rpx' : this.inputHeight + 'rpx';
  215. if (this.disabled) {
  216. style.pointerEvents = 'none'
  217. }
  218. style = Object.assign(style, this.customStyle);
  219. return style;
  220. },
  221. //
  222. getCursorSpacing() {
  223. return Number(this.cursorSpacing);
  224. },
  225. // 光标起始位置
  226. uSelectionStart() {
  227. return String(this.selectionStart);
  228. },
  229. // 光标结束位置
  230. uSelectionEnd() {
  231. return String(this.selectionEnd);
  232. }
  233. },
  234. created() {
  235. // 监听u-form-item发出的错误事件,将输入框边框变红色
  236. this.$on('on-form-item-error', this.onFormItemError);
  237. },
  238. methods: {
  239. /**
  240. * change 事件
  241. * @param event
  242. */
  243. handleInput(event) {
  244. let value = event.detail.value;
  245. // 判断是否去除空格
  246. if (this.trim) value = this.$u.trim(value);
  247. // vue 原生的方法 return 出去
  248. this.$emit('input', value);
  249. // 当前model 赋值
  250. this.defaultValue = value;
  251. // 过一个生命周期再发送事件给u-form-item,否则this.$emit('input')更新了父组件的值,但是微信小程序上
  252. // 尚未更新到u-form-item,导致获取的值为空,从而校验混论
  253. // 这里不能延时时间太短,或者使用this.$nextTick,否则在头条上,会造成混乱
  254. setTimeout(() => {
  255. // 头条小程序由于自身bug,导致中文下,每按下一个键(尚未完成输入),都会触发一次@input,导致错误,这里进行判断处理
  256. // #ifdef MP-TOUTIAO
  257. if (this.$u.trim(value) == this.lastValue) return;
  258. this.lastValue = value;
  259. // #endif
  260. // 将当前的值发送到 u-form-item 进行校验
  261. this.dispatch('u-form-item', 'on-form-change', value);
  262. }, 40)
  263. },
  264. /**
  265. * blur 事件
  266. * @param event
  267. */
  268. handleBlur(event) {
  269. // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
  270. // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
  271. setTimeout(() => {
  272. this.focused = false;
  273. }, 100)
  274. // vue 原生的方法 return 出去
  275. this.$emit('blur', event.detail.value);
  276. setTimeout(() => {
  277. // 头条小程序由于自身bug,导致中文下,每按下一个键(尚未完成输入),都会触发一次@input,导致错误,这里进行判断处理
  278. // #ifdef MP-TOUTIAO
  279. if (this.$u.trim(value) == this.lastValue) return;
  280. this.lastValue = value;
  281. // #endif
  282. // 将当前的值发送到 u-form-item 进行校验
  283. this.dispatch('u-form-item', 'on-form-blur', event.detail.value);
  284. }, 40)
  285. },
  286. onFormItemError(status) {
  287. this.validateState = status;
  288. },
  289. onFocus(event) {
  290. this.focused = true;
  291. this.$emit('focus');
  292. },
  293. onConfirm(e) {
  294. this.$emit('confirm', e.detail.value);
  295. },
  296. onClear(event) {
  297. this.$emit('input', '');
  298. },
  299. inputClick() {
  300. this.$emit('click');
  301. }
  302. }
  303. };
  304. </script>
  305. <style lang="scss" scoped>
  306. @import "../../libs/css/style.components.scss";
  307. .u-input {
  308. position: relative;
  309. flex: 1;
  310. @include vue-flex;
  311. &__input {
  312. height: $u-form-item-height;
  313. font-size: 28rpx;
  314. // color: #FFF;
  315. flex: 1;
  316. }
  317. &__textarea {
  318. width: auto;
  319. font-size: 28rpx;
  320. color: $u-main-color;
  321. padding: 10rpx 0;
  322. line-height: normal;
  323. flex: 1;
  324. }
  325. &--border {
  326. border-radius: 6rpx;
  327. border-radius: 4px;
  328. border: 1px solid $u-form-item-border-color;
  329. }
  330. &--error {
  331. border-color: $u-type-error !important;
  332. }
  333. &__right-icon {
  334. &__item {
  335. margin-left: 10rpx;
  336. }
  337. &--select {
  338. transition: transform .4s;
  339. &--reverse {
  340. transform: rotate(-180deg);
  341. }
  342. }
  343. }
  344. }
  345. </style>