-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdataStoringWrongWay.js
80 lines (69 loc) · 2.3 KB
/
dataStoringWrongWay.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
import { LightningElement, api } from "lwc";
import { getFormattedPhone } from "c/dataStoringUtils";
/**
* By given readable phone number returns only numeric symbols
* @param {string} formattedPhone phone with spaces, dashes, brackets
*/
const getNumbersFromFormattedPhone = (formattedPhone) => {
return formattedPhone.replace(/[\s()[\]{}\-+_=*#]/g, "");
};
export default class DataStoringWrongWay extends LightningElement {
isFocus = false;
@api set value(newValue) {
if (this.isFocus) {
this.inputValue = newValue;
} else {
this.inputValue = getFormattedPhone(newValue);
}
}
get value() {
return getNumbersFromFormattedPhone(this.inputValue);
}
get inputValue() {
const input = this.template.querySelector("lightning-input.phone-input");
if (input !== null) {
return input.value;
}
return undefined;
}
set inputValue(newValue) {
const input = this.template.querySelector("lightning-input.phone-input");
if (input !== null) {
input.value = newValue;
}
/*
//ISSUE: The value is not set on initializing - workaround 1
// Set timeout to set the value after the first render process
else setTimeout(() => {
const input = this.template.querySelector("lightning-input.phone-input");
if (input !== null) {
input.value = newValue;
}
}, 0);
// workaround 1 end
*/
/*
//ISSUE: The value is not set on initializing - workaround 2
// store new value in a field and update the input in `renderedCallback`
else this._value = newValue;
}
renderedCallback() {
if (this._value !== undefined) {
const input = this.template.querySelector("lightning-input.phone-input");
if (input !== null) {
input.value = this._value;
this._value = undefined;
}
}
// workaround 2 end
*/
}
handlePhoneFucus() {
this.isFocus = true;
this.inputValue = getNumbersFromFormattedPhone(this.inputValue);
}
handlePhoneBlur() {
this.isFocus = false;
this.inputValue = getFormattedPhone(this.inputValue);
}
}