uni-easyinput.vue 12 KB

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