u-input.vue 13 KB

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