text-input.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <div class="input">
  3. <label>
  4. <span class="label"><slot></slot></span>
  5. <input type="text"
  6. :placeholder="placeholder"
  7. :class="{error: error}"
  8. v-bind:value="value"
  9. v-on:input="input"
  10. v-on:change="change"
  11. v-on:keypress="keypress">
  12. </label>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. name: "text-input",
  18. props: ['value', 'placeholder', 'error'],
  19. methods: {
  20. clear: function () {
  21. this.$emit('clearError', null);
  22. },
  23. input: function (event) {
  24. this.clear();
  25. this.$emit('input', event.target.value);
  26. },
  27. change: function (event) {
  28. this.clear();
  29. this.$emit('change', event.target.value);
  30. },
  31. keypress: function (event) {
  32. if (event.keyCode !== 13 && event.keyCode !== 10)
  33. return;
  34. this.clear();
  35. this.$emit('enter', event.target.value)
  36. }
  37. }
  38. }
  39. </script>
  40. <style scoped>
  41. .label {
  42. font-size: 80%;
  43. }
  44. input {
  45. width: 100%;
  46. margin-top: 0.2rem;
  47. }
  48. input.error {
  49. background-color: rgba(255, 0, 0, 0.3);
  50. border-color: var(--error);
  51. }
  52. </style>