u-input.vue 14 KB

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