TextInputWithCount.vue 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <template>
  2. <div>
  3. <input
  4. :maxLength="maxLength"
  5. @input="emitInputValue"
  6. @blur="v.$touch()"
  7. type="text"
  8. placeholder="e.g. Amazing Course in Flutter!"
  9. class="input is-large">
  10. <span class="icon is-small is-right">
  11. {{remainingLength}}
  12. </span>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. currentValue: ''
  20. }
  21. },
  22. props: {
  23. maxLength: {
  24. type: Number,
  25. default: 50,
  26. required: false
  27. },
  28. v: {
  29. type: Object,
  30. required: true,
  31. }
  32. },
  33. computed : {
  34. inputLength() {
  35. return this.currentValue.length || 0
  36. },
  37. remainingLength(){
  38. if (this.inputLength > 0 && this.inputLength < this.maxLength) {
  39. return this.maxLength - this.inputLength
  40. } else if (this.inputLength === 0) {
  41. return this.maxLength
  42. } else {
  43. return 0
  44. }
  45. }
  46. },
  47. methods : {
  48. emitInputValue($event) {
  49. this.currentValue = $event.target.value
  50. this.$emit('input', this.currentValue)
  51. }
  52. }
  53. }
  54. </script>