@countriesdb/widget 0.1.7 → 0.1.9

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.
@@ -45,12 +45,19 @@ export function populateCountrySelect(select, countries, language, countryNameFi
45
45
  if (countryNameFilter) {
46
46
  filteredCountries = sortCountries(filteredCountries, language);
47
47
  }
48
- // Clear existing options (except default)
49
- const defaultOption = select.querySelector('option[value=""]') ||
50
- (select.dataset.defaultValue ? select.querySelector(`option[value="${select.dataset.defaultValue}"]`) : null);
51
- const defaultValue = select.dataset.defaultValue ?? '';
52
- // Keep default option if it exists
53
- select.innerHTML = defaultOption ? defaultOption.outerHTML : `<option value="${defaultValue}">${select.dataset.label || '&mdash;'}</option>`;
48
+ const isMultiple = select.hasAttribute('multiple');
49
+ if (isMultiple) {
50
+ // For multi-select, don't add default option
51
+ select.innerHTML = '';
52
+ }
53
+ else {
54
+ // Clear existing options (except default)
55
+ const defaultOption = select.querySelector('option[value=""]') ||
56
+ (select.dataset.defaultValue ? select.querySelector(`option[value="${select.dataset.defaultValue}"]`) : null);
57
+ const defaultValue = select.dataset.defaultValue ?? '';
58
+ // Keep default option if it exists
59
+ select.innerHTML = defaultOption ? defaultOption.outerHTML : `<option value="${defaultValue}">${select.dataset.label || '&mdash;'}</option>`;
60
+ }
54
61
  // Add country options
55
62
  filteredCountries.forEach((country) => {
56
63
  const option = document.createElement('option');
@@ -5,7 +5,7 @@ import type { SelectElement, WidgetState } from './types';
5
5
  /**
6
6
  * Trigger follow logic when a subdivision is selected
7
7
  */
8
- export declare function triggerFollowLogic(select: SelectElement, apiKey: string, backendUrl: string, state: WidgetState, followRelated: boolean, followUpward: boolean, updateSubdivisionSelectFn: (select: SelectElement, apiKey: string, countryCode: string) => Promise<void>): Promise<void>;
8
+ export declare function triggerFollowLogic(select: SelectElement, apiKey: string, backendUrl: string, state: WidgetState, followRelated: boolean, followUpward: boolean, updateSubdivisionSelectFn: (select: SelectElement, apiKey: string, countryCode: string) => Promise<void>, updateSubdivisionsFn?: (countrySelect: SelectElement, apiKey: string) => Promise<void>): Promise<void>;
9
9
  /**
10
10
  * Handle follow_related from subdivision change event
11
11
  */
@@ -5,7 +5,7 @@ import { dispatchUpdateEvent } from './event-system';
5
5
  /**
6
6
  * Trigger follow logic when a subdivision is selected
7
7
  */
8
- export async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelated, followUpward, updateSubdivisionSelectFn) {
8
+ export async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelated, followUpward, updateSubdivisionSelectFn, updateSubdivisionsFn) {
9
9
  if (!followRelated && !followUpward) {
10
10
  return;
11
11
  }
@@ -39,9 +39,17 @@ export async function triggerFollowLogic(select, apiKey, backendUrl, state, foll
39
39
  relatedSubsSelect.dataset._widgetTempPreselect = targetSubdivision; // one-time preselect
40
40
  }
41
41
  if (linkedCountrySelect.value !== targetCountry) {
42
+ // Directly set value like old widget did
42
43
  linkedCountrySelect.value = targetCountry;
43
44
  dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
44
- await updateSubdivisionSelectFn(linkedCountrySelect, apiKey, targetCountry);
45
+ // Update all subdivision selects for the new country (like old widget)
46
+ if (updateSubdivisionsFn) {
47
+ await updateSubdivisionsFn(linkedCountrySelect, apiKey);
48
+ }
49
+ else if (relatedSubsSelect) {
50
+ // Fallback: update just the one subdivision select
51
+ await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
52
+ }
45
53
  }
46
54
  else {
47
55
  if (relatedSubsSelect && targetSubdivision) {
@@ -61,9 +69,19 @@ export async function triggerFollowLogic(select, apiKey, backendUrl, state, foll
61
69
  }
62
70
  }
63
71
  if (linkedCountrySelect.value !== parentCode) {
72
+ // Directly set value like old widget did
64
73
  linkedCountrySelect.value = parentCode;
65
74
  dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
66
- await updateSubdivisionSelectFn(linkedCountrySelect, apiKey, parentCode);
75
+ // Update all subdivision selects for the new country (like old widget)
76
+ if (updateSubdivisionsFn) {
77
+ await updateSubdivisionsFn(linkedCountrySelect, apiKey);
78
+ }
79
+ else {
80
+ const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
81
+ if (relatedSubsSelect) {
82
+ await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
83
+ }
84
+ }
67
85
  }
68
86
  else if (parentSub) {
69
87
  const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
@@ -107,9 +125,12 @@ export async function handleFollowRelatedFromSubdivision(select, apiKey, backend
107
125
  relatedSubsSelect.dataset.preselected = targetSubdivision;
108
126
  }
109
127
  if (linkedCountrySelect.value !== targetCountry) {
128
+ // Directly set value like old widget did
110
129
  linkedCountrySelect.value = targetCountry;
111
130
  dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
112
- // Update the subdivision select for the new country
131
+ // Update all subdivision selects for the new country (like old widget)
132
+ // Note: updateSubdivisionsFn is not available in handleFollowRelatedFromSubdivision,
133
+ // so we update the subdivision select manually
113
134
  if (relatedSubsSelect) {
114
135
  await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
115
136
  }
@@ -157,9 +178,12 @@ export async function handleFollowUpwardFromSubdivision(select, apiKey, backendU
157
178
  }
158
179
  }
159
180
  if (linkedCountrySelect.value !== parentCode) {
181
+ // Directly set value like old widget did
160
182
  linkedCountrySelect.value = parentCode;
161
183
  dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'follow' });
162
- // Update the subdivision select for the new country
184
+ // Update all subdivision selects for the new country (like old widget)
185
+ // Note: updateSubdivisionsFn is not available in handleFollowUpwardFromSubdivision,
186
+ // so we update the subdivision select manually
163
187
  const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
164
188
  if (relatedSubsSelect) {
165
189
  await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
@@ -204,9 +228,12 @@ export async function handleFollowUpwardFromCountry(select, apiKey, backendUrl,
204
228
  }
205
229
  }
206
230
  }
231
+ // Directly set value like old widget did
207
232
  select.value = parentCode;
208
233
  dispatchUpdateEvent(select, { type: 'country', reason: 'regular' });
209
- // Update all linked subdivision selects for the new country
234
+ // Update all subdivision selects for the new country (like old widget)
235
+ // Note: updateSubdivisionsFn is not available in handleFollowUpwardFromCountry,
236
+ // so we update all subdivision selects manually
210
237
  const linkedSubdivisionSelects = Array.from(document.querySelectorAll(`.subdivision-selection[data-country="${select.dataset.name}"]`));
211
238
  for (const s of linkedSubdivisionSelects) {
212
239
  await updateSubdivisionSelectFn(s, apiKey, parentCode);
package/dist/index.esm.js CHANGED
@@ -46,12 +46,19 @@ function populateCountrySelect(select, countries, language, countryNameFilter) {
46
46
  if (countryNameFilter) {
47
47
  filteredCountries = sortCountries(filteredCountries, language);
48
48
  }
49
- // Clear existing options (except default)
50
- const defaultOption = select.querySelector('option[value=""]') ||
51
- (select.dataset.defaultValue ? select.querySelector(`option[value="${select.dataset.defaultValue}"]`) : null);
52
- const defaultValue = select.dataset.defaultValue ?? '';
53
- // Keep default option if it exists
54
- select.innerHTML = defaultOption ? defaultOption.outerHTML : `<option value="${defaultValue}">${select.dataset.label || '&mdash;'}</option>`;
49
+ const isMultiple = select.hasAttribute('multiple');
50
+ if (isMultiple) {
51
+ // For multi-select, don't add default option
52
+ select.innerHTML = '';
53
+ }
54
+ else {
55
+ // Clear existing options (except default)
56
+ const defaultOption = select.querySelector('option[value=""]') ||
57
+ (select.dataset.defaultValue ? select.querySelector(`option[value="${select.dataset.defaultValue}"]`) : null);
58
+ const defaultValue = select.dataset.defaultValue ?? '';
59
+ // Keep default option if it exists
60
+ select.innerHTML = defaultOption ? defaultOption.outerHTML : `<option value="${defaultValue}">${select.dataset.label || '&mdash;'}</option>`;
61
+ }
55
62
  // Add country options
56
63
  filteredCountries.forEach((country) => {
57
64
  const option = document.createElement('option');
@@ -169,7 +176,7 @@ function isWidgetInitiatedEvent(event) {
169
176
  /**
170
177
  * Trigger follow logic when a subdivision is selected
171
178
  */
172
- async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelated, followUpward, updateSubdivisionSelectFn) {
179
+ async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelated, followUpward, updateSubdivisionSelectFn, updateSubdivisionsFn) {
173
180
  if (!followRelated && !followUpward) {
174
181
  return;
175
182
  }
@@ -203,9 +210,17 @@ async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelat
203
210
  relatedSubsSelect.dataset._widgetTempPreselect = targetSubdivision; // one-time preselect
204
211
  }
205
212
  if (linkedCountrySelect.value !== targetCountry) {
213
+ // Directly set value like old widget did
206
214
  linkedCountrySelect.value = targetCountry;
207
215
  dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
208
- await updateSubdivisionSelectFn(linkedCountrySelect, apiKey, targetCountry);
216
+ // Update all subdivision selects for the new country (like old widget)
217
+ if (updateSubdivisionsFn) {
218
+ await updateSubdivisionsFn(linkedCountrySelect, apiKey);
219
+ }
220
+ else if (relatedSubsSelect) {
221
+ // Fallback: update just the one subdivision select
222
+ await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
223
+ }
209
224
  }
210
225
  else {
211
226
  if (relatedSubsSelect && targetSubdivision) {
@@ -225,9 +240,19 @@ async function triggerFollowLogic(select, apiKey, backendUrl, state, followRelat
225
240
  }
226
241
  }
227
242
  if (linkedCountrySelect.value !== parentCode) {
243
+ // Directly set value like old widget did
228
244
  linkedCountrySelect.value = parentCode;
229
245
  dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
230
- await updateSubdivisionSelectFn(linkedCountrySelect, apiKey, parentCode);
246
+ // Update all subdivision selects for the new country (like old widget)
247
+ if (updateSubdivisionsFn) {
248
+ await updateSubdivisionsFn(linkedCountrySelect, apiKey);
249
+ }
250
+ else {
251
+ const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
252
+ if (relatedSubsSelect) {
253
+ await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
254
+ }
255
+ }
231
256
  }
232
257
  else if (parentSub) {
233
258
  const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
@@ -271,9 +296,12 @@ async function handleFollowRelatedFromSubdivision(select, apiKey, backendUrl, st
271
296
  relatedSubsSelect.dataset.preselected = targetSubdivision;
272
297
  }
273
298
  if (linkedCountrySelect.value !== targetCountry) {
299
+ // Directly set value like old widget did
274
300
  linkedCountrySelect.value = targetCountry;
275
301
  dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'regular' });
276
- // Update the subdivision select for the new country
302
+ // Update all subdivision selects for the new country (like old widget)
303
+ // Note: updateSubdivisionsFn is not available in handleFollowRelatedFromSubdivision,
304
+ // so we update the subdivision select manually
277
305
  if (relatedSubsSelect) {
278
306
  await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, targetCountry);
279
307
  }
@@ -321,9 +349,12 @@ async function handleFollowUpwardFromSubdivision(select, apiKey, backendUrl, sta
321
349
  }
322
350
  }
323
351
  if (linkedCountrySelect.value !== parentCode) {
352
+ // Directly set value like old widget did
324
353
  linkedCountrySelect.value = parentCode;
325
354
  dispatchUpdateEvent(linkedCountrySelect, { type: 'country', reason: 'follow' });
326
- // Update the subdivision select for the new country
355
+ // Update all subdivision selects for the new country (like old widget)
356
+ // Note: updateSubdivisionsFn is not available in handleFollowUpwardFromSubdivision,
357
+ // so we update the subdivision select manually
327
358
  const relatedSubsSelect = document.querySelector(`.subdivision-selection[data-country="${linkedCountrySelect.dataset.name}"]`);
328
359
  if (relatedSubsSelect) {
329
360
  await updateSubdivisionSelectFn(relatedSubsSelect, apiKey, parentCode);
@@ -368,9 +399,12 @@ async function handleFollowUpwardFromCountry(select, apiKey, backendUrl, state,
368
399
  }
369
400
  }
370
401
  }
402
+ // Directly set value like old widget did
371
403
  select.value = parentCode;
372
404
  dispatchUpdateEvent(select, { type: 'country', reason: 'regular' });
373
- // Update all linked subdivision selects for the new country
405
+ // Update all subdivision selects for the new country (like old widget)
406
+ // Note: updateSubdivisionsFn is not available in handleFollowUpwardFromCountry,
407
+ // so we update all subdivision selects manually
374
408
  const linkedSubdivisionSelects = Array.from(document.querySelectorAll(`.subdivision-selection[data-country="${select.dataset.name}"]`));
375
409
  for (const s of linkedSubdivisionSelects) {
376
410
  await updateSubdivisionSelectFn(s, apiKey, parentCode);
@@ -509,7 +543,7 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
509
543
  reason: 'preselected',
510
544
  });
511
545
  valueSetByWidget = true;
512
- await triggerFollowLogic(select, apiKey, backendUrl, state, config.followRelated, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code));
546
+ await triggerFollowLogic(select, apiKey, backendUrl, state, config.followRelated, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code), (countrySelect, key) => updateSubdivisions(countrySelect, key, backendUrl, state, config));
513
547
  }
514
548
  else {
515
549
  // Try GeoIP preselect
@@ -533,7 +567,7 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
533
567
  reason: 'geoip',
534
568
  });
535
569
  valueSetByWidget = true;
536
- await triggerFollowLogic(select, apiKey, backendUrl, state, config.followRelated, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code));
570
+ await triggerFollowLogic(select, apiKey, backendUrl, state, config.followRelated, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code), (countrySelect, key) => updateSubdivisions(countrySelect, key, backendUrl, state, config));
537
571
  }
538
572
  }
539
573
  }