m-input.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view class="m-input-view">
  3. <input
  4. :focus="focus"
  5. :type="inputType == 'password' ? 'text' : inputType"
  6. :value="value"
  7. @input="onInput"
  8. class="m-input-input"
  9. :placeholder="placeholder"
  10. :password="type == 'password' && !showPassword"
  11. :maxlength="maxlength"
  12. @focus="onFocus"
  13. @blur="onBlur" />
  14. <!-- 优先显示密码可见按钮 -->
  15. <view v-if="clearable&&!displayable&&value.length" class="m-input-icon">
  16. <uni-icons color="#666666" type="clear" @click="clear"></uni-icons>
  17. </view>
  18. <view v-if="displayable" class="m-input-icon">
  19. <uni-icons
  20. :style="{color:showPassword?'#666666':'#cccccc'}"
  21. type="eye"
  22. @click.stop="showPassword = !showPassword"></uni-icons>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. props: {
  29. /**
  30. * 输入类型
  31. */
  32. type: String,
  33. /**
  34. * 值
  35. */
  36. value: String,
  37. modelValue:String,
  38. /**
  39. * 占位符
  40. */
  41. placeholder: String,
  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. maxlength: {
  64. type: Number,
  65. default: -1
  66. }
  67. },
  68. model: {
  69. prop: 'value',
  70. event: 'input'
  71. },
  72. data() {
  73. return {
  74. /**
  75. * 显示密码明文
  76. */
  77. showPassword: false,
  78. /**
  79. * 是否获取焦点
  80. */
  81. isFocus: false
  82. }
  83. },
  84. computed: {
  85. inputType() {
  86. const type = this.type
  87. return type === 'password' ? 'text' : type
  88. }
  89. },
  90. methods: {
  91. clear() {
  92. this.$emit('input', '')
  93. this.$emit('update:modelValue','')
  94. },
  95. onFocus() {
  96. this.isFocus = true
  97. },
  98. onBlur() {
  99. this.$nextTick(() => {
  100. this.isFocus = false
  101. })
  102. },
  103. onInput(e) {
  104. this.$emit('input', e.detail.value)
  105. this.$emit('update:modelValue',e.detail.value)
  106. }
  107. }
  108. }
  109. </script>
  110. <style>
  111. .m-input-view {
  112. display: inline-flex;
  113. flex-direction: row;
  114. align-items: center;
  115. /* width: 100%; */
  116. flex: 1;
  117. padding: 0 10px;
  118. margin-top: 20rpx;
  119. }
  120. .m-input-input {
  121. flex: 1;
  122. width: 100%;
  123. min-height: 100%;
  124. line-height: inherit;
  125. background-color: rgba(0, 0, 0, 0);
  126. }
  127. .m-input-icon {
  128. width: 20px;
  129. font-size: 20px;
  130. line-height: 20px;
  131. color: #666666;
  132. }
  133. </style>