@countriesdb/widget 0.1.26 → 0.1.27

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.js CHANGED
@@ -844,6 +844,11 @@
844
844
  const dataDefaultValue = !isMultiple
845
845
  ? select.dataset.defaultValue
846
846
  : undefined;
847
+ // Preserve user's current selection before clearing innerHTML
848
+ // This prevents preselected values from overwriting user selections
849
+ const currentValue = select.value;
850
+ const defaultValue = select.dataset.defaultValue || '';
851
+ const hasUserSelection = currentValue && currentValue !== defaultValue && currentValue.trim() !== '';
847
852
  select.innerHTML = buildSubdivisionOptionsHTML(subdivisions, select, subdivisionsLanguage, config.showSubdivisionType, config.allowParentSelection, config.subdivisionNameFilter);
848
853
  // Restore data attributes after setting innerHTML
849
854
  if (!isMultiple) {
@@ -854,41 +859,53 @@
854
859
  select.dataset.defaultValue = dataDefaultValue;
855
860
  }
856
861
  }
857
- // Manual preselection wins
858
- // Check if preselected value exists (applyPreselectedValue will read it from select element)
859
- const hasPreselectedValue = (select.getAttribute('data-preselected') || select.dataset.preselected || select.dataset._widgetTempPreselect) !== undefined;
860
- if (hasPreselectedValue) {
861
- applyPreselectedValue(select, apiKey);
862
- dispatchUpdateEvent(select, {
863
- type: 'subdivision',
864
- reason: 'preselected',
865
- });
866
- valueSetByWidget = true;
867
- 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));
862
+ // Restore user's selection if it exists in the new options (user selection takes priority)
863
+ let userSelectionRestored = false;
864
+ if (hasUserSelection && !isMultiple) {
865
+ const optionExists = Array.from(select.options).some(opt => opt.value === currentValue);
866
+ if (optionExists) {
867
+ select.value = currentValue;
868
+ userSelectionRestored = true;
869
+ // Don't dispatch event here - user already selected it, no need to notify again
870
+ }
868
871
  }
869
- else {
870
- // Try GeoIP preselect
871
- if (shouldUseGeoIP) {
872
- const preselectedSubdivision = subdivisions.find((subdivision) => subdivision.preselected);
873
- if (preselectedSubdivision) {
874
- const isMultiple = select.hasAttribute('multiple');
875
- if (isMultiple) {
876
- // For multi-select, find and select the option
877
- const option = Array.from(select.options).find((opt) => opt.value === preselectedSubdivision.code);
878
- if (option) {
879
- option.selected = true;
872
+ // Manual preselection only if user hasn't selected anything
873
+ if (!userSelectionRestored) {
874
+ // Check if preselected value exists (applyPreselectedValue will read it from select element)
875
+ const hasPreselectedValue = (select.getAttribute('data-preselected') || select.dataset.preselected || select.dataset._widgetTempPreselect) !== undefined;
876
+ if (hasPreselectedValue) {
877
+ applyPreselectedValue(select, apiKey);
878
+ dispatchUpdateEvent(select, {
879
+ type: 'subdivision',
880
+ reason: 'preselected',
881
+ });
882
+ valueSetByWidget = true;
883
+ 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));
884
+ }
885
+ else {
886
+ // Try GeoIP preselect (only if user hasn't selected anything)
887
+ if (shouldUseGeoIP) {
888
+ const preselectedSubdivision = subdivisions.find((subdivision) => subdivision.preselected);
889
+ if (preselectedSubdivision) {
890
+ const isMultiple = select.hasAttribute('multiple');
891
+ if (isMultiple) {
892
+ // For multi-select, find and select the option
893
+ const option = Array.from(select.options).find((opt) => opt.value === preselectedSubdivision.code);
894
+ if (option) {
895
+ option.selected = true;
896
+ }
880
897
  }
898
+ else {
899
+ // Single select: set value directly
900
+ select.value = preselectedSubdivision.code;
901
+ }
902
+ dispatchUpdateEvent(select, {
903
+ type: 'subdivision',
904
+ reason: 'geoip',
905
+ });
906
+ valueSetByWidget = true;
907
+ 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));
881
908
  }
882
- else {
883
- // Single select: set value directly
884
- select.value = preselectedSubdivision.code;
885
- }
886
- dispatchUpdateEvent(select, {
887
- type: 'subdivision',
888
- reason: 'geoip',
889
- });
890
- valueSetByWidget = true;
891
- 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));
892
909
  }
893
910
  }
894
911
  }