cl-input.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <template>
  2. <view class="cl-input" :class="[classList]">
  3. <view class="cl-input__prepend" v-if="$slots.prepend">
  4. <slot name="prepend"></slot>
  5. </view>
  6. <view class="cl-input__wrap">
  7. <text
  8. class="cl-input__icon"
  9. :class="[prefixIcon]"
  10. v-if="prefixIcon"
  11. @tap="prefixIconTap"
  12. ></text>
  13. <text
  14. class="cl-input__icon"
  15. :class="[suffixIcon]"
  16. v-if="suffixIcon"
  17. @tap="suffixIconTap"
  18. ></text>
  19. <template v-if="type == 'password'">
  20. <input
  21. class="cl-input__inner"
  22. v-model="value2"
  23. type="password"
  24. :password="password"
  25. :placeholder="placeholder"
  26. :disabled="isDisabled"
  27. :focus="focus"
  28. :placeholder-style="placeholderStyle"
  29. :placeholder-class="placeholderClass"
  30. :maxlength="maxlength"
  31. :cursor-spacing="cursorSpacing"
  32. :confirm-hold="confirmHold"
  33. :adjust-position="adjustPosition"
  34. :holdKeyboard="holdKeyboard"
  35. @input="onInput"
  36. @focus="onFocus"
  37. @blur="onBlur"
  38. @confirm="onConfirm"
  39. @keyboardheightchange="onKeyboardheightchange"
  40. />
  41. </template>
  42. <template v-else-if="type == 'number'">
  43. <input
  44. class="cl-input__inner"
  45. v-model="value2"
  46. type="number"
  47. :password="password"
  48. :placeholder="placeholder"
  49. :disabled="isDisabled"
  50. :focus="focus"
  51. :placeholder-style="placeholderStyle"
  52. :placeholder-class="placeholderClass"
  53. :maxlength="maxlength"
  54. :cursor-spacing="cursorSpacing"
  55. :confirm-hold="confirmHold"
  56. :adjust-position="adjustPosition"
  57. :holdKeyboard="holdKeyboard"
  58. @input="onInput"
  59. @focus="onFocus"
  60. @blur="onBlur"
  61. @confirm="onConfirm"
  62. @keyboardheightchange="onKeyboardheightchange"
  63. />
  64. </template>
  65. <template v-else-if="type == 'idcard'">
  66. <input
  67. class="cl-input__inner"
  68. v-model="value2"
  69. type="idcard"
  70. :password="password"
  71. :placeholder="placeholder"
  72. :disabled="isDisabled"
  73. :focus="focus"
  74. :placeholder-style="placeholderStyle"
  75. :placeholder-class="placeholderClass"
  76. :maxlength="maxlength"
  77. :cursor-spacing="cursorSpacing"
  78. :confirm-hold="confirmHold"
  79. :adjust-position="adjustPosition"
  80. :holdKeyboard="holdKeyboard"
  81. @input="onInput"
  82. @focus="onFocus"
  83. @blur="onBlur"
  84. @confirm="onConfirm"
  85. @keyboardheightchange="onKeyboardheightchange"
  86. />
  87. </template>
  88. <template v-else-if="type == 'digit'">
  89. <input
  90. class="cl-input__inner"
  91. v-model="value2"
  92. type="digit"
  93. :password="password"
  94. :placeholder="placeholder"
  95. :disabled="isDisabled"
  96. :focus="focus"
  97. :placeholder-style="placeholderStyle"
  98. :placeholder-class="placeholderClass"
  99. :maxlength="maxlength"
  100. :cursor-spacing="cursorSpacing"
  101. :confirm-hold="confirmHold"
  102. :adjust-position="adjustPosition"
  103. :holdKeyboard="holdKeyboard"
  104. @input="onInput"
  105. @focus="onFocus"
  106. @blur="onBlur"
  107. @confirm="onConfirm"
  108. @keyboardheightchange="onKeyboardheightchange"
  109. />
  110. </template>
  111. <template v-else>
  112. <input
  113. class="cl-input__inner"
  114. v-model="value2"
  115. type="text"
  116. :password="password"
  117. :placeholder="placeholder"
  118. :disabled="isDisabled"
  119. :focus="focus"
  120. :placeholder-style="placeholderStyle"
  121. :placeholder-class="placeholderClass"
  122. :maxlength="maxlength"
  123. :cursor-spacing="cursorSpacing"
  124. :confirm-type="confirmType"
  125. :confirm-hold="confirmHold"
  126. :adjust-position="adjustPosition"
  127. :holdKeyboard="holdKeyboard"
  128. @input="onInput"
  129. @focus="onFocus"
  130. @blur="onBlur"
  131. @confirm="onConfirm"
  132. @keyboardheightchange="onKeyboardheightchange"
  133. />
  134. </template>
  135. <!-- #ifdef MP-ALIPAY -->
  136. <text class="cl-input__clear cl-icon-close-border" @tap="clear" v-if="clearable"></text>
  137. <!-- #endif -->
  138. <!-- #ifndef MP-ALIPAY -->
  139. <text
  140. class="cl-input__clear cl-icon-close-border"
  141. @tap="clear"
  142. v-if="isFocus && clearable"
  143. ></text>
  144. <!-- #endif -->
  145. </view>
  146. <view class="cl-input__append" v-if="$slots.append">
  147. <slot name="append"></slot>
  148. </view>
  149. </view>
  150. </template>
  151. <script>
  152. import Form from "../../mixins/form";
  153. /**
  154. * input 输入框
  155. * @description 该组件基于官方的 input 组件。参数与官方一致,同时添加新的支持。
  156. * @tutorial https://docs.cool-js.com/uni/components/basic/input.html
  157. * @property {Boolean} round 是否圆角
  158. * @property {Boolean} border 是否带有边框
  159. * @property {String} prefixIcon 前缀图标
  160. * @property {String} suffixIcon 后缀图标
  161. * @event {Function} change 值发生改变时触发
  162. * @event {Function} search 搜索时触发
  163. * @event {Function} clear 清空值时触发
  164. * @event {Function} prefix-icon-tap 前缀图标点击时
  165. * @event {Function} suffix-icon-tap 后缀图标点击时
  166. * @example <cl-input v-model="val"></cl-input>
  167. */
  168. export default {
  169. name: "cl-input",
  170. props: {
  171. value: [String, Number],
  172. type: {
  173. type: String,
  174. default: "text",
  175. },
  176. password: Boolean,
  177. placeholder: String,
  178. clearable: Boolean,
  179. placeholderStyle: String,
  180. placeholderClass: String,
  181. disabled: Boolean,
  182. round: Boolean,
  183. border: {
  184. type: Boolean,
  185. default: true,
  186. },
  187. focus: Boolean,
  188. maxlength: {
  189. type: [Number, String],
  190. default: 140,
  191. },
  192. cursorSpacing: {
  193. type: Number,
  194. default: 0,
  195. },
  196. confirmType: {
  197. type: String,
  198. default: "done",
  199. },
  200. confirmHold: Boolean,
  201. adjustPosition: {
  202. type: Boolean,
  203. default: true,
  204. },
  205. holdKeyboard: {
  206. type: Boolean,
  207. default: false,
  208. },
  209. prefixIcon: String,
  210. suffixIcon: String,
  211. },
  212. computed: {
  213. classList() {
  214. let list = [];
  215. if (this.prefixIcon) {
  216. list.push("cl-input--prefix");
  217. }
  218. if (this.suffixIcon) {
  219. list.push("cl-input--suffix");
  220. }
  221. if (this.isFocus) {
  222. list.push("cl-input--focus");
  223. }
  224. if (this.isDisabled) {
  225. list.push("is-disabled");
  226. }
  227. if (this.round) {
  228. list.push("is-round");
  229. }
  230. if (this.border) {
  231. list.push("is-border");
  232. }
  233. return list.join(" ");
  234. },
  235. },
  236. mixins: [Form],
  237. data() {
  238. return {
  239. value2: null,
  240. isFocus: false,
  241. };
  242. },
  243. watch: {
  244. value: {
  245. immediate: true,
  246. handler(val) {
  247. this.value2 = val;
  248. },
  249. },
  250. },
  251. methods: {
  252. onInput() {
  253. this.$emit("input", this.value2);
  254. this.$emit("change", this.value2);
  255. },
  256. onFocus(e) {
  257. this.$emit("focus", e);
  258. this.isFocus = true;
  259. },
  260. onBlur(e) {
  261. this.$emit("blur", e);
  262. setTimeout(() => {
  263. this.isFocus = false;
  264. }, 0);
  265. },
  266. onConfirm(e) {
  267. this.$emit("confirm", e);
  268. this.search();
  269. },
  270. onKeyboardheightchange(e) {
  271. this.$emit("keyboardheightchange", e);
  272. },
  273. search() {
  274. this.$emit("search", this.value2);
  275. },
  276. clear() {
  277. this.value2 = "";
  278. this.$emit("input", "");
  279. this.$emit("change", "");
  280. this.$emit("clear");
  281. },
  282. prefixIconTap() {
  283. this.$emit("prefix-icon-tap", this.value2);
  284. },
  285. suffixIconTap() {
  286. this.$emit("suffix-icon-tap", this.value2);
  287. },
  288. },
  289. };
  290. </script>