u-input.vue 14 KB

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