@countriesdb/widget 0.1.25 → 0.1.26

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.
@@ -35,27 +35,47 @@ export async function setupSubdivisionSelection(apiKey, backendUrl, state, confi
35
35
  select.innerHTML += `<option value="${defaultValue}" disabled>Error: No country select present</option>`;
36
36
  }
37
37
  }
38
+ // Remove old event handlers if they exist (for re-initialization)
39
+ const oldHandlers = state.eventHandlers.get(select);
40
+ if (oldHandlers) {
41
+ if (oldHandlers.update) {
42
+ select.removeEventListener('change', oldHandlers.update);
43
+ }
44
+ if (oldHandlers.followRelated) {
45
+ select.removeEventListener('change', oldHandlers.followRelated);
46
+ }
47
+ if (oldHandlers.followUpward) {
48
+ select.removeEventListener('change', oldHandlers.followUpward);
49
+ }
50
+ }
51
+ // Create new event handlers
52
+ const handlers = {};
38
53
  // Always dispatch an update event for user-initiated subdivision changes
39
- select.addEventListener('change', (event) => {
54
+ handlers.update = (event) => {
40
55
  if (isWidgetInitiatedEvent(event)) {
41
56
  return;
42
57
  }
43
58
  dispatchUpdateEvent(select, { type: 'subdivision', reason: 'regular' });
44
- });
59
+ };
60
+ select.addEventListener('change', handlers.update);
45
61
  // --- follow_related (forward direction) ---
46
- select.addEventListener('change', async (event) => {
62
+ handlers.followRelated = async (event) => {
47
63
  if (isWidgetInitiatedEvent(event)) {
48
64
  return;
49
65
  }
50
66
  await handleFollowRelatedFromSubdivision(select, apiKey, backendUrl, state, config.followRelated, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code));
51
- });
67
+ };
68
+ select.addEventListener('change', handlers.followRelated);
52
69
  // --- follow_upward (reverse direction) ---
53
- select.addEventListener('change', async (event) => {
70
+ handlers.followUpward = async (event) => {
54
71
  if (isWidgetInitiatedEvent(event)) {
55
72
  return;
56
73
  }
57
74
  await handleFollowUpwardFromSubdivision(select, apiKey, backendUrl, state, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, config, code));
58
- });
75
+ };
76
+ select.addEventListener('change', handlers.followUpward);
77
+ // Store handlers for future cleanup
78
+ state.eventHandlers.set(select, handlers);
59
79
  }
60
80
  }
61
81
  /**
@@ -274,8 +294,13 @@ export async function setupCountrySelection(apiKey, backendUrl, state, config, s
274
294
  // Subdivisions will be loaded by event handler
275
295
  loadedInitialSubdivisions = true;
276
296
  }
277
- // On change => update subdivisions
278
- select.addEventListener('change', async (event) => {
297
+ // Remove old event handlers if they exist (for re-initialization)
298
+ const oldCountryHandlers = state.eventHandlers.get(select);
299
+ if (oldCountryHandlers?.countryChange) {
300
+ select.removeEventListener('change', oldCountryHandlers.countryChange);
301
+ }
302
+ // Create new country change handler
303
+ const countryChangeHandler = async (event) => {
279
304
  if (isWidgetInitiatedEvent(event)) {
280
305
  return;
281
306
  }
@@ -297,7 +322,12 @@ export async function setupCountrySelection(apiKey, backendUrl, state, config, s
297
322
  if (config.followUpward && !select.multiple && picked.is_subdivision_of) {
298
323
  await handleFollowUpwardFromCountry(select, apiKey, backendUrl, state, config.followUpward, (s, key, code) => updateSubdivisionSelect(s, key, backendUrl, state, subdivisionConfig, code));
299
324
  }
300
- });
325
+ };
326
+ // Store and attach the handler
327
+ const countryHandlers = state.eventHandlers.get(select) || {};
328
+ countryHandlers.countryChange = countryChangeHandler;
329
+ state.eventHandlers.set(select, countryHandlers);
330
+ select.addEventListener('change', countryChangeHandler);
301
331
  }
302
332
  catch (error) {
303
333
  console.error('Failed to fetch countries:', error);
package/dist/types.d.ts CHANGED
@@ -30,9 +30,16 @@ export interface UpdateEventDetail {
30
30
  export interface ReadyEventDetail extends UpdateEventDetail {
31
31
  phase: 'initial' | 'reload';
32
32
  }
33
+ export interface EventHandlers {
34
+ update?: (event: Event) => void;
35
+ followRelated?: (event: Event) => void;
36
+ followUpward?: (event: Event) => void;
37
+ countryChange?: (event: Event) => void;
38
+ }
33
39
  export interface WidgetState {
34
40
  countriesMap: WeakMap<SelectElement, Country[]>;
35
41
  subdivisionsMap: WeakMap<SelectElement, Subdivision[]>;
36
42
  subdivisionsLanguageMap: WeakMap<SelectElement, string>;
37
43
  isInitializing: Set<SelectElement>;
44
+ eventHandlers: WeakMap<SelectElement, EventHandlers>;
38
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@countriesdb/widget",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "description": "Country and state/province select widget with ISO 3166-1 and ISO 3166-2 codes. Auto-populates dropdowns with up-to-date country and subdivision data in multiple languages. Easy integration for forms, location selection, and address validation.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",