Files
Quick-Ling/app/components/TextInput.vue
T

121 lines
2.6 KiB
Vue

<script setup lang="ts">
interface Props {
label?: string;
modelValue: string;
placeholder?: string;
disabled?: boolean;
minRows?: number;
maxRows?: number;
}
const props = withDefaults(defineProps<Props>(), {
placeholder: "Enter text to translate...",
disabled: false,
minRows: 3,
maxRows: 12,
});
const emit = defineEmits<{
(e: "update:modelValue", value: string): void;
}>();
const textareaRef = ref<HTMLTextAreaElement | null>(null);
function autoResize() {
const textarea = textareaRef.value;
if (!textarea) return;
textarea.style.height = "auto";
textarea.style.height = `${textarea.scrollHeight}px`;
const style = getComputedStyle(textarea);
const lineHeight = parseFloat(style.lineHeight) || 24;
const minRows = props.minRows * lineHeight;
const maxRows = props.maxRows * lineHeight;
const currentHeight = textarea.scrollHeight;
if (currentHeight < minRows) {
textarea.style.height = `${minRows}px`;
} else if (currentHeight > maxRows) {
textarea.style.height = `${maxRows}px`;
textarea.style.overflowY = "auto";
} else {
textarea.style.overflowY = "hidden";
}
}
function handleInput(event: Event) {
const target = event.target as HTMLTextAreaElement;
emit("update:modelValue", target.value);
autoResize();
}
const charCount = computed(() => props.modelValue.length);
const wordCount = computed(() => {
const text = props.modelValue.trim();
return text ? text.split(/\s+/).length : 0;
});
watch(
() => props.modelValue,
() => {
nextTick(autoResize);
},
);
</script>
<template>
<div class="text-input">
<label v-if="props.label" class="text-input-label">
{{ props.label }}
</label>
<textarea
ref="textareaRef"
class="text-input-area textarea"
:value="props.modelValue"
:placeholder="props.placeholder"
:disabled="props.disabled"
:rows="minRows"
@input="handleInput"
/>
<div class="text-input-stats">
<span class="stat">{{ wordCount }} words</span>
<span class="stat">{{ charCount }} characters</span>
</div>
</div>
</template>
<style scoped>
.text-input {
display: flex;
flex-direction: column;
flex: 1;
}
.text-input-label {
margin-bottom: 0.25rem;
font-size: 0.875rem;
font-weight: 500;
}
.text-input-area {
min-height: calc(24px * 3);
font-size: 1rem;
overflow-y: auto;
resize: none;
}
.text-input-stats {
display: flex;
gap: 1rem;
margin-top: 0.5rem;
font-size: 0.75rem;
color: var(--muted-color);
}
.stat {
background: var(--stat-bg-color);
padding: 0.25rem 0.5rem;
border-radius: 4px;
}
</style>