1
1
<template >
2
- <div
3
- :class =" {'auto-height': !rows}"
4
- class =" textarea-wrap"
5
- >
6
- <div
7
- v-if =" !rows"
8
- class =" textarea-shadow c-textarea-input"
9
- >
2
+ <div :class =" {'auto-height': !rows}" class =" c-textarea-input" >
3
+ <div v-if =" !rows" class =" textarea-shadow textarea" >
10
4
{{ value }}
11
5
</div > <!-- 用于撑起文本框自适应高度 -->
12
6
<textarea
16
10
:maxlength =" maxlength"
17
11
:readonly =" readonly"
18
12
:rows =" rows ? rows : 1"
19
- class =" c- textarea-input "
20
- @input =" $emit('input', $event.target.value) "
13
+ class =" textarea"
14
+ @input =" onInput "
21
15
/>
22
- <span
23
- v-show =" maxlength && value.length > warnlength"
24
- class =" word-length"
25
- >
16
+ <span v-show =" maxlength && value.length > warnlength" class =" word-length" >
26
17
{{ value.length }} / {{ maxlength }}
27
18
</span >
28
19
</div >
@@ -39,39 +30,48 @@ export default {
39
30
placeholder: { type: String , default: ' ' },
40
31
rows: { type: [Number , String ], default: 0 },
41
32
},
33
+ methods: {
34
+ onInput ($event ) {
35
+ let content = $event .target .value
36
+ // 解决 emoji 字符被用于 maxlength 时被算作1个字符的问题(而'😫'.length => 2)
37
+ this .maxlength && (content = content .substr (0 , Number (this .maxlength )))
38
+ this .$emit (' input' , content)
39
+ },
40
+ },
42
41
}
43
42
</script >
44
43
45
44
<style lang="less" scoped>
46
- .textarea-wrap {
45
+ .c- textarea-input {
47
46
position : relative ;
48
47
width : 100% ;
48
+ margin-bottom : 40px ;
49
49
height : calc (~ " 100% + 40px" );
50
- padding-right : 20px ;
51
50
align-self : flex-start ;
52
51
53
- .textarea-shadow {
54
- opacity : 0 ;
52
+ .textarea {
53
+ width : 100 % ;
55
54
min-height : 100px ;
56
55
word-wrap : break-word ;
57
56
word-break : break-all ;
57
+ font-size : inherit ;
58
+ text-align : inherit ;
59
+ line-height : inherit ;
60
+ }
61
+
62
+ .textarea-shadow {
63
+ opacity : 0 ;
58
64
}
59
65
60
66
textarea {
61
67
position : absolute ;
62
68
top : 0 ;
63
69
display : block ;
64
- font-size : 30px ;
65
- padding-bottom : 28px ;
66
- width : calc (~ " 100% - 20px" );
67
70
height : 100% ;
68
71
overflow : hidden ;
69
72
resize : none ;
70
73
outline : none ;
71
- min-height : 100px ;
72
74
background-color : transparent ;
73
- word-wrap : break-word ;
74
- word-break : break-all ;
75
75
76
76
& [readonly] {
77
77
color : #999 ;
@@ -80,6 +80,9 @@ export default {
80
80
}
81
81
82
82
.word-length {
83
+ position : absolute ;
84
+ bottom : -34px ;
85
+ right : 10px ;
83
86
display : block ;
84
87
font-size : 22px ;
85
88
text-align : right ;
0 commit comments