-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathtextarea.tsx
70 lines (66 loc) · 2.49 KB
/
textarea.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { defineComponent, inject, nextTick, onMounted, SetupContext, shallowRef, toRefs, watch } from 'vue';
import { FORM_ITEM_TOKEN, FormItemContext } from '../../form';
import { textareaProps, TextareaProps } from './textarea-types';
import { useNamespace } from '@devui/shared/utils';
import { useTextareaRender } from './composables/use-textarea-render';
import { useTextareaEvent } from './composables/use-textarea-event';
import { useTextareaAutosize } from './composables/use-textarea-autosize';
import './textarea.scss';
export default defineComponent({
name: 'DTextarea',
inheritAttrs: false,
props: textareaProps,
emits: ['update:modelValue', 'update', 'focus', 'blur', 'change', 'keydown'],
setup(props: TextareaProps, ctx: SetupContext) {
const { modelValue } = toRefs(props);
const formItemContext = inject(FORM_ITEM_TOKEN, undefined) as FormItemContext;
const textarea = shallowRef<HTMLTextAreaElement>();
const ns = useNamespace('textarea');
const { isFocus, textareaDisabled, wrapClasses } = useTextareaRender(props);
const { onFocus, onBlur, onInput, onChange, onKeydown, onCompositionStart, onCompositionUpdate, onCompositionEnd } = useTextareaEvent(
isFocus,
props,
ctx
);
const { textareaStyle, updateTextareaStyle } = useTextareaAutosize(props, textarea);
watch(
() => props.modelValue,
() => {
if (props.validateEvent) {
formItemContext?.validate('change').catch((err) => console.warn(err));
}
nextTick(() => updateTextareaStyle());
}
);
onMounted(() => {
updateTextareaStyle();
});
return () => (
<div style="width: 100%">
<textarea
ref={textarea}
{...ctx.attrs}
value={modelValue.value}
autofocus={props.autofocus}
placeholder={props.placeholder}
disabled={textareaDisabled.value}
style={textareaStyle.value}
class={wrapClasses.value}
onCompositionstart={onCompositionStart}
onCompositionupdate={onCompositionUpdate}
onCompositionend={onCompositionEnd}
onInput={onInput}
onFocus={onFocus}
onBlur={onBlur}
onChange={onChange}
onKeydown={onKeydown}></textarea>
{props.showCount && (
<div class={ns.e('show-count')}>
{modelValue.value.length}
{!(ctx.attrs.maxlength ?? false) ? '' : ' / ' + ctx.attrs.maxlength}
</div>
)}
</div>
);
},
});