uni-easyinput.vue 11 KB

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