@antify/ui 4.2.0 → 4.2.1

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.
@@ -24,16 +24,15 @@ import {
24
24
  ref, nextTick,
25
25
  } from 'vue';
26
26
 
27
- const phoneInputNativeRef = ref<HTMLInputElement | null>(null);
28
-
29
27
  defineOptions({
30
28
  inheritAttrs: false,
31
29
  });
32
30
 
33
31
  const props = withDefaults(defineProps<{
34
32
  modelValue: string | null;
35
- countryValue: string | number | null;
33
+ countryValue?: string | number | null;
36
34
  countries?: Country[];
35
+ inputRef?: null | HTMLInputElement;
37
36
 
38
37
  //Common Props
39
38
  size?: Size;
@@ -53,7 +52,6 @@ const props = withDefaults(defineProps<{
53
52
  searchable?: boolean;
54
53
  countryMaxHeight?: string;
55
54
  countryValueKey?: CountryValueKey;
56
- countryErrorMessage?: string;
57
55
  countrySortable?: boolean;
58
56
 
59
57
  //AntBaseInput Props
@@ -61,6 +59,7 @@ const props = withDefaults(defineProps<{
61
59
  nullable?: boolean;
62
60
  locale?: Locale;
63
61
  }>(), {
62
+ inputRef: null,
64
63
  size: Size.md,
65
64
  state: InputState.base,
66
65
  searchable: true,
@@ -68,7 +67,6 @@ const props = withDefaults(defineProps<{
68
67
  countryPlaceholder: 'Select country',
69
68
  placeholder: 'Enter phone number',
70
69
  countryValueKey: CountryValueKey.dialCode,
71
- countryErrorMessage: 'Please select a country code or start with "+"',
72
70
  countrySortable: true,
73
71
  messages: () => [],
74
72
  nullable: true,
@@ -79,13 +77,27 @@ const props = withDefaults(defineProps<{
79
77
  const emit = defineEmits([
80
78
  'update:modelValue',
81
79
  'update:countryValue',
80
+ 'update:inputRef',
82
81
  'select-country',
83
82
  'validate',
84
83
  'blur',
85
84
  ]);
86
85
 
87
- const _countryValue = useVModel(props, 'countryValue', emit);
88
86
  const _phoneNumber = useVModel(props, 'modelValue', emit);
87
+ const internalInputRef = ref<HTMLInputElement | null>(null);
88
+ const _inputRef = useVModel(props, 'inputRef', emit);
89
+ const internalCountryValue = ref<string | number | null>(null);
90
+
91
+ const _countryValue = computed({
92
+ get: () => {
93
+ return props.countryValue !== undefined ? props.countryValue : internalCountryValue.value;
94
+ },
95
+ set: (val) => {
96
+ emit('update:countryValue', val);
97
+
98
+ internalCountryValue.value = val;
99
+ },
100
+ });
89
101
 
90
102
  const updateFullValue = (countryId: string | number | null, rawPhone: string | null) => {
91
103
  if (!rawPhone) {
@@ -104,26 +116,8 @@ const updateFullValue = (countryId: string | number | null, rawPhone: string | n
104
116
  }
105
117
  };
106
118
 
107
- const showCountryError = computed(() => {
108
- const val = props.modelValue || '';
109
-
110
- return props.countryValue == null && val.length > 0 && !val.startsWith('+');
111
- });
112
-
113
- const allMessages = computed(() => {
114
- const msgs = [
115
- ...(props.messages || []),
116
- ];
117
-
118
- if (showCountryError.value) {
119
- msgs.push(props.countryErrorMessage);
120
- }
121
-
122
- return msgs;
123
- });
124
-
125
119
  const currentCountry = computed(() => {
126
- return props.countries.find(c => String(c[props.countryValueKey]) === String(props.countryValue));
120
+ return props.countries.find(c => String(c[props.countryValueKey]) === String(_countryValue.value));
127
121
  });
128
122
 
129
123
  const sortedCountriesByDialCode = computed(() => {
@@ -213,7 +207,7 @@ function onCountrySelect(country: Country) {
213
207
  emit('select-country', country);
214
208
 
215
209
  nextTick(() => {
216
- phoneInputNativeRef.value?.focus();
210
+ internalInputRef.value?.focus();
217
211
  });
218
212
  }
219
213
 
@@ -232,13 +226,13 @@ function onKeyPress(event: KeyboardEvent) {
232
226
  return;
233
227
  }
234
228
 
235
- if (props.countryValue && charStr === '+') {
229
+ if (_countryValue.value && charStr === '+') {
236
230
  event.preventDefault();
237
231
 
238
232
  return;
239
233
  }
240
234
 
241
- if (!props.countryValue && charStr === '+' && currentRawValue.length > 0) {
235
+ if (!_countryValue.value && charStr === '+' && currentRawValue.length > 0) {
242
236
  event.preventDefault();
243
237
  }
244
238
 
@@ -269,6 +263,11 @@ function onPaste(event: ClipboardEvent) {
269
263
  }
270
264
  }
271
265
 
266
+ function onBlur(e: FocusEvent) {
267
+ emit('blur', e);
268
+ emit('validate', _phoneNumber.value);
269
+ }
270
+
272
271
  watch(_countryValue, (newCountryId, oldCountryId) => {
273
272
  if (newCountryId === oldCountryId) {
274
273
  return;
@@ -284,13 +283,29 @@ watch(_countryValue, (newCountryId, oldCountryId) => {
284
283
 
285
284
  updateFullValue(newCountryId, body);
286
285
  });
286
+
287
+ watch(() => props.modelValue, (newVal) => {
288
+ if (newVal && newVal.startsWith('+') && !_countryValue.value) {
289
+ const country = findCountryByPhone(newVal);
290
+
291
+ if (country) {
292
+ _countryValue.value = country[props.countryValueKey] as string | number;
293
+ }
294
+ }
295
+ }, {
296
+ immediate: true,
297
+ });
298
+
299
+ watch(internalInputRef, (el) => {
300
+ _inputRef.value = el;
301
+ });
287
302
  </script>
288
303
 
289
304
  <template>
290
305
  <AntField
291
306
  :label="label"
292
- :messages="allMessages"
293
- :state="showCountryError ? InputState.danger : state"
307
+ :messages="messages"
308
+ :state="state"
294
309
  :size="size"
295
310
  :skeleton="skeleton"
296
311
  :description="description"
@@ -305,7 +320,7 @@ watch(_countryValue, (newCountryId, oldCountryId) => {
305
320
  :countries="countries"
306
321
  :size="size"
307
322
  :locale="locale"
308
- :state="showCountryError ? InputState.danger : state"
323
+ :state="state"
309
324
  :disabled="disabled"
310
325
  :readonly="readonly"
311
326
  :skeleton="skeleton"
@@ -324,10 +339,10 @@ watch(_countryValue, (newCountryId, oldCountryId) => {
324
339
 
325
340
  <AntBaseInput
326
341
  v-model="formattedNumber"
327
- v-model:input-ref="phoneInputNativeRef"
342
+ v-model:input-ref="internalInputRef"
328
343
  :nullable="nullable"
329
344
  :type="BaseInputType.text"
330
- :state="showCountryError ? InputState.danger : state"
345
+ :state="state"
331
346
  :size="size"
332
347
  :skeleton="skeleton"
333
348
  v-bind="$attrs"
@@ -337,8 +352,7 @@ watch(_countryValue, (newCountryId, oldCountryId) => {
337
352
  :grouped="Grouped.right"
338
353
  wrapper-class="flex-grow"
339
354
  class="-ml-px"
340
- @validate="val => $emit('validate', val)"
341
- @blur="e => $emit('blur', e)"
355
+ @blur="onBlur"
342
356
  @keydown="onKeyPress"
343
357
  @paste="onPaste"
344
358
  />
@@ -35,6 +35,9 @@ const meta = {
35
35
  onValidate: {
36
36
  action: "validate"
37
37
  },
38
+ onBlur: {
39
+ action: "blur"
40
+ },
38
41
  nullable: {
39
42
  control: "boolean",
40
43
  description: "Shows the clear icon in the input field."
@@ -37,6 +37,9 @@ const meta = {
37
37
  onValidate: {
38
38
  action: "validate"
39
39
  },
40
+ onBlur: {
41
+ action: "blur"
42
+ },
40
43
  nullable: {
41
44
  control: "boolean",
42
45
  description: "Shows the clear icon in the input field."
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antify/ui",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {