Skip to content

Commit

Permalink
fix(Textarea): resolved indicator error (#902)
Browse files Browse the repository at this point in the history
* fix(Textarea): resolved indicator error

* test: update unit case
  • Loading branch information
anlyyao authored Jun 27, 2023
1 parent 4bb8b44 commit 505f555
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
6 changes: 4 additions & 2 deletions src/textarea/__test__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ describe('Textarea.vue', () => {
const wrapper = mount(
<Textarea label="标题" v-model={value.value} maxcharacter={maxcharacter} onChange={onChange} />,
);
expect(onChange).toBeCalledTimes(1);
const el = wrapper.find('textarea').element;
await simulateEvent(el, '一个汉字等于两个字符,超出会被剪切', 'input');
expect(onChange).toBeCalledTimes(1);
expect(onChange).toBeCalledTimes(2);
expect(onChange).toHaveBeenCalledWith('一');
});

it(': autosize ', async () => {
const value = ref('');
const onChange = vi.fn();
const wrapper = mount(<Textarea label="标题" v-model={value.value} autosize onChange={onChange} />);
expect(onChange).toBeCalledTimes(1);
const el = wrapper.find('textarea').element;
await simulateEvent(el, '这里是一段很长很长很长的长文本,支持自动换行', 'input');
expect(onChange).toBeCalledTimes(1);
expect(onChange).toBeCalledTimes(2);
});

it(': placeholder', async () => {
Expand Down
15 changes: 6 additions & 9 deletions src/textarea/demos/maxcharacter.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<template>
<t-textarea
name="标签文字"
label="标签文字"
placeholder="请输入文字"
:maxcharacter="500"
indicator
:allow-input-over-max="true"
/>
<t-textarea v-model="value" name="标签文字" label="标签文字" placeholder="请输入文字" :maxcharacter="500" indicator />
</template>
<script lang="ts" setup></script>
<script lang="ts" setup>
import { ref } from 'vue';
const value = ref('');
</script>
15 changes: 6 additions & 9 deletions src/textarea/demos/maxlength.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<template>
<t-textarea
name="标签文字"
label="标签文字"
placeholder="请输入文字"
:maxlength="500"
indicator
:allow-input-over-max="true"
/>
<t-textarea v-model="value" name="标签文字" label="标签文字" placeholder="请输入文字" :maxlength="500" indicator />
</template>
<script lang="ts" setup></script>
<script lang="ts" setup>
import { ref } from 'vue';
const value = ref('');
</script>
16 changes: 8 additions & 8 deletions src/textarea/textarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,33 @@ export default defineComponent({
const handleInput = (e: any) => {
if (e.isComposing || e.inputType === 'insertCompositionText') return;
textareaValueChangeHandle(e);
textareaValueChangeHandle();
};
const textareaValueChangeHandle = (e: InputEvent) => {
const { target } = e;
const { value } = target as HTMLInputElement;
const textareaValueChangeHandle = () => {
const textarea = textareaRef.value as HTMLInputElement;
if (
!props.allowInputOverMax &&
props.maxcharacter &&
props.maxcharacter > 0 &&
!Number.isNaN(props.maxcharacter)
) {
const { length = 0, characters = '' } = getCharacterLength(value, props.maxcharacter) as {
const { length = 0, characters = '' } = getCharacterLength(textarea.value, props.maxcharacter) as {
length: number;
characters: string;
};
setInnerValue(characters);
textareaLength.value = length;
} else {
setInnerValue(value);
textareaLength.value = String(innerValue.value).length;
setInnerValue(textarea.value);
textareaLength.value = String(textarea.value).length;
}
nextTick(() => setInputValue(innerValue.value));
adjustTextareaHeight();
};
const handleCompositionend = (e: InputEvent | CompositionEvent) => {
textareaValueChangeHandle(e as InputEvent);
textareaValueChangeHandle();
};
const handleFocus = (e: FocusEvent) => {
Expand All @@ -135,6 +134,7 @@ export default defineComponent({
if (props.autofocus) {
textareaRef.value?.focus();
}
textareaValueChangeHandle();
adjustTextareaHeight();
});
Expand Down

0 comments on commit 505f555

Please sign in to comment.