u-input.vue 13 KB

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