u-input.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <template>
  2. <view
  3. class="u-input"
  4. :class="inputClass"
  5. :style="[wrapperStyle]"
  6. >
  7. <view class="u-input__content">
  8. <view
  9. class="u-input__content__prefix-icon"
  10. v-if="prefixIcon || $slots.prefix"
  11. >
  12. <slot name="prefix">
  13. <u-icon
  14. :name="prefixIcon"
  15. size="18"
  16. :customStyle="prefixIconStyle"
  17. ></u-icon>
  18. </slot>
  19. </view>
  20. <view
  21. class="u-input__content__field-wrapper"
  22. @tap="clickHandler"
  23. >
  24. <!-- 根据uni-app的input组件文档,H5和APP中只要声明了password参数(无论true还是false),type均失效,此时
  25. 为了防止type=number时,又存在password属性,type无效,此时需要设置password为undefined
  26. -->
  27. <input
  28. class="u-input__content__field-wrapper__field"
  29. :style="[inputStyle]"
  30. :type="type"
  31. :focus="focus"
  32. :cursor="cursor"
  33. :value="innerValue"
  34. :auto-blur="autoBlur"
  35. <!-- #ifdef H5 -->
  36. :disabled="disabled"
  37. :readonly="readonly"
  38. <!-- #endif -->
  39. <!-- #ifndef H5 -->
  40. :disabled="disabled || readonly"
  41. <!-- #endif -->
  42. :maxlength="maxlength"
  43. :always-embed="alwaysEmbed"
  44. :placeholder="placeholder"
  45. :placeholder-style="placeholderStyle"
  46. :placeholder-class="placeholderClass"
  47. :confirm-type="confirmType"
  48. :confirm-hold="confirmHold"
  49. :hold-keyboard="holdKeyboard"
  50. :cursor-color="cursorColor"
  51. :cursor-spacing="cursorSpacing"
  52. :adjust-position="adjustPosition"
  53. :selection-end="selectionEnd"
  54. :selection-start="selectionStart"
  55. :password="isPassword"
  56. :ignoreCompositionEvent="ignoreCompositionEvent"
  57. @input="onInput"
  58. @blur="onBlur"
  59. @focus="onFocus"
  60. @confirm="onConfirm"
  61. @keyboardheightchange="onkeyboardheightchange"
  62. />
  63. </view>
  64. <view
  65. class="u-input__content__clear"
  66. v-if="isShowClear"
  67. @tap="onClear"
  68. >
  69. <u-icon
  70. name="close"
  71. size="11"
  72. color="#ffffff"
  73. customStyle="line-height: 12px"
  74. ></u-icon>
  75. </view>
  76. <view
  77. v-if="showPasswordToggle && (password || type === 'password')"
  78. class="u-input__content__eye"
  79. @tap.stop="handlePassword"
  80. >
  81. <u-icon :name="showPassword ? 'eye-fill' : 'eye-off'" :size="eyeIconSize" :color="eyeIconColor"></u-icon>
  82. </view>
  83. <view
  84. class="u-input__content__subfix-icon"
  85. v-if="suffixIcon || $slots.suffix"
  86. >
  87. <slot name="suffix">
  88. <u-icon
  89. :name="suffixIcon"
  90. size="18"
  91. :customStyle="suffixIconStyle"
  92. ></u-icon>
  93. </slot>
  94. </view>
  95. </view>
  96. </view>
  97. </template>
  98. <script>
  99. import props from "./props.js"
  100. import mixin from '../../libs/mixin/mixin'
  101. import mpMixin from '../../libs/mixin/mpMixin'
  102. /**
  103. * Input 输入框
  104. * @description 此组件为一个输入框,默认没有边框和样式,是专门为配合表单组件u-form而设计的,利用它可以快速实现表单验证,输入内容,下拉选择等功能。
  105. * @tutorial https://uviewui.com/components/input.html
  106. * @property {String | Number} value 输入的值
  107. * @property {String} type 输入框类型,见上方说明 ( 默认 'text' )
  108. * @property {Boolean} fixed 如果 textarea 是在一个 position:fixed 的区域,需要显示指定属性 fixed 为 true,兼容性:微信小程序、百度小程序、字节跳动小程序、QQ小程序 ( 默认 false )
  109. * @property {Boolean} disabled 是否禁用输入框 ( 默认 false )
  110. * @property {String} disabledColor 禁用状态时的背景色( 默认 '#f5f7fa' )
  111. * @property {Boolean} clearable 是否显示清除控件 ( 默认 false )
  112. * @property {Boolean} password 是否密码类型 ( 默认 false )
  113. * @property {Boolean} showPasswordToggle 是否显示密码切换图标( 默认 true )
  114. * @property {String | Number} maxlength 最大输入长度,设置为 -1 的时候不限制最大长度 ( 默认 -1 )
  115. * @property {String} placeholder 输入框为空时的占位符
  116. * @property {String} placeholderClass 指定placeholder的样式类,注意页面或组件的style中写了scoped时,需要在类名前写/deep/ ( 默认 'input-placeholder' )
  117. * @property {String | Object} placeholderStyle 指定placeholder的样式,字符串/对象形式,如"color: red;"
  118. * @property {String} confirmType 设置右下角按钮的文字,兼容性详见uni-app文档 ( 默认 'done' )
  119. * @property {Boolean} confirmHold 点击键盘右下角按钮时是否保持键盘不收起,H5无效 ( 默认 false )
  120. * @property {Boolean} holdKeyboard focus时,点击页面的时候不收起键盘,微信小程序有效 ( 默认 false )
  121. * @property {Boolean} focus 自动获取焦点,在 H5 平台能否聚焦以及软键盘是否跟随弹出,取决于当前浏览器本身的实现。nvue 页面不支持,需使用组件的 focus()、blur() 方法控制焦点 ( 默认 false )
  122. * @property {Boolean} autoBlur 键盘收起时,是否自动失去焦点,目前仅App3.0.0+有效 ( 默认 false )
  123. * @property {Boolean} disableDefaultPadding 是否去掉 iOS 下的默认内边距,仅微信小程序,且type=textarea时有效 ( 默认 false )
  124. * @property {Boolean} alwaysEmbed 强制 input 处于同层状态,默认 focus 时 input 会切到非同层状态 ( 默认 false )
  125. * @property {String | Number} cursor 指定focus时光标的位置( 默认 -1 )
  126. * @property {String} cursorColor 指定focus时光标颜色
  127. * @property {String | Number} cursorSpacing 输入框聚焦时底部与键盘的距离 ( 默认 30 )
  128. * @property {String | Number} selectionStart 光标起始位置,自动聚集时有效,需与selection-end搭配使用 ( 默认 -1 )
  129. * @property {String | Number} selectionEnd 光标结束位置,自动聚集时有效,需与selection-start搭配使用 ( 默认 -1 )
  130. * @property {Boolean} adjustPosition 键盘弹起时,是否自动上推页面 ( 默认 true )
  131. * @property {String} inputAlign 输入框内容对齐方式( 默认 'left' )
  132. * @property {String | Number} fontSize 输入框字体的大小 ( 默认 '15px' )
  133. * @property {String} color 输入框字体颜色 ( 默认 '#303133' )
  134. * @property {Function} formatter 内容式化函数
  135. * @property {String} prefixIcon 输入框前置图标
  136. * @property {String | Object} prefixIconStyle 前置图标样式,对象或字符串
  137. * @property {String} suffixIcon 输入框后置图标
  138. * @property {String | Object} suffixIconStyle 后置图标样式,对象或字符串
  139. * @property {String} border 边框类型,surround-四周边框,bottom-底部边框,none-无边框 ( 默认 'surround' )
  140. * @property {Boolean} readonly 是否只读,与disabled不同之处在于disabled会置灰组件,而readonly则不会 ( 默认 false )
  141. * @property {String} shape 输入框形状,circle-圆形,square-方形 ( 默认 'square' )
  142. * @property {Object} customStyle 定义需要用到的外部样式
  143. * @property {Boolean} ignoreCompositionEvent 是否忽略组件内对文本合成系统事件的处理。
  144. * @property {String} backgroundColor 背景颜色
  145. * @property {String} round 设置圆角值
  146. * @property {String} borderColor 边框颜色
  147. * @example <u-input v-model="value" :password="true" suffix-icon="lock-fill" />
  148. */
  149. export default {
  150. name: "u-input",
  151. mixins: [mpMixin, mixin, props],
  152. data() {
  153. return {
  154. // 输入框的值
  155. innerValue: "",
  156. // value是否第一次变化,在watch中,由于加入immediate属性,会在第一次触发,此时不应该认为value发生了变化
  157. firstChange: true,
  158. // value绑定值的变化是由内部还是外部引起的
  159. changeFromInner: false,
  160. // 过滤处理方法
  161. innerFormatter: value => value,
  162. // 是否显示密码
  163. showPassword: false
  164. };
  165. },
  166. watch: {
  167. //#ifdef VUE2
  168. value: {
  169. immediate: true,
  170. handler(newVal, oldVal) {
  171. this.init(newVal);
  172. },
  173. },
  174. // #endif
  175. // #ifdef VUE3
  176. modelValue: {
  177. immediate: true,
  178. handler(newVal, oldVal) {
  179. this.init(newVal);
  180. },
  181. }
  182. // #endif
  183. },
  184. computed: {
  185. // 实际传给input的password属性
  186. isPassword() {
  187. const isPasswordType = this.password || this.type === 'password';
  188. if (!isPasswordType) return false;
  189. return !this.showPassword;
  190. },
  191. // 是否显示清除控件
  192. isShowClear() {
  193. const { clearable, readonly, innerValue } = this;
  194. return !!clearable && !readonly && innerValue !== "";
  195. },
  196. // 组件的类名
  197. inputClass() {
  198. let classes = [],
  199. { border, disabled, shape } = this;
  200. border === "surround" && (classes = classes.concat(["u-border", "u-input--radius"]));
  201. border === "bottom" && (classes = classes.concat(["u-border-bottom"]));
  202. return classes.join(" ");
  203. },
  204. // 组件的样式
  205. wrapperStyle() {
  206. const style = {};
  207. if(this.backgroundColor){
  208. style.backgroundColor = this.backgroundColor;
  209. }
  210. //nvue 必须分开写才有效
  211. if(this.borderColor){
  212. style.borderLeftColor = this.borderColor;
  213. style.borderTopColor = this.borderColor;
  214. style.borderRightColor = this.borderColor;
  215. style.borderBottomColor = this.borderColor;
  216. }
  217. if(this.border !== "bottom"){
  218. style.borderRadius = uni.$u.addUnit(this.shape == "circle" ? "100px" : this.round)
  219. }
  220. // 禁用状态下,被背景色加上对应的样式
  221. if (this.disabled) {
  222. style.backgroundColor = this.disabledColor;
  223. }
  224. // 无边框时,去除内边距
  225. if (this.border === "none" && this.backgroundColor == '') {
  226. style.padding = "0";
  227. } else {
  228. // 由于uni-app的iOS开发者能力有限,导致需要分开写才有效
  229. style.paddingTop = "6px";
  230. style.paddingBottom = "6px";
  231. style.paddingLeft = "9px";
  232. style.paddingRight = "9px";
  233. }
  234. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle));
  235. },
  236. // 输入框的样式
  237. inputStyle() {
  238. const style = {
  239. color: this.color,
  240. fontSize: uni.$u.addUnit(this.fontSize),
  241. textAlign: this.inputAlign
  242. };
  243. return style;
  244. },
  245. },
  246. // #ifdef VUE3
  247. emits: ['update:modelValue', 'focus', 'blur', 'change', 'confirm', 'clear', 'keyboardheightchange'],
  248. // #endif
  249. methods: {
  250. init(newVal){
  251. this.innerValue = newVal;
  252. // 在H5中,外部value变化后,修改input中的值,不会触发@input事件,此时手动调用值变化方法
  253. if (this.firstChange === false && this.changeFromInner === false) {
  254. this.valueChange();
  255. }
  256. this.firstChange = false;
  257. // 重置changeFromInner的值为false,标识下一次引起默认为外部引起的
  258. this.changeFromInner = false;
  259. },
  260. // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
  261. setFormatter(e) {
  262. this.innerFormatter = e
  263. },
  264. // 当键盘输入时,触发input事件
  265. onInput(e) {
  266. let { value = "" } = e.detail || {};
  267. const formatter = this.formatter || this.innerFormatter;
  268. const formatValue = formatter(value)
  269. // 为了避免props的单向数据流特性,需要先将innerValue值设置为当前值,再在$nextTick中重新赋予设置后的值才有效
  270. this.innerValue = value
  271. this.$nextTick(() => {
  272. this.innerValue = formatValue;
  273. this.valueChange();
  274. })
  275. },
  276. // 输入框失去焦点时触发
  277. onBlur(event) {
  278. this.$emit("blur", event.detail.value);
  279. // 尝试调用u-form的验证方法
  280. uni.$u.formValidate(this, "blur");
  281. },
  282. // 输入框聚焦时触发
  283. onFocus(e) {
  284. this.$emit("focus", e);
  285. },
  286. // 点击完成按钮时触发
  287. onConfirm(event) {
  288. this.$emit("confirm", this.innerValue);
  289. },
  290. // 键盘高度发生变化的时候触发此事件
  291. // 兼容性:微信小程序2.7.0+、App 3.1.0+
  292. onkeyboardheightchange(e) {
  293. this.$emit("keyboardheightchange", e);
  294. },
  295. // 内容发生变化,进行处理
  296. valueChange() {
  297. const value = this.innerValue;
  298. this.$nextTick(() => {
  299. // #ifdef VUE2
  300. this.$emit("input", value);
  301. // #endif
  302. // #ifdef VUE3
  303. this.$emit("update:modelValue", value);
  304. // #endif
  305. // 标识value值的变化是由内部引起的
  306. this.changeFromInner = true;
  307. this.$emit("change", value);
  308. // 尝试调用u-form的验证方法
  309. uni.$u.formValidate(this, "change");
  310. });
  311. },
  312. // 点击清除控件
  313. onClear() {
  314. this.innerValue = "";
  315. this.$nextTick(() => {
  316. this.valueChange();
  317. this.$emit("clear");
  318. });
  319. },
  320. handlePassword() {
  321. this.showPassword = !this.showPassword;
  322. },
  323. /**
  324. * 在安卓nvue上,事件无法冒泡
  325. * 在某些时间,我们希望监听u-from-item的点击事件,此时会导致点击u-form-item内的u-input后
  326. * 无法触发u-form-item的点击事件,这里通过手动调用u-form-item的方法进行触发
  327. */
  328. clickHandler() {
  329. if (this.disabled || this.readonly) {
  330. uni.hideKeyboard();
  331. }
  332. // #ifdef APP-NVUE
  333. if (uni.$u.os() === "android") {
  334. const formItem = uni.$u.$parent.call(this, "u-form-item");
  335. if (formItem) {
  336. formItem.clickHandler();
  337. }
  338. }
  339. // #endif
  340. }
  341. },
  342. };
  343. </script>
  344. <style lang="scss" scoped>
  345. @import "../../libs/css/components.scss";
  346. .u-input {
  347. @include flex(row);
  348. align-items: center;
  349. justify-content: space-between;
  350. flex: 1;
  351. // #ifdef H5
  352. // 处理H5下,input输入框disabled状态下,无法触发click事件问题
  353. ::v-deep .uni-input-input:disabled {
  354. pointer-events: none;
  355. }
  356. // #endif
  357. &__content {
  358. flex: 1;
  359. @include flex(row);
  360. align-items: center;
  361. justify-content: space-between;
  362. &__field-wrapper {
  363. position: relative;
  364. @include flex(row);
  365. margin: 0;
  366. flex: 1;
  367. &__field {
  368. line-height: 26px;
  369. text-align: left;
  370. color: $u-main-color;
  371. height: 24px;
  372. font-size: 15px;
  373. flex: 1;
  374. }
  375. }
  376. &__clear {
  377. width: 20px;
  378. height: 20px;
  379. border-radius: 100px;
  380. background-color: #c6c7cb;
  381. @include flex(row);
  382. align-items: center;
  383. justify-content: center;
  384. transform: scale(0.82);
  385. margin-left: 4px;
  386. z-index: 10000;
  387. }
  388. &__eye {
  389. width: 20px;
  390. height: 20px;
  391. @include flex(row);
  392. align-items: center;
  393. justify-content: center;
  394. margin-left: 4px;
  395. z-index: 10000;
  396. }
  397. &__subfix-icon {
  398. margin-left: 4px;
  399. z-index: 10000;
  400. }
  401. &__prefix-icon {
  402. margin-right: 4px;
  403. z-index: 10000;
  404. }
  405. }
  406. }
  407. </style>