-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathv-textarea.vue
89 lines (86 loc) · 2.46 KB
/
v-textarea.vue
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
<template>
<div class="vmc-text-area" :class="{invalid: !valid}">
<div :class="{'vmc-1px': border}">
<textarea :rows="coerce.rows" :placeholder="placeholder" v-model="localValue" @input="_onInput()"></textarea>
</div>
<div class="vmc-textarea-number">{{length}}/{{max}}</div>
</div>
</template>
<script type="es6">
export default {
props: {
value: String,
placeholder: String,
min: {
type: [Number, String],
default: 0
},
max: {
type: [Number, String],
default: 99999
},
rows: {
type: [Number, String],
default: 5
},
border: {
type: Boolean,
default: true
}
},
data() {
return {
valid: true,
localValue: this.value
}
},
methods: {
_onInput() {
var len = this.length;
if (!isNaN(this.coerce.max) && len > this.coerce.max) {
this.localValue = this.localValue.substr(0, this.coerce.max);
}
this._checkValue();
},
_checkValue() {
var len = this.length;
if (!isNaN(this.coerce.min) && len < this.coerce.min) {
return this.valid = false;
}
this.valid = true;
}
},
computed: {
length() {
var value = this.localValue;
if (value === undefined || value === null) return 0;
return String(value).length;
},
coerce: {
get() {
return {
min: parseInt(this.min),
max: parseInt(this.max),
rows: parseInt(this.rows)
}
}
}
},
mounted() {
this._checkValue();
},
watch: {
value(value) {
if (value !== this.localValue) {
this.localValue = value;
}
},
localValue(value) {
this.$emit('input', value);
},
valid(value) {
this.$emit('on-valid', value);
}
}
}
</script>