You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

660 lines
18 KiB

3 weeks ago
  1. <template>
  2. <view class="uni-easyinput" :class="{ 'uni-easyinput-error': msg }" :style="boxStyle">
  3. <view class="uni-easyinput__content" :class="inputContentClass" :style="inputContentStyle">
  4. <uni-icons v-if="prefixIcon" class="content-clear-icon" :type="prefixIcon" color="#c0c4cc" @click="onClickIcon('prefix')" size="22"></uni-icons>
  5. <slot name="left">
  6. </slot>
  7. <!-- #ifdef MP-ALIPAY -->
  8. <textarea :enableNative="enableNative" v-if="type === 'textarea'" class="uni-easyinput__content-textarea" :class="{ 'input-padding': inputBorder }" :name="name" :value="val" :placeholder="placeholder" :placeholderStyle="placeholderStyle" :disabled="disabled" placeholder-class="uni-easyinput__placeholder-class" :maxlength="inputMaxlength" :focus="focused" :autoHeight="autoHeight" :cursor-spacing="cursorSpacing" :adjust-position="adjustPosition" @input="onInput" @blur="_Blur" @focus="_Focus" @confirm="onConfirm" @keyboardheightchange="onkeyboardheightchange"></textarea>
  9. <input :enableNative="enableNative" v-else :type="type === 'password' ? 'text' : type" class="uni-easyinput__content-input" :style="inputStyle" :name="name" :value="val" :password="!showPassword && type === 'password'" :placeholder="placeholder" :placeholderStyle="placeholderStyle" placeholder-class="uni-easyinput__placeholder-class" :disabled="disabled" :maxlength="inputMaxlength" :focus="focused" :confirmType="confirmType" :cursor-spacing="cursorSpacing" :adjust-position="adjustPosition" @focus="_Focus" @blur="_Blur" @input="onInput" @confirm="onConfirm" @keyboardheightchange="onkeyboardheightchange" />
  10. <!-- #endif -->
  11. <!-- #ifndef MP-ALIPAY -->
  12. <textarea v-if="type === 'textarea'" class="uni-easyinput__content-textarea" :class="{ 'input-padding': inputBorder }" :name="name" :value="val" :placeholder="placeholder" :placeholderStyle="placeholderStyle" :disabled="disabled" placeholder-class="uni-easyinput__placeholder-class" :maxlength="inputMaxlength" :focus="focused" :autoHeight="autoHeight" :cursor-spacing="cursorSpacing" :adjust-position="adjustPosition" @input="onInput" @blur="_Blur" @focus="_Focus" @confirm="onConfirm" @keyboardheightchange="onkeyboardheightchange"></textarea>
  13. <input v-else :type="type === 'password' ? 'text' : type" class="uni-easyinput__content-input" :style="inputStyle" :name="name" :value="val" :password="!showPassword && type === 'password'" :placeholder="placeholder" :placeholderStyle="placeholderStyle" placeholder-class="uni-easyinput__placeholder-class" :disabled="disabled" :maxlength="inputMaxlength" :focus="focused" :confirmType="confirmType" :cursor-spacing="cursorSpacing" :adjust-position="adjustPosition" @focus="_Focus" @blur="_Blur" @input="onInput" @confirm="onConfirm" @keyboardheightchange="onkeyboardheightchange" />
  14. <!-- #endif -->
  15. <template v-if="type === 'password' && passwordIcon">
  16. <!-- 开启密码时显示小眼睛 -->
  17. <uni-icons v-if="isVal" class="content-clear-icon" :class="{ 'is-textarea-icon': type === 'textarea' }" :type="showPassword ? 'eye-slash-filled' : 'eye-filled'" :size="22" :color="focusShow ? primaryColor : '#c0c4cc'" @click="onEyes"></uni-icons>
  18. </template>
  19. <template v-if="suffixIcon">
  20. <uni-icons v-if="suffixIcon" class="content-clear-icon" :type="suffixIcon" color="#c0c4cc" @click="onClickIcon('suffix')" size="22"></uni-icons>
  21. </template>
  22. <template v-else>
  23. <uni-icons v-if="clearable && isVal && !disabled && type !== 'textarea'" class="content-clear-icon" :class="{ 'is-textarea-icon': type === 'textarea' }" type="clear" :size="clearSize" :color="msg ? '#dd524d' : focusShow ? primaryColor : '#c0c4cc'" @click="onClear"></uni-icons>
  24. </template>
  25. <slot name="right"></slot>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. /**
  31. * Easyinput 输入框
  32. * @description 此组件可以实现表单的输入与校验包括 "text" "textarea" 类型
  33. * @tutorial https://ext.dcloud.net.cn/plugin?id=3455
  34. * @property {String} value 输入内容
  35. * @property {String } type 输入框的类型默认text password/text/textarea/..
  36. * @value text 文本输入键盘
  37. * @value textarea 多行文本输入键盘
  38. * @value password 密码输入键盘
  39. * @value number 数字输入键盘注意iOS上app-vue弹出的数字键盘并非9宫格方式
  40. * @value idcard 身份证输入键盘支付宝百度QQ小程序
  41. * @value digit 带小数点的数字键盘 App的nvue页面微信支付宝百度头条QQ小程序支持
  42. * @property {Boolean} clearable 是否显示右侧清空内容的图标控件点击可清空输入框内容默认true
  43. * @property {Boolean} autoHeight 是否自动增高输入区域type为textarea时有效默认true
  44. * @property {String } placeholder 输入框的提示文字
  45. * @property {String } placeholderStyle placeholder的样式(内联样式字符串)"color: #ddd"
  46. * @property {Boolean} focus 是否自动获得焦点默认false
  47. * @property {Boolean} disabled 是否禁用默认false
  48. * @property {Number } maxlength 最大输入长度设置为 -1 的时候不限制最大长度默认140
  49. * @property {String } confirmType 设置键盘右下角按钮的文字仅在type="text"时生效默认done
  50. * @property {Number } clearSize 清除图标的大小单位px默认15
  51. * @property {String} prefixIcon 输入框头部图标
  52. * @property {String} suffixIcon 输入框尾部图标
  53. * @property {String} primaryColor 设置主题色默认#2979ff
  54. * @property {Boolean} trim 是否自动去除两端的空格
  55. * @property {Boolean} cursorSpacing 指定光标与键盘的距离单位 px
  56. * @property {Boolean} ajust-position 当键盘弹起时是否上推内容默认值true
  57. * @value both 去除两端空格
  58. * @value left 去除左侧空格
  59. * @value right 去除右侧空格
  60. * @value start 去除左侧空格
  61. * @value end 去除右侧空格
  62. * @value all 去除全部空格
  63. * @value none 不去除空格
  64. * @property {Boolean} inputBorder 是否显示input输入框的边框默认true
  65. * @property {Boolean} passwordIcon type=password时是否显示小眼睛图标
  66. * @property {Object} styles 自定义颜色
  67. * @event {Function} input 输入框内容发生变化时触发
  68. * @event {Function} focus 输入框获得焦点时触发
  69. * @event {Function} blur 输入框失去焦点时触发
  70. * @event {Function} confirm 点击完成按钮时触发
  71. * @event {Function} iconClick 点击图标时触发
  72. * @example <uni-easyinput v-model="mobile"></uni-easyinput>
  73. */
  74. function obj2strClass(obj) {
  75. let classess = '';
  76. for (let key in obj) {
  77. const val = obj[key];
  78. if (val) {
  79. classess += `${key} `;
  80. }
  81. }
  82. return classess;
  83. }
  84. function obj2strStyle(obj) {
  85. let style = '';
  86. for (let key in obj) {
  87. const val = obj[key];
  88. style += `${key}:${val};`;
  89. }
  90. return style;
  91. }
  92. export default {
  93. name: 'uni-easyinput',
  94. emits: [
  95. 'click',
  96. 'iconClick',
  97. 'update:modelValue',
  98. 'input',
  99. 'focus',
  100. 'blur',
  101. 'confirm',
  102. 'clear',
  103. 'eyes',
  104. 'change',
  105. 'keyboardheightchange'
  106. ],
  107. model: {
  108. prop: 'modelValue',
  109. event: 'update:modelValue'
  110. },
  111. options: {
  112. // #ifdef MP-TOUTIAO
  113. virtualHost: false,
  114. // #endif
  115. // #ifndef MP-TOUTIAO
  116. virtualHost: true
  117. // #endif
  118. },
  119. inject: {
  120. form: {
  121. from: 'uniForm',
  122. default: null
  123. },
  124. formItem: {
  125. from: 'uniFormItem',
  126. default: null
  127. }
  128. },
  129. props: {
  130. name: String,
  131. value: [Number, String],
  132. modelValue: [Number, String],
  133. type: {
  134. type: String,
  135. default: 'text'
  136. },
  137. clearable: {
  138. type: Boolean,
  139. default: true
  140. },
  141. autoHeight: {
  142. type: Boolean,
  143. default: false
  144. },
  145. placeholder: {
  146. type: String,
  147. default: ' '
  148. },
  149. placeholderStyle: String,
  150. focus: {
  151. type: Boolean,
  152. default: false
  153. },
  154. disabled: {
  155. type: Boolean,
  156. default: false
  157. },
  158. maxlength: {
  159. type: [Number, String],
  160. default: 140
  161. },
  162. confirmType: {
  163. type: String,
  164. default: 'done'
  165. },
  166. clearSize: {
  167. type: [Number, String],
  168. default: 24
  169. },
  170. inputBorder: {
  171. type: Boolean,
  172. default: true
  173. },
  174. prefixIcon: {
  175. type: String,
  176. default: ''
  177. },
  178. suffixIcon: {
  179. type: String,
  180. default: ''
  181. },
  182. trim: {
  183. type: [Boolean, String],
  184. default: false
  185. },
  186. cursorSpacing: {
  187. type: Number,
  188. default: 0
  189. },
  190. passwordIcon: {
  191. type: Boolean,
  192. default: true
  193. },
  194. adjustPosition: {
  195. type: Boolean,
  196. default: true
  197. },
  198. primaryColor: {
  199. type: String,
  200. default: '#2979ff'
  201. },
  202. styles: {
  203. type: Object,
  204. default () {
  205. return {
  206. color: '#333',
  207. backgroundColor: '#fff',
  208. disableColor: '#F7F6F6',
  209. borderColor: '#e5e5e5'
  210. };
  211. }
  212. },
  213. errorMessage: {
  214. type: [String, Boolean],
  215. default: ''
  216. },
  217. // #ifdef MP-ALIPAY
  218. enableNative: {
  219. type: Boolean,
  220. default: false
  221. }
  222. // #endif
  223. },
  224. data() {
  225. return {
  226. focused: false,
  227. val: '',
  228. showMsg: '',
  229. border: false,
  230. isFirstBorder: false,
  231. showClearIcon: false,
  232. showPassword: false,
  233. focusShow: false,
  234. localMsg: '',
  235. isEnter: false // 用于判断当前是否是使用回车操作
  236. };
  237. },
  238. computed: {
  239. // 输入框内是否有值
  240. isVal() {
  241. const val = this.val;
  242. // fixed by mehaotian 处理值为0的情况,字符串0不在处理范围
  243. if (val || val === 0) {
  244. return true;
  245. }
  246. return false;
  247. },
  248. msg() {
  249. // console.log('computed', this.form, this.formItem);
  250. // if (this.form) {
  251. // return this.errorMessage || this.formItem.errMsg;
  252. // }
  253. // TODO 处理头条 formItem 中 errMsg 不更新的问题
  254. return this.localMsg || this.errorMessage;
  255. },
  256. // 因为uniapp的input组件的maxlength组件必须要数值,这里转为数值,用户可以传入字符串数值
  257. inputMaxlength() {
  258. return Number(this.maxlength);
  259. },
  260. // 处理外层样式的style
  261. boxStyle() {
  262. return `color:${
  263. this.inputBorder && this.msg ? '#e43d33' : this.styles.color
  264. };`;
  265. },
  266. // input 内容的类和样式处理
  267. inputContentClass() {
  268. return obj2strClass({
  269. 'is-input-border': this.inputBorder,
  270. 'is-input-error-border': this.inputBorder && this.msg,
  271. 'is-textarea': this.type === 'textarea',
  272. 'is-disabled': this.disabled,
  273. 'is-focused': this.focusShow
  274. });
  275. },
  276. inputContentStyle() {
  277. const focusColor = this.focusShow ?
  278. this.primaryColor :
  279. this.styles.borderColor;
  280. const borderColor =
  281. this.inputBorder && this.msg ? '#dd524d' : focusColor;
  282. return obj2strStyle({
  283. 'border-color': borderColor || '#e5e5e5',
  284. 'background-color': this.disabled ?
  285. this.styles.disableColor : this.styles.backgroundColor
  286. });
  287. },
  288. // input右侧样式
  289. inputStyle() {
  290. const paddingRight =
  291. this.type === 'password' || this.clearable || this.prefixIcon ?
  292. '' :
  293. '10px';
  294. return obj2strStyle({
  295. 'padding-right': paddingRight,
  296. 'padding-left': this.prefixIcon ? '' : '10px',
  297. ...this.styles
  298. });
  299. }
  300. },
  301. watch: {
  302. value(newVal) {
  303. // fix by mehaotian 解决 值为null的情况下,input报错的bug
  304. if (newVal === null) {
  305. this.val = '';
  306. return
  307. }
  308. this.val = newVal;
  309. },
  310. modelValue(newVal) {
  311. if (newVal === null) {
  312. this.val = '';
  313. return
  314. }
  315. this.val = newVal;
  316. },
  317. focus(newVal) {
  318. this.$nextTick(() => {
  319. this.focused = this.focus;
  320. this.focusShow = this.focus;
  321. });
  322. }
  323. },
  324. created() {
  325. this.init();
  326. // TODO 处理头条vue3 computed 不监听 inject 更改的问题(formItem.errMsg)
  327. if (this.form && this.formItem) {
  328. this.$watch('formItem.errMsg', newVal => {
  329. this.localMsg = newVal;
  330. });
  331. }
  332. },
  333. mounted() {
  334. this.$nextTick(() => {
  335. this.focused = this.focus;
  336. this.focusShow = this.focus;
  337. });
  338. },
  339. methods: {
  340. /**
  341. * 初始化变量值
  342. */
  343. init() {
  344. if (this.value || this.value === 0) {
  345. this.val = this.value;
  346. } else if (
  347. this.modelValue ||
  348. this.modelValue === 0 ||
  349. this.modelValue === ''
  350. ) {
  351. this.val = this.modelValue;
  352. } else {
  353. // fix by ht 如果初始值为null,则input报错,待框架修复
  354. this.val = '';
  355. }
  356. },
  357. /**
  358. * 点击图标时触发
  359. * @param {Object} type
  360. */
  361. onClickIcon(type) {
  362. this.$emit('iconClick', type);
  363. },
  364. /**
  365. * 显示隐藏内容密码框时生效
  366. */
  367. onEyes() {
  368. this.showPassword = !this.showPassword;
  369. this.$emit('eyes', this.showPassword);
  370. },
  371. /**
  372. * 输入时触发
  373. * @param {Object} event
  374. */
  375. onInput(event) {
  376. let value = event.detail.value;
  377. // 判断是否去除空格
  378. if (this.trim) {
  379. if (typeof this.trim === 'boolean' && this.trim) {
  380. value = this.trimStr(value);
  381. }
  382. if (typeof this.trim === 'string') {
  383. value = this.trimStr(value, this.trim);
  384. }
  385. }
  386. if (this.errMsg) this.errMsg = '';
  387. this.val = value;
  388. // TODO 兼容 vue2
  389. this.$emit('input', value);
  390. // TODO 兼容 vue3
  391. this.$emit('update:modelValue', value);
  392. },
  393. /**
  394. * 外部调用方法
  395. * 获取焦点时触发
  396. * @param {Object} event
  397. */
  398. onFocus() {
  399. this.$nextTick(() => {
  400. this.focused = true;
  401. });
  402. this.$emit('focus', null);
  403. },
  404. _Focus(event) {
  405. this.focusShow = true;
  406. this.$emit('focus', event);
  407. },
  408. /**
  409. * 外部调用方法
  410. * 失去焦点时触发
  411. * @param {Object} event
  412. */
  413. onBlur() {
  414. this.focused = false;
  415. this.$emit('blur', null);
  416. },
  417. _Blur(event) {
  418. let value = event.detail.value;
  419. this.focusShow = false;
  420. this.$emit('blur', event);
  421. // 根据类型返回值,在event中获取的值理论上讲都是string
  422. if (this.isEnter === false) {
  423. this.$emit('change', this.val);
  424. }
  425. // 失去焦点时参与表单校验
  426. if (this.form && this.formItem) {
  427. const { validateTrigger } = this.form;
  428. if (validateTrigger === 'blur') {
  429. this.formItem.onFieldChange();
  430. }
  431. }
  432. },
  433. /**
  434. * 按下键盘的发送键
  435. * @param {Object} e
  436. */
  437. onConfirm(e) {
  438. this.$emit('confirm', this.val);
  439. this.isEnter = true;
  440. this.$emit('change', this.val);
  441. this.$nextTick(() => {
  442. this.isEnter = false;
  443. });
  444. },
  445. /**
  446. * 清理内容
  447. * @param {Object} event
  448. */
  449. onClear(event) {
  450. this.val = '';
  451. // TODO 兼容 vue2
  452. this.$emit('input', '');
  453. // TODO 兼容 vue2
  454. // TODO 兼容 vue3
  455. this.$emit('update:modelValue', '');
  456. // 点击叉号触发
  457. this.$emit('clear');
  458. },
  459. /**
  460. * 键盘高度发生变化的时候触发此事件
  461. * 兼容性微信小程序2.7.0+App 3.1.0+
  462. * @param {Object} event
  463. */
  464. onkeyboardheightchange(event) {
  465. this.$emit('keyboardheightchange', event);
  466. },
  467. /**
  468. * 去除空格
  469. */
  470. trimStr(str, pos = 'both') {
  471. if (pos === 'both') {
  472. return str.trim();
  473. } else if (pos === 'left') {
  474. return str.trimLeft();
  475. } else if (pos === 'right') {
  476. return str.trimRight();
  477. } else if (pos === 'start') {
  478. return str.trimStart();
  479. } else if (pos === 'end') {
  480. return str.trimEnd();
  481. } else if (pos === 'all') {
  482. return str.replace(/\s+/g, '');
  483. } else if (pos === 'none') {
  484. return str;
  485. }
  486. return str;
  487. }
  488. }
  489. };
  490. </script>
  491. <style lang="scss">
  492. $uni-error: #e43d33;
  493. $uni-border-1: #dcdfe6 !default;
  494. .uni-easyinput {
  495. /* #ifndef APP-NVUE */
  496. width: 100%;
  497. /* #endif */
  498. flex: 1;
  499. position: relative;
  500. text-align: left;
  501. color: #333;
  502. font-size: 14px;
  503. }
  504. .uni-easyinput__content {
  505. flex: 1;
  506. /* #ifndef APP-NVUE */
  507. width: 100%;
  508. display: flex;
  509. box-sizing: border-box;
  510. // min-height: 36px;
  511. /* #endif */
  512. flex-direction: row;
  513. align-items: center;
  514. // 处理border动画刚开始显示黑色的问题
  515. border-color: #fff;
  516. transition-property: border-color;
  517. transition-duration: 0.3s;
  518. }
  519. .uni-easyinput__content-input {
  520. /* #ifndef APP-NVUE */
  521. width: auto;
  522. /* #endif */
  523. position: relative;
  524. overflow: hidden;
  525. flex: 1;
  526. line-height: 1;
  527. font-size: 14px;
  528. height: 35px;
  529. }
  530. .uni-easyinput__placeholder-class {
  531. color: #999;
  532. font-size: 12px;
  533. // font-weight: 200;
  534. }
  535. .is-textarea {
  536. align-items: flex-start;
  537. }
  538. .is-textarea-icon {
  539. margin-top: 5px;
  540. }
  541. .uni-easyinput__content-textarea {
  542. position: relative;
  543. overflow: hidden;
  544. flex: 1;
  545. line-height: 1.5;
  546. font-size: 14px;
  547. margin: 6px;
  548. margin-left: 0;
  549. height: 80px;
  550. min-height: 80px;
  551. /* #ifndef APP-NVUE */
  552. min-height: 80px;
  553. width: auto;
  554. /* #endif */
  555. }
  556. .input-padding {
  557. padding-left: 10px;
  558. }
  559. .content-clear-icon {
  560. padding: 0 5px;
  561. }
  562. .label-icon {
  563. margin-right: 5px;
  564. margin-top: -1px;
  565. }
  566. // 显示边框
  567. .is-input-border {
  568. /* #ifndef APP-NVUE */
  569. display: flex;
  570. box-sizing: border-box;
  571. /* #endif */
  572. flex-direction: row;
  573. align-items: center;
  574. border: 1px solid $uni-border-1;
  575. border-radius: 4px;
  576. /* #ifdef MP-ALIPAY */
  577. overflow: hidden;
  578. /* #endif */
  579. }
  580. .uni-error-message {
  581. position: absolute;
  582. bottom: -17px;
  583. left: 0;
  584. line-height: 12px;
  585. color: $uni-error;
  586. font-size: 12px;
  587. text-align: left;
  588. }
  589. .uni-error-msg--boeder {
  590. position: relative;
  591. bottom: 0;
  592. line-height: 22px;
  593. }
  594. .is-input-error-border {
  595. border-color: $uni-error;
  596. .uni-easyinput__placeholder-class {
  597. color: mix(#fff, $uni-error, 50%);
  598. }
  599. }
  600. .uni-easyinput--border {
  601. margin-bottom: 0;
  602. padding: 10px 15px;
  603. // padding-bottom: 0;
  604. border-top: 1px #eee solid;
  605. }
  606. .uni-easyinput-error {
  607. padding-bottom: 0;
  608. }
  609. .is-first-border {
  610. /* #ifndef APP-NVUE */
  611. border: none;
  612. /* #endif */
  613. /* #ifdef APP-NVUE */
  614. border-width: 0;
  615. /* #endif */
  616. }
  617. .is-disabled {
  618. background-color: #f7f6f6;
  619. color: #d5d5d5;
  620. .uni-easyinput__placeholder-class {
  621. color: #d5d5d5;
  622. font-size: 12px;
  623. }
  624. }
  625. </style>