u-field.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <template>
  2. <view class="u-field" :class="{'u-field-border': itemIndex > 0 }">
  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" :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 || 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 {Boolean} right-icon 输入框右边的图标名称,限uView的图标名称(默认false)
  58. * @property {Boolean} required 是否必填,左边您显示红色"*"号(默认false)
  59. * @property {String} label 输入框左边的文字提示
  60. * @property {Boolean} password 是否密码输入方式(用点替换文字),type为text时有效(默认false)
  61. * @property {Boolean} clearable 是否显示右侧清空内容的图标控件(输入框有内容,且获得焦点时才显示),点击可清空输入框内容(默认true)
  62. * @property {Number String} label-width label的宽度,单位rpx(默认130)
  63. * @property {String} label-align label的文字对齐方式(默认left)
  64. * @property {Object} field-style 自定义输入框的样式,对象形式
  65. * @property {Number | String} clear-size 清除图标的大小,单位rpx(默认30)
  66. * @property {String} input-align 输入框内容对齐方式(默认left)
  67. * @property {String} icon-color 左边通过icon配置的图标的颜色(默认#606266)
  68. * @property {Boolean} auto-height 是否自动增高输入区域,type为textarea时有效(默认true)
  69. * @property {String Boolean} error-message 显示的错误提示内容,如果为空字符串或者false,则不显示错误信息
  70. * @property {String} placeholder 输入框的提示文字
  71. * @property {String} placeholder-style placeholder的样式(内联样式,字符串),如"color: #ddd"
  72. * @property {Boolean} focus 是否自动获得焦点(默认false)
  73. * @property {Boolean} fixed 如果type为textarea,且在一个"position:fixed"的区域,需要指明为true(默认false)
  74. * @property {Boolean} disabled 是否不可输入(默认false)
  75. * @property {Number String} maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
  76. * @property {String} confirm-type 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
  77. * @event {Function} input 输入框内容发生变化时触发
  78. * @event {Function} focus 输入框获得焦点时触发
  79. * @event {Function} blur 输入框失去焦点时触发
  80. * @event {Function} confirm 点击完成按钮时触发
  81. * @event {Function} right-icon-click 通过right-icon生成的图标被点击时触发
  82. * @event {Function} click 输入框被点击或者通过right-icon生成的图标被点击时触发,这样设计是考虑到传递右边的图标,一般都为需要弹出"picker"等操作时的场景,点击倒三角图标,理应发出此事件,见上方说明
  83. * @example <u-field v-model="mobile" label="手机号" required :error-message="errorMessage"></u-field>
  84. */
  85. export default {
  86. name:"u-field",
  87. props: {
  88. icon: String,
  89. rightIcon: String,
  90. // arrowDirection: {
  91. // type: String,
  92. // default: 'right'
  93. // },
  94. required: Boolean,
  95. label: String,
  96. password: Boolean,
  97. clearable: {
  98. type: Boolean,
  99. default: true
  100. },
  101. // 左边标题的宽度单位rpx
  102. labelWidth: {
  103. type: [Number, String],
  104. default: 130
  105. },
  106. // 对齐方式,left|center|right
  107. labelAlign: {
  108. type: String,
  109. default: 'left'
  110. },
  111. inputAlign: {
  112. type: String,
  113. default: 'left'
  114. },
  115. iconColor: {
  116. type: String,
  117. default: '#606266'
  118. },
  119. autoHeight: {
  120. type: Boolean,
  121. default: true
  122. },
  123. errorMessage: {
  124. type: [String, Boolean],
  125. default: ''
  126. },
  127. placeholder: String,
  128. placeholderStyle: String,
  129. focus: Boolean,
  130. fixed: Boolean,
  131. value: [Number, String],
  132. type: {
  133. type: String,
  134. default: 'text'
  135. },
  136. disabled: {
  137. type: Boolean,
  138. default: false
  139. },
  140. maxlength: {
  141. type: [Number, String],
  142. default: 140
  143. },
  144. confirmType: {
  145. type: String,
  146. default: 'done'
  147. },
  148. // lable的位置,可选为 left-左边,top-上边
  149. labelPosition: {
  150. type: String,
  151. default: 'left'
  152. },
  153. // 输入框的自定义样式
  154. fieldStyle: {
  155. type: Object,
  156. default() {
  157. return {}
  158. }
  159. },
  160. // 清除按钮的大小
  161. clearSize: {
  162. type: [Number, String],
  163. default: 30
  164. }
  165. },
  166. inject: ['uCellGroup'],
  167. data() {
  168. return {
  169. focused: false,
  170. itemIndex: 0,
  171. };
  172. },
  173. created() {
  174. if(this.uCellGroup) {
  175. this.itemIndex = this.uCellGroup.index++;
  176. }
  177. },
  178. computed: {
  179. inputWrapStyle() {
  180. let style = {};
  181. style.textAlign = this.inputAlign;
  182. // 判断lable的位置,如果是left的话,让input左边两边有间隙
  183. if(this.labelPosition == 'left') {
  184. style.margin = `0 8rpx`;
  185. } else {
  186. // 如果lable是top的,input的左边就没必要有间隙了
  187. style.marginRight = `8rpx`;
  188. }
  189. return style;
  190. },
  191. rightIconStyle() {
  192. let style = {};
  193. if (this.arrowDirection == 'top') style.transform = 'roate(-90deg)';
  194. if (this.arrowDirection == 'bottom') style.transform = 'roate(90deg)';
  195. else style.transform = 'roate(0deg)';
  196. return style;
  197. },
  198. labelStyle() {
  199. let style = {};
  200. if(this.labelAlign == 'left') style.justifyContent = 'flext-start';
  201. if(this.labelAlign == 'center') style.justifyContent = 'center';
  202. if(this.labelAlign == 'right') style.justifyContent = 'flext-end';
  203. return style;
  204. },
  205. // uni不支持在computed中写style.justifyContent = 'center'的形式,故用此方法
  206. justifyContent() {
  207. if(this.labelAlign == 'left') return 'flex-start';
  208. if(this.labelAlign == 'center') return 'center';
  209. if(this.labelAlign == 'right') return 'flex-end';
  210. },
  211. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  212. inputMaxlength() {
  213. return Number(this.maxlength)
  214. },
  215. // label的位置
  216. fieldInnerStyle() {
  217. let style = {};
  218. if(this.labelPosition == 'left') {
  219. style.flexDirection = 'row';
  220. } else {
  221. style.flexDirection = 'column';
  222. }
  223. return style;
  224. }
  225. },
  226. methods: {
  227. onInput(event) {
  228. this.$emit('input', event.target.value);
  229. },
  230. onFocus(event) {
  231. this.focused = true;
  232. this.$emit('focus', event);
  233. },
  234. onBlur(event) {
  235. this.focused = false;
  236. this.$emit('blur', event);
  237. },
  238. onConfirm(e) {
  239. this.$emit('confirm', e.detail.value);
  240. },
  241. onClear(event) {
  242. this.$emit('input', '');
  243. },
  244. rightIconClick() {
  245. this.$emit('right-icon-click');
  246. this.$emit('click');
  247. },
  248. fieldClick() {
  249. this.$emit('click');
  250. }
  251. }
  252. };
  253. </script>
  254. <style lang="scss" scoped>
  255. .u-field {
  256. font-size: 28rpx;
  257. padding: 20rpx 28rpx;
  258. text-align: left;
  259. position: relative;
  260. color: $u-main-color;
  261. }
  262. .u-field-inner {
  263. display: flex;
  264. align-items: center;
  265. }
  266. .u-textarea-inner {
  267. align-items: flex-start;
  268. }
  269. .u-textarea-class {
  270. min-height: 96rpx;
  271. width: auto;
  272. font-size: 28rpx;
  273. }
  274. .fild-body {
  275. display: flex;
  276. flex: 1;
  277. align-items: center;
  278. }
  279. .u-arror-right {
  280. margin-left: 8rpx;
  281. }
  282. .u-label-text {
  283. display: inline-block;
  284. }
  285. .u-label-left-gap {
  286. margin-left: 6rpx;
  287. }
  288. .u-label-postion-top {
  289. flex-direction: column;
  290. align-items: flex-start;
  291. }
  292. .u-label {
  293. width: 130rpx;
  294. flex: 1 1 130rpx;
  295. text-align: left;
  296. position: relative;
  297. display: flex;
  298. align-items: center;
  299. }
  300. .u-required::before {
  301. content: '*';
  302. position: absolute;
  303. left: -16rpx;
  304. font-size: 14px;
  305. color: $u-type-error;
  306. height: 9px;
  307. line-height: 1;
  308. }
  309. .u-field__input-wrap {
  310. position: relative;
  311. overflow: hidden;
  312. font-size: 28rpx;
  313. height: 48rpx;
  314. flex: 1;
  315. width: auto;
  316. }
  317. .u-clear-icon {
  318. display: flex;
  319. align-items: center;
  320. }
  321. .u-field-border:after {
  322. left: 32rpx!important;
  323. position: absolute;
  324. box-sizing: border-box;
  325. content: ' ';
  326. pointer-events: none;
  327. right: 0;
  328. top: 0;
  329. border-bottom: 1px solid $u-border-color;
  330. -webkit-transform: scaleY(0.5);
  331. transform: scaleY(0.5);
  332. }
  333. .u-error-message {
  334. color: $u-type-error;
  335. font-size: 26rpx;
  336. text-align: left;
  337. }
  338. .placeholder-style {
  339. color: rgb(150, 151, 153);
  340. }
  341. .u-input-class {
  342. font-size: 28rpx;
  343. }
  344. </style>