forked from Tencent/tdesign-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textarea.tsx
199 lines (190 loc) · 5.97 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import Vue from 'vue';
import isFunction from 'lodash/isFunction';
import { prefix } from '../config';
import CLASSNAMES from '../utils/classnames';
import props from './props';
import { TextareaValue } from './type';
import { getPropsApiByEvent, getCharacterLength } from '../utils/helper';
import calcTextareaHeight from './calcTextareaHeight';
import { renderTNodeJSX } from '../utils/render-tnode';
import { ClassName } from '../common';
const name = `${prefix}-textarea`;
const TEXTAREA_TIPS_CLASS = `${prefix}-textarea__tips`;
const TEXTAREA_LIMIT = `${name}__limit`;
function getValidAttrs(obj: object): object {
const newObj = {};
Object.keys(obj).forEach((key) => {
if (typeof obj[key] !== 'undefined') {
newObj[key] = obj[key];
}
});
return newObj;
}
export default Vue.extend({
name: 'TTextarea',
props: {
...props,
},
data() {
return {
formDisabled: undefined,
focused: false,
mouseHover: false,
textareaStyle: {},
};
},
computed: {
tDisabled(): boolean {
return this.formDisabled || this.disabled;
},
textareaClasses(): ClassName {
return [
name,
{
[`${prefix}-is-disabled`]: this.tDisabled,
[`${prefix}-is-readonly`]: this.readonly,
},
];
},
inputAttrs(): Record<string, any> {
return getValidAttrs({
autofocus: this.autofocus,
disabled: this.tDisabled,
readonly: this.readonly,
placeholder: this.placeholder,
maxlength: this.maxlength || undefined,
name: this.name || undefined,
unselectable: this.readonly ? 'on' : 'off',
});
},
characterNumber(): number {
const characterInfo = getCharacterLength(String(this.value || ''));
if (typeof characterInfo === 'object') {
return characterInfo.length;
}
return characterInfo;
},
},
mounted() {
this.adjustTextareaHeight();
},
methods: {
adjustTextareaHeight() {
if (this.autosize === true) {
this.textareaStyle = calcTextareaHeight(this.$refs.refTextareaElem as HTMLTextAreaElement);
} else if (typeof this.autosize === 'object') {
this.textareaStyle = calcTextareaHeight(
this.$refs.refTextareaElem as HTMLTextAreaElement,
this.autosize?.minRows,
this.autosize?.maxRows,
);
} else if (this.$attrs.rows) {
this.textareaStyle = { height: 'auto', minHeight: 'auto' };
}
},
emitEvent(name: string, value: string | number, context: object) {
this.$emit(name, value, context);
const handleName = getPropsApiByEvent(name);
isFunction(this[handleName]) && this[handleName](value, context);
},
focus(): void {
const input = this.$refs.refTextareaElem as HTMLInputElement;
input?.focus();
},
blur(): void {
const input = this.$refs.refTextareaElem as HTMLInputElement;
input?.blur();
},
handleInput(e: any): void {
if (e.isComposing || e.inputType === 'insertCompositionText') return;
this.inputValueChangeHandle(e);
},
onCompositionend(e: InputEvent) {
this.inputValueChangeHandle(e);
},
inputValueChangeHandle(e: InputEvent) {
const { target } = e;
let val = (target as HTMLInputElement).value;
if (this.maxcharacter && this.maxcharacter >= 0) {
const stringInfo = getCharacterLength(val, this.maxcharacter);
val = typeof stringInfo === 'object' && stringInfo.characters;
}
this.$emit('input', val);
this.emitEvent('change', val, { e });
this.$nextTick(() => this.setInputValue(val));
this.adjustTextareaHeight();
},
setInputValue(v: TextareaValue = ''): void {
const textareaElem = this.$refs.refTextareaElem as HTMLInputElement;
const sV = String(v);
if (!textareaElem) {
return;
}
if (textareaElem.value !== sV) {
textareaElem.value = sV;
}
},
emitKeyDown(e: KeyboardEvent) {
if (this.tDisabled) return;
this.emitEvent('keydown', this.value, { e });
},
emitKeyUp(e: KeyboardEvent) {
if (this.tDisabled) return;
this.emitEvent('keyup', this.value, { e });
},
emitKeypress(e: KeyboardEvent) {
if (this.tDisabled) return;
this.emitEvent('keypress', this.value, { e });
},
emitFocus(e: FocusEvent) {
if (this.tDisabled) return;
this.focused = true;
this.emitEvent('focus', this.value, { e });
},
emitBlur(e: FocusEvent) {
this.focused = false;
this.emitEvent('blur', this.value, { e });
},
},
render() {
const inputEvents = getValidAttrs({
focus: this.emitFocus,
blur: this.emitBlur,
keydown: this.emitKeyDown,
keyup: this.emitKeyUp,
keypress: this.emitKeypress,
});
const classes = [
`${name}__inner`,
{
[`${prefix}-is-${this.status}`]: this.status,
[CLASSNAMES.STATUS.disabled]: this.tDisabled,
[CLASSNAMES.STATUS.focused]: this.focused,
[`${prefix}-resize-none`]: typeof this.autosize === 'object',
},
];
const tips = renderTNodeJSX(this, 'tips');
return (
<div class={this.textareaClasses}>
<textarea
onInput={this.handleInput}
onCompositionend={this.onCompositionend}
{...{ attrs: { ...this.$attrs, ...this.inputAttrs }, on: inputEvents }}
value={this.value}
class={classes}
style={this.textareaStyle}
ref="refTextareaElem"
></textarea>
{this.maxcharacter ? (
<span class={`${name}__limit`}>{`${this.characterNumber}/${this.maxcharacter}`}</span>
) : null}
{!this.maxcharacter && this.maxlength ? (
<span class={TEXTAREA_LIMIT}>{`${this.value ? String(this.value)?.length : 0}/${this.maxlength}`}</span>
) : null}
{tips && (
<div class={`${TEXTAREA_TIPS_CLASS} ${prefix}-textarea__tips--${this.status || 'normal'}`}>{tips}</div>
)}
</div>
);
},
});