edvoyui-component-library-test-flight 0.0.240 → 0.0.242

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.
@@ -79,7 +79,7 @@ import {
79
79
  type PhoneMeta,
80
80
  type VueTelInputProps,
81
81
  } from "vue-tel-input";
82
- import { getExampleNumber } from "libphonenumber-js";
82
+ import { type CountryCode, getExampleNumber } from "libphonenumber-js";
83
83
  import mobileExamples from "libphonenumber-js/mobile/examples";
84
84
  import "vue-tel-input/vue-tel-input.css";
85
85
 
@@ -160,9 +160,9 @@ const id = generateUID();
160
160
 
161
161
  const hasError = computed({
162
162
  set: (value) => {
163
- emit("update:isValid", value);
163
+ emit("update:isValid", !value);
164
164
  },
165
- get: () => isValid.value,
165
+ get: () => !isValid.value,
166
166
  });
167
167
 
168
168
  const inputValue = computed(() => {
@@ -173,24 +173,40 @@ const mobile = ref(String(props.modelValue ?? ""));
173
173
  const isTouched = ref(false);
174
174
  const telephoneIsValid = ref(props.isValid);
175
175
  const activeCountry = ref<CountryObject | null>(null);
176
+ const firstTime = ref(true);
176
177
  const dynamicMaxLength = ref<number>(props.length);
178
+ const normalizeValueForCountry = (
179
+ value: string | number | null | undefined,
180
+ country: CountryObject | null,
181
+ ) => {
182
+ const stringValue = String(value ?? "").trim();
183
+ if (!stringValue || stringValue.startsWith("+") || !country?.dialCode) {
184
+ return stringValue;
185
+ }
186
+
187
+ return `+${country.dialCode}${stringValue.replace(/\D/g, "")}`;
188
+ };
177
189
  const getCountryMaxLength = (
178
190
  country: CountryObject | PhoneMeta["country"] | null,
179
191
  ) => {
180
192
  if (!country) return props.length;
181
193
 
182
- const countryCode = typeof country === "string" ? country : country.iso2;
183
- const example = getExampleNumber(countryCode, mobileExamples);
194
+ const rawCode = typeof country === "string" ? country : country.iso2;
195
+ const countryCode = (
196
+ rawCode ? String(rawCode).toUpperCase() : ""
197
+ ) as CountryCode;
198
+ const example = countryCode
199
+ ? getExampleNumber(countryCode, mobileExamples)
200
+ : undefined;
184
201
  if (!example) return props.length;
185
202
 
186
- return Math.max(
187
- example.nationalNumber.length + 5,
188
- example.formatInternational().length,
189
- );
203
+ const dialCodeLength = String(example.countryCallingCode ?? "").length;
204
+
205
+ return example.nationalNumber.length + dialCodeLength + 1;
190
206
  };
191
207
  const listener = computed(() => props.modelValue);
192
208
  watch(listener, (value) => {
193
- mobile.value = String(value ?? "");
209
+ mobile.value = normalizeValueForCountry(value, activeCountry.value);
194
210
  });
195
211
  watch(isValid, (value) => {
196
212
  telephoneIsValid.value = value;
@@ -241,10 +257,10 @@ const onInputChanged = (
241
257
  phoneObject: TelephoneInputMeta,
242
258
  ) => {
243
259
  telephoneIsValid.value = Boolean(phoneObject.valid);
244
- hasError.value = telephoneIsValid.value;
260
+ hasError.value = !telephoneIsValid.value;
245
261
 
246
262
  dynamicMaxLength.value = getCountryMaxLength(
247
- activeCountry.value ?? phoneObject.country ?? null,
263
+ phoneObject.country ?? activeCountry.value ?? null,
248
264
  );
249
265
 
250
266
  emit("update:modelValue", phoneObject.number);
@@ -255,10 +271,12 @@ const onBlur = () => {
255
271
  emit("blur");
256
272
  };
257
273
 
258
- const firstTime = ref(true);
259
274
  const onCountryChanged = (country: CountryObject) => {
260
275
  activeCountry.value = country;
261
276
  dynamicMaxLength.value = getCountryMaxLength(country);
277
+ if (firstTime.value && props.modelValue) {
278
+ mobile.value = normalizeValueForCountry(props.modelValue, country);
279
+ }
262
280
  if (!firstTime.value) {
263
281
  emit("update:modelValue", "");
264
282
  }
@@ -287,12 +305,19 @@ const externalErrorMessage = computed(() => {
287
305
  });
288
306
 
289
307
  const internalErrorMessage = computed(() => {
290
- if (!isTouched.value || props.readonly || props.disabled) return "";
308
+ if (
309
+ !isTouched.value ||
310
+ telephoneIsValid.value ||
311
+ props.readonly ||
312
+ props.disabled
313
+ ) {
314
+ return "";
315
+ }
291
316
 
292
317
  const value = String(mobile.value ?? "").trim();
293
318
  if (!value) return props.required ? "Mobile number is required" : "";
294
319
 
295
- return telephoneIsValid.value ? "" : "Enter a valid phone number";
320
+ return "Enter a valid phone number";
296
321
  });
297
322
 
298
323
  const errorMessage = computed(