25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

u-field.vue 10 KiB

2 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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" @touchstart="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. data() {
  187. return {
  188. focused: false,
  189. itemIndex: 0,
  190. };
  191. },
  192. computed: {
  193. inputWrapStyle() {
  194. let style = {};
  195. style.textAlign = this.inputAlign;
  196. // 判断lable的位置,如果是left的话,让input左边两边有间隙
  197. if(this.labelPosition == 'left') {
  198. style.margin = `0 8rpx`;
  199. } else {
  200. // 如果lable是top的,input的左边就没必要有间隙了
  201. style.marginRight = `8rpx`;
  202. }
  203. return style;
  204. },
  205. rightIconStyle() {
  206. let style = {};
  207. if (this.arrowDirection == 'top') style.transform = 'roate(-90deg)';
  208. if (this.arrowDirection == 'bottom') style.transform = 'roate(90deg)';
  209. else style.transform = 'roate(0deg)';
  210. return style;
  211. },
  212. labelStyle() {
  213. let style = {};
  214. if(this.labelAlign == 'left') style.justifyContent = 'flext-start';
  215. if(this.labelAlign == 'center') style.justifyContent = 'center';
  216. if(this.labelAlign == 'right') style.justifyContent = 'flext-end';
  217. return style;
  218. },
  219. // uni不支持在computed中写style.justifyContent = 'center'的形式,故用此方法
  220. justifyContent() {
  221. if(this.labelAlign == 'left') return 'flex-start';
  222. if(this.labelAlign == 'center') return 'center';
  223. if(this.labelAlign == 'right') return 'flex-end';
  224. },
  225. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  226. inputMaxlength() {
  227. return Number(this.maxlength)
  228. },
  229. // label的位置
  230. fieldInnerStyle() {
  231. let style = {};
  232. if(this.labelPosition == 'left') {
  233. style.flexDirection = 'row';
  234. } else {
  235. style.flexDirection = 'column';
  236. }
  237. return style;
  238. }
  239. },
  240. methods: {
  241. onInput(event) {
  242. this.$emit('input', event.target.value);
  243. },
  244. onFocus(event) {
  245. this.focused = true;
  246. this.$emit('focus', event);
  247. },
  248. onBlur(event) {
  249. this.focused = false;
  250. this.$emit('blur', event);
  251. },
  252. onConfirm(e) {
  253. this.$emit('confirm', e.detail.value);
  254. },
  255. onClear(event) {
  256. this.$emit('input', '');
  257. },
  258. rightIconClick() {
  259. this.$emit('right-icon-click');
  260. this.$emit('click');
  261. },
  262. fieldClick() {
  263. this.$emit('click');
  264. }
  265. }
  266. };
  267. </script>
  268. <style lang="scss" scoped>
  269. @import "../../libs/css/style.components.scss";
  270. .u-field {
  271. font-size: 28rpx;
  272. padding: 20rpx 28rpx;
  273. text-align: left;
  274. position: relative;
  275. color: $u-main-color;
  276. }
  277. .u-field-inner {
  278. display: flex;
  279. align-items: center;
  280. }
  281. .u-textarea-inner {
  282. align-items: flex-start;
  283. }
  284. .u-textarea-class {
  285. min-height: 96rpx;
  286. width: auto;
  287. font-size: 28rpx;
  288. }
  289. .fild-body {
  290. display: flex;
  291. flex: 1;
  292. align-items: center;
  293. }
  294. .u-arror-right {
  295. margin-left: 8rpx;
  296. }
  297. .u-label-text {
  298. display: inline-block;
  299. }
  300. .u-label-left-gap {
  301. margin-left: 6rpx;
  302. }
  303. .u-label-postion-top {
  304. flex-direction: column;
  305. align-items: flex-start;
  306. }
  307. .u-label {
  308. width: 130rpx;
  309. flex: 1 1 130rpx;
  310. text-align: left;
  311. position: relative;
  312. display: flex;
  313. align-items: center;
  314. }
  315. .u-required::before {
  316. content: '*';
  317. position: absolute;
  318. left: -16rpx;
  319. font-size: 14px;
  320. color: $u-type-error;
  321. height: 9px;
  322. line-height: 1;
  323. }
  324. .u-field__input-wrap {
  325. position: relative;
  326. overflow: hidden;
  327. font-size: 28rpx;
  328. height: 48rpx;
  329. flex: 1;
  330. width: auto;
  331. }
  332. .u-clear-icon {
  333. display: flex;
  334. align-items: center;
  335. }
  336. .u-error-message {
  337. color: $u-type-error;
  338. font-size: 26rpx;
  339. text-align: left;
  340. }
  341. .placeholder-style {
  342. color: rgb(150, 151, 153);
  343. }
  344. .u-input-class {
  345. font-size: 28rpx;
  346. }
  347. .u-button-wrap {
  348. margin-left: 8rpx;
  349. }
  350. </style>