u-input.vue 13 KB

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