@everymatrix/general-input-hsl 0.25.42 → 0.25.44

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.
@@ -1,10 +1,10 @@
1
1
  import { h } from "@stencil/core";
2
2
  import { translate } from "../../utils/locale.utils";
3
+ import { getFailedRegexRule, getRuleErrorMessage } from "../../utils/utils";
3
4
  import tooltipIcon from "../../utils/tooltipIcon.svg";
4
5
  export class TextInput {
5
6
  constructor() {
6
7
  this.value = '';
7
- this.validationPattern = [];
8
8
  this.duplicateInputValue = null;
9
9
  this.handleInput = (event) => {
10
10
  const inputElement = event.target;
@@ -18,8 +18,7 @@ export class TextInput {
18
18
  clearTimeout(this.debounceTime);
19
19
  }
20
20
  this.debounceTime = setTimeout(() => {
21
- this.isValid = this.setValidity();
22
- this.errorMessage = this.setErrorMessage();
21
+ this.updateValidity();
23
22
  this.validityStateHandler({ valid: this.isValid, name: this.name });
24
23
  this.emitValueHandler(true);
25
24
  }, 500);
@@ -27,8 +26,7 @@ export class TextInput {
27
26
  this.handleBlur = (event) => {
28
27
  this.value = event.target.value;
29
28
  this.touched = true;
30
- this.isValid = this.setValidity();
31
- this.errorMessage = this.setErrorMessage();
29
+ this.updateValidity();
32
30
  this.validityStateHandler({ valid: this.isValid, name: this.name });
33
31
  };
34
32
  this.setClientStyling = () => {
@@ -59,10 +57,11 @@ export class TextInput {
59
57
  if (newValue) {
60
58
  this.value = newValue;
61
59
  requestAnimationFrame(() => {
62
- this.isValid = this.setValidity();
63
- if (!this.isValid && !this.touched) {
60
+ const { isValid, errorMessage } = this.validate();
61
+ this.isValid = isValid;
62
+ if (!isValid && !this.touched) {
64
63
  this.touched = true;
65
- this.errorMessage = this.setErrorMessage();
64
+ this.errorMessage = errorMessage;
66
65
  }
67
66
  });
68
67
  }
@@ -98,15 +97,13 @@ export class TextInput {
98
97
  if (this.isDuplicateInput && this.name === event.detail.name + 'Duplicate') {
99
98
  this.duplicateInputValue = event.detail.value;
100
99
  if (this.touched) {
101
- this.isValid = this.setValidity();
102
- this.errorMessage = this.setErrorMessage();
100
+ this.updateValidity();
103
101
  }
104
102
  }
105
103
  if (this.name === event.detail.name + 'Duplicate'
106
104
  && this.name.replace('Duplicate', '') === event.detail.name
107
105
  && this.touched === true) {
108
- this.isValid = this.setValidity();
109
- this.errorMessage = this.setErrorMessage();
106
+ this.updateValidity();
110
107
  }
111
108
  }
112
109
  handleValidationChange(event) {
@@ -114,15 +111,10 @@ export class TextInput {
114
111
  return;
115
112
  }
116
113
  this.validation = event.detail.validation;
117
- this.validationPattern = this.setPattern();
118
114
  if (this.touched) {
119
- this.isValid = this.setValidity();
120
- this.errorMessage = this.setErrorMessage();
115
+ this.updateValidity();
121
116
  }
122
117
  }
123
- connectedCallback() {
124
- this.validationPattern = this.setPattern();
125
- }
126
118
  componentDidRender() {
127
119
  // start custom styling area
128
120
  if (!this.limitStylingAppends && this.stylingContainer) {
@@ -139,7 +131,7 @@ export class TextInput {
139
131
  this.validation.minLength &&
140
132
  this.inputReference.value.length < this.validation.minLength) {
141
133
  this.touched = true;
142
- this.errorMessage = this.setErrorMessage();
134
+ this.errorMessage = this.validate().errorMessage;
143
135
  }
144
136
  }
145
137
  componentDidLoad() {
@@ -150,54 +142,40 @@ export class TextInput {
150
142
  this.touched = true;
151
143
  }
152
144
  }
153
- this.isValid = this.setValidity();
145
+ this.isValid = this.validate().isValid;
154
146
  }
155
- setValidity() {
156
- if (this.isDuplicateInput && this.duplicateInputValue !== this.value) {
157
- return false;
158
- }
147
+ updateValidity() {
148
+ const { isValid, errorMessage } = this.validate();
149
+ this.isValid = isValid;
150
+ this.errorMessage = errorMessage;
151
+ }
152
+ validate() {
153
+ var _a;
159
154
  if (!this.inputReference) {
160
- return false;
155
+ return { isValid: false, errorMessage: '' };
161
156
  }
162
157
  const inputValue = this.inputReference.value;
163
- const inputIsValid = this.inputReference.validity.valid;
164
- const inputMatchValidation = !inputValue
165
- || (this.validationPattern.length > 0 ? this.validationPattern.some((pattern) => inputValue.match(pattern) !== null) : true);
166
- // Fallback: if HTML5 says valid but minlength constraint not met, it's invalid
167
- const isTooShort = this.validation.minLength && inputValue.length > 0 && inputValue.length < this.validation.minLength;
168
- return !isTooShort && inputIsValid && inputMatchValidation;
169
- }
170
- setPattern() {
171
- const custom = this.validation.custom;
172
- if (!(custom === null || custom === void 0 ? void 0 : custom.length))
173
- return [];
174
- return custom
175
- .filter(v => v.rule === 'regex')
176
- .map(v => v.pattern);
177
- }
178
- setErrorMessage() {
179
- var _a, _b;
180
- const inputValue = this.inputReference.value;
181
158
  if (this.inputReference.validity.valueMissing) {
182
- return translate('requiredError', this.language);
159
+ return { isValid: false, errorMessage: translate('requiredError', this.language) };
183
160
  }
184
161
  // Check length manually (fallback for HTML5 validation API issues)
185
162
  const isTooShort = this.validation.minLength && inputValue.length > 0 && inputValue.length < this.validation.minLength;
186
163
  const isTooLong = this.validation.maxLength && inputValue.length > this.validation.maxLength;
187
164
  if (this.inputReference.validity.tooShort || this.inputReference.validity.tooLong || isTooShort || isTooLong) {
188
- return translate('lengthError', this.language, { values: { minLength: this.validation.minLength, maxLength: this.validation.maxLength } });
165
+ return {
166
+ isValid: false,
167
+ errorMessage: translate('lengthError', this.language, { values: { minLength: this.validation.minLength, maxLength: this.validation.maxLength } })
168
+ };
189
169
  }
190
- const isCustomError = !(inputValue === '' && !this.validation.mandatory)
191
- && (this.validationPattern.length > 0 ? this.validationPattern.some((pattern) => inputValue.match(pattern) == null) : false);
192
- if (isCustomError) {
193
- const { errorKey: errorCode, errorMessage } = this.validation.custom.find(customValidation => customValidation.rule === 'regex');
194
- return translate(`${errorCode}`, this.language) || errorMessage;
170
+ const failedRegexRule = inputValue ? getFailedRegexRule(inputValue, this.validation.custom) : undefined;
171
+ if (failedRegexRule) {
172
+ return { isValid: false, errorMessage: getRuleErrorMessage(failedRegexRule, this.language) };
195
173
  }
196
174
  if (this.isDuplicateInput && this.duplicateInputValue !== this.value) {
197
- const errorCode = (_a = this.validation.custom.find(customRule => customRule.rule === 'duplicate-input')) === null || _a === void 0 ? void 0 : _a.errorKey;
198
- const errorMessage = (_b = this.validation.custom.find(customRule => customRule.rule === 'duplicate-input')) === null || _b === void 0 ? void 0 : _b.errorMessage;
199
- return translate(`${errorCode}`, this.language) ? translate(`${errorCode}`, this.language) : errorMessage;
175
+ const duplicateRule = (_a = this.validation.custom) === null || _a === void 0 ? void 0 : _a.find(customRule => customRule.rule === 'duplicate-input');
176
+ return { isValid: false, errorMessage: duplicateRule ? getRuleErrorMessage(duplicateRule, this.language) : '' };
200
177
  }
178
+ return { isValid: this.inputReference.validity.valid, errorMessage: '' };
201
179
  }
202
180
  renderTooltip() {
203
181
  if (this.showTooltip) {
@@ -210,8 +188,8 @@ export class TextInput {
210
188
  if (this.touched) {
211
189
  invalidClass = this.isValid == true || this.isValid == undefined ? '' : 'text__input--invalid';
212
190
  }
213
- return h("div", { key: '045e3183bdb1c7279abbd045043d789387956de5', class: `text__wrapper ${this.name}__input ${this.autofilled ? 'text__wrapper--autofilled' : ''}`, ref: el => this.stylingContainer = el }, h("div", { key: '3534d1261f9ccb9c0942f01d0758d9fd65743e40', class: 'text__wrapper--flex' }, h("label", { key: '171155d911e2c1048b003be8541671f0cdeb1ce5', class: `text__label ${this.validation.mandatory ? 'text__label--required' : ''}`, htmlFor: `${this.name}__input` }, this.displayName), h("div", { key: '8a0b0abe15eaf77b5efc5fa6374a8faeea24cf94', class: 'text__wrapper--relative' }, this.tooltip &&
214
- h("img", { key: 'aa0b0b33cdb370289db8444c3dba915ef8eb77be', class: 'text__tooltip-icon', src: tooltipIcon, alt: "", ref: (el) => this.tooltipIconReference = el, onClick: () => this.showTooltip = !this.showTooltip }), this.renderTooltip())), h("input", { key: 'cc47bb443384396eefd7bf61f767f8646f7e2639', name: this.name, id: `${this.name}__input`, value: this.defaultValue, type: 'text', class: `text__input ${invalidClass}`, placeholder: `${this.placeholder}`, ref: (el) => this.inputReference = el, readOnly: this.autofilled, required: this.validation.mandatory, minlength: this.validation.minLength, maxlength: this.validation.maxLength, onInput: this.handleInput, onBlur: this.handleBlur }), h("small", { key: 'ce7b97e0a7db8eb87c608690bad3cf99a565ba9f', class: 'text__error-message' }, this.errorMessage));
191
+ return h("div", { key: '206f2e3c3cf18b4405c07be422bb6173ac3a2ac2', class: `text__wrapper ${this.name}__input ${this.autofilled ? 'text__wrapper--autofilled' : ''}`, ref: el => this.stylingContainer = el }, h("div", { key: 'b6aa572d3f984a386a850b3f968a674eb94f7816', class: 'text__wrapper--flex' }, h("label", { key: '7fba5554aac3af6444225cb6da166276470dba50', class: `text__label ${this.validation.mandatory ? 'text__label--required' : ''}`, htmlFor: `${this.name}__input` }, this.displayName), h("div", { key: '9677b43f2f5e185e4d7fcfe007dd73a16d50876b', class: 'text__wrapper--relative' }, this.tooltip &&
192
+ h("img", { key: '69360ef5dd851cf64110f82189c2a27f99c843f8', class: 'text__tooltip-icon', src: tooltipIcon, alt: "", ref: (el) => this.tooltipIconReference = el, onClick: () => this.showTooltip = !this.showTooltip }), this.renderTooltip())), h("input", { key: '46b8d857acfb2d4fdd874984f555fc4457b7d53f', name: this.name, id: `${this.name}__input`, value: this.defaultValue, type: 'text', class: `text__input ${invalidClass}`, placeholder: `${this.placeholder}`, ref: (el) => this.inputReference = el, readOnly: this.autofilled, required: this.validation.mandatory, minlength: this.validation.minLength, maxlength: this.validation.maxLength, onInput: this.handleInput, onBlur: this.handleBlur }), h("small", { key: '5db72a6b2c969e3af37eaf03ce3a77524a5f25d2', class: 'text__error-message' }, this.errorMessage));
215
193
  }
216
194
  static get is() { return "text-input"; }
217
195
  static get encapsulation() { return "shadow"; }
@@ -1,8 +1,8 @@
1
1
  import { h } from "@stencil/core";
2
2
  import { translate } from "../../utils/locale.utils";
3
+ import { getFailedRegexRule, getRuleErrorMessage } from "../../utils/utils";
3
4
  export class TwofaInput {
4
5
  constructor() {
5
- this.validationPattern = '';
6
6
  this.stylingContainer = null;
7
7
  this.tooltipReference = null;
8
8
  this.tooltipIconReference = null;
@@ -61,8 +61,7 @@ export class TwofaInput {
61
61
  if (nextInput) {
62
62
  nextInput.focus();
63
63
  }
64
- this.setValidity();
65
- this.setErrorMessage();
64
+ this.updateValidity();
66
65
  };
67
66
  this.setClientStyling = () => {
68
67
  let sheet = document.createElement('style');
@@ -115,7 +114,6 @@ export class TwofaInput {
115
114
  }
116
115
  }
117
116
  connectedCallback() {
118
- this.validationPattern = this.setPattern();
119
117
  this.code = new Array(this.validation.maxLength).fill('');
120
118
  }
121
119
  componentDidRender() {
@@ -127,7 +125,7 @@ export class TwofaInput {
127
125
  }
128
126
  }
129
127
  componentDidLoad() {
130
- this.setValidity();
128
+ this.isValid = this.validate().isValid;
131
129
  }
132
130
  handleKeyDown(e, idx) {
133
131
  if (e.key === 'Backspace') {
@@ -140,8 +138,7 @@ export class TwofaInput {
140
138
  prevInput === null || prevInput === void 0 ? void 0 : prevInput.focus();
141
139
  }
142
140
  }
143
- this.setValidity();
144
- this.setErrorMessage();
141
+ this.updateValidity();
145
142
  }
146
143
  handlePaste(e) {
147
144
  var _a;
@@ -160,33 +157,20 @@ export class TwofaInput {
160
157
  if (lastInput) {
161
158
  lastInput.focus();
162
159
  }
163
- this.setValidity();
164
- this.setErrorMessage();
160
+ this.updateValidity();
165
161
  }
166
- setValidity() {
162
+ validate() {
167
163
  const code = this.code.join('');
168
- const inputMatchLength = code.length === this.validation.maxLength;
169
- const inputMatchValidation = code.match(this.validationPattern) !== null;
170
- this.isValid = inputMatchLength && inputMatchValidation;
171
- }
172
- setPattern() {
173
- var _a, _b;
174
- if (((_a = this.validation.custom) === null || _a === void 0 ? void 0 : _a.length) > 0) {
175
- return (_b = this.validation.custom.find(customValidation => customValidation.rule === 'regex')) === null || _b === void 0 ? void 0 : _b.pattern;
176
- }
164
+ const failedRule = getFailedRegexRule(code, this.validation.custom);
165
+ return {
166
+ isValid: code.length === this.validation.maxLength && !failedRule,
167
+ errorMessage: failedRule ? getRuleErrorMessage(failedRule, this.language) : ''
168
+ };
177
169
  }
178
- setErrorMessage() {
179
- var _a;
180
- const inputMatchValidation = this.code.join('').match(this.validationPattern) !== null;
181
- if (!inputMatchValidation) {
182
- const errorKey = (_a = this.validation.custom.find(customValidation => customValidation.rule === 'regex')) === null || _a === void 0 ? void 0 : _a.errorKey;
183
- if (errorKey) {
184
- this.errorMessage = translate(errorKey, this.language);
185
- }
186
- }
187
- else {
188
- this.errorMessage = '';
189
- }
170
+ updateValidity() {
171
+ const { isValid, errorMessage } = this.validate();
172
+ this.isValid = isValid;
173
+ this.errorMessage = errorMessage;
190
174
  }
191
175
  renderTooltip() {
192
176
  if (this.showTooltip) {
@@ -195,9 +179,9 @@ export class TwofaInput {
195
179
  return null;
196
180
  }
197
181
  render() {
198
- return (h("div", { key: '5764336a8995a1ec0e7ff432ce85c8a317e3f01c', class: "twofa" }, h("div", { key: '8ea47ba6bb5f552afcde936f1de8c544f89e12a7', class: 'twofa__error-message' }, h("p", { key: '6930908b1f5df7030d96a442779203747aa938b2' }, this.errorMessage)), h("div", { key: '56f116fa0ac4883d1b225b7165b2d5271a661d9d', class: "twofa__description", innerHTML: translate('twofaDescription', this.language, { values: { destination: this.destination } }) }), h("div", { key: 'b2ecadecbc8802c5b4dd09d81dc77e66f2108990', class: "twofa__input-wrapper", ref: this.setContainerRef }, this.code.map((char, idx) => {
182
+ return (h("div", { key: '31164396a440bde033871a5cbf651546895d8c3d', class: "twofa" }, h("div", { key: 'cc50746fb709c3853cd11fd13c68be93efd811b4', class: 'twofa__error-message' }, h("p", { key: '4a34b178dd4b018daf81ee654f91f79813f801db' }, this.errorMessage)), h("div", { key: '0ac2a794fcb27deb3f40b87fe4758e19e35b53b2', class: "twofa__description", innerHTML: translate('twofaDescription', this.language, { values: { destination: this.destination } }) }), h("div", { key: '69fdbfc5be1af6fab8b386179eaa25ea181aec3a', class: "twofa__input-wrapper", ref: this.setContainerRef }, this.code.map((char, idx) => {
199
183
  return (h("input", { key: idx, ref: el => this.setInputRef(el, idx), id: `otp-input-${idx}`, type: "text", maxLength: 2, value: char, onInput: (event) => this.handleInput(event, idx), onKeyDown: (event) => this.handleKeyDown(event, idx), onPaste: (event) => this.handlePaste(event) }));
200
- })), h("div", { key: '76635a96eb73d77a3259f26d7d74f738e705acf1', class: "twofa__button-wrapper" }, h("p", { key: '66b15896c126c4c7e8d3ba6d22829992df4cfde9', class: "twofa__resend-message" }, translate('twofaResendMessage', this.language)), h("button", { key: '7e81ec829cfeb48e911d7b88aa9ec7d6f1cbf4e2', class: "twofa__resend-button", onClick: this.resendCodeHandler, disabled: !this.isResendButtonAvailable }, this.isResendButtonAvailable
184
+ })), h("div", { key: '9a9f50ed47da2204cd31a27939d18b42011a1114', class: "twofa__button-wrapper" }, h("p", { key: 'ad90ba428278f90a05f706c67452551f3c75a199', class: "twofa__resend-message" }, translate('twofaResendMessage', this.language)), h("button", { key: 'cead0e523e873c20dd21982e318b1e37998480b8', class: "twofa__resend-button", onClick: this.resendCodeHandler, disabled: !this.isResendButtonAvailable }, this.isResendButtonAvailable
201
185
  ? translate('twofaResendButton', this.language)
202
186
  : this.formatTime()))));
203
187
  }
@@ -1,3 +1,10 @@
1
+ import { translate } from "./locale.utils";
1
2
  export function format(first, middle, last) {
2
3
  return (first || '') + (middle ? ` ${middle}` : '') + (last ? ` ${last}` : '');
3
4
  }
5
+ export function getFailedRegexRule(value, custom) {
6
+ return (custom || []).find(rule => rule.rule === 'regex' && !new RegExp(rule.pattern).test(value));
7
+ }
8
+ export function getRuleErrorMessage(rule, language) {
9
+ return translate(rule.errorKey, language) || rule.errorMessage;
10
+ }