多租户商城-商户小程序端
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.

362 lines
9.9 KiB

2 years ago
  1. <template>
  2. <view
  3. class="u-input"
  4. :class="{
  5. 'u-input--border': border,
  6. 'u-input--error': validateState
  7. }"
  8. :style="{
  9. padding: `0 ${border ? 20 : 0}rpx`,
  10. borderColor: borderColor,
  11. textAlign: inputAlign
  12. }"
  13. @tap.stop="inputClick"
  14. >
  15. <textarea
  16. v-if="type == 'textarea'"
  17. class="u-input__input u-input__textarea"
  18. :style="[getStyle]"
  19. :value="defaultValue"
  20. :placeholder="placeholder"
  21. :placeholderStyle="placeholderStyle"
  22. :disabled="disabled"
  23. :maxlength="inputMaxlength"
  24. :fixed="fixed"
  25. :focus="focus"
  26. :autoHeight="autoHeight"
  27. :selection-end="uSelectionEnd"
  28. :selection-start="uSelectionStart"
  29. :cursor-spacing="getCursorSpacing"
  30. @input="handleInput"
  31. @blur="handleBlur"
  32. @focus="onFocus"
  33. @confirm="onConfirm"
  34. />
  35. <input
  36. v-else
  37. class="u-input__input"
  38. :type="type == 'password' ? 'text' : type"
  39. :style="[getStyle]"
  40. :value="defaultValue"
  41. :password="type == 'password' && !showPassword"
  42. :placeholder="placeholder"
  43. :placeholderStyle="placeholderStyle"
  44. :disabled="disabled || type === 'select'"
  45. :maxlength="inputMaxlength"
  46. :focus="focus"
  47. :confirmType="confirmType"
  48. :cursor-spacing="getCursorSpacing"
  49. :selection-end="uSelectionEnd"
  50. :selection-start="uSelectionStart"
  51. @focus="onFocus"
  52. @blur="handleBlur"
  53. @input="handleInput"
  54. @confirm="onConfirm"
  55. />
  56. <view class="u-input__right-icon u-flex">
  57. <view class="u-input__right-icon__clear u-input__right-icon__item" v-if="clearable && value != '' && focused">
  58. <u-icon size="32" name="close-circle-fill" color="#c0c4cc" @touchstart="onClear"/>
  59. </view>
  60. <view class="u-input__right-icon__clear u-input__right-icon__item" v-if="passwordIcon && type == 'password'">
  61. <u-icon size="32" :name="!showPassword ? 'eye' : 'eye-fill'" color="#c0c4cc" @click="showPassword = !showPassword"/>
  62. </view>
  63. <view class="u-input__right-icon--select u-input__right-icon__item" v-if="type == 'select'" :class="{
  64. 'u-input__right-icon--select--reverse': selectOpen
  65. }">
  66. <u-icon name="arrow-down-fill" size="26" color="#c0c4cc"></u-icon>
  67. </view>
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. import Emitter from '../../libs/util/emitter.js';
  73. /**
  74. * input 输入框
  75. * @description 此组件为一个输入框默认没有边框和样式是专门为配合表单组件u-form而设计的利用它可以快速实现表单验证输入内容下拉选择等功能
  76. * @tutorial http://uviewui.com/components/input.html
  77. * @property {String} type 模式选择见官网说明
  78. * @property {Boolean} clearable 是否显示右侧的清除图标(默认true)
  79. * @property {} v-model 用于双向绑定输入框的值
  80. * @property {String} input-align 输入框文字的对齐方式(默认left)
  81. * @property {String} placeholder placeholder显示值(默认 '请输入内容')
  82. * @property {Boolean} disabled 是否禁用输入框(默认false)
  83. * @property {String Number} maxlength 输入框的最大可输入长度(默认140)
  84. * @property {String Number} selection-start 光标起始位置自动聚焦时有效需与selection-end搭配使用默认-1
  85. * @property {String Number} maxlength 光标结束位置自动聚焦时有效需与selection-start搭配使用默认-1
  86. * @property {String Number} cursor-spacing 指定光标与键盘的距离单位px(默认0)
  87. * @property {String} placeholderStyle placeholder的样式字符串形式"color: red;"(默认 "color: #c0c4cc;")
  88. * @property {String} confirm-type 设置键盘右下角按钮的文字仅在type为text时生效(默认done)
  89. * @property {Object} custom-style 自定义输入框的样式对象形式
  90. * @property {Boolean} focus 是否自动获得焦点(默认false)
  91. * @property {Boolean} fixed 如果type为textarea且在一个"position:fixed"的区域需要指明为true(默认false)
  92. * @property {Boolean} password-icon type为password时是否显示右侧的密码查看图标(默认true)
  93. * @property {Boolean} border 是否显示边框(默认false)
  94. * @property {String} border-color 输入框的边框颜色(默认#dcdfe6)
  95. * @property {Boolean} auto-height 是否自动增高输入区域type为textarea时有效(默认true)
  96. * @property {String Number} height 高度单位rpx(text类型时为70textarea时为100)
  97. * @example <u-input v-model="value" :type="type" :border="border" />
  98. */
  99. export default {
  100. name: 'u-input',
  101. mixins: [Emitter],
  102. props: {
  103. value: {
  104. type: [String, Number],
  105. default: ''
  106. },
  107. // 输入框的类型,textarea,text,number
  108. type: {
  109. type: String,
  110. default: 'text'
  111. },
  112. inputAlign: {
  113. type: String,
  114. default: 'left'
  115. },
  116. placeholder: {
  117. type: String,
  118. default: '请输入内容'
  119. },
  120. disabled: {
  121. type: Boolean,
  122. default: false
  123. },
  124. maxlength: {
  125. type: [Number, String],
  126. default: 140
  127. },
  128. placeholderStyle: {
  129. type: String,
  130. default: 'color: #c0c4cc;'
  131. },
  132. confirmType: {
  133. type: String,
  134. default: 'done'
  135. },
  136. // 输入框的自定义样式
  137. customStyle: {
  138. type: Object,
  139. default() {
  140. return {};
  141. }
  142. },
  143. // 如果 textarea 是在一个 position:fixed 的区域,需要显示指定属性 fixed 为 true
  144. fixed: {
  145. type: Boolean,
  146. default: false
  147. },
  148. // 是否自动获得焦点
  149. focus: {
  150. type: Boolean,
  151. default: false
  152. },
  153. // 密码类型时,是否显示右侧的密码图标
  154. passwordIcon: {
  155. type: Boolean,
  156. default: true
  157. },
  158. // input|textarea是否显示边框
  159. border: {
  160. type: Boolean,
  161. default: false
  162. },
  163. // 输入框的边框颜色
  164. borderColor: {
  165. type: String,
  166. default: '#dcdfe6'
  167. },
  168. autoHeight: {
  169. type: Boolean,
  170. default: true
  171. },
  172. // type=select时,旋转右侧的图标,标识当前处于打开还是关闭select的状态
  173. // open-打开,close-关闭
  174. selectOpen: {
  175. type: Boolean,
  176. default: false
  177. },
  178. // 高度,单位rpx
  179. height: {
  180. type: [Number, String],
  181. default: ''
  182. },
  183. // 是否可清空
  184. clearable: {
  185. type: Boolean,
  186. default: true
  187. },
  188. // 指定光标与键盘的距离,单位 px
  189. cursorSpacing: {
  190. type: [Number, String],
  191. default: 0
  192. },
  193. // 光标起始位置,自动聚焦时有效,需与selection-end搭配使用
  194. selectionStart: {
  195. type: [Number, String],
  196. default: -1
  197. },
  198. // 光标结束位置,自动聚焦时有效,需与selection-start搭配使用
  199. selectionEnd: {
  200. type: [Number, String],
  201. default: -1
  202. },
  203. // 是否自动去除两端的空格
  204. trim: {
  205. type: Boolean,
  206. default: true
  207. }
  208. },
  209. data() {
  210. return {
  211. defaultValue: this.value,
  212. inputHeight: 70, // input的高度
  213. textareaHeight: 100, // textarea的高度
  214. validateState: false, // 当前input的验证状态,用于错误时,边框是否改为红色
  215. focused: false, // 当前是否处于获得焦点的状态
  216. showPassword: false, // 是否预览密码
  217. };
  218. },
  219. watch: {
  220. value(nVal, oVal) {
  221. this.defaultValue = nVal;
  222. // 当值发生变化,且为select类型时(此时input被设置为disabled,不会触发@input事件),模拟触发@input事件
  223. if(nVal != oVal && this.type == 'select') this.handleInput({
  224. detail: {
  225. value: nVal
  226. }
  227. })
  228. },
  229. },
  230. computed: {
  231. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  232. inputMaxlength() {
  233. return Number(this.maxlength);
  234. },
  235. getStyle() {
  236. let style = {};
  237. // 如果没有自定义高度,就根据type为input还是textare来分配一个默认的高度
  238. style.minHeight = this.height ? this.height + 'rpx' : this.type == 'textarea' ?
  239. this.textareaHeight + 'rpx' : this.inputHeight + 'rpx';
  240. style = Object.assign(style, this.customStyle);
  241. return style;
  242. },
  243. //
  244. getCursorSpacing() {
  245. return Number(this.cursorSpacing);
  246. },
  247. // 光标起始位置
  248. uSelectionStart() {
  249. return String(this.selectionStart);
  250. },
  251. // 光标结束位置
  252. uSelectionEnd() {
  253. return String(this.selectionEnd);
  254. }
  255. },
  256. created() {
  257. // 监听u-form-item发出的错误事件,将输入框边框变红色
  258. this.$on('on-form-item-error', this.onFormItemError);
  259. },
  260. methods: {
  261. /**
  262. * change 事件
  263. * @param event
  264. */
  265. handleInput(event) {
  266. let value = event.detail.value;
  267. // 判断是否去除空格
  268. if(this.trim) value = this.$u.trim(value);
  269. // 当前model 赋值
  270. this.defaultValue = value;
  271. // vue 原生的方法 return 出去
  272. this.$emit('input', value);
  273. // 过一个生命周期再发送事件给u-form-item,否则this.$emit('input')更新了父组件的值,但是微信小程序上
  274. // 尚未更新到u-form-item,导致获取的值为空,从而校验混论
  275. this.$nextTick(() => {
  276. // 将当前的值发送到 u-form-item 进行校验
  277. this.dispatch('u-form-item', 'on-form-change', value);
  278. });
  279. },
  280. /**
  281. * blur 事件
  282. * @param event
  283. */
  284. handleBlur(event) {
  285. this.focused = false;
  286. // vue 原生的方法 return 出去
  287. this.$emit('blur', event.detail.value);
  288. this.$nextTick(() => {
  289. // 将当前的值发送到 u-form-item 进行校验
  290. this.dispatch('u-form-item', 'on-form-blur', event.detail.value);
  291. });
  292. },
  293. onFormItemError(status) {
  294. this.validateState = status;
  295. },
  296. onFocus(event) {
  297. this.focused = true;
  298. this.$emit('focus');
  299. },
  300. onConfirm(e) {
  301. this.$emit('confirm', e.detail.value);
  302. },
  303. onClear(event) {
  304. this.$emit('input', '');
  305. },
  306. inputClick() {
  307. this.$emit('click');
  308. }
  309. }
  310. };
  311. </script>
  312. <style lang="scss" scoped>
  313. .u-input {
  314. position: relative;
  315. flex: 1;
  316. display: flex;
  317. &__input {
  318. //height: $u-form-item-height;
  319. font-size: 28rpx;
  320. color: $u-main-color;
  321. flex: 1;
  322. }
  323. &__textarea {
  324. width: auto;
  325. font-size: 28rpx;
  326. color: $u-main-color;
  327. padding: 10rpx 0;
  328. line-height: normal;
  329. flex: 1;
  330. }
  331. &--border {
  332. border-radius: 6rpx;
  333. border-radius: 4px;
  334. border: 1px solid $u-form-item-border-color;
  335. }
  336. &--error {
  337. border-color: $u-type-error!important;
  338. }
  339. &__right-icon {
  340. &__item {
  341. margin-left: 10rpx;
  342. }
  343. &--select {
  344. transition: transform .4s;
  345. &--reverse {
  346. transform: rotate(-180deg);
  347. }
  348. }
  349. }
  350. }
  351. </style>