m-input.vue 2.2 KB

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