12
12
v-model =" valueComputed"
13
13
v-bind =" { ...computedProps, ...listeners, ...validationAriaAttributes }"
14
14
:style =" computedStyle"
15
- :rows =" computedRowsCount"
16
15
:loading =" isLoading"
17
16
ref =" textarea"
18
17
:ariaLabel =" $props.label"
25
24
</template >
26
25
27
26
<script lang="ts">
28
- import { computed , defineComponent , CSSProperties , shallowRef } from ' vue'
27
+ import { computed , defineComponent , CSSProperties , shallowRef , ref , watch } from ' vue'
29
28
import pick from ' lodash/pick.js'
30
29
import { VaInputWrapper } from ' ../va-input-wrapper'
31
30
@@ -117,24 +116,53 @@ export default defineComponent({
117
116
const isResizable = computed (() => {
118
117
return props .resize && ! props .autosize
119
118
})
119
+ const textareaHeight = ref (' auto' )
120
120
121
- const computedRowsCount = computed <number | undefined >(() => {
122
- if (! props .autosize ) {
123
- return undefined
121
+ const updateTextareaHeight = () => {
122
+ if (! textarea .value ) {
123
+ return false
124
+ }
125
+ console .log (' updateTextareaHeight' )
126
+ textarea .value .style .height = ' 0'
127
+ const scrollHeight = textarea .value ?.scrollHeight
128
+ const lineHeight = parseInt (
129
+ window .getComputedStyle (textarea .value )?.lineHeight ,
130
+ 10 ,
131
+ )
132
+ let rows = scrollHeight / lineHeight
133
+
134
+ console .log (' scrollHeight' , scrollHeight )
135
+ console .log (' lineHeight' , lineHeight )
136
+ console .log (' rows' , rows )
137
+ console .log (' Rounded Rows = ' , Math .round (scrollHeight / lineHeight ))
138
+
139
+ if (props .maxRows ) {
140
+ rows = Math .max (props .minRows , Math .min (rows , props .maxRows ))
124
141
}
125
142
126
- const rows = valueComputed .value ? valueComputed .value .toString ().split (' \n ' ).length : 1
127
-
128
- if (! props .maxRows ) {
129
- return rows
143
+ if (props .minRows ) {
144
+ rows = Math .max (props .minRows , rows )
130
145
}
131
146
132
- return Math .max (props .minRows , Math .min (rows , props .maxRows ))
133
- })
147
+ const height = rows * lineHeight + ' px'
148
+
149
+ console .log (' height = ' , height )
150
+
151
+ textareaHeight .value = ' ' + height
152
+ }
153
+
154
+ watch ([valueComputed , textarea ], updateTextareaHeight , { immediate: true })
134
155
135
- const computedStyle = computed (() => ({
136
- resize: isResizable .value ? undefined : ' none' ,
137
- }) as CSSProperties )
156
+ const computedStyle = computed (
157
+ () =>
158
+ ({
159
+ resize: isResizable .value ? undefined : ' none' ,
160
+ height:
161
+ props .autosize && textareaHeight .value
162
+ ? textareaHeight .value
163
+ : undefined ,
164
+ } as CSSProperties ),
165
+ )
138
166
139
167
const computedProps = computed (() => ({
140
168
... pick (props , [' disabled' , ' readonly' , ' placeholder' , ' ariaLabel' ]),
@@ -147,13 +175,13 @@ export default defineComponent({
147
175
computedError ,
148
176
computedErrorMessages ,
149
177
isLoading ,
150
- computedRowsCount ,
151
178
valueComputed ,
152
179
vaInputWrapperProps: filterComponentProps (VaInputWrapperProps ),
153
180
textarea ,
154
181
computedStyle ,
155
182
listeners: createListeners (emit ),
156
183
computedProps ,
184
+ textareaHeight ,
157
185
focus ,
158
186
blur ,
159
187
}
0 commit comments