@countriesdb/widget 0.1.33 → 0.1.35

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.
@@ -132,6 +132,12 @@ export function applyPreselectedValue(select, apiKey) {
132
132
  if (select.dataset._widgetTempPreselect !== undefined) {
133
133
  delete select.dataset._widgetTempPreselect;
134
134
  }
135
+ // Mark permanent preselect as consumed after first application
136
+ // This prevents it from being reapplied on user-initiated country changes
137
+ // But reload: true will clear this flag to allow reapplication
138
+ if (permanent !== undefined && permanent !== null && String(permanent).trim() !== '') {
139
+ select.dataset._widgetPreselectedConsumed = '1';
140
+ }
135
141
  }
136
142
  /**
137
143
  * Handle API error by showing error message in select
package/dist/index.esm.js CHANGED
@@ -133,6 +133,12 @@ function applyPreselectedValue(select, apiKey) {
133
133
  if (select.dataset._widgetTempPreselect !== undefined) {
134
134
  delete select.dataset._widgetTempPreselect;
135
135
  }
136
+ // Mark permanent preselect as consumed after first application
137
+ // This prevents it from being reapplied on user-initiated country changes
138
+ // But reload: true will clear this flag to allow reapplication
139
+ if (permanent !== undefined && permanent !== null && String(permanent).trim() !== '') {
140
+ select.dataset._widgetPreselectedConsumed = '1';
141
+ }
136
142
  }
137
143
  /**
138
144
  * Handle API error by showing error message in select
@@ -621,7 +627,13 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
621
627
  if (!userSelectionRestored) {
622
628
  // Check if preselected value exists (applyPreselectedValue will read it from select element)
623
629
  const hasPreselectedValue = (select.getAttribute('data-preselected') || select.dataset.preselected || select.dataset._widgetTempPreselect) !== undefined;
624
- if (hasPreselectedValue) {
630
+ const preselectedConsumed = select.dataset._widgetPreselectedConsumed === '1';
631
+ // Only apply preselected if:
632
+ // 1. It exists AND
633
+ // 2. Either it hasn't been consumed yet, OR this is the initial load (not a reload)
634
+ // This prevents reapplying preselected values on user-initiated country changes
635
+ // But allows them on initial load and on reload: true (which clears the consumed flag)
636
+ if (hasPreselectedValue && (!preselectedConsumed || !isReload)) {
625
637
  applyPreselectedValue(select, apiKey);
626
638
  dispatchUpdateEvent(select, {
627
639
  type: 'subdivision',
@@ -636,16 +648,64 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
636
648
  const preselectedSubdivision = subdivisions.find((subdivision) => subdivision.preselected);
637
649
  if (preselectedSubdivision) {
638
650
  const isMultiple = select.hasAttribute('multiple');
651
+ let subdivisionToSelect = preselectedSubdivision;
652
+ // Check if the preselected subdivision is disabled (parent with children when allowParentSelection is false)
653
+ if (!config.allowParentSelection && !isMultiple) {
654
+ const option = Array.from(select.options).find((opt) => opt.value === preselectedSubdivision.code);
655
+ // If option is disabled, find the first child subdivision
656
+ if (option && option.disabled) {
657
+ const tree = buildSubdivisionTree(subdivisions);
658
+ // Find the node in the tree
659
+ function findNodeInTree(nodes, code) {
660
+ for (const node of nodes) {
661
+ if (node.code === code) {
662
+ return node;
663
+ }
664
+ if (node.children && node.children.length > 0) {
665
+ const found = findNodeInTree(node.children, code);
666
+ if (found)
667
+ return found;
668
+ }
669
+ }
670
+ return null;
671
+ }
672
+ const preselectedNode = findNodeInTree(tree, preselectedSubdivision.code);
673
+ // Get the first child (recursively if needed)
674
+ function getFirstChild(node) {
675
+ if (!node || !node.children || node.children.length === 0) {
676
+ return null;
677
+ }
678
+ // Sort children to get consistent first child
679
+ const sortedChildren = [...node.children].sort((a, b) => {
680
+ const nameA = a.name || '';
681
+ const nameB = b.name || '';
682
+ return nameA.localeCompare(nameB);
683
+ });
684
+ const firstChild = sortedChildren[0];
685
+ // If first child also has children and would be disabled, recurse
686
+ if (firstChild.children && firstChild.children.length > 0) {
687
+ const deeperChild = getFirstChild(firstChild);
688
+ return deeperChild || firstChild;
689
+ }
690
+ return firstChild;
691
+ }
692
+ const firstChild = preselectedNode ? getFirstChild(preselectedNode) : null;
693
+ if (firstChild) {
694
+ // Find the subdivision object by code
695
+ subdivisionToSelect = subdivisions.find((sub) => sub.code === firstChild.code) || preselectedSubdivision;
696
+ }
697
+ }
698
+ }
639
699
  if (isMultiple) {
640
700
  // For multi-select, find and select the option
641
- const option = Array.from(select.options).find((opt) => opt.value === preselectedSubdivision.code);
701
+ const option = Array.from(select.options).find((opt) => opt.value === subdivisionToSelect.code);
642
702
  if (option) {
643
703
  option.selected = true;
644
704
  }
645
705
  }
646
706
  else {
647
707
  // Single select: set value directly
648
- select.value = preselectedSubdivision.code;
708
+ select.value = subdivisionToSelect.code;
649
709
  }
650
710
  dispatchUpdateEvent(select, {
651
711
  type: 'subdivision',
@@ -935,6 +995,16 @@ async function CountriesWidgetLoad(options = {}) {
935
995
  if (shouldReload) {
936
996
  NS.initialized = false;
937
997
  NS.initPromise = null;
998
+ // Clear consumed preselected flags to allow preselected values to be reapplied on reload: true
999
+ // This ensures full reload behavior where preselected values are reapplied
1000
+ if (typeof document !== 'undefined') {
1001
+ const subdivisionSelects = document.querySelectorAll('.subdivision-selection');
1002
+ subdivisionSelects.forEach((select) => {
1003
+ if (select.dataset._widgetPreselectedConsumed) {
1004
+ delete select.dataset._widgetPreselectedConsumed;
1005
+ }
1006
+ });
1007
+ }
938
1008
  }
939
1009
  // Share the same promise across concurrent calls
940
1010
  NS.initPromise = (async () => {