Skip to content

Commit

Permalink
refactor(textarea): autosize 样式计算逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinHsu committed Dec 8, 2023
1 parent 7f0e26f commit cb65b1a
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/textarea/textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default defineComponent({
const textareaLength = ref(0);
const { value, modelValue } = toRefs(props);
const [innerValue, setInnerValue] = useVModel(value, modelValue, props.defaultValue, props.onChange);
const propsAutosize = computed(() => props.autosize);
const textareaClass = computed(() => [
`${componentName}`,
{
Expand Down Expand Up @@ -85,6 +84,8 @@ export default defineComponent({
const adjustTextareaHeight = () => {
if (props.autosize === true) {
textareaStyle.value = calcTextareaHeight(textareaRef.value as HTMLTextAreaElement);
} else if (props.autosize === false) {
textareaStyle.value = calcTextareaHeight(textareaRef.value as HTMLTextAreaElement, 1, 1);
} else if (typeof props.autosize === 'object') {
const { minRows, maxRows } = props.autosize;
textareaStyle.value = calcTextareaHeight(textareaRef.value as HTMLTextAreaElement, minRows, maxRows);
Expand Down Expand Up @@ -144,18 +145,12 @@ export default defineComponent({
adjustTextareaHeight();
});
});
watch(propsAutosize, (value) => {
switch (value) {
case true:
textareaStyle.value = calcTextareaHeight(textareaRef.value as HTMLTextAreaElement);
break;
case false:
textareaStyle.value = calcTextareaHeight(textareaRef.value as HTMLTextAreaElement, 1, 1);
break;
default:
break;
}
});
watch(
() => props.autosize,
() => {
adjustTextareaHeight();
},
);
return {
componentName,
...toRefs(props),
Expand Down

0 comments on commit cb65b1a

Please sign in to comment.