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