u-input.vue 14 KB

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