m-input.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view class="m-input-view">
  3. <input
  4. :focus="focus"
  5. :type="inputType == 'password' ? 'text' : inputType"
  6. :value="modelValue"
  7. @input="onInput"
  8. class="m-input-input"
  9. :placeholder="placeholder"
  10. :password="type == 'password' && !showPassword"
  11. :maxlength="maxlength"
  12. @focus="onFocus"
  13. @blur="onBlur" />
  14. <!-- 优先显示密码可见按钮 -->
  15. <view v-if="clearable&&!displayable&&modelValue.length" class="m-input-icon">
  16. <uni-icons color="#666666" type="clear" @click="clear"></uni-icons>
  17. </view>
  18. <view v-if="displayable" class="m-input-icon">
  19. <uni-icons
  20. :style="{color:showPassword?'#666666':'#cccccc'}"
  21. type="eye"
  22. @click.stop="showPassword = !showPassword"></uni-icons>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. emits: ["update:modelValue", "input", "change", "confirm", "clear", "blur", "focus", "click", "touchstart"],
  29. props: {
  30. /**
  31. * 输入类型
  32. */
  33. type: String,
  34. /**
  35. * 值
  36. */
  37. modelValue:String,
  38. /**
  39. * 占位符
  40. */
  41. placeholder: String,
  42. /**
  43. * 是否显示清除按钮
  44. */
  45. clearable: {
  46. type: [Boolean, String],
  47. default: false
  48. },
  49. /**
  50. * 是否显示密码可见按钮
  51. */
  52. displayable: {
  53. type: [Boolean, String],
  54. default: false
  55. },
  56. /**
  57. * 自动获取焦点
  58. */
  59. focus: {
  60. type: [Boolean, String],
  61. default: false
  62. },
  63. maxlength: {
  64. type: Number,
  65. default: -1
  66. }
  67. },
  68. data() {
  69. return {
  70. /**
  71. * 显示密码明文
  72. */
  73. showPassword: false,
  74. /**
  75. * 是否获取焦点
  76. */
  77. isFocus: false
  78. }
  79. },
  80. computed: {
  81. inputType() {
  82. const type = this.type
  83. return type === 'password' ? 'text' : type
  84. }
  85. },
  86. methods: {
  87. clear() {
  88. this.$emit('update:modelValue','')
  89. },
  90. onFocus() {
  91. this.isFocus = true
  92. },
  93. onBlur() {
  94. this.$nextTick(() => {
  95. this.isFocus = false
  96. })
  97. },
  98. onInput(e) {
  99. this.$emit('update:modelValue',e.detail.value)
  100. }
  101. }
  102. }
  103. </script>
  104. <style>
  105. .m-input-view {
  106. display: inline-flex;
  107. flex-direction: row;
  108. align-items: center;
  109. /* width: 100%; */
  110. flex: 1;
  111. padding: 0 10px;
  112. margin-top: 20rpx;
  113. }
  114. .m-input-input {
  115. flex: 1;
  116. width: 100%;
  117. min-height: 100%;
  118. line-height: inherit;
  119. background-color: rgba(0, 0, 0, 0);
  120. }
  121. .m-input-icon {
  122. width: 20px;
  123. font-size: 20px;
  124. line-height: 20px;
  125. color: #666666;
  126. }
  127. </style>