u-field.vue 12 KB

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