-
Notifications
You must be signed in to change notification settings - Fork 1
/
dw-textarea.js
513 lines (449 loc) · 12.7 KB
/
dw-textarea.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import { LitElement, html, css } from '@dreamworld/pwa-helpers/lit.js';
import { styleMap } from 'lit/directives/style-map.js';
/**
* Behaviors:
* - Provides way to undecorate textarea.
* - When minimum height is provided, it ocuupies that provided mminum height.
* - When maximum height is provided & content is larger than it, it produces scroll of itself.
* - When nothing provided by integrator (minimum / maximum height), It autogrows based on content.
* - Note: To apply autogrow feature, hack is applied here.
* - It contains one dummy text field to calculate height of content.
* - Why this hack needed?
* - To auto grow & auto shrink height of textarea based on it's content, there is no way to calculate content's height into textarea.
* So to achieve this, We have attached 1 dummy textarea with single line & set it's value. From that dummy textarea,
* We can compute it's scrollHeight & set height of actaul visible textarea to that scrollHeight.
*
*
* Usage pattern:
* - `<dw-textarea undecorated .minHeight=${56} .maxHeight=${128} ></dw-textarea>`
*/
export class DwTextarea extends LitElement {
static get styles() {
return [
css`
:host {
display: block;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
color: var(--mdc-theme-text-primary);
position: relative;
}
:host[hidden] {
display: none;
}
:host([disabled]),
:host([readOnly]) {
pointer-events: none;
}
:host([disabled]) {
opacity: 0.3;
}
textarea {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
display: block;
resize: none;
outline: none;
background-color: transparent;
color: inherit;
font-family: inherit;
font-size: inherit;
font-weight: inherit;
letter-spacing: inherit;
line-height: inherit;
padding: var(--dw-textarea-padding, 0px);
width: 100%;
border: 1px solid var(--divider-color);
border-radius: 2px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
}
textarea:focus {
outline: none;
}
:host([showPlaceholderOnFocusOnly]) textarea:not(:focus)::placeholder {
color: transparent;
}
::-webkit-input-placeholder {
/* Edge */
color: var(--mdc-theme-text-hint-on-background, rgba(0, 0, 0, 0.38));
}
:-ms-input-placeholder {
/* Internet Explorer 10-11 */
color: var(--mdc-theme-text-hint-on-background, rgba(0, 0, 0, 0.38));
}
::placeholder {
color: var(--mdc-theme-text-hint-on-background, rgba(0, 0, 0, 0.38));
}
:host(:not([undecorated])) textarea:focus {
border: 1px solid var(--mdc-theme-secondary);
}
:host([undecorated]),
:host([undecorated]) textarea {
border: none;
}
#dummy-textarea {
position: absolute;
top: -99999px;
height: auto;
visibility: hidden;
}
`,
];
}
static get properties() {
return {
/**
* Represent textarea value.
*/
value: { type: String },
/**
* Minimum height of textarea
*/
minHeight: { type: Number },
/**
* Max height of textarea. After that vertical scroll is available.
*/
maxHeight: { type: Number },
/**
* Maximum length of textarea
*/
maxLength: { type: Number },
/**
* Mininum number of characters.
*/
minLength: { type: Number },
/**
* Set to true to make input field readonly.
*/
readOnly: { type: Boolean, reflect: true },
/**
* Set to true to make input field disabled.
*/
disabled: { type: Boolean, reflect: true },
/**
* Set to `true` to mark the input as required.
*/
required: { type: Boolean },
/**
* A placeholder for textarea.
*/
placeholder: { type: String },
/**
* Disabled enter in input.
*/
disabledEnter: { type: Boolean },
/**
* When it's `true`. It doesn not show border.
*/
undecorated: { type: Boolean, reflect: true },
showPlaceholderOnFocusOnly: { type: Boolean, reflect: true },
};
}
/**
* Getter of `value` property.
*/
get value() {
return this._value;
}
/**
* Setter of `value` property.
*/
set value(value) {
let oldValue = this._value;
if (oldValue == value) {
return;
}
this._value = value;
this.requestUpdate('value', oldValue);
if (this._textarea) {
this._textarea.value = this._value;
}
this.updateComplete.then(() => {
this._resize();
});
}
get _dummyTextarea() {
return this.renderRoot.querySelector('#dummy-textarea');
}
get _textarea() {
return this.renderRoot && this.renderRoot.querySelector('#textarea');
}
render() {
return html`<textarea
id="textarea"
rows="1"
style=${styleMap(this._textareaStyle())}
.value="${this.value}"
.maxLength="${this.maxLength}"
.minLength="${this.minLength}"
.name="${this.name}"
?disabled="${this.disabled}"
?required="${this.required}"
?readonly="${this.readOnly}"
.placeholder="${this.placeholder}"
@input="${this._onInput}"
@change="${this._onChange}"
@blur="${this._onInputBlur}"
@cut="${this._resize}"
@paste="${this._onPaste}"
@keypress="${this._onKeyPress}"
@keydown="${this._onKeyDown}"
></textarea>
<textarea .value="${this.value}" rows="1" disabled id="dummy-textarea"></textarea> `;
}
constructor() {
super();
this.name = '';
this.value = '';
this.disabled = false;
this.required = false;
this.placeholder = '';
this.readOnly = false;
this.minHeight = 42;
this.maxLength = 524288;
this.minLength = 0;
}
firstUpdated(changeProps) {
super.firstUpdated && super.firstUpdated(changeProps);
this._resize();
}
/**
* Call this to set focus in the input.
* @public
*/
focus() {
this.moveToEnd();
}
/**
* Set focus to end of text.
* This method is just for backward compatibility.
* @protected
*/
focusToEnd() {
this.moveToEnd();
}
/**
* Move focus to end of text & resize textarea
* @protected
*/
moveToEnd() {
this.updateComplete.then(() => {
if (!this._textarea) {
console.warn('dw-textarea: "textarea" element not found. Somehow "moveToEnd" method is triggered after "disconnectedCallback"');
return;
}
if (typeof this._textarea.selectionStart == 'number') {
this._textarea.selectionStart = this._textarea.selectionEnd = this._textarea.value.length;
this._textarea.focus();
} else if (typeof this._textarea.createTextRange != 'undefined') {
this._textarea.focus();
let range = this._textarea.createTextRange();
range.collapse(false);
range.select();
} else {
console.error('dw-textarea: Error while moving cursor to end.');
}
this._resize();
});
}
/**
* Move focus to start of text & resize textarea
* @protected
*/
moveToStart() {
this.updateComplete.then(() => {
if (!this._textarea) {
console.warn('dw-textarea: "textarea" element not found. Somehow "moveToStart" method is triggered after "disconnectedCallback"');
return;
}
if (typeof this._textarea.selectionStart == 'number') {
this._textarea.selectionStart = this._textarea.selectionEnd = 0;
this._textarea.focus();
} else if (typeof this._textarea.createTextRange != 'undefined') {
this._textarea.focus();
let range = this._textarea.createTextRange();
range.collapse(true);
range.select();
} else {
console.error('dw-textarea: Error while moving cursor to start.');
}
this._resize();
});
}
/**
* Call this to remove focus in the input.
* @public
*/
blur() {
this.updateComplete.then(() => {
if (!this._textarea) {
console.warn('dw-textarea: "textarea" element not found. Somehow "blur" method is triggered after "disconnectedCallback"');
return;
}
this._textarea.blur();
});
}
/**
* Call this to perform validation of the textarea.
* @public
*/
validate() {
return this._textarea.checkValidity();
}
/**
* Returns true if `value` is valid
*/
get validity() {
return this._textarea.validity;
}
/**
* Checkes input validity
*/
checkValidity() {
return this._textarea.checkValidity();
}
setCustomValidity(msg = '') {
this._textarea?.setCustomValidity(msg);
}
/**
* Resize input based on content.
* @protected
*/
_resize() {
if (!this._textarea || !this._dummyTextarea || this.minHeight === this.maxHeight) {
return;
}
const dummyTextareaScrollHeight = this._dummyTextarea.scrollHeight;
const scrollHeight = dummyTextareaScrollHeight % 2 === 0 ? dummyTextareaScrollHeight : dummyTextareaScrollHeight + 1;
if (scrollHeight < this.minHeight) {
this.style.height = this._textarea.style.height = this.minHeight + 'px';
return;
}
if (this.maxHeight && scrollHeight > this.maxHeight) {
this.style.height = this._textarea.style.height = this.maxHeight + 'px';
return;
}
//Note: When `textarea` has `box-sizing: border-box;` & it's `height` is same as it's `scrollHeight`,
// It produces scroll of it's border height, So including border height into it.
const borderHeight = this.undecorated ? 0 : 2;
this.style.height = this._textarea.style.height = scrollHeight + borderHeight + 'px';
}
/**
* Invoked when any key press on `textarea`.
* @protected
*/
_onKeyPress(e) {
var keyCode = e.keyCode || e.which;
//If enter key and disabled Enter.
if (keyCode === 13 && this.disabledEnter) {
e.preventDefault && e.preventDefault();
return false;
}
}
/**
* Invoked when any key down on `textarea`.
* @protected
*/
_onKeyDown(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
this._onEnterKeyDown(e);
return;
}
if (keyCode === 27) {
this._onEscKeyDown(e);
}
}
/**
* Invoked when `enter` key press on `textarea`.
* @protected
*/
_onEnterKeyDown(e) {
this.dispatchEvent(
new CustomEvent('enter', {
detail: { value: this._textarea.value, event: e },
})
);
if (this.disabledEnter) {
e.preventDefault && e.preventDefault();
return false;
}
}
/**
* Invoked when `esc` key press on `texarea`.
* @protected
*/
_onEscKeyDown(e) {
this.dispatchEvent(
new CustomEvent('esc', {
detail: { value: this._textarea.value, event: e },
})
);
}
/**
* Invoked when user paste text on `texarea`.
* If `disabledEnter` property is true then replace all `enter` key to `space` key.
* @protected
*/
_onPaste() {
this._resize();
if (this.disabledEnter) {
//Paste value is not get on paste value so use timeout.
setTimeout(() => {
const value = (this._textarea && this._textarea.value) || '';
this.value = value.replace(/(\r\n|\n|\r)/gm, ' ');
});
}
}
/**
* @returns {Object} - Text area style based on `minHeight` and `maxHeight` property.
* @protected
*/
_textareaStyle() {
return {
'min-height': this.minHeight + 'px',
'max-height': this.maxHeight + 'px',
};
}
/**
* Invoked when user type in the input.
* Triggers `value-changed` event.
* @protected
*/
_onInput() {
let value = this._textarea.value;
if (value && this.disabledEnter) {
value = value.replace(/(\r\n|\n|\r)/gm, ' ');
}
this.value = value;
this._resize();
this.dispatchEvent(
new CustomEvent('value-changed', {
detail: { value: this.value },
})
);
//NOTE: Don't need to dispatch as it bubbles up.
// this.dispatchEvent(new CustomEvent('input'));
}
_onChange() {
this.dispatchEvent(new CustomEvent('change'));
}
/**
* Invokes on input blur.
* Validates input value.
* Trigger `blur` event.
* @protected
*/
_onInputBlur(e) {
this.dispatchEvent(
new CustomEvent('blur', {
detail: { value: this._textarea.value, event: e },
})
);
this.validate();
}
}
customElements.define('dw-textarea', DwTextarea);