edvoyui-component-library-test-flight 0.0.239 → 0.0.241

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.
@@ -184,10 +184,10 @@ declare const __VLS_component: import('../../../node_modules/vue').DefineCompone
184
184
  search: (...args: any[]) => void;
185
185
  "update:modelValue": (...args: any[]) => void;
186
186
  tag: (...args: any[]) => void;
187
+ open: (...args: any[]) => void;
187
188
  "update:searchString": (...args: any[]) => void;
188
189
  deselected: (...args: any[]) => void;
189
190
  selectedOption: (...args: any[]) => void;
190
- open: (...args: any[]) => void;
191
191
  }, string, import('../../../node_modules/vue').PublicProps, Readonly<import('../../../node_modules/vue').ExtractPropTypes<{
192
192
  clearable: {
193
193
  type: BooleanConstructor;
@@ -350,10 +350,10 @@ declare const __VLS_component: import('../../../node_modules/vue').DefineCompone
350
350
  onSearch?: ((...args: any[]) => any) | undefined;
351
351
  "onUpdate:modelValue"?: ((...args: any[]) => any) | undefined;
352
352
  onTag?: ((...args: any[]) => any) | undefined;
353
+ onOpen?: ((...args: any[]) => any) | undefined;
353
354
  "onUpdate:searchString"?: ((...args: any[]) => any) | undefined;
354
355
  onDeselected?: ((...args: any[]) => any) | undefined;
355
356
  onSelectedOption?: ((...args: any[]) => any) | undefined;
356
- onOpen?: ((...args: any[]) => any) | undefined;
357
357
  }>, {
358
358
  keys: string[];
359
359
  loading: boolean;
@@ -73,9 +73,18 @@
73
73
 
74
74
  <script lang="ts" setup>
75
75
  import { computed, onUpdated, PropType, ref, toRefs, watch } from "vue";
76
- import { VueTelInput, VueTelInputCountryOption } from "vue-tel-input";
76
+ import {
77
+ VueTelInput,
78
+ type CountryObject,
79
+ type PhoneMeta,
80
+ type VueTelInputProps,
81
+ } from "vue-tel-input";
82
+ import { getExampleNumber } from "libphonenumber-js";
83
+ import mobileExamples from "libphonenumber-js/mobile/examples";
77
84
  import "vue-tel-input/vue-tel-input.css";
78
85
 
86
+ type TelephoneInputMeta = PhoneMeta & { number: string };
87
+
79
88
  const emit = defineEmits(["update:modelValue", "blur", "update:isValid"]);
80
89
 
81
90
  const props = defineProps({
@@ -160,21 +169,56 @@ const inputValue = computed(() => {
160
169
  return props.modelValue === 0 ? true : !!props.modelValue;
161
170
  });
162
171
 
163
- const mobile = ref(props.modelValue);
172
+ const mobile = ref(String(props.modelValue ?? ""));
164
173
  const isTouched = ref(false);
165
174
  const telephoneIsValid = ref(props.isValid);
175
+ const activeCountry = ref<CountryObject | null>(null);
176
+ const firstTime = ref(true);
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
+ };
189
+ const getCountryMaxLength = (
190
+ country: CountryObject | PhoneMeta["country"] | null,
191
+ ) => {
192
+ if (!country) return props.length;
193
+
194
+ const countryCode = typeof country === "string" ? country : country.iso2;
195
+ const example = getExampleNumber(countryCode, mobileExamples);
196
+ if (!example) return props.length;
197
+
198
+ const dialCodeLength = String(example.countryCallingCode ?? "").length;
199
+
200
+ return example.nationalNumber.length + dialCodeLength + 1;
201
+ };
166
202
  const listener = computed(() => props.modelValue);
167
203
  watch(listener, (value) => {
168
- mobile.value = value;
204
+ mobile.value = normalizeValueForCountry(value, activeCountry.value);
169
205
  });
170
206
  watch(isValid, (value) => {
171
207
  telephoneIsValid.value = value;
172
208
  });
209
+ watch(
210
+ () => props.length,
211
+ (value) => {
212
+ if (!activeCountry.value) {
213
+ dynamicMaxLength.value = value;
214
+ }
215
+ },
216
+ );
173
217
 
174
218
  // Make bindProps a reactive computed property
175
- const bindProps = computed(() => ({
219
+ const bindProps = computed<VueTelInputProps>(() => ({
176
220
  mode: "international",
177
- autoFormat: false,
221
+ autoFormat: true,
178
222
  autoDefaultCountry: true,
179
223
  styleClasses: "eui-tel",
180
224
  invalidMsg: "Required mobile number",
@@ -187,7 +231,7 @@ const bindProps = computed(() => ({
187
231
  name: props.name,
188
232
  id: id,
189
233
  placeholder: props.placeholder,
190
- maxlength: props.length,
234
+ maxlength: dynamicMaxLength.value,
191
235
  showDialCode: true,
192
236
  tabindex: 0,
193
237
  readonly: props.readonly,
@@ -203,9 +247,30 @@ const bindProps = computed(() => ({
203
247
  },
204
248
  }));
205
249
 
206
- const onInputChanged = (_formattedNumber: string, phoneObject: any) => {
250
+ const onInputChanged = (
251
+ _formattedNumber: string,
252
+ phoneObject: TelephoneInputMeta,
253
+ ) => {
254
+ const countryCode = phoneObject.country ?? activeCountry.value?.iso2;
255
+ const example = countryCode
256
+ ? getExampleNumber(countryCode, mobileExamples)
257
+ : undefined;
258
+ const dialCode =
259
+ example?.countryCallingCode ?? activeCountry.value?.dialCode ?? "";
260
+ const phoneDigits = String(phoneObject.number ?? "").replace(/\D/g, "");
261
+ const nationalDigits = phoneDigits.slice(String(dialCode).length);
262
+
263
+ if (nationalDigits.length > 0) {
264
+ isTouched.value = true;
265
+ }
266
+
207
267
  telephoneIsValid.value = Boolean(phoneObject.valid);
208
268
  hasError.value = telephoneIsValid.value;
269
+
270
+ dynamicMaxLength.value = getCountryMaxLength(
271
+ activeCountry.value ?? phoneObject.country ?? null,
272
+ );
273
+
209
274
  emit("update:modelValue", phoneObject.number);
210
275
  };
211
276
 
@@ -214,8 +279,12 @@ const onBlur = () => {
214
279
  emit("blur");
215
280
  };
216
281
 
217
- const firstTime = ref(true);
218
- const onCountryChanged = (_event: VueTelInputCountryOption) => {
282
+ const onCountryChanged = (country: CountryObject) => {
283
+ activeCountry.value = country;
284
+ dynamicMaxLength.value = getCountryMaxLength(country);
285
+ if (firstTime.value && props.modelValue) {
286
+ mobile.value = normalizeValueForCountry(props.modelValue, country);
287
+ }
219
288
  if (!firstTime.value) {
220
289
  emit("update:modelValue", "");
221
290
  }
@@ -135,5 +135,5 @@ declare const _default: import('../../../node_modules/vue').DefineComponent<impo
135
135
  cols: number;
136
136
  }, {}, {}, {}, string, import('../../../node_modules/vue').ComponentProvideOptions, true, {
137
137
  inputRef: HTMLTextAreaElement;
138
- }, any>;
138
+ }, HTMLDivElement>;
139
139
  export default _default;
@@ -60,10 +60,10 @@
60
60
  >
61
61
  </textarea>
62
62
  </div>
63
- </div>
64
- <template v-if="errors && Object.keys(errors).length">
63
+ <template v-if="errors && Object.keys(errors).length">
65
64
  <EUIErrorMessage :errors="errors" :name="name" />
66
65
  </template>
66
+ </div>
67
67
  </template>
68
68
  <script setup lang="ts">
69
69
  import { PropType, ref, watch, computed } from "vue";