m-input.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view class="m-input-view">
  3. <input :focus="focus_" :maxlength="maxlength" :type="inputType" :value="value" @input="onInput" class="m-input-input"
  4. placeholder-style="color:rgba(170, 170, 170, 1);" :placeholder="placeholder" :password="type==='password'&&!showPassword"
  5. @focus="onFocus" @blur="onBlur" />
  6. <!-- 优先显示密码可见按钮 -->
  7. <view v-if="clearable_&&!displayable_&&value.length" class="m-input-icon">
  8. <!-- <m-icon color="rgba(170, 170, 170, 1)" type="clear" size="20" @click="clear"></m-icon> -->
  9. <text class="cuIcon-roundclose" @click="clear"></text>
  10. </view>
  11. <view v-if="displayable_" class="m-input-icon">
  12. <!-- <m-icon :color="showPassword?'rgba(170, 170, 170, 1)':'#cccccc'" type="eye" size="20" @click="display"></m-icon> -->
  13. <!-- <image mode="scaleToFill" class="password-img" v-if="showPassword" src="../static/m-icon/sPassword.png" @click="display"></image> -->
  14. <!-- <image mode="scaleToFill" class="password-img" v-else src="../static/m-icon/cPassword.png" @click="display"></image> -->
  15. <text :class="showPassword?'cuIcon-attention':'cuIcon-attentionfill'" @click="display"></text>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. import mIcon from './m-icon/m-icon.vue'
  21. export default {
  22. components: {
  23. mIcon
  24. },
  25. props: {
  26. /**
  27. * 输入类型
  28. */
  29. type: String,
  30. /**
  31. * 值
  32. */
  33. value: String,
  34. /**
  35. * 占位符
  36. */
  37. placeholder: String,
  38. maxlength: {
  39. type: String,
  40. default: "-1"
  41. },
  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. },
  64. model: {
  65. prop: 'value',
  66. event: 'input'
  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. clearable_() {
  86. return String(this.clearable) !== 'false'
  87. },
  88. displayable_() {
  89. return String(this.displayable) !== 'false'
  90. },
  91. focus_() {
  92. return String(this.focus) !== 'false'
  93. }
  94. },
  95. methods: {
  96. clear() {
  97. this.$emit('input', '')
  98. },
  99. display() {
  100. this.showPassword = !this.showPassword
  101. },
  102. onFocus() {
  103. this.isFocus = true
  104. },
  105. onBlur() {
  106. this.$nextTick(() => {
  107. this.isFocus = false
  108. })
  109. },
  110. onInput(e) {
  111. this.$emit('input', e.target.value)
  112. }
  113. }
  114. }
  115. </script>
  116. <style>
  117. .m-input-view {
  118. display: flex;
  119. flex-direction: row;
  120. align-items: center;
  121. width: 580rpx;
  122. flex: 1;
  123. padding: 0 10rpx;
  124. /* position: relative; */
  125. }
  126. .m-input-input {
  127. flex: 1;
  128. width: 100%;
  129. height: 25px;
  130. color: rgba(170, 170, 170, 1);
  131. color: 18px;
  132. /* border: 1rpx solid #007AFF; */
  133. }
  134. .m-input-icon {
  135. width: 30px;
  136. /* height: 30px; */
  137. /* position: absolute;
  138. */
  139. margin-right: 40rpx;
  140. margin-left: 10rpx;
  141. }
  142. .m-input-icon text {
  143. font-size: 20px;
  144. color: rgba(170, 170, 170, 1)
  145. }
  146. .password-img {
  147. /* background-color: #007AFF; */
  148. width: 18px;
  149. height: 14px;
  150. }
  151. </style>