AI销管
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

385 righe
11 KiB

  1. <template>
  2. <view class="u-field" :class="{'u-border-top': borderTop, 'u-border-bottom': borderBottom }">
  3. <view class="u-field-inner" :class="[type == 'textarea' ? 'u-textarea-inner' : '', 'u-label-postion-' + labelPosition]">
  4. <view class="u-label" :class="[required ? 'u-required' : '']" :style="{
  5. justifyContent: justifyContent,
  6. flex: labelPosition == 'left' ? `0 0 ${labelWidth}rpx` : '1'
  7. }">
  8. <view class="u-icon-wrap" v-if="icon">
  9. <u-icon size="32" :custom-style="iconStyle" :name="icon" :color="iconColor" class="u-icon"></u-icon>
  10. </view>
  11. <slot name="icon"></slot>
  12. <text class="u-label-text" :class="[this.$slots.icon || icon ? 'u-label-left-gap' : '']">{{ label }}</text>
  13. </view>
  14. <view class="fild-body">
  15. <view class="u-flex-1 u-flex" :style="[inputWrapStyle]">
  16. <textarea v-if="type == 'textarea'" class="u-flex-1 u-textarea-class" :style="[fieldStyle]" :value="value"
  17. :placeholder="placeholder" :placeholderStyle="placeholderStyle" :disabled="disabled" :maxlength="inputMaxlength"
  18. :focus="focus" :autoHeight="autoHeight" :fixed="fixed" @input="onInput" @blur="onBlur" @focus="onFocus" @confirm="onConfirm"
  19. @tap="fieldClick" />
  20. <input
  21. v-else
  22. :style="[fieldStyle]"
  23. :type="type"
  24. class="u-flex-1 u-field__input-wrap"
  25. :value="value"
  26. :password="password || this.type === 'password'"
  27. :placeholder="placeholder"
  28. :placeholderStyle="placeholderStyle"
  29. :disabled="disabled"
  30. :maxlength="inputMaxlength"
  31. :focus="focus"
  32. :confirmType="confirmType"
  33. @focus="onFocus"
  34. @blur="onBlur"
  35. @input="onInput"
  36. @confirm="onConfirm"
  37. @tap="fieldClick"
  38. />
  39. </view>
  40. <u-icon :size="clearSize" v-if="clearable && value != '' && focused" name="close-circle-fill" color="#c0c4cc" class="u-clear-icon" @click="onClear"/>
  41. <view class="u-button-wrap"><slot name="right" /></view>
  42. <u-icon v-if="rightIcon" @click="rightIconClick" :name="rightIcon" color="#c0c4cc" :style="[rightIconStyle]" size="26" class="u-arror-right" />
  43. </view>
  44. </view>
  45. <view v-if="errorMessage !== false && errorMessage != ''" class="u-error-message" :style="{
  46. paddingLeft: labelWidth + 'rpx'
  47. }">{{ errorMessage }}</view>
  48. </view>
  49. </template>
  50. <script>
  51. /**
  52. * field 输入框
  53. * @description 借助此组件,可以实现表单的输入, 有"text"和"textarea"类型的,此外,借助uView的picker和actionSheet组件可以快速实现上拉菜单,时间,地区选择等, 为表单解决方案的利器。
  54. * @tutorial https://www.uviewui.com/components/field.html
  55. * @property {String} type 输入框的类型(默认text)
  56. * @property {String} icon label左边的图标,限uView的图标名称
  57. * @property {Object} icon-style 左边图标的样式,对象形式
  58. * @property {Boolean} right-icon 输入框右边的图标名称,限uView的图标名称(默认false)
  59. * @property {Boolean} required 是否必填,左边您显示红色"*"号(默认false)
  60. * @property {String} label 输入框左边的文字提示
  61. * @property {Boolean} password 是否密码输入方式(用点替换文字),type为text时有效(默认false)
  62. * @property {Boolean} clearable 是否显示右侧清空内容的图标控件(输入框有内容,且获得焦点时才显示),点击可清空输入框内容(默认true)
  63. * @property {Number String} label-width label的宽度,单位rpx(默认130)
  64. * @property {String} label-align label的文字对齐方式(默认left)
  65. * @property {Object} field-style 自定义输入框的样式,对象形式
  66. * @property {Number | String} clear-size 清除图标的大小,单位rpx(默认30)
  67. * @property {String} input-align 输入框内容对齐方式(默认left)
  68. * @property {Boolean} border-bottom 是否显示field的下边框(默认true)
  69. * @property {Boolean} border-top 是否显示field的上边框(默认false)
  70. * @property {String} icon-color 左边通过icon配置的图标的颜色(默认#606266)
  71. * @property {Boolean} auto-height 是否自动增高输入区域,type为textarea时有效(默认true)
  72. * @property {String Boolean} error-message 显示的错误提示内容,如果为空字符串或者false,则不显示错误信息
  73. * @property {String} placeholder 输入框的提示文字
  74. * @property {String} placeholder-style placeholder的样式(内联样式,字符串),如"color: #ddd"
  75. * @property {Boolean} focus 是否自动获得焦点(默认false)
  76. * @property {Boolean} fixed 如果type为textarea,且在一个"position:fixed"的区域,需要指明为true(默认false)
  77. * @property {Boolean} disabled 是否不可输入(默认false)
  78. * @property {Number String} maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
  79. * @property {String} confirm-type 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
  80. * @event {Function} input 输入框内容发生变化时触发
  81. * @event {Function} focus 输入框获得焦点时触发
  82. * @event {Function} blur 输入框失去焦点时触发
  83. * @event {Function} confirm 点击完成按钮时触发
  84. * @event {Function} right-icon-click 通过right-icon生成的图标被点击时触发
  85. * @event {Function} click 输入框被点击或者通过right-icon生成的图标被点击时触发,这样设计是考虑到传递右边的图标,一般都为需要弹出"picker"等操作时的场景,点击倒三角图标,理应发出此事件,见上方说明
  86. * @example <u-field v-model="mobile" label="手机号" required :error-message="errorMessage"></u-field>
  87. */
  88. export default {
  89. name:"u-field",
  90. props: {
  91. icon: String,
  92. rightIcon: String,
  93. // arrowDirection: {
  94. // type: String,
  95. // default: 'right'
  96. // },
  97. required: Boolean,
  98. label: String,
  99. password: Boolean,
  100. clearable: {
  101. type: Boolean,
  102. default: true
  103. },
  104. // 左边标题的宽度单位rpx
  105. labelWidth: {
  106. type: [Number, String],
  107. default: 130
  108. },
  109. // 对齐方式,left|center|right
  110. labelAlign: {
  111. type: String,
  112. default: 'left'
  113. },
  114. inputAlign: {
  115. type: String,
  116. default: 'left'
  117. },
  118. iconColor: {
  119. type: String,
  120. default: '#606266'
  121. },
  122. autoHeight: {
  123. type: Boolean,
  124. default: true
  125. },
  126. errorMessage: {
  127. type: [String, Boolean],
  128. default: ''
  129. },
  130. placeholder: String,
  131. placeholderStyle: String,
  132. focus: Boolean,
  133. fixed: Boolean,
  134. value: [Number, String],
  135. type: {
  136. type: String,
  137. default: 'text'
  138. },
  139. disabled: {
  140. type: Boolean,
  141. default: false
  142. },
  143. maxlength: {
  144. type: [Number, String],
  145. default: 140
  146. },
  147. confirmType: {
  148. type: String,
  149. default: 'done'
  150. },
  151. // lable的位置,可选为 left-左边,top-上边
  152. labelPosition: {
  153. type: String,
  154. default: 'left'
  155. },
  156. // 输入框的自定义样式
  157. fieldStyle: {
  158. type: Object,
  159. default() {
  160. return {}
  161. }
  162. },
  163. // 清除按钮的大小
  164. clearSize: {
  165. type: [Number, String],
  166. default: 30
  167. },
  168. // lable左边的图标样式,对象形式
  169. iconStyle: {
  170. type: Object,
  171. default() {
  172. return {}
  173. }
  174. },
  175. // 是否显示上边框
  176. borderTop: {
  177. type: Boolean,
  178. default: false
  179. },
  180. // 是否显示下边框
  181. borderBottom: {
  182. type: Boolean,
  183. default: true
  184. },
  185. // 是否自动去除两端的空格
  186. trim: {
  187. type: Boolean,
  188. default: true
  189. }
  190. },
  191. data() {
  192. return {
  193. focused: false,
  194. itemIndex: 0,
  195. };
  196. },
  197. computed: {
  198. inputWrapStyle() {
  199. let style = {};
  200. style.textAlign = this.inputAlign;
  201. // 判断lable的位置,如果是left的话,让input左边两边有间隙
  202. if(this.labelPosition == 'left') {
  203. style.margin = `0 8rpx`;
  204. } else {
  205. // 如果lable是top的,input的左边就没必要有间隙了
  206. style.marginRight = `8rpx`;
  207. }
  208. return style;
  209. },
  210. rightIconStyle() {
  211. let style = {};
  212. if (this.arrowDirection == 'top') style.transform = 'roate(-90deg)';
  213. if (this.arrowDirection == 'bottom') style.transform = 'roate(90deg)';
  214. else style.transform = 'roate(0deg)';
  215. return style;
  216. },
  217. labelStyle() {
  218. let style = {};
  219. if(this.labelAlign == 'left') style.justifyContent = 'flext-start';
  220. if(this.labelAlign == 'center') style.justifyContent = 'center';
  221. if(this.labelAlign == 'right') style.justifyContent = 'flext-end';
  222. return style;
  223. },
  224. // uni不支持在computed中写style.justifyContent = 'center'的形式,故用此方法
  225. justifyContent() {
  226. if(this.labelAlign == 'left') return 'flex-start';
  227. if(this.labelAlign == 'center') return 'center';
  228. if(this.labelAlign == 'right') return 'flex-end';
  229. },
  230. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  231. inputMaxlength() {
  232. return Number(this.maxlength)
  233. },
  234. // label的位置
  235. fieldInnerStyle() {
  236. let style = {};
  237. if(this.labelPosition == 'left') {
  238. style.flexDirection = 'row';
  239. } else {
  240. style.flexDirection = 'column';
  241. }
  242. return style;
  243. }
  244. },
  245. methods: {
  246. onInput(event) {
  247. let value = event.detail.value;
  248. // 判断是否去除空格
  249. if(this.trim) value = this.$u.trim(value);
  250. this.$emit('input', value);
  251. },
  252. onFocus(event) {
  253. this.focused = true;
  254. this.$emit('focus', event);
  255. },
  256. onBlur(event) {
  257. // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
  258. // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
  259. setTimeout(() => {
  260. this.focused = false;
  261. }, 100)
  262. this.$emit('blur', event);
  263. },
  264. onConfirm(e) {
  265. this.$emit('confirm', e.detail.value);
  266. },
  267. onClear(event) {
  268. this.$emit('input', '');
  269. },
  270. rightIconClick() {
  271. this.$emit('right-icon-click');
  272. this.$emit('click');
  273. },
  274. fieldClick() {
  275. this.$emit('click');
  276. }
  277. }
  278. };
  279. </script>
  280. <style lang="scss" scoped>
  281. @import "../../libs/css/style.components.scss";
  282. .u-field {
  283. font-size: 28rpx;
  284. padding: 20rpx 28rpx;
  285. text-align: left;
  286. position: relative;
  287. color: $u-main-color;
  288. }
  289. .u-field-inner {
  290. @include vue-flex;
  291. align-items: center;
  292. }
  293. .u-textarea-inner {
  294. align-items: flex-start;
  295. }
  296. .u-textarea-class {
  297. min-height: 96rpx;
  298. width: auto;
  299. font-size: 28rpx;
  300. }
  301. .fild-body {
  302. @include vue-flex;
  303. flex: 1;
  304. align-items: center;
  305. }
  306. .u-arror-right {
  307. margin-left: 8rpx;
  308. }
  309. .u-label-text {
  310. /* #ifndef APP-NVUE */
  311. display: inline-flex;
  312. /* #endif */
  313. }
  314. .u-label-left-gap {
  315. margin-left: 6rpx;
  316. }
  317. .u-label-postion-top {
  318. flex-direction: column;
  319. align-items: flex-start;
  320. }
  321. .u-label {
  322. width: 130rpx;
  323. flex: 1 1 130rpx;
  324. text-align: left;
  325. position: relative;
  326. @include vue-flex;
  327. align-items: center;
  328. }
  329. .u-required::before {
  330. content: '*';
  331. position: absolute;
  332. left: -16rpx;
  333. font-size: 14px;
  334. color: $u-type-error;
  335. height: 9px;
  336. line-height: 1;
  337. }
  338. .u-field__input-wrap {
  339. position: relative;
  340. overflow: hidden;
  341. font-size: 28rpx;
  342. height: 48rpx;
  343. flex: 1;
  344. width: auto;
  345. }
  346. .u-clear-icon {
  347. @include vue-flex;
  348. align-items: center;
  349. }
  350. .u-error-message {
  351. color: $u-type-error;
  352. font-size: 26rpx;
  353. text-align: left;
  354. }
  355. .placeholder-style {
  356. color: rgb(150, 151, 153);
  357. }
  358. .u-input-class {
  359. font-size: 28rpx;
  360. }
  361. .u-button-wrap {
  362. margin-left: 8rpx;
  363. }
  364. </style>