u-input.vue 14 KB

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