m-input.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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" :maxlength="maxlength" @focus="onFocus" @blur="onBlur" />
  5. <!-- 优先显示密码可见按钮 -->
  6. <view v-if="clearable&&!displayable&&value.length" class="m-input-icon">
  7. <uni-icons color="#666666" type="clear" @click="clear"></uni-icons>
  8. </view>
  9. <view v-if="displayable" class="m-input-icon">
  10. <uni-icons :style="{color:showPassword?'#666666':'#cccccc'}" type="eye" @click="display"></uni-icons>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. props: {
  17. /**
  18. * 输入类型
  19. */
  20. type: String,
  21. /**
  22. * 值
  23. */
  24. value: String,
  25. /**
  26. * 占位符
  27. */
  28. placeholder: String,
  29. /**
  30. * 是否显示清除按钮
  31. */
  32. clearable: {
  33. type: [Boolean, String],
  34. default: false
  35. },
  36. /**
  37. * 是否显示密码可见按钮
  38. */
  39. displayable: {
  40. type: [Boolean, String],
  41. default: false
  42. },
  43. /**
  44. * 自动获取焦点
  45. */
  46. focus: {
  47. type: [Boolean, String],
  48. default: false
  49. },
  50. maxlength: {
  51. type: Number,
  52. default: -1
  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. min-height: 100%;
  111. line-height: inherit;
  112. background-color: rgba(0, 0, 0, 0);
  113. }
  114. .m-input-icon {
  115. width: 20px;
  116. font-size: 20px;
  117. line-height: 20px;
  118. color: #666666;
  119. }
  120. </style>