综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uv-input.vue 13KB

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