m-input.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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" size="20" @click="clear"></m-icon>
  8. </view>
  9. <view v-if="displayable_" class="m-input-icon">
  10. <m-icon :color="showPassword?'#666666':'#cccccc'" type="eye" size="20" @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. clearable_() {
  77. return String(this.clearable) !== 'false'
  78. },
  79. displayable_() {
  80. return String(this.displayable) !== 'false'
  81. },
  82. focus_() {
  83. return String(this.focus) !== 'false'
  84. }
  85. },
  86. methods: {
  87. clear() {
  88. this.$emit('input', '')
  89. },
  90. display() {
  91. this.showPassword = !this.showPassword
  92. },
  93. onFocus() {
  94. this.isFocus = true
  95. },
  96. onBlur() {
  97. this.$nextTick(() => {
  98. this.isFocus = false
  99. })
  100. },
  101. onInput(e) {
  102. this.$emit('input', e.target.value)
  103. }
  104. }
  105. }
  106. </script>
  107. <style>
  108. .m-input-view {
  109. display: flex;
  110. flex-direction: row;
  111. align-items: center;
  112. width: 100%;
  113. flex: 1;
  114. padding: 0 10rpx;
  115. }
  116. .m-input-input {
  117. flex: 1;
  118. width: 100%;
  119. height: 50px;
  120. line-height: 50px;
  121. font-size: 14px;
  122. }
  123. .m-input-icon {
  124. width: 20px;
  125. }
  126. </style>