u-field.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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="[$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="valueCom"
  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="valueCom"
  26. :password="password || 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 && valueCom != '' && 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. emits: ["update:modelValue", "input", "focus", "blur", "confirm", "right-icon-click", "click"],
  91. props: {
  92. value: [Number, String],
  93. modelValue: [Number, String],
  94. icon: String,
  95. rightIcon: String,
  96. arrowDirection: {
  97. type: String,
  98. default: 'right'
  99. },
  100. required: Boolean,
  101. label: String,
  102. password: Boolean,
  103. clearable: {
  104. type: Boolean,
  105. default: true
  106. },
  107. // 左边标题的宽度单位rpx
  108. labelWidth: {
  109. type: [Number, String],
  110. default: 130
  111. },
  112. // 对齐方式,left|center|right
  113. labelAlign: {
  114. type: String,
  115. default: 'left'
  116. },
  117. inputAlign: {
  118. type: String,
  119. default: 'left'
  120. },
  121. iconColor: {
  122. type: String,
  123. default: '#606266'
  124. },
  125. autoHeight: {
  126. type: Boolean,
  127. default: true
  128. },
  129. errorMessage: {
  130. type: [String, Boolean],
  131. default: ''
  132. },
  133. placeholder: String,
  134. placeholderStyle: String,
  135. focus: Boolean,
  136. fixed: Boolean,
  137. type: {
  138. type: String,
  139. default: 'text'
  140. },
  141. disabled: {
  142. type: Boolean,
  143. default: false
  144. },
  145. maxlength: {
  146. type: [Number, String],
  147. default: 140
  148. },
  149. confirmType: {
  150. type: String,
  151. default: 'done'
  152. },
  153. // lable的位置,可选为 left-左边,top-上边
  154. labelPosition: {
  155. type: String,
  156. default: 'left'
  157. },
  158. // 输入框的自定义样式
  159. fieldStyle: {
  160. type: Object,
  161. default() {
  162. return {}
  163. }
  164. },
  165. // 清除按钮的大小
  166. clearSize: {
  167. type: [Number, String],
  168. default: 30
  169. },
  170. // lable左边的图标样式,对象形式
  171. iconStyle: {
  172. type: Object,
  173. default() {
  174. return {}
  175. }
  176. },
  177. // 是否显示上边框
  178. borderTop: {
  179. type: Boolean,
  180. default: false
  181. },
  182. // 是否显示下边框
  183. borderBottom: {
  184. type: Boolean,
  185. default: true
  186. },
  187. // 是否自动去除两端的空格
  188. trim: {
  189. type: Boolean,
  190. default: true
  191. }
  192. },
  193. data() {
  194. return {
  195. focused: false,
  196. itemIndex: 0,
  197. };
  198. },
  199. computed: {
  200. valueCom() {
  201. // #ifdef VUE2
  202. return this.value;
  203. // #endif
  204. // #ifdef VUE3
  205. return this.modelValue;
  206. // #endif
  207. },
  208. inputWrapStyle() {
  209. let style = {};
  210. style.textAlign = this.inputAlign;
  211. // 判断lable的位置,如果是left的话,让input左边两边有间隙
  212. if(this.labelPosition == 'left') {
  213. style.margin = `0 8rpx`;
  214. } else {
  215. // 如果lable是top的,input的左边就没必要有间隙了
  216. style.marginRight = `8rpx`;
  217. }
  218. return style;
  219. },
  220. rightIconStyle() {
  221. let style = {};
  222. let arrowDirectionObj = {
  223. "top": "-90deg",
  224. "bottom": "90deg",
  225. "left": "180deg",
  226. "right": "0deg",
  227. };
  228. let deg = arrowDirectionObj[this.arrowDirection] || "0deg";
  229. style.transform = 'rotate('+deg+')';
  230. return style;
  231. },
  232. labelStyle() {
  233. let style = {};
  234. if(this.labelAlign == 'left') style.justifyContent = 'flext-start';
  235. if(this.labelAlign == 'center') style.justifyContent = 'center';
  236. if(this.labelAlign == 'right') style.justifyContent = 'flext-end';
  237. return style;
  238. },
  239. // uni不支持在computed中写style.justifyContent = 'center'的形式,故用此方法
  240. justifyContent() {
  241. if(this.labelAlign == 'left') return 'flex-start';
  242. if(this.labelAlign == 'center') return 'center';
  243. if(this.labelAlign == 'right') return 'flex-end';
  244. },
  245. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  246. inputMaxlength() {
  247. return Number(this.maxlength)
  248. },
  249. // label的位置
  250. fieldInnerStyle() {
  251. let style = {};
  252. if(this.labelPosition == 'left') {
  253. style.flexDirection = 'row';
  254. } else {
  255. style.flexDirection = 'column';
  256. }
  257. return style;
  258. }
  259. },
  260. methods: {
  261. onInput(event) {
  262. let value = event.detail.value;
  263. // 判断是否去除空格
  264. if(this.trim) value = this.$u.trim(value);
  265. this.$emit('input', value);
  266. this.$emit("update:modelValue", value);
  267. },
  268. onFocus(event) {
  269. this.focused = true;
  270. this.$emit('focus', event);
  271. },
  272. onBlur(event) {
  273. // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
  274. // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
  275. setTimeout(() => {
  276. this.focused = false;
  277. }, 100)
  278. this.$emit('blur', event);
  279. },
  280. onConfirm(e) {
  281. this.$emit('confirm', e.detail.value);
  282. },
  283. onClear(event) {
  284. this.$emit('input', '');
  285. this.$emit("update:modelValue", '');
  286. },
  287. rightIconClick() {
  288. this.$emit('right-icon-click');
  289. this.$emit('click');
  290. },
  291. fieldClick() {
  292. this.$emit('click');
  293. }
  294. }
  295. };
  296. </script>
  297. <style lang="scss" scoped>
  298. @import "../../libs/css/style.components.scss";
  299. .u-field {
  300. font-size: 28rpx;
  301. padding: 20rpx 28rpx;
  302. text-align: left;
  303. position: relative;
  304. color: $u-main-color;
  305. }
  306. .u-field-inner {
  307. @include vue-flex;
  308. align-items: center;
  309. }
  310. .u-textarea-inner {
  311. align-items: flex-start;
  312. }
  313. .u-textarea-class {
  314. min-height: 96rpx;
  315. width: auto;
  316. font-size: 28rpx;
  317. }
  318. .fild-body {
  319. @include vue-flex;
  320. flex: 1;
  321. align-items: center;
  322. }
  323. .u-arror-right {
  324. margin-left: 8rpx;
  325. }
  326. .u-label-text {
  327. /* #ifndef APP-NVUE */
  328. display: inline-flex;
  329. /* #endif */
  330. }
  331. .u-label-left-gap {
  332. margin-left: 6rpx;
  333. }
  334. .u-label-postion-top {
  335. flex-direction: column;
  336. align-items: flex-start;
  337. }
  338. .u-label {
  339. width: 130rpx;
  340. flex: 1 1 130rpx;
  341. text-align: left;
  342. position: relative;
  343. @include vue-flex;
  344. align-items: center;
  345. }
  346. .u-required::before {
  347. content: '*';
  348. position: absolute;
  349. left: -16rpx;
  350. font-size: 14px;
  351. color: $u-type-error;
  352. height: 9px;
  353. line-height: 1;
  354. }
  355. .u-field__input-wrap {
  356. position: relative;
  357. overflow: hidden;
  358. font-size: 28rpx;
  359. height: 48rpx;
  360. flex: 1;
  361. width: auto;
  362. }
  363. .u-clear-icon {
  364. @include vue-flex;
  365. align-items: center;
  366. }
  367. .u-error-message {
  368. color: $u-type-error;
  369. font-size: 26rpx;
  370. text-align: left;
  371. }
  372. .placeholder-style {
  373. color: rgb(150, 151, 153);
  374. }
  375. .u-input-class {
  376. font-size: 28rpx;
  377. }
  378. .u-button-wrap {
  379. margin-left: 8rpx;
  380. }
  381. </style>