optimize-input.vue 14 KB

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