@countriesdb/widget 0.1.35 → 0.1.36

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.
package/dist/index.esm.js CHANGED
@@ -649,50 +649,36 @@ async function updateSubdivisionSelect(select, apiKey, backendUrl, state, config
649
649
  if (preselectedSubdivision) {
650
650
  const isMultiple = select.hasAttribute('multiple');
651
651
  let subdivisionToSelect = preselectedSubdivision;
652
- // Check if the preselected subdivision is disabled (parent with children when allowParentSelection is false)
652
+ // If preselected is disabled and allowParentSelection is false, find first enabled child
653
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;
654
+ const preselectedOption = Array.from(select.options).find((opt) => opt.value === preselectedSubdivision.code);
655
+ if (preselectedOption?.disabled) {
656
+ // Find direct children by parent_id
657
+ const directChildren = subdivisions.filter((sub) => sub.parent_id === preselectedSubdivision.id);
658
+ if (directChildren.length > 0) {
659
+ // Sort by name and get first child
660
+ const sorted = directChildren.sort((a, b) => {
661
+ return (a.name || '').localeCompare(b.name || '', subdivisionsLanguage, { sensitivity: 'accent' });
662
+ });
663
+ const firstChild = sorted[0];
664
+ // Check if first child is also disabled (has its own children)
665
+ const firstChildOption = Array.from(select.options).find((opt) => opt.value === firstChild.code);
666
+ if (firstChildOption?.disabled) {
667
+ // First child is disabled, find its first child (grandchild)
668
+ const grandChildren = subdivisions.filter((sub) => sub.parent_id === firstChild.id);
669
+ if (grandChildren.length > 0) {
670
+ const sortedGrandChildren = grandChildren.sort((a, b) => {
671
+ return (a.name || '').localeCompare(b.name || '', subdivisionsLanguage, { sensitivity: 'accent' });
672
+ });
673
+ subdivisionToSelect = sortedGrandChildren[0];
663
674
  }
664
- if (node.children && node.children.length > 0) {
665
- const found = findNodeInTree(node.children, code);
666
- if (found)
667
- return found;
675
+ else {
676
+ subdivisionToSelect = firstChild;
668
677
  }
669
678
  }
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;
679
+ else {
680
+ subdivisionToSelect = firstChild;
677
681
  }
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
682
  }
697
683
  }
698
684
  }