uni-easyinput.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <template>
  2. <view class="uni-easyinput" :class="{'uni-easyinput-error':msg}" :style="{color:inputBorder && msg?'#dd524d':styles.color}">
  3. <view class="uni-easyinput__content" :class="{'is-input-border':inputBorder ,'is-input-error-border':inputBorder && msg,'is-textarea':type==='textarea','is-disabled':disabled}"
  4. :style="{'border-color':inputBorder && msg?'#dd524d':styles.borderColor,'background-color':disabled?styles.disableColor:'#fff'}">
  5. <uni-icons v-if="prefixIcon" class="content-clear-icon" :type="prefixIcon" color="#c0c4cc" @click="onClickIcon('prefix')"></uni-icons>
  6. <textarea v-if="type === 'textarea'" class="uni-easyinput__content-textarea" :class="{'input-padding':inputBorder}"
  7. :name="name" :value="val" :placeholder="placeholder" :placeholderStyle="placeholderStyle" :disabled="disabled"
  8. :maxlength="inputMaxlength" :focus="focused" :autoHeight="autoHeight" @input="onInput" @blur="onBlur" @focus="onFocus"
  9. @confirm="onConfirm"></textarea>
  10. <input v-else :type="type === 'password'?'text':type" class="uni-easyinput__content-input" :style="{
  11. 'padding-right':type === 'password' ||clearable || prefixIcon?'':'10px',
  12. 'padding-left':prefixIcon?'':'10px'
  13. }"
  14. :name="name" :value="val" :password="!showPassword && type === 'password'" :placeholder="placeholder"
  15. :placeholderStyle="placeholderStyle" :disabled="disabled" :maxlength="inputMaxlength" :focus="focused" @focus="onFocus"
  16. @blur="onBlur" @input="onInput" @confirm="onConfirm" />
  17. <template v-if="type === 'password'">
  18. <uni-icons v-if="val != '' " class="content-clear-icon" :class="{'is-textarea-icon':type==='textarea'}" :type="showPassword?'eye-slash-filled':'eye-filled'"
  19. :size="18" color="#c0c4cc" @click="onEyes"></uni-icons>
  20. </template>
  21. <template v-else-if="suffixIcon">
  22. <uni-icons v-if="suffixIcon" class="content-clear-icon" :type="suffixIcon" color="#c0c4cc" @click="onClickIcon('suffix')"></uni-icons>
  23. </template>
  24. <template v-else>
  25. <uni-icons class="content-clear-icon" :class="{'is-textarea-icon':type==='textarea'}" type="clear" :size="clearSize"
  26. v-if="clearable && focused && val " color="#c0c4cc" @click="onClear"></uni-icons>
  27. </template>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. /**
  33. * Field 输入框
  34. * @description 此组件可以实现表单的输入与校验,包括 "text" 和 "textarea" 类型。
  35. * @tutorial https://ext.dcloud.net.cn/plugin?id=21001
  36. * @property {String| Number} value 输入内容
  37. * @property {String } type 输入框的类型(默认text) password/text/textarea/..
  38. * @value text 文本输入键盘
  39. * @value textarea 多行文本输入键盘
  40. * @value password 密码输入键盘
  41. * @value number 数字输入键盘,注意iOS上app-vue弹出的数字键盘并非9宫格方式
  42. * @value idcard 身份证输入键盘,信、支付宝、百度、QQ小程序
  43. * @value digit 带小数点的数字键盘 ,App的nvue页面、微信、支付宝、百度、头条、QQ小程序支持
  44. * @property {Boolean} clearable 是否显示右侧清空内容的图标控件(输入框有内容,且获得焦点时才显示),点击可清空输入框内容(默认true)
  45. * @property {Boolean} autoHeight 是否自动增高输入区域,type为textarea时有效(默认true)
  46. * @property {String } placeholder 输入框的提示文字
  47. * @property {String } placeholderStyle placeholder的样式(内联样式,字符串),如"color: #ddd"
  48. * @property {Boolean} focus 是否自动获得焦点(默认false)
  49. * @property {Boolean} disabled 是否不可输入(默认false)
  50. * @property {Number } maxlength 最大输入长度,设置为 -1 的时候不限制最大长度(默认140)
  51. * @property {String } confirmType 设置键盘右下角按钮的文字,仅在type="text"时生效(默认done)
  52. * @property {Number } clearSize 清除图标的大小,单位px(默认15)
  53. * @property {String} prefixIcon 输入框头部图标
  54. * @property {String} suffixIcon 输入框尾部图标
  55. * @property {Boolean} trim 是否自动去除两端的空格
  56. * @property {Boolean} inputBorder 是否显示input输入框的边框(默认false)
  57. * @property {Object} styles 自定义颜色
  58. * @event {Function} input 输入框内容发生变化时触发
  59. * @event {Function} focus 输入框获得焦点时触发
  60. * @event {Function} blur 输入框失去焦点时触发
  61. * @event {Function} confirm 点击完成按钮时触发
  62. * @event {Function} iconClick 点击图标时触发
  63. * @example <uni-easyinput v-model="mobile"></uni-easyinput>
  64. */
  65. import {
  66. debounce,
  67. throttle
  68. } from './common.js'
  69. export default {
  70. name: 'uni-easyinput',
  71. props: {
  72. name: String,
  73. value: [Number, String],
  74. type: {
  75. type: String,
  76. default: 'text'
  77. },
  78. clearable: {
  79. type: Boolean,
  80. default: true
  81. },
  82. autoHeight: {
  83. type: Boolean,
  84. default: false
  85. },
  86. placeholder: String,
  87. placeholderStyle: String,
  88. focus: {
  89. type: Boolean,
  90. default: false
  91. },
  92. disabled: {
  93. type: Boolean,
  94. default: false
  95. },
  96. maxlength: {
  97. type: [Number, String],
  98. default: 140
  99. },
  100. confirmType: {
  101. type: String,
  102. default: 'done'
  103. },
  104. // 清除按钮的大小
  105. clearSize: {
  106. type: [Number, String],
  107. default: 15
  108. },
  109. // 是否显示 input 边框
  110. inputBorder: {
  111. type: Boolean,
  112. default: true
  113. },
  114. prefixIcon: {
  115. type: String,
  116. default: ''
  117. },
  118. suffixIcon: {
  119. type: String,
  120. default: ''
  121. },
  122. // 是否自动去除两端的空格
  123. trim: {
  124. type: Boolean,
  125. default: true
  126. },
  127. // 自定义样式
  128. styles: {
  129. type: Object,
  130. default () {
  131. return {
  132. color: '#333',
  133. disableColor: '#eee',
  134. borderColor: '#e5e5e5'
  135. }
  136. }
  137. }
  138. },
  139. data() {
  140. return {
  141. focused: false,
  142. errMsg: '',
  143. val: '',
  144. showMsg: '',
  145. border: false,
  146. isFirstBorder: false,
  147. showClearIcon: false,
  148. showPassword: false
  149. };
  150. },
  151. computed: {
  152. msg() {
  153. return this.errorMessage || this.errMsg;
  154. },
  155. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,给用户可以传入字符串数值
  156. inputMaxlength() {
  157. return Number(this.maxlength);
  158. },
  159. },
  160. watch: {
  161. value(newVal) {
  162. if (this.errMsg) this.errMsg = ''
  163. this.val = newVal
  164. if (this.formItem) {
  165. this.formItem.setValue(newVal)
  166. }
  167. },
  168. focus(newVal) {
  169. this.$nextTick(() => {
  170. this.focused = this.focus
  171. })
  172. }
  173. },
  174. created() {
  175. this.val = this.value
  176. this.form = this.getForm('uniForms')
  177. this.formItem = this.getForm('uniFormsItem')
  178. if (this.formItem) {
  179. if (this.formItem.name) {
  180. this.rename = this.formItem.name
  181. this.form.inputChildrens.push(this)
  182. }
  183. }
  184. },
  185. mounted() {
  186. // this.onInput = throttle(this.input, 500)
  187. this.$nextTick(() => {
  188. this.focused = this.focus
  189. })
  190. },
  191. methods: {
  192. /**
  193. * 初始化变量值
  194. */
  195. init() {
  196. },
  197. onClickIcon(type) {
  198. this.$emit('iconClick', type)
  199. },
  200. /**
  201. * 获取父元素实例
  202. */
  203. getForm(name = 'uniForms') {
  204. let parent = this.$parent;
  205. let parentName = parent.$options.name;
  206. while (parentName !== name) {
  207. parent = parent.$parent;
  208. if (!parent) return false;
  209. parentName = parent.$options.name;
  210. }
  211. return parent;
  212. },
  213. onEyes() {
  214. this.showPassword = !this.showPassword
  215. },
  216. onInput(event) {
  217. let value = event.detail.value;
  218. // 判断是否去除空格
  219. if (this.trim) value = this.trimStr(value);
  220. if (this.errMsg) this.errMsg = ''
  221. this.val = value
  222. this.$emit('input', value);
  223. },
  224. onFocus(event) {
  225. this.focused = true;
  226. this.$emit('focus', event);
  227. },
  228. onBlur(event) {
  229. let value = event.detail.value;
  230. // 最开始使用的是监听图标@touchstart事件,自从hx2.8.4后,此方法在微信小程序出错
  231. // 这里改为监听点击事件,手点击清除图标时,同时也发生了@blur事件,导致图标消失而无法点击,这里做一个延时
  232. setTimeout(() => {
  233. this.focused = false;
  234. }, 100);
  235. this.$emit('blur', event);
  236. },
  237. onConfirm(e) {
  238. this.$emit('confirm', e.detail.value);
  239. },
  240. onClear(event) {
  241. this.val = '';
  242. this.$emit('input', '');
  243. },
  244. fieldClick() {
  245. this.$emit('click');
  246. },
  247. trimStr(str, pos = 'both') {
  248. if (pos == 'both') {
  249. return str.replace(/^\s+|\s+$/g, '');
  250. } else if (pos == 'left') {
  251. return str.replace(/^\s*/, '');
  252. } else if (pos == 'right') {
  253. return str.replace(/(\s*$)/g, '');
  254. } else if (pos == 'all') {
  255. return str.replace(/\s+/g, '');
  256. } else {
  257. return str;
  258. }
  259. }
  260. }
  261. };
  262. </script>
  263. <style lang="scss" scoped>
  264. .uni-easyinput {
  265. /* #ifndef APP-NVUE */
  266. width: 100%;
  267. /* #endif */
  268. flex: 1;
  269. position: relative;
  270. // padding: 16px 14px;
  271. text-align: left;
  272. color: #333;
  273. font-size: 14px;
  274. }
  275. .uni-easyinput__content {
  276. flex: 1;
  277. /* #ifndef APP-NVUE */
  278. width: 100%;
  279. display: flex;
  280. /* #endif */
  281. flex-direction: row;
  282. align-items: center;
  283. box-sizing: border-box;
  284. min-height: 36px;
  285. }
  286. .uni-easyinput__content-input {
  287. position: relative;
  288. overflow: hidden;
  289. flex: 1;
  290. width: auto;
  291. line-height: 2;
  292. font-size: 14px;
  293. // padding-right: 10px;
  294. }
  295. .is-textarea {
  296. align-items: flex-start;
  297. }
  298. .is-textarea-icon {
  299. margin-top: 5px;
  300. }
  301. .uni-easyinput__content-textarea {
  302. position: relative;
  303. overflow: hidden;
  304. flex: 1;
  305. width: auto;
  306. line-height: 1.5;
  307. font-size: 14px;
  308. // padding-right: 10px;
  309. padding-top: 6px;
  310. padding-bottom: 10px;
  311. // box-sizing: border-box;
  312. min-height: 80px;
  313. height: 80px;
  314. }
  315. .input-padding {
  316. padding-left: 10px;
  317. }
  318. .content-clear-icon {
  319. padding: 0 5px;
  320. }
  321. .label-icon {
  322. margin-right: 5px;
  323. margin-top: -1px;
  324. }
  325. // 显示边框
  326. .is-input-border {
  327. /* #ifndef APP-NVUE */
  328. display: flex;
  329. /* #endif */
  330. flex-direction: row;
  331. align-items: center;
  332. border: 1px solid $uni-border-color;
  333. border-radius: 4px;
  334. box-sizing: border-box;
  335. }
  336. .uni-easyinput__right {
  337. // margin-left: 5px;
  338. }
  339. // 必填
  340. .is-required {
  341. color: $uni-color-error;
  342. }
  343. .uni-error-message {
  344. position: absolute;
  345. bottom: -17px;
  346. left: 0;
  347. line-height: 12px;
  348. color: $uni-color-error;
  349. font-size: 12px;
  350. text-align: left;
  351. }
  352. .uni-error-msg--boeder {
  353. position: relative;
  354. bottom: 0;
  355. line-height: 22px;
  356. }
  357. .is-input-error-border {
  358. border-color: $uni-color-error;
  359. }
  360. .uni-easyinput--border {
  361. margin-bottom: 0;
  362. padding: 10px 15px;
  363. // padding-bottom: 0;
  364. border-top: 1px #eee solid;
  365. }
  366. .uni-easyinput-error {
  367. padding-bottom: 0;
  368. }
  369. .is-first-border {
  370. border: none;
  371. }
  372. .is-disabled {
  373. background-color: #eee;
  374. }
  375. </style>