@countriesdb/widget 0.1.35 → 1.0.0

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.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Widget initialization logic
3
3
  */
4
- import { CountriesDBClient, buildSubdivisionTree } from '@countriesdb/widget-core';
4
+ import { CountriesDBClient } from '@countriesdb/widget-core';
5
5
  import { initializeSelect, populateCountrySelect, buildSubdivisionOptionsHTML, applyPreselectedValue, handleApiError, } from './dom-manipulation';
6
6
  import { dispatchReadyEvent, dispatchUpdateEvent, isWidgetInitiatedEvent, } from './event-system';
7
7
  import { triggerFollowLogic, handleFollowRelatedFromSubdivision, handleFollowUpwardFromSubdivision, handleFollowUpwardFromCountry, } from './follow-logic';
@@ -178,50 +178,36 @@ export async function updateSubdivisionSelect(select, apiKey, backendUrl, state,
178
178
  if (preselectedSubdivision) {
179
179
  const isMultiple = select.hasAttribute('multiple');
180
180
  let subdivisionToSelect = preselectedSubdivision;
181
- // Check if the preselected subdivision is disabled (parent with children when allowParentSelection is false)
181
+ // If preselected is disabled and allowParentSelection is false, find first enabled child
182
182
  if (!config.allowParentSelection && !isMultiple) {
183
- const option = Array.from(select.options).find((opt) => opt.value === preselectedSubdivision.code);
184
- // If option is disabled, find the first child subdivision
185
- if (option && option.disabled) {
186
- const tree = buildSubdivisionTree(subdivisions);
187
- // Find the node in the tree
188
- function findNodeInTree(nodes, code) {
189
- for (const node of nodes) {
190
- if (node.code === code) {
191
- return node;
183
+ const preselectedOption = Array.from(select.options).find((opt) => opt.value === preselectedSubdivision.code);
184
+ if (preselectedOption?.disabled) {
185
+ // Find direct children by parent_id
186
+ const directChildren = subdivisions.filter((sub) => sub.parent_id === preselectedSubdivision.id);
187
+ if (directChildren.length > 0) {
188
+ // Sort by name and get first child
189
+ const sorted = directChildren.sort((a, b) => {
190
+ return (a.name || '').localeCompare(b.name || '', subdivisionsLanguage, { sensitivity: 'accent' });
191
+ });
192
+ const firstChild = sorted[0];
193
+ // Check if first child is also disabled (has its own children)
194
+ const firstChildOption = Array.from(select.options).find((opt) => opt.value === firstChild.code);
195
+ if (firstChildOption?.disabled) {
196
+ // First child is disabled, find its first child (grandchild)
197
+ const grandChildren = subdivisions.filter((sub) => sub.parent_id === firstChild.id);
198
+ if (grandChildren.length > 0) {
199
+ const sortedGrandChildren = grandChildren.sort((a, b) => {
200
+ return (a.name || '').localeCompare(b.name || '', subdivisionsLanguage, { sensitivity: 'accent' });
201
+ });
202
+ subdivisionToSelect = sortedGrandChildren[0];
192
203
  }
193
- if (node.children && node.children.length > 0) {
194
- const found = findNodeInTree(node.children, code);
195
- if (found)
196
- return found;
204
+ else {
205
+ subdivisionToSelect = firstChild;
197
206
  }
198
207
  }
199
- return null;
200
- }
201
- const preselectedNode = findNodeInTree(tree, preselectedSubdivision.code);
202
- // Get the first child (recursively if needed)
203
- function getFirstChild(node) {
204
- if (!node || !node.children || node.children.length === 0) {
205
- return null;
208
+ else {
209
+ subdivisionToSelect = firstChild;
206
210
  }
207
- // Sort children to get consistent first child
208
- const sortedChildren = [...node.children].sort((a, b) => {
209
- const nameA = a.name || '';
210
- const nameB = b.name || '';
211
- return nameA.localeCompare(nameB);
212
- });
213
- const firstChild = sortedChildren[0];
214
- // If first child also has children and would be disabled, recurse
215
- if (firstChild.children && firstChild.children.length > 0) {
216
- const deeperChild = getFirstChild(firstChild);
217
- return deeperChild || firstChild;
218
- }
219
- return firstChild;
220
- }
221
- const firstChild = preselectedNode ? getFirstChild(preselectedNode) : null;
222
- if (firstChild) {
223
- // Find the subdivision object by code
224
- subdivisionToSelect = subdivisions.find((sub) => sub.code === firstChild.code) || preselectedSubdivision;
225
211
  }
226
212
  }
227
213
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@countriesdb/widget",
3
- "version": "0.1.35",
3
+ "version": "1.0.0",
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",