index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div class="m-input-default">
  3. <div class="m-slot-prepend" v-if="slot && slot.prepend">
  4. <slot name="prepend"></slot>
  5. </div>
  6. <div class="demo-input" :class="groupClass">
  7. <i :class="['iconfont','left-icon',leftIcon]" v-if="leftIcon && !showPassword"></i>
  8. <input
  9. ref="inputRef"
  10. :class="styleClass"
  11. :type="showPassword ? (passwordVisible ? 'text': 'password') : 'text' "
  12. :placeholder="placeholder"
  13. :value="modelValue"
  14. :disabled="disabled"
  15. :autofocus="autofocus"
  16. :readonly="readonly"
  17. @input="iptChange"
  18. @focus="handleFocus"
  19. @blur="handleBlur"
  20. @change="handleChange">
  21. <i class="iconfont icon-close" v-if="clearable && modelValue" @click="clear"></i>
  22. <i :class="['iconfont','right-icon',rightIcon]" v-if="rightIcon && !showPassword"></i>
  23. <i
  24. :class="['iconfont','password-icon','icon-browse']"
  25. v-if="showPassword"
  26. @click="showPwd(type)">
  27. </i>
  28. </div>
  29. <div class="m-slot-append" v-if="slot && slot.append">
  30. <slot name="append"></slot>
  31. </div>
  32. </div>
  33. </template>
  34. <script>
  35. import { ref,computed,useSlots } from 'vue-demi';
  36. export default {
  37. name: 'm-input',
  38. props: {
  39. modelValue: String | Number,
  40. placeholder: {
  41. type: String,
  42. default: ''
  43. },
  44. disabled: {
  45. type: Boolean,
  46. default: false
  47. },
  48. clearable: {
  49. type: Boolean,
  50. default: false
  51. },
  52. leftIcon: {
  53. type: String,
  54. default: ""
  55. },
  56. rightIcon: {
  57. type: String,
  58. default: ""
  59. },
  60. size: {
  61. type: String,
  62. default: 'default'
  63. },
  64. showPassword: {
  65. type: Boolean,
  66. default: false
  67. },
  68. autofocus: {
  69. type: Boolean,
  70. default: false
  71. },
  72. readonly: {
  73. type: Boolean,
  74. default: false
  75. }
  76. },
  77. emits:['update:modelValue','input','focus','blur','change','clear'],
  78. setup(props,{ emit }) {
  79. const passwordVisible = ref(false)
  80. const inputRef = ref(null)
  81. const slot = useSlots()
  82. const styleClass = computed(() => {
  83. return {
  84. 'm-input-disabled': props.disabled,
  85. 'm-input-leftIcon': props.leftIcon,
  86. 'm-input-rightIcon': props.rightIcon,
  87. [`m-input--${props.size}`]: props.size
  88. }
  89. })
  90. const groupClass = computed(() => {
  91. return {
  92. 'm-input-group__prepend': slot.prepend,
  93. 'm-input-group__append': slot.append,
  94. }
  95. })
  96. const iptChange = (e) => {
  97. emit('update:modelValue',e.target.value)
  98. emit('input',e.target.value)
  99. }
  100. const clear = () => {
  101. emit('update:modelValue', '')
  102. emit('clear')
  103. }
  104. // 显示隐藏密码
  105. const showPwd = () => {
  106. passwordVisible.value = !passwordVisible.value
  107. }
  108. const handleFocus = (e) => {
  109. emit('focus',e)
  110. }
  111. const handleBlur = (e) => {
  112. emit('blur', e)
  113. }
  114. const handleChange = (e) => {
  115. emit('change',e)
  116. }
  117. return {
  118. slot,
  119. styleClass,
  120. groupClass,
  121. iptChange,
  122. clear,
  123. passwordVisible,
  124. showPwd,
  125. handleFocus,
  126. handleBlur,
  127. handleChange
  128. }
  129. }
  130. }
  131. </script>
  132. <style lang="scss" scoped>
  133. @import '../../styles/components/input.scss';
  134. </style>