index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <u-input
  3. :key="inpType"
  4. v-bind="__attrs"
  5. v-on="__listeners"
  6. ref="uInput"
  7. >
  8. <!-- <template :slot="name" v-for="(_, name) in $scopedSlots">
  9. <slot :name="name"></slot>
  10. </template>-->
  11. <template slot="prefix">
  12. <slot name="prefix"></slot>
  13. </template>
  14. <template slot="suffix">
  15. <slot name="suffix"></slot>
  16. </template>
  17. <template v-if="showPassword" slot="suffix">
  18. <u-icon
  19. v-show="inpType === 'password'"
  20. @click="toggleType('text')"
  21. name="eye-off"
  22. size="20"
  23. ></u-icon>
  24. <u-icon
  25. v-show="inpType === 'text'"
  26. @click="toggleType('password')"
  27. name="eye-fill"
  28. size="20"
  29. ></u-icon>
  30. </template>
  31. </u-input>
  32. </template>
  33. <script>
  34. import uProps from 'uview-ui/components/u-input/props';
  35. const customProps = {
  36. // 自定义props
  37. showPassword: Boolean, // 是否显示切换密码图标
  38. };
  39. export default {
  40. name: 'cInput',
  41. model: {
  42. prop: 'value',
  43. event: 'input',
  44. },
  45. props: {
  46. ...uProps.props,
  47. ...customProps,
  48. },
  49. data() {
  50. return {
  51. inpType: this.$props.type,
  52. };
  53. },
  54. watch: {
  55. type: {
  56. handler(val) {
  57. this.inpType = val;
  58. },
  59. immediate: true,
  60. },
  61. },
  62. computed: {
  63. __attrs() {
  64. return {
  65. ...this.$props,
  66. ...this.$attrs,
  67. type: this.inpType,
  68. placeholder: this.$props.placeholder || '请输入',
  69. };
  70. },
  71. __listeners() {
  72. return {
  73. ...this.$listeners,
  74. input: this.onInput,
  75. blur: this.onBlur,
  76. };
  77. },
  78. },
  79. methods: {
  80. //去掉汉字
  81. removeChinese(strValue) {
  82. if (strValue) {
  83. const reg = /[\u4e00-\u9fa5]/g;
  84. return strValue.replace(reg, '');
  85. }
  86. return '';
  87. },
  88. onInput(val) {
  89. const reg = /[\u4E00-\u9FA5]+/;
  90. if (this.showPassword && reg.test(val)) {
  91. const newVal = this.removeChinese(val);
  92. this.$refs.uInput.innerValue = newVal;
  93. return;
  94. }
  95. this.$emit('input', val);
  96. },
  97. toggleType(type) {
  98. this.inpType = type;
  99. },
  100. onBlur(val = '') {
  101. const modelValue = val.trim();
  102. this.$emit('input', modelValue);
  103. this.$emit('blur', modelValue);
  104. },
  105. },
  106. };
  107. </script>