uni-easyinput.vue 15 KB

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