jb-textarea 2.2.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/JBTextarea.ts DELETED
@@ -1,306 +0,0 @@
1
- import HTML from './JBTextarea.html';
2
- import CSS from './JBTextarea.scss';
3
- import { JBTextareaElements, JBTextareaValidationItem, ValidationResult, ValidationResultItem, ValidationResultSummary } from './Types';
4
- export {JBTextareaValidationItem};
5
- export class JBTextareaWebComponent extends HTMLElement {
6
- #value = '';
7
- get value() {
8
- return this.#value;
9
- }
10
- set value(value) {
11
- this.#value = value;
12
- this.#textareaElement.value = value;
13
- if(this.autoHeight){
14
- this.changeHeightToContentSize();
15
- }
16
- }
17
- constructor() {
18
- super();
19
- this.initWebComponent();
20
- this.initProp();
21
- }
22
- #textareaElement!: HTMLTextAreaElement;
23
- #elements!: JBTextareaElements;
24
- initWebComponent() {
25
- const shadowRoot = this.attachShadow({
26
- mode: 'open'
27
- });
28
- const html = `<style>${CSS}</style>` + '\n' + HTML;
29
- const element = document.createElement('template');
30
- element.innerHTML = html;
31
- shadowRoot.appendChild(element.content.cloneNode(true));
32
- this.#textareaElement = shadowRoot.querySelector('.textarea-box textarea')!;
33
- this.#elements = {
34
- textarea: this.#textareaElement,
35
- label: shadowRoot.querySelector('label')!,
36
- labelValue:shadowRoot.querySelector('label .label-value')!,
37
- messageBox:shadowRoot.querySelector('.message-box')!
38
- }
39
- this.registerEventListener();
40
- }
41
- registerEventListener() {
42
- this.#elements.textarea.addEventListener('change', (e) => this.onInputChange(e));
43
- this.#elements.textarea.addEventListener('beforeinput', this.onInputBeforeInput.bind(this));
44
- this.#elements.textarea.addEventListener('input', (e) => this.onInputInput((e as unknown as InputEvent)));
45
- this.#elements.textarea.addEventListener('keypress', this.onInputKeyPress.bind(this));
46
- this.#elements.textarea.addEventListener('keyup', this.onInputKeyup.bind(this));
47
- this.#elements.textarea.addEventListener('keydown', this.onInputKeyDown.bind(this));
48
- }
49
- validationList:JBTextareaValidationItem[] = [];
50
- autoHeight = false;
51
- validation:ValidationResultSummary = {
52
- isValid: null,
53
- message: null
54
- }
55
- initProp() {
56
- this.value = this.getAttribute('value') || '';
57
-
58
- }
59
- static get observedAttributes() {
60
- return ['label', 'message', 'value', 'placeholder'];
61
- }
62
- attributeChangedCallback(name:string, oldValue:string, newValue:string) {
63
- // do something when an attribute has changed
64
- this.onAttributeChange(name, newValue);
65
- }
66
- onAttributeChange(name:string, value:string) {
67
- switch (name) {
68
- case 'label':
69
- this.#elements.labelValue.innerHTML = value;
70
- if(value == null || value == undefined || value == ""){
71
- this.#elements.label.classList.add('--hide');
72
- }else{
73
- this.#elements.label.classList.remove('--hide');
74
- }
75
- break;
76
- case 'message':
77
- this.#elements.messageBox.innerHTML = value;
78
- break;
79
- case 'placeholder':
80
- this.#textareaElement.setAttribute('placeholder', value);
81
- break;
82
- case 'value':
83
- this.value = value;
84
- break;
85
- }
86
-
87
- }
88
- changeHeightToContentSize() {
89
- this.#textareaElement.style.height = "4px";
90
- this.#textareaElement.style.height = (this.#textareaElement.scrollHeight)+"px";
91
- }
92
- onInputBeforeInput(e: InputEvent){
93
- this.#dispatchBeforeInputEvent(e);
94
- }
95
- #dispatchBeforeInputEvent(e: InputEvent): boolean {
96
- const eventInitDict = {
97
- bubbles: e.bubbles,
98
- cancelable: e.cancelable,
99
- composed: e.composed,
100
- data: e.data,
101
- isComposing: e.isComposing,
102
- inputType: e.inputType,
103
- dataTransfer: e.dataTransfer,
104
- view: e.view,
105
- detail: e.detail,
106
- targetRanges: e.getTargetRanges(),
107
- };
108
- const event = new InputEvent('beforeinput', eventInitDict);
109
- this.dispatchEvent(event);
110
- if (event.defaultPrevented) {
111
- e.preventDefault();
112
- }
113
- return event.defaultPrevented;
114
- }
115
- onInputKeyDown(e:KeyboardEvent){
116
- const keyDownnInitObj:KeyboardEventInit = {
117
- key:e.key,
118
- keyCode:e.keyCode,
119
- code:e.code,
120
- ctrlKey:e.ctrlKey,
121
- shiftKey:e.shiftKey,
122
- altKey:e.altKey,
123
- charCode:e.charCode,
124
- which:e.which,
125
- cancelable: true,
126
- location:e.location,
127
- detail:e.detail,
128
- bubbles:e.bubbles,
129
- composed:e.composed,
130
- isComposing:e.isComposing,
131
- metaKey:e.metaKey,
132
- repeat:e.repeat,
133
- view:e.view
134
- };
135
- const event = new KeyboardEvent("keydown",keyDownnInitObj);
136
- const isEventNotPrevented = this.dispatchEvent(event);
137
- if(!isEventNotPrevented){
138
- //if event prevented by e.preventDefault();
139
- e.preventDefault();
140
- }
141
- }
142
- onInputKeyPress(e:KeyboardEvent) {
143
- const keyPressInitObj:KeyboardEventInit = {
144
- key:e.key,
145
- keyCode:e.keyCode,
146
- code:e.code,
147
- ctrlKey:e.ctrlKey,
148
- shiftKey:e.shiftKey,
149
- altKey:e.altKey,
150
- charCode:e.charCode,
151
- which:e.which,
152
- cancelable: true,
153
- location:e.location,
154
- detail:e.detail,
155
- bubbles:e.bubbles,
156
- composed:e.composed,
157
- isComposing:e.isComposing,
158
- metaKey:e.metaKey,
159
- repeat:e.repeat,
160
- view:e.view
161
- };
162
- const event = new KeyboardEvent('keypress',keyPressInitObj);
163
- const isEventNotPrevented = this.dispatchEvent(event);
164
- if(!isEventNotPrevented){
165
- //if event prevented by e.preventDefault();
166
- e.preventDefault();
167
- }
168
- }
169
- onInputInput(e:InputEvent){
170
- if(this.autoHeight){
171
- this.changeHeightToContentSize();
172
- }
173
- this.#triggerInputEvent(e);
174
- }
175
- #triggerInputEvent(e:InputEvent){
176
- const inputInitObject:InputEventInit = {
177
- bubbles:e.bubbles,
178
- cancelable:e.cancelable,
179
- composed:e.composed,
180
- data:e.data,
181
- dataTransfer:e.dataTransfer,
182
- detail:e.detail,
183
- inputType:e.inputType,
184
- isComposing:e.isComposing,
185
- targetRanges:e.getTargetRanges(),
186
- view:e.view
187
- };
188
- const event = new InputEvent("input",inputInitObject);
189
- this.dispatchEvent(event);
190
- }
191
- onInputKeyup(e:KeyboardEvent) {
192
- const inputText = (e.target as HTMLTextAreaElement).value;
193
- //here is the rare time we update _value directly becuase we want trigger event that may read value directly from dom
194
- this.#value = inputText;
195
- this.triggerInputValidation(false);
196
- const keyUpInitObj:KeyboardEventInit = {
197
- key:e.key,
198
- keyCode:e.keyCode,
199
- code:e.code,
200
- ctrlKey:e.ctrlKey,
201
- shiftKey:e.shiftKey,
202
- altKey:e.altKey,
203
- charCode:e.charCode,
204
- which:e.which,
205
- bubbles:e.bubbles,
206
- cancelable:e.cancelable,
207
- composed:e.composed,
208
- detail:e.detail,
209
- isComposing:e.isComposing,
210
- location:e.location,
211
- metaKey:e.metaKey,
212
- repeat:e.repeat,
213
- view:e.view
214
- };
215
- const event = new KeyboardEvent('keyup',keyUpInitObj);
216
- this.dispatchEvent(event);
217
- }
218
- onInputChange(e:Event) {
219
- const inputText = (e.target as HTMLTextAreaElement).value;
220
- this.triggerInputValidation(true);
221
- //here is the rare time we update _value directly becuase we want trigger event that may read value directly from dom
222
- this.#value = inputText;
223
- const validationObject = this.checkInputValidation(inputText);
224
- const event = new CustomEvent('change',{
225
- detail: {
226
- isValid: validationObject.isAllValid,
227
- validationObject:validationObject,
228
- },
229
- });
230
- this.dispatchEvent(event);
231
- }
232
- triggerInputValidation(showError = true) {
233
- // this method is for use out of component for example if user click on submit button and developer want to check if all fields are valid
234
- //takeAction determine if we want to show user error in web component difualtManner or developer will handle it by himself
235
- const inputText = this.#textareaElement.value;
236
- const validationResult = this.checkInputValidation(inputText);
237
- if (showError == true && !validationResult.isAllValid) {
238
- const firstFault = validationResult.validationList.find(x => !x.isValid);
239
- if(firstFault && firstFault.message){
240
- this.showValidationError(firstFault.message);
241
- }
242
- } else if (validationResult.isAllValid) {
243
- this.clearValidationError();
244
- }
245
- return validationResult;
246
- }
247
- checkInputValidation(inputText:string):ValidationResult{
248
- const validationResult:ValidationResult = {
249
- validationList: [],
250
- isAllValid: true
251
- };
252
- this.validationList.forEach((validation:JBTextareaValidationItem) => {
253
- const res = this.checkValidation(inputText, validation);
254
- validationResult.validationList.push(res);
255
- if (!res.isValid) {
256
- validationResult.isAllValid = false;
257
- }
258
- });
259
- return validationResult;
260
- }
261
- checkValidation(text:string, validation:JBTextareaValidationItem) {
262
- let testRes = true;
263
- if(validation.validator instanceof RegExp){
264
- testRes = validation.validator.test(text);
265
- validation.validator.lastIndex = 0;
266
- }
267
- if (typeof validation.validator == "function") {
268
- testRes = validation.validator(text);
269
- }
270
- if (!testRes) {
271
- return {
272
- isValid: false,
273
- message: validation.message,
274
- validation: validation
275
- };
276
- }
277
- return {
278
- isValid: true,
279
- message: '',
280
- validation: validation
281
- };
282
- }
283
- showValidationError(error:string) {
284
- this.validation = {
285
- isValid: false,
286
- message: error
287
- };
288
- this.#elements.messageBox.innerHTML = error;
289
- this.#elements.messageBox.classList.add('error');
290
- }
291
- clearValidationError() {
292
- this.validation = {
293
- isValid: true,
294
- message: null
295
- };
296
- const text = this.getAttribute('message') || '';
297
- this.#elements.messageBox.innerHTML = text;
298
- this.#elements.messageBox.classList.remove('error');
299
- }
300
- }
301
-
302
- const myElementNotExists = !customElements.get('jb-textarea');
303
- if(myElementNotExists){
304
- //prevent duplicate registering
305
- window.customElements.define('jb-textarea', JBTextareaWebComponent);
306
- }
package/lib/Types.ts DELETED
@@ -1,23 +0,0 @@
1
- export type JBTextareaElements = {
2
- textarea: HTMLTextAreaElement;
3
- label: HTMLLabelElement;
4
- labelValue: HTMLSpanElement;
5
- messageBox: HTMLDivElement;
6
- }
7
- export type ValidationResultSummary = {
8
- isValid:boolean | null;
9
- message:string | null;
10
- }
11
- export type ValidationResultItem = {
12
- isValid:boolean | null;
13
- message:string | null;
14
- validation:JBTextareaValidationItem;
15
- }
16
- export type ValidationResult = {
17
- validationList:ValidationResultItem[];
18
- isAllValid:boolean;
19
- }
20
- export type JBTextareaValidationItem = {
21
- validator: RegExp | ((value:string)=>boolean);
22
- message:string;
23
- }
package/lib/global.d.ts DELETED
@@ -1,11 +0,0 @@
1
- type FileStringModule = {
2
- readonly default: string;
3
- }
4
- declare module '*.scss' {
5
- const value: FileStringModule;
6
- export default value;
7
- }
8
- declare module '*.html' {
9
- const value: FileStringModule;
10
- export default value.default
11
- }
File without changes
File without changes