all files / src/input/ input.vue

94.44% Statements 17/18
100% Branches 8/8
90.91% Functions 10/11
94.44% Lines 17/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                                                                                          36×                 20×             21×                 21×                                                                                                                                                                    
<template>
  <div class="am-input-wrapper" @mouseenter="hovering=true" @mouseleave="hovering=false" ref="gInput">
    <template v-if="type=='textarea'">
      <textarea :rows="rows" :value="nativeValue" :placeholder="placeholder" :disabled="disabled" :readonly="readonly" ref="textarea" @input="onInput" @focus="onFocus" @change="onChange" @blur="onBlur"></textarea>
    </template>
    <template v-else>
      <am-icon class="icon prefix" :name="prefix" v-if="prefix"></am-icon>
      <input type="text" class="inputbox" :value="nativeValue" :placeholder="placeholder" :disabled="disabled" :readonly="readonly" ref="input" @input="onInput" @keyup.enter="onKeyup" @focus="onFocus" @change="onChange" @blur="onBlur">
      <am-icon class="icon suffix" :name="suffix" v-if="suffix"></am-icon>
      <am-icon class="icon suffix clear" name="delete" v-if="visibleClear" @click.stop="onClear"></am-icon>
    </template>
  </div>
</template>
 
<script>
import AmIcon from "../icon";
export default {
  name: "amInput",
  components: {
    AmIcon
  },
  props: {
    value: {
      type: [String, Number]
    },
    disabled: {
      type: Boolean,
      default: false
    },
    clearable: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: "请输入内容"
    },
    prefix: {
      type: String,
      default: ""
    },
    suffix: {
      type: String,
      default: ""
    },
    type: {
      type: String,
      default: "text",
      validator(val) {
        return ["text", "textarea"].indexOf(val) > -1;
      }
    },
    rows: {
      type: Number,
      default: 2
    }
  },
  data() {
    return {
      focused: false,
      hovering: false
    };
  },
  computed: {
    visibleClear() {
      return (
        this.clearable &&
        !this.disabled &&
        !this.readonly &&
        !!this.nativeValue &&
        (this.hovering || this.focused)
      );
    },
    nativeValue() {
      return !this.value ? "" : this.value;
    }
  },
  methods: {
    setRawValue(value) {
      this.$refs.input.value = value;
    },
    onInput(event) {
      this.focused = true;
      this.$emit("input", event.target.value);
    },
    onKeyup(event) {
      this.focused = false;
      this.$emit("keyup-enter", event);
    },
    onChange(event) {
      this.focused = true;
      this.$emit("change", event);
    },
    onBlur(event) {
      this.focused = false;
      this.$emit("blur", event);
    },
    onFocus(event) {
      this.focused = true;
      this.$emit("focus", event);
    },
    onClear() {
      this.$emit("input", "");
      this.$emit("change", "");
      this.$emit("clear");
    }
  }
};
</script>
 
<style lang="scss" scoped>
@import "var";
.am-input-wrapper {
  display: inline-flex;
  align-items: center;
  font-size: $input-font-size;
  width: 100%;
  position: relative;
  .prefix,
  .suffix {
    position: absolute;
    top: 50%;
    z-index: 2;
    color: #bbb;
    fill: #bbb;
    line-height: 0;
    transform: translateY(-50%);
  }
  .prefix {
    left: 0.8em;
  }
  .suffix {
    right: 0.3em;
  }
  > .inputbox:not(:first-child) {
    padding-left: 30px;
  }
  > .inputbox:not(:last-child) {
    padding-right: 22px;
  }
  > textarea,
  > .inputbox {
    width: 100%;
    padding: 2px 8px;
    line-height: 28px;
    font-size: inherit;
    border: 1px solid $input-border-color;
    border-radius: $border-radius;
    &:hover {
      border-color: $input-border-color-hover;
    }
    &:focus {
      outline: none;
      box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
    }
    &[disabled],
    &[readonly] {
      color: #bbb;
      background: #fff;
      border-color: #bbb;
      cursor: not-allowed;
    }
  }
  > textarea {
    height: auto;
    line-height: 1.5;
  }
}
</style>