Skip to content

Commit

Permalink
fix(BaseTextarea): improve autogrow and add maxHeight property
Browse files Browse the repository at this point in the history
  • Loading branch information
stafyniaksacha committed Dec 7, 2023
1 parent 95fd474 commit 6610084
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions components/form/BaseTextarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export interface TextareaProps {
*/
autogrow?: boolean
/**
* The maximum height of the textarea when autogrow is enabled.
*/
maxHeight?: number
/**
* A set of classes to apply to the various elements of the textarea.
*/
Expand Down Expand Up @@ -151,6 +156,7 @@ const props = withDefaults(defineProps<TextareaProps>(), {
contrast: 'default',
rows: 4,
error: false,
maxHeight: undefined,
classes: () => ({}),
})
const emits = defineEmits<{
Expand Down Expand Up @@ -202,16 +208,28 @@ function fitSize() {
if (props.autogrow) {
textareaRef.value.style.height = 'auto'
textareaRef.value.style.height = textareaRef.value.scrollHeight + 'px'
textareaRef.value.style.height =
Math.min(
props.maxHeight ?? Number.POSITIVE_INFINITY,
1 + textareaRef.value.scrollHeight,
) + 'px'
}
}
watch(
() => props.modelValue,
[
() => props.modelValue,
() => props.autogrow,
() => props.maxHeight,
textareaRef,
],
async () => {
await nextTick()
fitSize()
},
{
immediate: true,
},
)
defineExpose({
Expand Down

0 comments on commit 6610084

Please sign in to comment.