<template>
  <div class="input">
    <label>
      <span class="label"><slot></slot></span>
      <input type="text"
             :placeholder="placeholder"
             :class="{error: error}"
             v-bind:value="value"
             v-on:input="input"
             v-on:change="change"
             v-on:keypress="keypress">
    </label>
  </div>
</template>

<script>
export default {
  name: "text-input",
  props: ['value', 'placeholder', 'error'],
  methods: {
    clear: function () {
      this.$emit('clearError', null);
    },
    input: function (event) {
      this.clear();
      this.$emit('input', event.target.value);
    },
    change: function (event) {
      this.clear();
      this.$emit('change', event.target.value);
    },
    keypress: function (event) {
      if (event.keyCode !== 13 && event.keyCode !== 10)
        return;

      this.clear();
      this.$emit('enter', event.target.value)
    }
  }
}
</script>

<style scoped>
.label {
  font-size: 80%;
}

input {
  width: 100%;
  margin-top: 0.2rem;
}

input.error {
  background-color: rgba(255, 0, 0, 0.3);
  border-color: var(--error);
}
</style>
