康巴易测肤/伤疤uniapp小程序类
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

370 行
12KB

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