u-input.vue 11 KB

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