import { defineComponent, onBeforeUpdate, onMounted, type PropType, ref, } from 'vue'; import InputWrapper from './_input'; const component = defineComponent({ props: { class: { type: String, default: '', }, modelValue: String, value: { type: String, default: '', }, placeholder: { type: String, default: '', }, label: { type: String, default: '', }, invalidText: { type: String, default: '', }, helperText: { type: String, default: '', }, disabled: { type: Boolean, default: false, }, minHeight: { type: Number, default: 44, }, propPath: String, format: Function as PropType<(value: string) => string>, }, emits: { input: (_: string) => { return true; }, 'update:modelValue': (_?: string) => { return true; }, }, setup(props, ctx) { const height = ref(props.minHeight); const textareaRef = ref(null); const initialValue = props.value ? props.value : props.modelValue ? props.modelValue : ''; const logic = { inputHandler(event: Event) { const element = event.target as HTMLTextAreaElement; if (!element) { return; } if (props.format) { element.value = props.format(element.value); } ctx.emit('update:modelValue', element.value); ctx.emit('input', element.value); }, handleHeight(event: Event) { const element = event.target as HTMLInputElement; if (!element) { return; } if (element.value.length > 40 || element.value.includes('\n')) { height.value = 22.5 * 13 + 12; } else { height.value = 44; } }, findDraggableParent(el: HTMLElement): HTMLElement | null { if (el.id === 'widget_wrapper') { return el; } const parent = el.parentNode as HTMLElement; if (parent) { return logic.findDraggableParent(parent); } return null; }, }; onMounted(() => { if (textareaRef.value) { textareaRef.value.value = initialValue; } logic.handleHeight({ target: textareaRef.value } as never); }); onBeforeUpdate(() => { if (textareaRef.value) { if (props.value !== textareaRef.value.value) { textareaRef.value.value = props.value; logic.handleHeight({ target: textareaRef.value } as never); } } }); return () => { return (